Skip to content

Commit

Permalink
Merge pull request #230 from saeyslab/issue-198-idents
Browse files Browse the repository at this point in the history
Issue 198 fix
  • Loading branch information
csangara authored Oct 2, 2023
2 parents c981f92 + ada34e8 commit 4ae630d
Show file tree
Hide file tree
Showing 32 changed files with 2,319 additions and 2,072 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nichenetr
Type: Package
Title: NicheNet: Modeling Intercellular Communication by Linking Ligands to Target Genes
Version: 2.0.3
Version: 2.0.4
Authors@R: c(person("Robin", "Browaeys", role = c("aut")),
person("Chananchida", "Sang-aram", role = c("aut", "cre"), email = "chananchida.sangaram@ugent.be"))
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.
Expand Down
12 changes: 6 additions & 6 deletions R/application_prediction.R
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ nichenet_seuratobj_aggregate = function(receiver, seurat_obj, condition_colname,
if (verbose == TRUE){print("Perform DE analysis in receiver cell")}

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

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

seurat_obj_subset = seurat_obj %>% subset(idents = sender_celltypes)
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
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
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
rm(seurat_obj_subset)

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

seurat_obj_receiver_affected= subset(seurat_obj, idents = receiver_affected)
seurat_obj_receiver_affected = SetIdent(seurat_obj_receiver_affected, value = seurat_obj_receiver_affected[[condition_colname]])
seurat_obj_receiver_affected = SetIdent(seurat_obj_receiver_affected, value = seurat_obj_receiver_affected[[condition_colname, drop=TRUE]])
seurat_obj_receiver_affected= subset(seurat_obj_receiver_affected, idents = condition_oi)

seurat_obj_receiver_reference= subset(seurat_obj, idents = receiver_reference)
seurat_obj_receiver_reference = SetIdent(seurat_obj_receiver_reference, value = seurat_obj_receiver_reference[[condition_colname]])
seurat_obj_receiver_reference = SetIdent(seurat_obj_receiver_reference, value = seurat_obj_receiver_reference[[condition_colname, drop=TRUE]])
seurat_obj_receiver_reference= subset(seurat_obj_receiver_reference, idents = condition_reference)

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

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

}
seuratObj_sender = SetIdent(seuratObj_sender, value = seuratObj_sender[[condition_colname]])
seuratObj_sender = SetIdent(seuratObj_sender, value = seuratObj_sender[[condition_colname, drop=TRUE]])
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")

SeuratV4 = c("avg_log2FC") %in% colnames(DE_table_sender)
Expand Down
2 changes: 1 addition & 1 deletion R/prioritization.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ process_table_to_ic = function(table_object, table_type = "expression",
#' ligand_target_matrix = ligand_target_matrix %>% .[!is.na(rownames(ligand_target_matrix)), !is.na(colnames(ligand_target_matrix))]
#'
#' # Ligand activity analysis
#' seurat_obj_receiver = subset(seurat_obj, idents = receiver) %>% SetIdent(value = .[["aggregate"]])
#' seurat_obj_receiver = subset(seurat_obj, idents = receiver) %>% SetIdent(value = .[["aggregate", drop = TRUE]])
#' geneset_oi = FindMarkers(object = seurat_obj_receiver, ident.1 = "LCMV, ident.2 = "SS, min.pct = 0.10) %>% rownames_to_column("gene") %>%
#' filter(p_val_adj <= 0.05 & abs(avg_log2FC) >= 0.25) %>% pull(gene) %>% .[. %in% rownames(ligand_target_matrix)]
#' expressed_genes_sender = sender_celltypes %>% unique() %>% lapply(get_expressed_genes, seurat_obj, 0.10) %>% unlist() %>% unique()
Expand Down
120 changes: 120 additions & 0 deletions R/supporting_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
rownames(RNA@scale.data) = newnames
}
}

if (length(RNA@var.features) > 0){
newnames = convert_alias_to_symbols(RNA@var.features, organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
RNA@var.features = newnames
}

if (nrow(RNA@meta.features) > 0){
newnames = convert_alias_to_symbols(rownames(RNA@meta.features), organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
rownames(RNA@meta.features) = newnames
}

} else {"Unequal gene sets: nrow(seurat_obj@assays$RNA) != nrow(newnames)"}

if(!is.null(RNA@counts)){
Expand Down Expand Up @@ -280,6 +297,28 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
}
}

if (length(RNA@var.features) > 0){
dim_before = length(RNA@var.features)
RNA@var.features = RNA@var.features %>% .[!is.na(.)]
dim_after = length(RNA@var.features)
if(dim_before != dim_after){
print("length of var.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}

if (nrow(RNA@meta.features) > 0){
dim_before = dim(RNA@meta.features)
RNA@meta.features = RNA@meta.features %>% .[!is.na(rownames(.)), ]
dim_after = dim(RNA@meta.features)
if(sum(dim_before != dim_after) > 0){
print("length of meta.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}

seurat_obj@assays$RNA = RNA

if(!is.null( seurat_obj@assays$SCT)){
Expand Down Expand Up @@ -313,6 +352,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
rownames(SCT@scale.data) = newnames
}
}

if (length(SCT@var.features) > 0){
newnames = convert_alias_to_symbols(SCT@var.features, organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
SCT@var.features = newnames
}

if (nrow(SCT@meta.features) > 0){
newnames = convert_alias_to_symbols(rownames(SCT@meta.features), organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
rownames(SCT@meta.features) = newnames
}

} else {"Unequal gene sets: nrow(seurat_obj@assays$SCT) != nrow(newnames)"}

if(!is.null(SCT@counts)){
Expand Down Expand Up @@ -345,6 +401,30 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
print(paste0("after: ",dim_before))
}
}

if (length(SCT@var.features) > 0){
dim_before = length(SCT@var.features)
SCT@var.features = SCT@var.features %>% .[!is.na(.)]
dim_after = length(SCT@var.features)
if(dim_before != dim_after){
print("length of var.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}


if (nrow(SCT@meta.features) > 0){
dim_before = dim(SCT@meta.features)
SCT@meta.features = SCT@meta.features %>% .[!is.na(rownames(.)), ]
dim_after = dim(SCT@meta.features)
if(sum(dim_before != dim_after) > 0){
print("length of meta.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}

seurat_obj@assays$SCT = SCT
}

Expand Down Expand Up @@ -383,6 +463,23 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
rownames(integrated@scale.data) = newnames
}
}

if (length(integrated@var.features) > 0){
newnames = convert_alias_to_symbols(integrated@var.features, organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
integrated@var.features = newnames
}

if (nrow(integrated@meta.features) > 0){
newnames = convert_alias_to_symbols(rownames(integrated@meta.features), organism = organism, verbose = FALSE)
doubles = newnames %>% table() %>% .[. > 1] %>% names()
genes_remove = (names(newnames[newnames %in% doubles]) != (newnames[newnames %in% doubles])) %>% .[. == TRUE] %>% names()
newnames[genes_remove] = genes_remove # set the doubles back to their old names
rownames(integrated@meta.features) = newnames
}

} else {"Unequal gene sets: nrow(seurat_obj@assays$integrated) != nrow(newnames)"}

if(!is.null(integrated@counts)){
Expand Down Expand Up @@ -415,6 +512,29 @@ alias_to_symbol_seurat = function(seurat_obj, organism) {
print(paste0("after: ",dim_before))
}
}

if (length(integrated@var.features) > 0){
dim_before = length(integrated@var.features)
integrated@var.features = integrated@var.features %>% .[!is.na(.)]
dim_after = length(integrated@var.features)
if(dim_before != dim_after){
print("length of var.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}

if (nrow(integrated@meta.features) > 0){
dim_before = dim(integrated@meta.features)
integrated@meta.features = integrated@meta.features %>% .[!is.na(rownames(.)), ]
dim_after = dim(integrated@meta.features)
if(sum(dim_before != dim_after) > 0){
print("length of meta.features changed")
print(paste0("before: ",dim_before))
print(paste0("after: ",dim_before))
}
}

seurat_obj@assays$integrated = integrated
}

Expand Down
4 changes: 2 additions & 2 deletions man/generate_prioritization_tables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-differential_nichenet.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("Differential NicheNet pipeline works", {
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

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

niches = list(
"LCMV_niche" = list(
Expand Down Expand Up @@ -90,7 +90,7 @@ test_that("Differential NicheNet pipeline works", {
length(geneset_niche2)

top_n_target = 250

niche_geneset_list = list(
"LCMV_niche" = list(
"receiver" = niches[[1]]$receiver,
Expand Down
2 changes: 1 addition & 1 deletion vignettes/differential_nichenet_pEMT.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ seurat_obj@meta.data$celltype_aggregate %>% table() %>% sort(decreasing = TRUE)

```{r}
celltype_id = "celltype_aggregate" # metadata column name of the cell type of interest
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id]])
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id, drop=TRUE]])
```

## Read in the NicheNet ligand-receptor network and ligand-target matrix
Expand Down
2 changes: 1 addition & 1 deletion vignettes/differential_nichenet_pEMT.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ seurat_obj@meta.data$celltype_aggregate %>% table() %>% sort(decreasing = TRUE)

``` r
celltype_id = "celltype_aggregate" # metadata column name of the cell type of interest
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id]])
seurat_obj = SetIdent(seurat_obj, value = seurat_obj[[celltype_id, drop=TRUE]])
```

## Read in the NicheNet ligand-receptor network and ligand-target matrix
Expand Down
Loading

0 comments on commit 4ae630d

Please sign in to comment.