Packages
library(HGNChelper)
library(openxlsx)
source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/gene_sets_prepare.R"); source("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/R/sctype_score_.R")
gs_list <- gene_sets_prepare("https://raw.githubusercontent.com/IanevskiAleksandr/sc-type/master/ScTypeDB_short.xlsx", "Brain") # e.g. Immune system, Liver, Pancreas, Kidney, Eye, Brain
cluster_resolution = "RNA_snn_res.0.4"
# extract scaled scRNA-seq matrix
seurat_package_v5 <- isFALSE('counts' %in% names(attributes(obj.srt[["RNA"]])));
scRNAseqData_scaled <- if (seurat_package_v5) as.matrix(obj.srt[["RNA"]]$scale.data) else as.matrix(obj.srt[["RNA"]]@scale.data)
# run ScType
es.max <- sctype_score(scRNAseqData = scRNAseqData_scaled,
scaled = TRUE,
gs = gs_list$gs_positive,
gs2 = gs_list$gs_negative)
cL_results <- do.call("rbind", lapply(unique(obj.srt@meta.data[, cluster_resolution]), function(cl) {
cl_cells <- rownames(obj.srt@meta.data[obj.srt@meta.data[, cluster_resolution] == cl, ])
# Maximum scores of each clusters
es.max.cl <- sort(rowSums(es.max[, cl_cells]), decreasing = TRUE)
# data frame
df <- data.frame(
cluster = cl,
type = names(es.max.cl),
scores = es.max.cl,
ncells = sum(obj.srt@meta.data[, cluster_resolution] == cl)
)
# top 10
return(head(df, 10))
}))
# cL_results %>% saveRDS(paste0(dir, "rds/cL_results.rds"))
sctype_scores <- cL_results %>% group_by(cluster) %>% top_n(n = 5, wt = scores)
sctype_scores <- sctype_scores %>% filter(scores> 0)
sctype_scores$cluster = paste0("cluster ", sctype_scores$cluster)
sctype_scores %>% DT::datatable(extensions = "Buttons",
filter = 'bottom',
width = "800px",
options = list(autoWidth = TRUE,
fixedHeader = TRUE,
dom="Bfrtip", buttons=c("csv","excel")))
df = sctype_scores
df$cluster = factor(df$cluster, levels = c(paste0("cluster ", 0:17)))
df %>% ggplot(aes(cluster, scores, fill=type)) +
geom_col(position = "dodge2", color="grey7", linewidth =0.1) +
theme_bw() +
RotatedAxis()
df = sctype_scores
df$cluster = factor(df$cluster, levels = c(paste0("cluster ", 0:17)))
df %>% ggplot(aes(cluster, scores, fill=type)) +
geom_col(position = "dodge2", color="grey7", linewidth =0.1) +
theme_bw() +
facet_wrap(.~type, ncol = 3) +
RotatedAxis()
es.max.t = t(es.max)
es.max.t = es.max.t[colnames(obj.srt),] %>% as.data.frame()
obj.srt = AddMetaData(object = obj.srt, metadata = es.max.t)
VlnPlot(obj.srt, group.by = "RNA_snn_res.0.4",
features = c("Cholinergic neurons",
"Dopaminergic neurons",
"Endothelial cells",
"GABAergic neurons",
"Glutamatergic neurons",
"Immature neurons",
"Mature neurons",
"Microglial cells",
"Myelinating Schwann cells",
"Neural Progenitor cells",
"Oligodendrocyte precursor cells",
"Oligodendrocytes",
"Radial glial cells",
"Schwann precursor cells",
"Serotonergic neurons"),
stack = T, flip = T, sort = T) +NoLegend()
es.max.t = t(es.max)
es.max.t = es.max.t[colnames(obj.srt),] %>% as.data.frame()
# obj.srt = AddMetaData(object = obj.srt, metadata = es.max.t)
input.mtx= es.max.t
test.cor <- WGCNA::cor(input.mtx, use = 'pairwise.complete.obs')
#test.cor <- WGCNA::cor(input.mtx %>% t(), use = 'pairwise.complete.obs')
my.colors <- c(colorRampPalette(colors = c("navy","white"))(15),
colorRampPalette(colors = c("white","red"))(30))
# test.cor %>% pheatmap::pheatmap(color = my.colors)
p =test.cor %>% pheatmap::pheatmap(color = my.colors)
test.cor[p$tree_row$order,p$tree_col$order] %>%
DT::datatable(extensions = "Buttons",
filter = 'bottom',
width = "800px",
options = list(autoWidth = TRUE,
fixedHeader = TRUE,
pageLength= 22,
scrollX=T,
dom="Bfrtip", buttons=c("csv","excel")))
sessionInfo()
## R version 4.4.1 (2024-06-14)
## Platform: aarch64-apple-darwin20
## Running under: macOS Sonoma 14.6.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/Detroit
## tzcode source: internal
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] openxlsx_4.2.7 HGNChelper_0.8.14
## [3] SeuratWrappers_0.4.0 SingleCellExperiment_1.26.0
## [5] SummarizedExperiment_1.34.0 Biobase_2.64.0
## [7] GenomicRanges_1.56.1 GenomeInfoDb_1.40.1
## [9] IRanges_2.38.1 S4Vectors_0.42.1
## [11] BiocGenerics_0.50.0 MatrixGenerics_1.16.0
## [13] matrixStats_1.4.1 scCustomize_2.1.2
## [15] Seurat_5.1.0 SeuratObject_5.0.2
## [17] sp_2.1-4 dplyr_1.1.4
## [19] ggrepel_0.9.6 ggplot2_3.5.1
##
## loaded via a namespace (and not attached):
## [1] spatstat.sparse_3.1-0 lubridate_1.9.3 httr_1.4.7
## [4] RColorBrewer_1.1-3 doParallel_1.0.17 dynamicTreeCut_1.63-1
## [7] tools_4.4.1 sctransform_0.4.1 backports_1.5.0
## [10] utf8_1.2.4 R6_2.5.1 DT_0.33
## [13] lazyeval_0.2.2 uwot_0.2.2 withr_3.0.2
## [16] gridExtra_2.3 preprocessCore_1.66.0 progressr_0.15.1
## [19] WGCNA_1.73 cli_3.6.3 spatstat.explore_3.3-3
## [22] fastDummies_1.7.4 labeling_0.4.3 sass_0.4.9
## [25] spatstat.data_3.1-4 ggridges_0.5.6 pbapply_1.7-2
## [28] foreign_0.8-86 R.utils_2.12.3 parallelly_1.39.0
## [31] rstudioapi_0.16.0 impute_1.78.0 RSQLite_2.3.8
## [34] generics_0.1.3 shape_1.4.6.1 ica_1.0-3
## [37] spatstat.random_3.3-2 crosstalk_1.2.1 zip_2.3.1
## [40] GO.db_3.19.1 Matrix_1.7-0 ggbeeswarm_0.7.2
## [43] fansi_1.0.6 abind_1.4-8 R.methodsS3_1.8.2
## [46] lifecycle_1.0.4 yaml_2.3.10 snakecase_0.11.1
## [49] SparseArray_1.4.8 Rtsne_0.17 paletteer_1.6.0
## [52] grid_4.4.1 blob_1.2.4 promises_1.3.2
## [55] crayon_1.5.3 miniUI_0.1.1.1 lattice_0.22-6
## [58] cowplot_1.1.3 KEGGREST_1.44.1 pillar_1.9.0
## [61] knitr_1.49 future.apply_1.11.3 codetools_0.2-20
## [64] leiden_0.4.3.1 glue_1.8.0 spatstat.univar_3.1-1
## [67] data.table_1.16.2 remotes_2.5.0 vctrs_0.6.5
## [70] png_0.1-8 spam_2.11-0 gtable_0.3.6
## [73] rematch2_2.1.2 cachem_1.1.0 xfun_0.49
## [76] S4Arrays_1.4.1 mime_0.12 survival_3.6-4
## [79] pheatmap_1.0.12 iterators_1.0.14 fitdistrplus_1.2-1
## [82] ROCR_1.0-11 nlme_3.1-164 bit64_4.5.2
## [85] RcppAnnoy_0.0.22 bslib_0.8.0 irlba_2.3.5.1
## [88] vipor_0.4.7 KernSmooth_2.23-24 rpart_4.1.23
## [91] splitstackshape_1.4.8 colorspace_2.1-1 DBI_1.2.3
## [94] Hmisc_5.2-0 nnet_7.3-19 ggrastr_1.0.2
## [97] tidyselect_1.2.1 bit_4.5.0 compiler_4.4.1
## [100] htmlTable_2.4.3 DelayedArray_0.30.1 plotly_4.10.4
## [103] checkmate_2.3.2 scales_1.3.0 lmtest_0.9-40
## [106] stringr_1.5.1 digest_0.6.37 goftest_1.2-3
## [109] spatstat.utils_3.1-1 rmarkdown_2.29 XVector_0.44.0
## [112] htmltools_0.5.8.1 pkgconfig_2.0.3 base64enc_0.1-3
## [115] fastmap_1.2.0 rlang_1.1.4 GlobalOptions_0.1.2
## [118] htmlwidgets_1.6.4 UCSC.utils_1.0.0 shiny_1.9.1
## [121] farver_2.1.2 jquerylib_0.1.4 zoo_1.8-12
## [124] jsonlite_1.8.9 R.oo_1.27.0 magrittr_2.0.3
## [127] Formula_1.2-5 GenomeInfoDbData_1.2.12 dotCall64_1.2
## [130] patchwork_1.3.0 munsell_0.5.1 Rcpp_1.0.13-1
## [133] reticulate_1.40.0 stringi_1.8.4 zlibbioc_1.50.0
## [136] MASS_7.3-60.2 plyr_1.8.9 parallel_4.4.1
## [139] listenv_0.9.1 forcats_1.0.0 deldir_2.0-4
## [142] Biostrings_2.72.1 splines_4.4.1 tensor_1.5
## [145] circlize_0.4.16 igraph_2.1.1 fastcluster_1.2.6
## [148] spatstat.geom_3.3-4 RcppHNSW_0.6.0 reshape2_1.4.4
## [151] evaluate_1.0.1 BiocManager_1.30.25 ggprism_1.0.5
## [154] foreach_1.5.2 httpuv_1.6.15 RANN_2.6.2
## [157] tidyr_1.3.1 purrr_1.0.2 polyclip_1.10-7
## [160] future_1.34.0 scattermore_1.2 rsvd_1.0.5
## [163] janitor_2.2.0 xtable_1.8-4 RSpectra_0.16-2
## [166] later_1.4.1 viridisLite_0.4.2 tibble_3.2.1
## [169] memoise_2.0.1 beeswarm_0.4.0 AnnotationDbi_1.66.0
## [172] cluster_2.1.6 timechange_0.3.0 globals_0.16.3