Skip to content

Commit 4ae630d

Browse files
authored
Merge pull request #230 from saeyslab/issue-198-idents
Issue 198 fix
2 parents c981f92 + ada34e8 commit 4ae630d

32 files changed

+2319
-2072
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: nichenetr
22
Type: Package
33
Title: NicheNet: Modeling Intercellular Communication by Linking Ligands to Target Genes
4-
Version: 2.0.3
4+
Version: 2.0.4
55
Authors@R: c(person("Robin", "Browaeys", role = c("aut")),
66
person("Chananchida", "Sang-aram", role = c("aut", "cre"), email = "chananchida.sangaram@ugent.be"))
77
Description: This package allows you the investigate intercellular communication from a computational perspective. More specifically, it allows to investigate how interacting cells influence each other's gene expression. Functionalities of this package (e.g. including predicting extracellular upstream regulators and their affected target genes) build upon a probabilistic model of ligand-target links that was inferred by data-integration.

R/application_prediction.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ nichenet_seuratobj_aggregate = function(receiver, seurat_obj, condition_colname,
847847
if (verbose == TRUE){print("Perform DE analysis in receiver cell")}
848848

849849
seurat_obj_receiver= subset(seurat_obj, idents = receiver)
850-
seurat_obj_receiver = SetIdent(seurat_obj_receiver, value = seurat_obj_receiver[[condition_colname]])
850+
seurat_obj_receiver = SetIdent(seurat_obj_receiver, value = seurat_obj_receiver[[condition_colname, drop=TRUE]])
851851
DE_table_receiver = FindMarkers(object = seurat_obj_receiver, ident.1 = condition_oi, ident.2 = condition_reference, min.pct = expression_pct) %>% rownames_to_column("gene")
852852

853853
SeuratV4 = c("avg_log2FC") %in% colnames(DE_table_receiver)
@@ -1030,7 +1030,7 @@ nichenet_seuratobj_aggregate = function(receiver, seurat_obj, condition_colname,
10301030
names(order_ligands_adapted) = NULL
10311031

10321032
seurat_obj_subset = seurat_obj %>% subset(idents = sender_celltypes)
1033-
seurat_obj_subset = SetIdent(seurat_obj_subset, value = seurat_obj_subset[[condition_colname]]) %>% subset(idents = condition_oi) ## only shows cells of the condition of interest
1033+
seurat_obj_subset = SetIdent(seurat_obj_subset, value = seurat_obj_subset[[condition_colname, drop=TRUE]]) %>% subset(idents = condition_oi) ## only shows cells of the condition of interest
10341034
rotated_dotplot = DotPlot(seurat_obj %>% subset(cells = Cells(seurat_obj_subset)), features = order_ligands_adapted, cols = "RdYlBu") + coord_flip() + theme(legend.text = element_text(size = 10), legend.title = element_text(size = 12)) # flip of coordinates necessary because we want to show ligands in the rows when combining all plots
10351035
rm(seurat_obj_subset)
10361036

@@ -1782,11 +1782,11 @@ nichenet_seuratobj_aggregate_cluster_de = function(seurat_obj, receiver_affected
17821782
if (verbose == TRUE){print("Perform DE analysis between two receiver cell clusters")}
17831783

17841784
seurat_obj_receiver_affected= subset(seurat_obj, idents = receiver_affected)
1785-
seurat_obj_receiver_affected = SetIdent(seurat_obj_receiver_affected, value = seurat_obj_receiver_affected[[condition_colname]])
1785+
seurat_obj_receiver_affected = SetIdent(seurat_obj_receiver_affected, value = seurat_obj_receiver_affected[[condition_colname, drop=TRUE]])
17861786
seurat_obj_receiver_affected= subset(seurat_obj_receiver_affected, idents = condition_oi)
17871787

17881788
seurat_obj_receiver_reference= subset(seurat_obj, idents = receiver_reference)
1789-
seurat_obj_receiver_reference = SetIdent(seurat_obj_receiver_reference, value = seurat_obj_receiver_reference[[condition_colname]])
1789+
seurat_obj_receiver_reference = SetIdent(seurat_obj_receiver_reference, value = seurat_obj_receiver_reference[[condition_colname, drop=TRUE]])
17901790
seurat_obj_receiver_reference= subset(seurat_obj_receiver_reference, idents = condition_reference)
17911791

17921792
seurat_obj_receiver = merge(seurat_obj_receiver_affected, seurat_obj_receiver_reference)
@@ -2003,14 +2003,14 @@ get_lfc_celltype = function(celltype_oi, seurat_obj, condition_colname, conditio
20032003
requireNamespace("Seurat")
20042004
requireNamespace("dplyr")
20052005
if(!is.null(celltype_col)){
2006-
seurat_obj_celltype = SetIdent(seurat_obj, value = seurat_obj[[celltype_col]])
2006+
seurat_obj_celltype = SetIdent(seurat_obj, value = seurat_obj[[celltype_col, drop=TRUE]])
20072007
seuratObj_sender = subset(seurat_obj_celltype, idents = celltype_oi)
20082008

20092009
} else {
20102010
seuratObj_sender = subset(seurat_obj, idents = celltype_oi)
20112011

20122012
}
2013-
seuratObj_sender = SetIdent(seuratObj_sender, value = seuratObj_sender[[condition_colname]])
2013+
seuratObj_sender = SetIdent(seuratObj_sender, value = seuratObj_sender[[condition_colname, drop=TRUE]])
20142014
DE_table_sender = FindMarkers(object = seuratObj_sender, ident.1 = condition_oi, ident.2 = condition_reference, min.pct = expression_pct, logfc.threshold = 0.05) %>% rownames_to_column("gene")
20152015

20162016
SeuratV4 = c("avg_log2FC") %in% colnames(DE_table_sender)

R/prioritization.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ process_table_to_ic = function(table_object, table_type = "expression",
267267
#' ligand_target_matrix = ligand_target_matrix %>% .[!is.na(rownames(ligand_target_matrix)), !is.na(colnames(ligand_target_matrix))]
268268
#'
269269
#' # Ligand activity analysis
270-
#' seurat_obj_receiver = subset(seurat_obj, idents = receiver) %>% SetIdent(value = .[["aggregate"]])
270+
#' seurat_obj_receiver = subset(seurat_obj, idents = receiver) %>% SetIdent(value = .[["aggregate", drop = TRUE]])
271271
#' geneset_oi = FindMarkers(object = seurat_obj_receiver, ident.1 = "LCMV, ident.2 = "SS, min.pct = 0.10) %>% rownames_to_column("gene") %>%
272272
#' filter(p_val_adj <= 0.05 & abs(avg_log2FC) >= 0.25) %>% pull(gene) %>% .[. %in% rownames(ligand_target_matrix)]
273273
#' expressed_genes_sender = sender_celltypes %>% unique() %>% lapply(get_expressed_genes, seurat_obj, 0.10) %>% unlist() %>% unique()

R/supporting_functions.R

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
247247
rownames(RNA@scale.data) = newnames
248248
}
249249
}
250+
251+
if (length(RNA@var.features) > 0){
252+
newnames = convert_alias_to_symbols(RNA@var.features, organism = organism, verbose = FALSE)
253+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
254+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
255+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
256+
RNA@var.features = newnames
257+
}
258+
259+
if (nrow(RNA@meta.features) > 0){
260+
newnames = convert_alias_to_symbols(rownames(RNA@meta.features), organism = organism, verbose = FALSE)
261+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
262+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
263+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
264+
rownames(RNA@meta.features) = newnames
265+
}
266+
250267
} else {"Unequal gene sets: nrow(seurat_obj@assays$RNA) != nrow(newnames)"}
251268

252269
if(!is.null(RNA@counts)){
@@ -280,6 +297,28 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
280297
}
281298
}
282299

300+
if (length(RNA@var.features) > 0){
301+
dim_before = length(RNA@var.features)
302+
RNA@var.features = RNA@var.features %>% .[!is.na(.)]
303+
dim_after = length(RNA@var.features)
304+
if(dim_before != dim_after){
305+
print("length of var.features changed")
306+
print(paste0("before: ",dim_before))
307+
print(paste0("after: ",dim_before))
308+
}
309+
}
310+
311+
if (nrow(RNA@meta.features) > 0){
312+
dim_before = dim(RNA@meta.features)
313+
RNA@meta.features = RNA@meta.features %>% .[!is.na(rownames(.)), ]
314+
dim_after = dim(RNA@meta.features)
315+
if(sum(dim_before != dim_after) > 0){
316+
print("length of meta.features changed")
317+
print(paste0("before: ",dim_before))
318+
print(paste0("after: ",dim_before))
319+
}
320+
}
321+
283322
seurat_obj@assays$RNA = RNA
284323

285324
if(!is.null( seurat_obj@assays$SCT)){
@@ -313,6 +352,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
313352
rownames(SCT@scale.data) = newnames
314353
}
315354
}
355+
356+
if (length(SCT@var.features) > 0){
357+
newnames = convert_alias_to_symbols(SCT@var.features, organism = organism, verbose = FALSE)
358+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
359+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
360+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
361+
SCT@var.features = newnames
362+
}
363+
364+
if (nrow(SCT@meta.features) > 0){
365+
newnames = convert_alias_to_symbols(rownames(SCT@meta.features), organism = organism, verbose = FALSE)
366+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
367+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
368+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
369+
rownames(SCT@meta.features) = newnames
370+
}
371+
316372
} else {"Unequal gene sets: nrow(seurat_obj@assays$SCT) != nrow(newnames)"}
317373

