2.4 Main Theme: Xenium
 
library("dplyr")
library("Seurat")
library("knitr")
library("ggplot2")
library("BiocManager")
library("here")
#BiocManager::install("EnhancedVolcano")
library("EnhancedVolcano") #volcano plot
#install.packages('DESeq2') #for DEG
library("DESeq2")
library("tidyverse") #tidy up data
library("styler") #tidy up data
library("scCustomize") #for color scales)
if (!require("kableExtra")) {install.packages("kableExtra"); require("kableExtra")} # for color brewer
if (!require("RColorBrewer")) {install.packages("RColorBrewer"); require("RColorBrewer")} # for color brewer
if (!require("sctransform")) {install.packages("sctransform"); require("sctransform")} # for data normalization
if (!require("glmGamPoi")) {BiocManager::install('glmGamPoi'); require("glmGamPoi")} # for data normalization, sctransform
if (!require("cowplot")) {install.packages("cowplot"); require("cowplot")} # for figure layout
if (!require("patchwork")) {install.packages("patchwork"); require("patchwork")} # for figure patching
if (!require("openxlsx")) {install.packages("openxlsx"); require("openxlsx")} # to save .xlsx files
# install.packages("styler")
set.seed(12345)
# here()Welcome to the Single-Cell Omics Research and Education Club!
If this is your time to the club, I want to extend and extra-special welcome to you!
I’m Jonathan Nelson, an Assistant Professor at the University of Southern California. I’m a wet scientist turned wet+dry scientist. I’ve been working with single-cell RNAseq data for the past 5 years and I’m excited to share what I’ve learned with you.
We believe that bioinformatics is a constantly evolving field, and that ongoing learning and professional development is essential to staying up-to-date. We encourage members to share their knowledge and experiences with each other, and to seek out opportunities for continued learning.
We believe that access to bioinformatics support should be available to everyone. We strive to create a welcoming and inclusive environment where all members can feel comfortable asking for help and contributing to the group.
We believe that working together is key to achieving success in bioinformatics. We value the diversity of perspectives and backgrounds that each member brings, and we encourage open communication and the sharing of ideas.
We believe in conducting ourselves with honesty and professionalism in all our interactions. We hold ourselves to high ethical standards and respect the privacy and confidentiality of all members.
We believe in approaching each other with empathy and kindness. We understand that bioinformatics can be a challenging and sometimes frustrating field, and we strive to support each other through these difficulties.
I know a lot of this has been going on in the background for everyone and I wanted to bring it to the forefront. My expectation is that we have 4-6 more meetings about spatial transcriptomics…and then we’ll re-evaluate.
Email me you would like me to add anyone: j.nelson@med.usc.edu
Today’s code (this html file) will be posted to the SCORE website (https://usckrc.github.io/website/score.html)
 
https://open.spotify.com/album/4hruYceqit29o6m4arpAql?si=8-2MnzK2SDyZLmsnF0Q1ig
 
 
  p <- VlnPlot(
    seurat_subset,
    features = gene,
    group.by = "subclass.l2",
    split.by = "Enrollment.Category",
    pt.size = 0.5 
  ) +
    stat_summary(
      fun = mean,
      geom = "point",
      color = "red",
      size = 5,
      position = position_dodge(width = 0.9)  # aligns dots with split violins
    ) +
    ggtitle(paste("Violin plot of", gene, "\n(Red dots = group means)")) +
    theme(axis.text.x = element_text(angle = 0, hjust = 0.5))
  
  print(p)https://github.com/jokergoo/ComplexHeatmap
fxn_genes <- c("SLC9A3", "ATP1A1", "ATP1B1",  "SLC5A1", "SLC5A2", "CLDN2", "SLC4A4", "SLC34A1", "SLC34A3")
injury_genes <- c("HAVCR1", "HIF1A", "HMOX1",  "PCK1")
albumin_genes <- c("LRP2", "CUBN", "CCL5", "DAB2", "CLTA", "CLTB", "CLTC", "VIL1")
differentiated_fxn_genes <- c("CTSL", "LAMP1","ABCD3", "SLC15A2")
# define columns and rows
pt_order <- c(
"HR_PT-S1", "AKI_PT-S1", "CKD_PT-S1", 
"HR_PT-S2", "AKI_PT-S2", "CKD_PT-S2", 
"HR_PT-S3", "AKI_PT-S3", "CKD_PT-S3")
all_genes <- c(fxn_genes, injury_genes, albumin_genes, differentiated_fxn_genes) 
# Use Seurat to get category+subclass averages
expr_avg <- AverageExpression(
  KPMP,
  features = all_genes,
  group.by = c("Enrollment.Category", "subclass.l2")
)$RNA
# keep only PT-S1–S3 subclasses
expr_avg <- expr_avg[, grepl("PT-S[123]", colnames(expr_avg))]
# rename Enrollment.Category with line breaks
colnames(expr_avg) <- gsub("Healthy Reference", "HR", colnames(expr_avg))
# scale by row
expr_scaled <- t(scale(t(expr_avg)))
# functional block mapping  
block_name <- c(
  setNames(rep("Markers of Function", length(fxn_genes)), fxn_genes),
  setNames(rep("Markers of Injury", length(injury_genes)), injury_genes),
  setNames(rep("Genes Related to Albumin Uptake", length(albumin_genes)), albumin_genes),
  setNames(rep("Genes Related to Differentiated Function", length(differentiated_fxn_genes)), differentiated_fxn_genes)
)
 row_anno <- rowAnnotation(
  "Functional Block" = block_name[rownames(expr_scaled)],
  col = list("Functional Block" = c(
    "Markers of Function" = "#8B0000",
    "Markers of Injury" = "#4169E1",
    "Genes Related to Albumin Uptake" = "#006400",
    "Genes Related to Differentiated Function" = "#f8d568"
  )),
  show_annotation_name = FALSE,  # hides the "Functional Block" text at bottom
 annotation_legend_param = list(
    title_gp = gpar(fontsize = 18, fontface = "bold"),   
    labels_gp = gpar(fontsize = 14)                      
  )
)
 # Make labels for plotting
col_labels <- gsub("_PT-S[123]", "", colnames(expr_scaled))
#enrollment <- sapply(strsplit(colnames(expr_scaled), "_"), `[`, 1)
col_anno <- HeatmapAnnotation(
  Enrollment = anno_text(
    col_labels, 
    gp = gpar(fontsize = 14), 
    just = "center",
    rot = 0),
  annotation_height = unit(3, "mm")
)
# subclass info for column splitting
col_groups <- sapply(strsplit(colnames(expr_scaled), "_"), `[`, 2)
# drop subclass suffix from labels (after groups are made)
#colnames(expr_scaled) <- gsub("_PT-S[123]", "", colnames(expr_scaled))
# orignal heatmap
Heatmap(
  expr_scaled,
  name = "Expression",
  col = col_fun,
  cluster_rows = FALSE,
  cluster_columns = FALSE,
  show_column_names = FALSE,
  row_split = block_name[rownames(expr_scaled)],
  row_title = NULL,
  row_names_gp = gpar(fontsize = 14, fontface = "bold"),
  left_annotation = row_anno,
  top_annotation = col_anno,
  column_split = col_groups,   # split by PT-S1, PT-S2, PT-S3
  column_gap = unit(5, "mm"), 
  column_title_gp = gpar(fontsize = 18, fontface = "bold"), 
  heatmap_legend_param = list(
    title = "Scaled Expr",
    at = c(-2, 0, 2),
    labels = c("Low", "Mid", "High"),
    title_gp = gpar(fontsize = 18, fontface = "bold"),
    labels_gp = gpar(fontsize = 14)
  )
)
 
https://uscnorriscancer.usc.edu/molecular-genomics-core/
analysis_summary.htmlKey MetricsDecodingCell SegmentationAnalysis
 
https://satijalab.org/seurat/articles/seurat5_spatial_vignette_2
arrow packagexenium.obj <- subset(xenium.obj, subset = nCount_Xenium > 0)
VlnPlot(xenium.obj, features = c("nFeature_Xenium", "nCount_Xenium"), ncol = 2, pt.size = 0)xenium.obj <- SCTransform(xenium.obj, assay = "Xenium")
xenium.obj <- RunPCA(xenium.obj, npcs = 30, features = rownames(xenium.obj))
xenium.obj <- RunUMAP(xenium.obj, dims = 1:30)
xenium.obj <- FindNeighbors(xenium.obj, reduction = "pca", dims = 1:30)
xenium.obj <- FindClusters(xenium.obj, resolution = 0.3)
DimPlot(xenium.obj)high_count_cells <- xenium.obj@meta.data %>%
  filter(nCount_Xenium > 2500) %>%
  rownames()
df_export <- data.frame(
  cell_id = high_count_cells,
  group   = "Higher_than_2500_counts"
) %>%
  tibble::rownames_to_column("row") %>%
  select(-row)
# Save as CSV
write.csv(df_export, "cellid_higher_than_2500_counts.csv", row.names = FALSE)meta.dataDo you have a coding problem that you’d like some
support on? 
 Do you have a topic you’d like covered
at a future meeting?
Email me: j.nelson@med.usc.edu
## R version 4.5.1 (2025-06-13 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26100)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8 
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## time zone: America/Los_Angeles
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] openxlsx_4.2.8              patchwork_1.3.2            
##  [3] cowplot_1.2.0               glmGamPoi_1.20.0           
##  [5] sctransform_0.4.2           RColorBrewer_1.1-3         
##  [7] kableExtra_1.4.0            scCustomize_3.2.0          
##  [9] styler_1.10.3               lubridate_1.9.4            
## [11] forcats_1.0.0               stringr_1.5.1              
## [13] purrr_1.1.0                 readr_2.1.5                
## [15] tidyr_1.3.1                 tibble_3.3.0               
## [17] tidyverse_2.0.0             DESeq2_1.48.2              
## [19] SummarizedExperiment_1.38.1 Biobase_2.68.0             
## [21] MatrixGenerics_1.20.0       matrixStats_1.5.0          
## [23] GenomicRanges_1.60.0        GenomeInfoDb_1.44.2        
## [25] IRanges_2.42.0              S4Vectors_0.46.0           
## [27] BiocGenerics_0.54.0         generics_0.1.4             
## [29] EnhancedVolcano_1.26.0      ggrepel_0.9.6              
## [31] here_1.0.1                  BiocManager_1.30.26        
## [33] ggplot2_3.5.2               knitr_1.50                 
## [35] Seurat_5.3.0                SeuratObject_5.2.0         
## [37] sp_2.2-0                    dplyr_1.1.4                
## 
## loaded via a namespace (and not attached):
##   [1] RcppAnnoy_0.0.22        splines_4.5.1           later_1.4.4            
##   [4] R.oo_1.27.1             polyclip_1.10-7         janitor_2.2.1          
##   [7] fastDummies_1.7.5       lifecycle_1.0.4         rprojroot_2.1.1        
##  [10] globals_0.18.0          lattice_0.22-7          MASS_7.3-65            
##  [13] magrittr_2.0.3          plotly_4.11.0           sass_0.4.10            
##  [16] rmarkdown_2.29          jquerylib_0.1.4         yaml_2.3.10            
##  [19] httpuv_1.6.16           zip_2.3.3               spam_2.11-1            
##  [22] spatstat.sparse_3.1-0   reticulate_1.43.0       pbapply_1.7-4          
##  [25] abind_1.4-8             Rtsne_0.17              R.cache_0.17.0         
##  [28] R.utils_2.13.0          circlize_0.4.16         GenomeInfoDbData_1.2.14
##  [31] irlba_2.3.5.1           listenv_0.9.1           spatstat.utils_3.1-5   
##  [34] goftest_1.2-3           RSpectra_0.16-2         spatstat.random_3.4-1  
##  [37] fitdistrplus_1.2-4      parallelly_1.45.1       svglite_2.2.1          
##  [40] codetools_0.2-20        DelayedArray_0.34.1     xml2_1.4.0             
##  [43] shape_1.4.6.1           tidyselect_1.2.1        UCSC.utils_1.4.0       
##  [46] farver_2.1.2            spatstat.explore_3.5-2  jsonlite_2.0.0         
##  [49] progressr_0.15.1        ggridges_0.5.7          survival_3.8-3         
##  [52] systemfonts_1.2.3       tools_4.5.1             ica_1.0-3              
##  [55] Rcpp_1.1.0              glue_1.8.0              gridExtra_2.3          
##  [58] SparseArray_1.8.1       xfun_0.53               withr_3.0.2            
##  [61] fastmap_1.2.0           digest_0.6.37           timechange_0.3.0       
##  [64] R6_2.6.1                mime_0.13               textshaping_1.0.3      
##  [67] ggprism_1.0.7           colorspace_2.1-1        scattermore_1.2        
##  [70] tensor_1.5.1            spatstat.data_3.1-8     R.methodsS3_1.8.2      
##  [73] data.table_1.17.8       httr_1.4.7              htmlwidgets_1.6.4      
##  [76] S4Arrays_1.8.1          uwot_0.2.3              pkgconfig_2.0.3        
##  [79] gtable_0.3.6            lmtest_0.9-40           XVector_0.48.0         
##  [82] htmltools_0.5.8.1       dotCall64_1.2           scales_1.4.0           
##  [85] png_0.1-8               snakecase_0.11.1        spatstat.univar_3.1-4  
##  [88] rstudioapi_0.17.1       tzdb_0.5.0              reshape2_1.4.4         
##  [91] nlme_3.1-168            GlobalOptions_0.1.2     cachem_1.1.0           
##  [94] zoo_1.8-14              KernSmooth_2.23-26      parallel_4.5.1         
##  [97] miniUI_0.1.2            vipor_0.4.7             ggrastr_1.0.2          
## [100] pillar_1.11.0           grid_4.5.1              vctrs_0.6.5            
## [103] RANN_2.6.2              promises_1.3.3          beachmat_2.24.0        
## [106] xtable_1.8-4            cluster_2.1.8.1         paletteer_1.6.0        
## [109] beeswarm_0.4.0          evaluate_1.0.5          cli_3.6.5              
## [112] locfit_1.5-9.12         compiler_4.5.1          rlang_1.1.6            
## [115] crayon_1.5.3            future.apply_1.20.0     rematch2_2.1.2         
## [118] plyr_1.8.9              ggbeeswarm_0.7.2        stringi_1.8.7          
## [121] viridisLite_0.4.2       deldir_2.0-4            BiocParallel_1.42.2    
## [124] lazyeval_0.2.2          spatstat.geom_3.5-0     Matrix_1.7-3           
## [127] RcppHNSW_0.6.0          hms_1.1.3               future_1.67.0          
## [130] shiny_1.11.1            ROCR_1.0-11             igraph_2.1.4           
## [133] bslib_0.9.0