saeyslab / nichenetr

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

Error finding log fold change information of ligands from sender cells (Seurat V5 object) #258

Closed niksa01 closed 6 months ago

niksa01 commented 8 months ago

Hi,

I was running the following code (from the seurat_steps vignette) to find the log fold change information of ligands from sender cells

DE_table_all = Idents(seuratObj) %>% levels() %>% intersect(sender_celltypes) %>% lapply(get_lfc_celltype, seurat_obj = seuratObj, condition_colname = "orig.ident", condition_oi = condition_oi, condition_reference = condition_reference, expression_pct = 0.10, celltype_col = "clustergroup" ) %>% reduce(full_join)

but I ran into the following error

Error in FindMarkers.SCTAssay(object = data.use, latent.vars = latent.vars, : Object contains multiple models with unequal library sizes. Run PrepSCTFindMarkers() before running FindMarkers()

Could anyone help with this? Thanks!

Screen Shot 2024-03-08 at 9 22 39 AM
csangara commented 7 months ago

Hi,

Did you try already running PrepSCTFindMarkers() prior to running the code above, as suggested by the error message?

niksa01 commented 7 months ago

Yes, I did

csangara commented 7 months ago

What happens when you run FindMarkers on your object? get_lfc_celltype uses FindMarkers internally, so could you try running this (assuming that the Idents() of your object is the celltype)?

FindMarkers(seuratObj, ident.1 = condition_oi, ident.2 = condition_reference, subset.ident = celltype_name, group.by = condition_colname, min.pct = 0.1, logfc.threshold = 0.05)

For instance, if I run this on the example data,

FindMarkers(seuratObj, ident.1 = 'LCMV', ident.2 = 'SS', subset.ident = 'Mono', group.by = 'aggregate', min.pct = 0.1, logfc.threshold = 0.05)

It would give me the DE genes between LCMV and SS conditions in monocytes, which corresponds to the 'Mono' column returned by get_lfc_celltype.

csangara commented 7 months ago

I just recalled that to fix this issue, you should set recorrect_umi = FALSE in FindMarkers(): https://satijalab.org/seurat/archive/v4.3/sctransform_v2_vignette

If running on a subset of the original object after running PrepSCTFindMarkers(), FindMarkers() should be invoked with recorrect_umi = FALSE to use the existing corrected counts:

csangara commented 7 months ago

However, get_lfc_celltype currently doesn't allow you to pass extra parameters into FindMarkers. You can reproduce the results of get_lfc_celltype with something like this:

lapply(Idents(seuratObj) %>% levels() %>% intersect(sender_celltypes),
       function(ct) FindMarkers(seuratObj, ident.1 = condition_oi, ident.2 = condition_reference, subset.ident = ct,
                                group.by = "aggregate", min.pct = 0.1, logfc.threshold = 0.05, recorrect_umi = FALSE) %>%
         rownames_to_column("gene") %>% select(gene, avg_log2FC) %>% rename(!!ct := avg_log2FC)) %>%
  reduce(full_join)

In the next patch where I fix some bugs with Seurat V5 objects, I will allow for extra arguments to be passed from get_lfc_celltype.

csangara commented 6 months ago

get_lfc_celltype now allows you to pass extra arguments to FindMarkers