scplotter to work with CODEX data prepared by Giotto¶

See: https://drieslab.github.io/Giotto_website/articles/codex_mouse_spleen.html

Go back to scplotter documentation: https://pwwang.github.io/scplotter/

InĀ [1]:
# GiottoData::getSpatialDataset(
#   dataset = "codex_spleen",
#   directory = "data/codex_spleen",
#   verbose = TRUE,
#   dryrun = FALSE
# )
InĀ [2]:
library(Giotto)

# Ensure Giotto can access a python env
genv_exists <- suppressMessages(checkGiottoEnvironment())
print(genv_exists)

python_path <- file.path(Sys.getenv("HOME"), "miniconda3", "envs", "giotto_env", "bin", "python")
Sys.setenv(RETICULATE_PYTHON = python_path)

invisible(capture.output(suppressMessages(set_giotto_python_path(python_path = python_path))))

# library(scplotter)
devtools::load_all()
Loading required package: GiottoClass

Newer devel version of GiottoClass on GitHub: 0.4.8

Giotto Suite 4.2.1

[1] TRUE
ℹ Loading scplotter
InĀ [3]:
library(Giotto)

## Set instructions
results_folder <- "data/codex_spleen.results"

instructions <- createGiottoInstructions(
    save_dir = results_folder,
    save_plot = FALSE,
    show_plot = TRUE,
    return_plot = TRUE,
    python_path = python_path
)

data_path <- "data/codex_spleen/"

# 2. create giotto object from provided paths ####
expr_path <- paste0(data_path, "codex_BALBc_3_expression.txt.gz")
loc_path <- paste0(data_path, "codex_BALBc_3_coord.txt")
meta_path <- paste0(data_path, "codex_BALBc_3_annotation.txt")

# read in data information

# expression info
codex_expression <- readExprMatrix(expr_path, transpose = FALSE)
# cell coordinate info
codex_locations <- data.table::fread(loc_path)
# metadata
codex_metadata <- data.table::fread(meta_path)

## stitch x.y tile coordinates to global coordinates
xtilespan <- 1344
ytilespan <- 1008

# TODO: expand the documentation and input format of stitchTileCoordinates. Probably not enough information for new users.
stitch_file <- stitchTileCoordinates(location_file = codex_metadata,
                                     Xtilespan = xtilespan,
                                     Ytilespan = ytilespan)
codex_locations <- stitch_file[,.(Xcoord, Ycoord)]

# create Giotto object
codex_test <- createGiottoObject(expression = codex_expression,
                                 spatial_locs = codex_locations,
                                 instructions = instructions)

codex_metadata$cell_ID <- as.character(codex_metadata$cellID)
codex_test <- addCellMetadata(codex_test, new_metadata = codex_metadata,
                              by_column = TRUE,
                              column_cell_ID = "cell_ID")

# subset Giotto object
cell_metadata <- pDataDT(codex_test)
cell_IDs_to_keep <- cell_metadata[Imaging_phenotype_cell_type != "dirt" & Imaging_phenotype_cell_type != "noid" & Imaging_phenotype_cell_type != "capsule",]$cell_ID

codex_test <- subsetGiotto(codex_test,
                           cell_ids = cell_IDs_to_keep)

## filter
codex_test <- filterGiotto(gobject = codex_test,
                           expression_threshold = 1,
                           feat_det_in_min_cells = 10,
                           min_det_feats_per_cell = 2,
                           expression_values = "raw",
                           verbose = TRUE)

codex_test <- normalizeGiotto(gobject = codex_test,
                              scalefactor = 6000,
                              verbose = TRUE,
                              log_norm = FALSE,
                              library_size_norm = FALSE,
                              scale_feats = FALSE,
                              scale_cells = TRUE)

## add gene & cell statistics
codex_test <- addStatistics(gobject = codex_test,
                            expression_values = "normalized")

## adjust expression matrix for technical or known variables
codex_test <- adjustGiottoMatrix(gobject = codex_test,
                                 expression_values = "normalized",
                                 batch_columns = "sample_Xtile_Ytile",
                                 covariate_columns = NULL,
                                 return_gobject = TRUE,
                                 update_slot = "custom")
python already initialized in this session
 active environment : 'giotto_env'
 python version : 3.10

completed 1: preparation