318374
if(!is.null(SCT@counts)){
@@ -345,6 +401,30 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
345401
print(paste0("after: ",dim_before))
346402
}
347403
}
404+
405+
if (length(SCT@var.features) > 0){
406+
dim_before = length(SCT@var.features)
407+
SCT@var.features = SCT@var.features %>% .[!is.na(.)]
408+
dim_after = length(SCT@var.features)
409+
if(dim_before != dim_after){
410+
print("length of var.features changed")
411+
print(paste0("before: ",dim_before))
412+
print(paste0("after: ",dim_before))
413+
}
414+
}
415+
416+
417+
if (nrow(SCT@meta.features) > 0){
418+
dim_before = dim(SCT@meta.features)
419+
SCT@meta.features = SCT@meta.features %>% .[!is.na(rownames(.)), ]
420+
dim_after = dim(SCT@meta.features)
421+
if(sum(dim_before != dim_after) > 0){
422+
print("length of meta.features changed")
423+
print(paste0("before: ",dim_before))
424+
print(paste0("after: ",dim_before))
425+
}
426+
}
427+
348428
seurat_obj@assays$SCT = SCT
349429
}
350430

@@ -383,6 +463,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
383463
rownames(integrated@scale.data) = newnames
384464
}
385465
}
466+
467+
if (length(integrated@var.features) > 0){
468+
newnames = convert_alias_to_symbols(integrated@var.features, organism = organism, verbose = FALSE)
469+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
470+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
471+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
472+
integrated@var.features = newnames
473+
}
474+
475+
if (nrow(integrated@meta.features) > 0){
476+
newnames = convert_alias_to_symbols(rownames(integrated@meta.features), organism = organism, verbose = FALSE)
477+
doubles = newnames %>% table() %>% .[. > 1] %>% names()
478+
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
479+
newnames[genes_remove] = genes_remove # set the doubles back to their old names
480+
rownames(integrated@meta.features) = newnames
481+
}
482+
386483
} else {"Unequal gene sets: nrow(seurat_obj@assays$integrated) != nrow(newnames)"}
387484

