saeyslab / nichenetr

NicheNet: predict active ligand-target links between interacting cells
469 stars 117 forks source link

DE Sender Expression Ident Error #198

Closed jchincheong closed 5 months ago

jchincheong commented 1 year ago

Hi when trying to run the following command:

DE_table_all = Idents(seurat_obj) %>% levels() %>% intersect(sender_celltypes) %>% lapply(get_lfc_celltype, seurat_obj = seuratObj, condition_colname = "type", condition_oi = condition_oi, condition_reference = condition_reference, expression_pct = 0.10, celltype_col = NULL) %>% reduce(full_join) I get the following error:

Error in `Idents<-`:
! 'value' must be a factor or vector

I can't seem to figure out what the issue is. Or if it pertains to my seurat object or something else. Any input would greatly be appreciated.

csangara commented 1 year ago

Hi,

This indeed could be an issue with your Seurat object - can you run Idents(seurat_obj) to see what comes out?

If there's no identity that was set, you can either give the celltype column information in celltype_col or set the identity of your Seurat object with something like SetIdent(seuratObj, value = seuratObj[["celltype"]]) (assuming "celltype" is the metadata column containing cell type information).

In the Seurat object we provided in the vignette, the identity was already set as the cell type.

mesposit commented 1 year ago

I have the exact same issue as jchincheong:

I ran Idents(seuratObj) <- "celltype" as my cell type information is stored in a meta data column called "celltype" in my Seurat object. When I check what is now defined as the identity in my Seurat object, it looks fine:

> class(Idents(seuratObj))
[1] "factor"
> levels(Idents(seuratObj))
[1] "T"                       "CD14 Mono"               "NK"                      "Intermediate Mono"      
[5] "B memory & intermediate" "CD16 Mono"               "Conventional DC"         "B naive"                
[9] "Plasmacytoid DC"   

However, when I try to run get_lfc_celltype, I run into the same problem again. I also tried it with celltype_col = "celltype" but this didn't change anything.

Thanks in advance for any help!

mesposit commented 1 year ago

Hi again :)

I've just tried it again using the example data set you provide in your step-by-step vignette and there I run into the same error.

Is it possible that maybe the NichNet workflow described in this vignette is not compatible anymore with the most recent version of Seurat? I also had to update the Seurat object you provide using UpdateSeuratObject(object = seuratObj) to be able to work through the tutorial.

jchincheong commented 1 year ago

Hi I can confirm I was able to run my seurat object on a older version of seurat I had installed on a laptop. So the version of seurat may be the issue.

jessicaliu70 commented 1 year ago

I met the same error when running nichenet_seuratobj_aggregate . I think this is due to the seurat[[condition_colname]] return a data frame (maybe for newer seurat version), while it actually need a list, it should be seurat$condition_colname. I tried to fix the function myself, but failed. Can the developer fix this? Here is the code in nichenet_seuratobj_aggregate function: seurat_obj_receiver_affected = SetIdent(seurat_obj_receiver_affected, value = seurat_obj_receiver_affected[[condition_colname]])

colinmccornack commented 1 year ago

This temp fix worked for me for Seurat v5 objects when leaving the celltype null, though I think it might be less flexible since it doesn't call the SetIdent function (?), YMMV

get_lfc_celltype_edited = function(celltype_oi, seurat_obj, condition_colname, condition_oi, condition_reference, celltype_col = "celltype", expression_pct = 0.10){
  requireNamespace("Seurat")
  requireNamespace("dplyr")
  if(!is.null(celltype_col)){
    seurat_obj_celltype = SetIdent(seurat_obj, value = seurat_obj$celltype_col)
    seuratObj_sender = subset(seurat_obj_celltype, idents = celltype_oi)

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

  }
  Idents(seuratObj_sender) = condition_colname
  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)

  if(SeuratV4 == TRUE){
    DE_table_sender = DE_table_sender %>% as_tibble() %>% select(-p_val) %>% select(gene, avg_log2FC)
  } else {
    DE_table_sender = DE_table_sender %>% as_tibble() %>% select(-p_val) %>% select(gene, avg_logFC)
  }

  colnames(DE_table_sender) = c("gene",celltype_oi)
  return(DE_table_sender)
}
csangara commented 1 year ago

Hi all,

I will be working on fixing this ASAP, although it may take a while because I want to ensure that everything is still compatible with the previous Seurat versions.

cathalgking commented 1 year ago

Hi @csangara . Just checking if there has been any update on this issue?

csangara commented 1 year ago

Hi all,

This issue has been fixed in NicheNet v2.0.4. The vignettes and functions should now work with both Seurat v4 and v5.

As @jessicaliu70 mentioned, the issue was indeed with seurat_obj[[condition_colname]]. Although this operation returns a dataframe in both v4 and v5, an additional check has now been added in v5:

  if (!(is.factor(x = value) || is.atomic(x = value))) {
    abort(message = "'value' must be a factor or vector")
  }

Therefore, this can be fixed by either using seurat$condition_colname or seurat[[condition_colname, drop = TRUE]].

Thank you for your patience, and please let me know if there are still errors.