zktuong / ktplots

Some tools for plotting single-cell data
https://zktuong.github.io/ktplots/
MIT License
162 stars 31 forks source link

NA/NaN/Inf in foreign function call #64

Closed connerlambden closed 1 year ago

connerlambden commented 1 year ago

Hi, when plotting with plot_cpdb3, I'm getting the following error. I've double checked and made sure that my sce (SingleCellExperiment) object has no nans or infs. All colsums/rowsums are > 1.

p <- plot_cpdb3(cell_type1 = 'seurat_cluster_0', cell_type2 = 'seurat_cluster_1', scdata = sce, idents = 'seurat_clusters_names', means = means, pvals = pvals, keep_significant_only = TRUE, standard_scale = TRUE, remove_self = TRUE ) p

Gives the following error:

Error in hclust(d): NA/NaN/Inf in foreign function call (arg 10) Traceback:

  1. plot_cpdb3(cell_type1 = "seurat_cluster_0", cell_type2 = "seurat_cluster_1", . scdata = sce, idents = "seurat_clusters_names", means = means, . pvals = pvals, keep_significant_only = TRUE, standard_scale = TRUE, . remove_self = TRUE)
  2. plot_cpdb(cell_type1 = cell_type1, cell_type2 = cell_type2, scdata = scdata, . idents = idents, split.by = split.by, means = means, pvals = pvals, . keep_significant_only = keep_significant_only, standard_scale = standard_scale, . return_table = TRUE, degs_analysis = degs_analysis, ...)
  3. hclust(d)

Any ideas?

Additionally, when I try to make a dotplot using the following:

plot_cpdb(cell_type1 = "seurat_cluster_0", cell_type2 = "seurat_cluster_1", scdata = sce, idents = 'seurat_clusters_names', means = means, pvals = pvals, genes = genes_to_plot)

I get the following error:

Warning message in rep(yes, length.out = len): “'x' is NULL so the result will be NULL” ERROR while rich displaying an object: Error in ans[ypos] <- rep(yes, length.out = len)[ypos]: replacement has length zero

Thanks for the help!

zktuong commented 1 year ago

Hi @connerlambden, does plot_cpdb work with keep_significant_only = FALSE? Also are your files from deg_analysis mode in CellPhoneDB? If so, you may need to add the option deg_analysis =TRUE.

connerlambden commented 1 year ago

@zktuong thanks for the help! This is not deg_analysis.

For plot_cpdb, when I set keep_significant_only = TRUE, I get the following error:

Error in which(unlist(anysig)): argument to 'which' is not logical Traceback:

  1. plot_cpdb(cell_type1 = "seurat_cluster_0", cell_type2 = "seurat_cluster_1", . scdata = sce, idents = "seurat_clusters_names", means = means, . pvals = pvals, keep_significant_only = TRUE, genes = genes_to_plot)
  2. which(unlist(anysig))
  3. which(unlist(anysig))

and when I set keep_significant_only = FALSE I get the same error as before (“'x' is NULL so the result will be NULL”)

I was able to get plot_cpdb3 to work!

However, plot_cpdb2 is giving the error:

Error in h(simpleError(msg, call)): error in evaluating the argument 'i' in selecting a method for function '[': error in evaluating the argument 'x' in selecting a method for function 'which': subscript contains invalid names Traceback:

  1. plot_cpdb2(cell_type1 = "seurat_cluster_0", cell_type2 = "seurat_cluster_1", . scdata = sce, idents = "seurat_clusters_names", means = means, . pvals = pvals, deconvoluted = decon, desiredInteractions = list(c("seurat_cluster_0", . "seurat_cluster_1"), c("seurat_cluster_1", "seurat_cluster_0")), . interaction_grouping = interaction_annotation, edge_group_colors = c(Activating = "#e15759", . Chemotaxis = "#59a14f", Inhibitory = "#4e79a7", Intracellular trafficking = "#9c755f", . DC_development = "#B07aa1", Unknown = "#e7e7e7"), node_group_colors = c(seurat_cluster_0 = "red", . seurat_cluster_1 = "blue"), keep_significant_only = FALSE, . standard_scale = TRUE, remove_self = TRUE)