388485
if(!is.null(integrated@counts)){
@@ -415,6 +512,29 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
415512
print(paste0("after: ",dim_before))
416513
}
417514
}
515+
516+
if (length(integrated@var.features) > 0){
517+
dim_before = length(integrated@var.features)
518+
integrated@var.features = integrated@var.features %>% .[!is.na(.)]
519+
dim_after = length(integrated@var.features)
520+
if(dim_before != dim_after){
521+
print("length of var.features changed")
522+
print(paste0("before: ",dim_before))
523+
print(paste0("after: ",dim_before))
524+
}
525+
}
526+
527+
if (nrow(integrated@meta.features) > 0){
528+
dim_before = dim(integrated@meta.features)
529+
integrated@meta.features = integrated@meta.features %>% .[!is.na(rownames(.)), ]
530+
dim_after = dim(integrated@meta.features)
531+
if(sum(dim_before != dim_after) > 0){
532+
print("length of meta.features changed")
533+
print(paste0("before: ",dim_before))
534+
print(paste0("after: ",dim_before))
535+
}
536+
}
537+
418538
seurat_obj@assays$integrated = integrated
419539
}
420540

man/generate_prioritization_tables.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-differential_nichenet.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_that("Differential NicheNet pipeline works", {
1111
seurat_object_lite@meta.data$celltype_aggregate = paste(seurat_object_lite@meta.data$celltype, seurat_object_lite@meta.data$aggregate,sep = "_") # user adaptation required on own dataset
1212

1313
celltype_id = "celltype_aggregate" # metadata column name of the cell type of interest
14-
seurat_obj = SetIdent(seurat_object_lite, value = seurat_object_lite[[celltype_id]])
14+
seurat_obj = SetIdent(seurat_object_lite, value = seurat_object_lite[[celltype_id, drop=TRUE]])
1515

1616
niches = list(
1717
"LCMV_niche" = list(
@@ -90,7 +90,7 @@ test_that("Differential NicheNet pipeline works", {
9090
length(geneset_niche2)
9191

9292
top_n_target = 250
93-
93+
9494
niche_geneset_list = list(
9595
"LCMV_niche" = list(
9696
"receiver" = niches[[1]]$receiver,

vignettes/differential_nichenet_pEMT.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ seurat_obj@meta.data$celltype_aggregate %>% table() %>% sort(decreasing = TRUE)
7070

7171
```{r}
7272
celltype_id = "celltype_aggregate" # metadata column name of the cell type of interest
73-
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id]])
73+
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id, drop=TRUE]])
7474
```
7575

7676
## Read in the NicheNet ligand-receptor network and ligand-target matrix

vignettes/differential_nichenet_pEMT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ seurat_obj@meta.data$celltype_aggregate %>% table() %>% sort(decreasing = TRUE)
103103

104104
``` r
105105
celltype_id = "celltype_aggregate" # metadata column name of the cell type of interest
106-
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id]])
106+
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id, drop=TRUE]])
107107
```
108108

109109
## Read in the NicheNet ligand-receptor network and ligand-target matrix

0 commit comments

Comments
 (0)