completed 2: subset expression data

completed 3: subset spatial locations

completed 4: subset cell metadata

completed 5: subset feature metadata

completed 6: subset spatial network(s)

completed 7: subsetted dimension reductions

completed 8: subsetted nearest network(s)

completed 9: subsetted spatial enrichment results

Feature type:  rna 
Number of cells removed:  2  out of  80019 
Number of feats removed:  0  out of  30 
Setting expression [cell][rna] normalized

Setting expression [cell][rna] scaled

calculating statistics for "normalized" expression

Warning message:
ā€œThe `update_slot` argument of `adjustGiottoMatrix()` is deprecated as of <NA>
4.1.7.
ℹ Please use the `name` argument instead.
ℹ The deprecated feature was likely used in the base package.
  Please report the issue to the authors.ā€
Setting expression [cell][rna] custom

InĀ [4]:
options(repr.plot.width = 7, repr.plot.height = 7)

SpatDimPlot(codex_test, color_by = "lightblue", size = 0.25)
No description has been provided for this image
InĀ [5]:
options(repr.plot.width = 14, repr.plot.height = 7)

SpatDimPlot(
    codex_test,
    group_by = "sample_Xtile_Ytile",
    size = 0.2
)
No description has been provided for this image
InĀ [6]:
# PCA
codex_test <- runPCA(gobject = codex_test,
                     expression_values = "normalized",
                     scale_unit = TRUE,
                     method = "factominer")
# UMAP
codex_test <- runUMAP(codex_test,
                      dimensions_to_use = 1:14,
                      n_components = 2,
                      n_threads = 12)
## sNN network (default)
codex_test <- createNearestNetwork(gobject = codex_test,
                                   dimensions_to_use = 1:14,
                                   k = 20)

## 0.1 resolution
codex_test <- doLeidenCluster(gobject = codex_test,
                              resolution = 0.5,
                              n_iterations = 100,
                              name = "leiden")

clusters_cell_types <- c("naive B cells", "B cells", "B cells", "naive B cells",
                         "B cells", "macrophages", "erythroblasts",
                         "erythroblasts", "erythroblasts", "CD8 + T cells",
                         "Naive T cells", "CD4+ T cells", "Naive T cells",
                         "CD4+ T cells", "Dendritic cells", "NK cells",
                         "Dendritic cells", "Plasma cells", "endothelial cells",
                         "monocytes")

names(clusters_cell_types) <- c(2, 15, 13, 5, 8, 9, 19, 1, 10, 3, 12, 14, 4, 6,
                                7, 16, 17, 18, 11, 20)

codex_test <- annotateGiotto(gobject = codex_test,
                             annotation_vector = clusters_cell_types,
                             cluster_column = "leiden",
                             name = "cell_types")

cell_metadatadata <- pDataDT(codex_test)
subset_cell_ids <- cell_metadatadata[sample_Xtile_Ytile=="BALBc-3_X04_Y08"]$cell_ID

codex_test_zone1 <- subsetGiotto(codex_test,
                                 cell_ids = subset_cell_ids)
"hvf" was not found in the gene metadata information.
 all genes will be used.

Warning message in .run_pca_factominer(x = t_flex(expr_values), scale = scale_unit, :
ā€œncp > ncol(x), will be set to ncol(x)ā€
Setting dimension reduction [cell][rna] pca

Setting dimension reduction [cell][rna] umap

InĀ [48]:
options(repr.plot.width = 14, repr.plot.height = 6)

p1 <- CellDimPlot(codex_test, reduction = "pca", group_by = "cell_types")
p2 <- CellDimPlot(codex_test, reduction = "umap", group_by = "cell_types")

p1 + p2
No description has been provided for this image
InĀ [49]:
options(repr.plot.width = 7, repr.plot.height = 6)

CellDimPlot(codex_test, reduction = "umap", group_by = "leiden")
No description has been provided for this image
InĀ [7]:
options(repr.plot.width = 7, repr.plot.height = 7)

codex_test@cell_metadata$cell$rna$leiden <- factor(
    as.character(codex_test@cell_metadata$cell$rna$leiden),
    levels = unique(as.character(sort(codex_test@cell_metadata$cell$rna$leiden))))

SpatDimPlot(
    codex_test,
    group_by = "leiden",
    size = 0.2
)
No description has been provided for this image
InĀ [8]:
options(repr.plot.width = 12, repr.plot.height = 7)

SpatDimPlot(
    codex_test,
    group_by = "Imaging_phenotype_cell_type",
    size = 0.2
)
No description has been provided for this image
InĀ [9]:
options(repr.plot.width = 8, repr.plot.height = 5)

SpatDimPlot(
    codex_test_zone1,
    group_by = "Imaging_phenotype_cell_type",
    size = 1
)
No description has been provided for this image
InĀ [10]:
options(repr.plot.width = 10, repr.plot.height = 5)

SpatFeaturePlot(
    codex_test_zone1,
    features = c("CD8a","CD19"),
    layer = "scaled",
    size = 1
)
No description has been provided for this image
InĀ [11]:
x <- sessionInfo()
x <- capture.output(print(x))
# hide the BLAS/LAPACK paths
x <- x[!startsWith(x, "BLAS/LAPACK:")]
cat(paste(x, collapse = "\n"))
R version 4.4.3 (2025-02-28)
Platform: x86_64-conda-linux-gnu
Running under: Red Hat Enterprise Linux 8.10 (Ootpa)

Matrix products: default

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Chicago
tzcode source: system (glibc)

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

other attached packages:
[1] scplotter_0.4.0   Giotto_4.2.1      GiottoClass_0.4.7

loaded via a namespace (and not attached):
  [1] fs_1.6.6                    matrixStats_1.5.0          
  [3] spatstat.sparse_3.1-0       bitops_1.0-9               
  [5] sf_1.0-20                   devtools_2.4.5             
  [7] httr_1.4.7                  RColorBrewer_1.1-3         
  [9] repr_1.1.7                  profvis_0.4.0              
 [11] tools_4.4.3                 sctransform_0.4.2          
 [13] backports_1.5.0             DT_0.33                    
 [15] R6_2.6.1                    uwot_0.2.3                 
 [17] lazyeval_0.2.2              urlchecker_1.0.1           
 [19] withr_3.0.2                 sp_2.2-0                   
 [21] gridExtra_2.3               GiottoUtils_0.2.5          
 [23] progressr_0.15.1            quantreg_6.00              
 [25] cli_3.6.5                   Biobase_2.62.0             
 [27] spatstat.explore_3.4-3      fastDummies_1.7.5          
 [29] flashClust_1.01-2           iNEXT_3.0.1                
 [31] sandwich_3.1-1              labeling_0.4.3             
 [33] Seurat_5.3.0                mvtnorm_1.3-3              
 [35] spatstat.data_3.1-6         proxy_0.4-27               
 [37] ggridges_0.5.6              pbapply_1.7-2              
 [39] pbdZMQ_0.3-14               dbscan_1.2.2               
 [41] R.utils_2.13.0              stringdist_0.9.15          
 [43] parallelly_1.45.0           sessioninfo_1.2.3          
 [45] limma_3.62.2                VGAM_1.1-13                
 [47] rstudioapi_0.17.1           generics_0.1.4             
 [49] shape_1.4.6.1               gtools_3.9.5               
 [51] ica_1.0-3                   spatstat.random_3.4-1      
 [53] dplyr_1.1.4                 leaps_3.2                  
 [55] Matrix_1.7-3                S4Vectors_0.40.2           
 [57] abind_1.4-5                 R.methodsS3_1.8.2          
 [59] terra_1.8-42                lifecycle_1.0.4            
 [61] scatterplot3d_0.3-44        multcomp_1.4-28            
 [63] SummarizedExperiment_1.32.0 SparseArray_1.2.4          
 [65] Rtsne_0.17                  grid_4.4.3                 
 [67] promises_1.3.2              crayon_1.5.3               
 [69] miniUI_0.1.2                lattice_0.22-7             
 [71] cowplot_1.1.3               pillar_1.10.2              
 [73] GenomicRanges_1.54.1        rjson_0.2.23               
 [75] estimability_1.5.1          future.apply_1.20.0        
 [77] codetools_0.2-20            glue_1.8.0                 
 [79] spatstat.univar_3.1-3       data.table_1.17.4          
 [81] remotes_2.5.0               vctrs_0.6.5                
 [83] png_0.1-8                   spam_2.11-1                
 [85] gtable_0.3.6                assertthat_0.2.1           
 [87] cachem_1.1.0                S4Arrays_1.2.1             
 [89] mime_0.13                   tidygraph_1.3.0            
 [91] survival_3.8-3              SingleCellExperiment_1.24.0
 [93] units_0.8-5                 statmod_1.5.0              
 [95] TH.data_1.1-3               ellipsis_0.3.2             
 [97] scRepertoire_2.2.1          fitdistrplus_1.2-2         
 [99] ROCR_1.0-11                 nlme_3.1-168               
[101] usethis_3.1.0               RcppAnnoy_0.0.22           
[103] evd_2.3-7.1                 GenomeInfoDb_1.38.8        
[105] rprojroot_2.0.4             irlba_2.3.5.1              
[107] KernSmooth_2.23-26          DBI_1.2.3                  
[109] plotthis_0.7.1              colorspace_2.1-1           
[111] BiocGenerics_0.48.1         tidyselect_1.2.1           
[113] emmeans_1.11.1              compiler_4.4.3             
[115] SparseM_1.84-2              xml2_1.3.8                 
[117] desc_1.4.3                  ggdendro_0.2.0             
[119] DelayedArray_0.28.0         plotly_4.10.4              
[121] checkmate_2.3.2             scales_1.4.0               
[123] classInt_0.4-11             lmtest_0.9-40              
[125] multcompView_0.1-10         rappdirs_0.3.3             
[127] stringr_1.5.1               digest_0.6.37              
[129] goftest_1.2-3               spatstat.utils_3.1-4       
[131] XVector_0.42.0              htmltools_0.5.8.1          
[133] GiottoVisuals_0.2.12        pkgconfig_2.0.3            
[135] base64enc_0.1-3             MatrixGenerics_1.14.0      
[137] FactoMineR_2.11             fastmap_1.2.0              
[139] rlang_1.1.6                 GlobalOptions_0.1.2        
[141] htmlwidgets_1.6.4           shiny_1.10.0               
[143] farver_2.1.2                zoo_1.8-14                 
[145] jsonlite_2.0.0              R.oo_1.27.1                
[147] RCurl_1.98-1.17             magrittr_2.0.3             
[149] GenomeInfoDbData_1.2.11     dotCall64_1.2              
[151] patchwork_1.3.0             IRkernel_1.3.2             
[153] Rcpp_1.0.14                 evmix_2.12                 
[155] ggnewscale_0.5.1            viridis_0.6.5              
[157] reticulate_1.42.0           truncdist_1.0-2            
[159] stringi_1.8.7               ggalluvial_0.12.5          
[161] ggraph_2.2.1                zlibbioc_1.48.2            
[163] MASS_7.3-64                 plyr_1.8.9                 
[165] pkgbuild_1.4.8              parallel_4.4.3             
[167] listenv_0.9.1               ggrepel_0.9.6              
[169] forcats_1.0.0               deldir_2.0-4               
[171] graphlayouts_1.2.2          IRdisplay_1.1              
[173] splines_4.4.3               gridtext_0.1.5             
[175] tensor_1.5                  circlize_0.4.16            
[177] colorRamp2_0.1.0            igraph_2.0.3               
[179] uuid_1.2-1                  spatstat.geom_3.4-1        
[181] cubature_2.1.4              RcppHNSW_0.6.0             
[183] reshape2_1.4.4              stats4_4.4.3               
[185] pkgload_1.4.0               evaluate_1.0.3             
[187] SeuratObject_5.1.0          tweenr_2.0.3               
[189] httpuv_1.6.15               MatrixModels_0.5-4         
[191] RANN_2.6.2                  tidyr_1.3.1                
[193] purrr_1.0.4                 polyclip_1.10-7            
[195] future_1.58.0               scattermore_1.2            
[197] ggplot2_3.5.2               ggforce_0.4.2              
[199] xtable_1.8-4                e1071_1.7-16               
[201] RSpectra_0.16-2             later_1.4.2                
[203] class_7.3-23                viridisLite_0.4.2          
[205] gsl_2.1-8                   tibble_3.2.1               
[207] memoise_2.0.1               IRanges_2.36.0             
[209] cluster_2.1.8.1             globals_0.18.0