Skip to content

Commit 50f4001

Browse files
committed
added check for if underscore is in celltype name
1 parent dd7a859 commit 50f4001

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

R/prioritization.R

+14-2
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,26 @@ get_exprs_avg = function(seurat_obj, celltype_colname,
125125

126126
# Subset seurat object
127127
if (!is.null(condition_oi)) {
128-
seurat_obj = seurat_obj[,seurat_obj[[condition_colname]] == condition_oi]
128+
seurat_obj <- seurat_obj[,seurat_obj[[condition_colname]] == condition_oi]
129129
}
130130

131-
seurat_obj <- NormalizeData(seurat_obj, verbose = FALSE)
131+
celltypes <- unique(seurat_obj[[celltype_colname, drop=TRUE]])
132+
132133
avg_celltype <- AverageExpression(seurat_obj, group.by = celltype_colname, assays = assay_oi, ...) %>%
133134
.[[assay_oi]] %>% data.frame(check.names=FALSE) %>% rownames_to_column("gene") %>%
134135
pivot_longer(!gene, names_to = "cluster_id", values_to = "avg_expr")
135136

137+
# If any celltypes had an underscore in their name
138+
if (any(grepl("_", celltypes))){
139+
# Map the new names and the original names
140+
# This is so it works in the case the original name also already has a hyphen in in
141+
mapping <- data.frame(orig_name = sort(celltypes), cluster_id = sort(unique(avg_celltype$cluster_id)))
142+
avg_celltype <- avg_celltype %>% left_join(mapping, by = "cluster_id") %>%
143+
dplyr::select(gene, cluster_id = orig_name, avg_expr)
144+
145+
146+
}
147+
136148
return (avg_celltype)
137149

138150
}

tests/testthat/test-prioritization.R

+13-4
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,22 @@ test_that("Prioritization scheme works", {
172172
condition_colname = "aggregate", condition_oi = condition_oi,
173173
features = feature_list)
174174

175-
# Calculate condition specificity - only for datasets with two conditions!
176-
condition_markers <- FindMarkers(object = seurat_obj_test, ident.1 = condition_oi, ident.2 = condition_reference,
175+
# Test cell type names conversion for Seurat object
176+
# Replace space with underscore
177+
seurat_obj_test$celltype2 <- gsub(" ", "_", seurat_obj_test$celltype)
178+
new_celltypes <- suppressWarnings(get_exprs_avg(seurat_obj_test, "celltype2") %>% pull(cluster_id) %>% unique())
179+
expect_equal(new_celltypes, sort(unique(seurat_obj_test$celltype2)))
180+
181+
# Replace CD8 T with CD8_T-test & replace Mono with Mono-test
182+
seurat_obj_test$celltype2 <- gsub("CD8 T", "CD8_T-test", seurat_obj_test$celltype) %>% gsub("Mono", "Mono-test", .)
183+
new_celltypes <- suppressWarnings(get_exprs_avg(seurat_obj_test, "celltype2") %>% pull(cluster_id) %>% unique())
184+
expect_equal(new_celltypes, sort(unique(seurat_obj_test$celltype2)))
185+
186+
# Calculate condition specificity - only for datasets with two conditions!
187+
condition_markers <- FindMarkers(object = seurat_obj_test, ident.1 = condition_oi, ident.2 = condition_reference,
177188
group.by = "aggregate", min.pct = 0, logfc.threshold = 0,
178189
features = feature_list) %>% rownames_to_column("gene")
179190

180-
# TODO: TESTS FOR PROCESS_TABLE_TO_IC
181-
182191
# Combine DE of senders and receivers -> used for prioritization
183192
processed_DE_table <- process_table_to_ic(DE_table, table_type = "celltype_DE", lr_network,
184193
senders_oi = sender_celltypes, receivers_oi = receiver)

0 commit comments

Comments
 (0)