Skip to content

BUG: DimPlot ignores the reduction parameter if multiple reductions have the same key #9872

Closed
@nigiord

Description

@nigiord

Issue Description

When using DimPlot on a SeuratObject with multiple reductions that have the same key (for instance, PC_), the reduction parameter is ignored and the last inserted reduction is always the one showed.

Reproducing Code Example

library(Seurat)

genes <- paste0("Gene", 1:10)
counts <- matrix(rpois(40, lambda = 10), ncol = 4)
rownames(counts) <- genes
colnames(counts) <- paste0("cell", 1:4)

dummyobj <- CreateSeuratObject(counts = counts)

pcs <- prcomp(x = (dummyobj[["RNA"]]$counts))
rev_pcs <- prcomp(x = t(dummyobj[["RNA"]]$counts))

pca <- CreateDimReducObject(
  embeddings = pcs$rotation,
  loadings = pcs$x,
  stdev = pcs$sdev,
  key = "PC_",
  assay = "RNA"
)

rev_pca <- CreateDimReducObject(
  embeddings = rev_pcs$x,
  loadings = rev_pcs$rotation,
  stdev = rev_pcs$sdev,
  key = "PC_",  ## same key as above
  assay = "RNA"
)

dummyobj@reductions[["PCA"]] <- pca
dummyobj@reductions[["PCA_REV"]] <- rev_pca

DimPlot(dummyobj, reduction = "PCA")
DimPlot(dummyobj, reduction = "PCA_REV") # Plot exactly the same thing

Error Message

No error message, but only PCA_REV is plotted whichever reduction you choose, even if the reductions are about different assays.

Additional Comments

Bug comes from the use of FetchData there:

seurat/R/visualization.R

Lines 909 to 914 in 9354a78

data <- FetchData(
object = object,
vars = c(dims, group.by),
cells = cells,
clean = 'project'
)

The function does not take into account the reduction, only the name of the key, so it does not work as expected when you have multiple reductions with the same key.

Session Info

R version 4.3.3 (2024-02-29)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Big Sur ... 10.16

Matrix products: default
BLAS/LAPACK: /Users/giordano-n/micromamba/envs/muscadet/lib/libopenblasp-r0.
3.29.dylib;  LAPACK version 3.12.0

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

time zone: Europe/Paris
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] glue_1.8.0                  stringr_1.5.1              
 [3] dplyr_1.1.4                 Seurat_5.1.0               
 [5] SeuratObject_5.0.2          sp_2.2-0                   
 [7] SummarizedExperiment_1.32.0 Biobase_2.62.0             
 [9] GenomicRanges_1.54.1        GenomeInfoDb_1.38.1        
[11] IRanges_2.36.0              S4Vectors_0.40.2           
[13] BiocGenerics_0.48.1         MatrixGenerics_1.14.0      
[15] matrixStats_1.2.0           muscadet_0.1.0             
[17] logger_0.2.2                nvimcom_0.9-167            