And plot_cpdb4 gives the following warning message and then returns <NA>: Warning message: “Since you have set group, you should better set grid.col as a named vector where sector names are the vector names (should contain all sectors).” Warning message in structure(rand_color(length(unnamed_grid)), names = unnamed_grid): “restarting interrupted promise evaluation” Warning message in structure(rand_color(length(unnamed_grid)), names = unnamed_grid): “internal error -3 in R_decompress1”

Thanks.

zktuong commented 1 year ago

So there's a couple of issues here.

1) plot_cpdb

not sure what's going on, but your command is causing the internal functions to not generate a proper dataframe. is genes = genes_to_plot malformed?

2) plot_cpdb2 is interaction_grouping = interaction_annotation and edge_group_colors = c(Activating = "#e15759", . Chemotaxis = "#59a14f", Inhibitory = "#4e79a7", Intracellular trafficking = "#9c755f", . DC_development = "#B07aa1", Unknown = "#e7e7e7")

directly from the README? You should create one that is more complete for your dataset as my dataframe is only a subset of interactions.

saeedfc commented 1 year ago

Had same error with plot_cpdb, plot_cpdb2 etc,

cdb <- plot_cpdb(cell_type1 = celltype1, cell_type2 = celltype2,
              scdata = SOT_sc,
              idents = 'Fine_Annotation', # column name where the cell ids are located in the metadata
              means = means,
              pvals = pvals
Error in hclust(d) : NA/NaN/Inf in foreign function call (arg 10)

Apparently if I block the part where hierarchcial clustering is applied on the distance metrix; it works fine. SO i think the cluster_rows = T being default in plot_cpdb, this could carry over to plot_cpdb2 or 3 as well as they use plot_cpdb internally.

 # if (nrow(means_mat) > 2) {
  #   d <- dist(as.data.frame(means_mat))
  #   h <- hclust(d)
  #   means_mat <- means_mat[h$order, , drop = FALSE]
  #   pvals_mat <- pvals_mat[h$order, , drop = FALSE]

I retrieved the distance matrix;

a part of it looks like this ; I guess entire vectors with NA is the issue. Maybe including a filter on NA and warnign on which interaction is NA and hence being removed, could solve the issue

CXCL12-CXCR3                                                                                             
OSMR-OSM                                                                                                 
TGFB1-AR                                                                                                 
TGFB1-integrin-aVb6-complex                                                                              
ACKR3-CXCL12                                                                                             
IFNG-Type-II-IFNR                                                                                        
DPP4-CXCL12                                                                                              
TNF-TNFRSF1B                                                                                             
CXCL12-CXCR4                                                                                             
TGFB1-TGFbeta-receptor2                                                                                  
EGFR-TGFB1                                                                                               
TNF-TNFRSF1A                                                                                             
TGFB1-VDR                                                                                                
TGFB1-TGFBR3                                                                                             
TGFB1-TGFbeta-receptor1       0.15030635                                                                 
TNFRSF1B-GRN                  1.76503428              1.36840589                                         
TNFRSF1A-GRN                  0.31918960              0.60351636   1.07538970                            
TNFRSF1A-FASLG                        NA                      NA           NA           NA               
TNFSF13-TNFRSF1A                      NA                      NA           NA           NA             NA
zktuong commented 1 year ago

thanks @saeedfc

I think before adding the filter, i would like to first find out what's causing those NAs - are these with 0 interactions across the cellphonedb output?? By default cellphonedb would remove that prior to generating the output but I suppose it can still happen if you (un)luckily to land on celltypes-celltypes combinations that happened to not express the ligand receptors.

So, if you expand celltype1/2 to have more celltypes, does the error go away?

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.