loaded via a namespace (and not attached):
  [1] RcppAnnoy_0.0.22         splines_4.3.3           
  [3] later_1.4.1              prettyGraphs_2.1.6      
  [5] bitops_1.0-9             tibble_3.2.1            
  [7] polyclip_1.10-7          fastDummies_1.7.5       
  [9] lifecycle_1.0.4          fastcluster_1.2.6       
 [11] doParallel_1.0.17        globals_0.16.3          
 [13] lattice_0.22-6           prabclus_2.3-4          
 [15] MASS_7.3-60.0.1          dendextend_1.17.1       
 [17] magrittr_2.0.3           plotly_4.10.4           
 [19] httpuv_1.6.15            sctransform_0.4.1       
 [21] spam_2.11-1              flexmix_2.3-20          
 [23] spatstat.sparse_3.1-0    reticulate_1.41.0.1     
 [25] cowplot_1.1.3            pbapply_1.7-2           
 [27] RColorBrewer_1.1-3       ade4_1.7-23             
 [29] facets_0.6.2             abind_1.4-5             
 [31] zlibbioc_1.48.0          Rtsne_0.17              
 [33] purrr_1.0.4              RCurl_1.98-1.16         
 [35] nnet_7.3-20              circlize_0.4.16         
 [37] GenomeInfoDbData_1.2.11  ggrepel_0.9.6           
 [39] irlba_2.3.5.1            listenv_0.9.1           
 [41] spatstat.utils_3.1-3     goftest_1.2-3           
 [43] RSpectra_0.16-2          spatstat.random_3.3-2   
 [45] fitdistrplus_1.2-2       parallelly_1.42.0       
 [47] leiden_0.4.3.1           codetools_0.2-20        
 [49] DelayedArray_0.28.0      tidyselect_1.2.1        
 [51] shape_1.4.6.1            farver_2.1.2            
 [53] viridis_0.6.5            spatstat.explore_3.3-4  
 [55] jsonlite_1.9.1           GetoptLong_1.0.5        
 [57] e1071_1.7-16             progressr_0.15.1        
 [59] ggridges_0.5.6           survival_3.8-3          
 [61] iterators_1.0.14         foreach_1.5.2           
 [63] tools_4.3.3              ica_1.0-3               
 [65] Rcpp_1.0.14              gridExtra_2.3           
 [67] SparseArray_1.2.2        withr_3.0.2             
 [69] alluvial_0.1-2           fastmap_1.2.0           
 [71] SNFtool_2.3.1            caTools_1.18.2          
 [73] digest_0.6.37            R6_2.6.1                
 [75] mime_0.13                colorspace_2.1-1        
 [77] scattermore_1.2          tensor_1.5              
 [79] gtools_3.9.5             spatstat.data_3.1-6     
 [81] diptest_0.77-1           tidyr_1.3.1             
 [83] generics_0.1.3           data.table_1.14.10      
 [85] robustbase_0.99-4-1      class_7.3-23            
 [87] httr_1.4.7               htmlwidgets_1.6.4       
 [89] S4Arrays_1.2.0           uwot_0.2.3              
 [91] pkgconfig_2.0.3          gtable_0.3.6            
 [93] modeltools_0.2-23        ComplexHeatmap_2.18.0   
 [95] lmtest_0.9-40            XVector_0.42.0          
 [97] htmltools_0.5.8.1        dotCall64_1.2           
 [99] clusterSim_0.51-5        clue_0.3-66             
[101] scales_1.3.0             png_0.1-8               
[103] spatstat.univar_3.1-2    reshape2_1.4.4          
[105] rjson_0.2.23             nlme_3.1-167            
[107] proxy_0.4-27             zoo_1.8-13              
[109] GlobalOptions_0.1.2      KernSmooth_2.23-26      
[111] parallel_4.3.3           miniUI_0.1.1.1          
[113] ExPosition_2.8.23        RcppZiggurat_0.1.6      
[115] pillar_1.10.1            grid_4.3.3              
[117] vctrs_0.6.5              RANN_2.6.2              
[119] promises_1.3.2           xtable_1.8-4            
[121] cluster_2.1.8.1          cli_3.6.4               
[123] compiler_4.3.3           rlang_1.1.5             
[125] crayon_1.5.3             pctGCdata_0.3.0         
[127] future.apply_1.11.3      labeling_0.4.3          
[129] mclust_6.1.1             plyr_1.8.9              
[131] stringi_1.8.4            deldir_2.0-4            
[133] viridisLite_0.4.2        munsell_0.5.1           
[135] lazyeval_0.2.2           spatstat.geom_3.3-5     
[137] Matrix_1.6-4             RcppHNSW_0.6.0          
[139] patchwork_1.3.0          sparseMatrixStats_1.14.0
[141] future_1.34.0            ggplot2_3.5.1           
[143] fpc_2.2-13               shiny_1.10.0            
[145] kernlab_0.9-33           ROCR_1.0-11             
[147] Rfast_2.1.0              igraph_1.5.1            
[149] RcppParallel_5.1.9       DEoptimR_1.1-3-1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions