├── .gitignore
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── NAMESPACE
├── NEWS.md
├── R
├── .DS_Store
├── RcppExports.R
├── STList.R
├── STclust.R
├── STdiff.R
├── STdiff_volcano.R
├── STenrich.R
├── STenrich_OLD.R
├── STenrich_OLD2.R
├── STgradient.R
├── SThet.R
├── SThet_invdist_test.R
├── STplot.R
├── STplot_interpolation.R
├── classDefinitions.R
├── compare_SThet.R
├── detect_input.R
├── distribution_plots.R
├── filter_data.R
├── gene_interpolation.R
├── globals.R
├── import_smi.R
├── import_visium.R
├── import_xenium.R
├── old_SThet.R
├── plot_counts.R
├── plot_helpers.R
├── plot_image.R
├── plot_spatial_expression.R
├── plot_spatial_geneset.R
├── plot_spatial_meta.R
├── pseudobulk_samples.R
├── seurat_helpers.R
├── summarize_STlist.R
├── transform_data.R
├── utils.R
└── zzz.R
├── README.md
├── docs
├── .DS_Store
├── 404.html
├── LICENSE-text.html
├── LICENSE.html
├── apple-touch-icon-120x120.png
├── apple-touch-icon-152x152.png
├── apple-touch-icon-180x180.png
├── apple-touch-icon-60x60.png
├── apple-touch-icon-76x76.png
├── apple-touch-icon.png
├── articles
│ ├── .DS_Store
│ ├── basic_functions_vignette.html
│ ├── basic_functions_vignette_files
│ │ ├── dir_structure.png
│ │ ├── figure-html
│ │ │ ├── clinplot_chunk-1.png
│ │ │ ├── count_dstr-1.png
│ │ │ ├── filter_chunk-1.png
│ │ │ ├── genequilt_chunk2-1.png
│ │ │ ├── heatmap_chunk-1.png
│ │ │ ├── pca_chunk-1.png
│ │ │ ├── plot_tissue-1.png
│ │ │ ├── plotclustspots_chunk-1.png
│ │ │ ├── plotkrige_chunk1-1.png
│ │ │ ├── tissue_sim_2-1.png
│ │ │ └── tissue_sim_4-1.png
│ │ ├── kePrint-0.0.1
│ │ │ └── kePrint.js
│ │ ├── lightable-0.0.1
│ │ │ └── lightable.css
│ │ └── metadata_file.png
│ ├── img
│ │ ├── diagram_stenrich.png
│ │ ├── dir_structure.png
│ │ ├── metadata_file.png
│ │ ├── smi_dir_structure.png
│ │ └── spatial_gradients.png
│ ├── index.html
│ ├── spatial_differential_expression.html
│ ├── spatial_differential_expression_files
│ │ └── figure-html
│ │ │ ├── compare_pval_p-1.png
│ │ │ ├── plotclustspots_chunk-1.png
│ │ │ ├── vgm_model_plot-1.png
│ │ │ ├── vgm_plot-1.png
│ │ │ └── volc_plots2-1.png
│ ├── spatial_enrichment_gradients_smi.html
│ ├── spatial_enrichment_gradients_smi_files
│ │ └── figure-html
│ │ │ ├── count_dstr-1.png
│ │ │ ├── notch_plots-1.png
│ │ │ ├── plot_domains-1.png
│ │ │ ├── stg_plots-1.png
│ │ │ └── unnamed-chunk-1-1.png
│ └── troubleshooting.html
├── authors.html
├── bootstrap-toc.css
├── bootstrap-toc.js
├── deps
│ ├── bootstrap-5.1.0
│ │ ├── bootstrap.bundle.min.js
│ │ ├── bootstrap.bundle.min.js.map
│ │ └── bootstrap.min.css
│ ├── data-deps.txt
│ └── jquery-3.6.0
│ │ ├── jquery-3.6.0.js
│ │ ├── jquery-3.6.0.min.js
│ │ └── jquery-3.6.0.min.map
├── diagram_stenrich.png
├── dir_structure.png
├── docsearch.css
├── docsearch.js
├── favicon-16x16.png
├── favicon-32x32.png
├── favicon.ico
├── index.html
├── link.svg
├── logo.png
├── metadata_file.png
├── news
│ └── index.html
├── pkgdown.css
├── pkgdown.js
├── pkgdown.yml
├── reference
│ ├── Rplot001.png
│ ├── Rplot002.png
│ ├── STclust-1.png
│ ├── STclust.html
│ ├── STdiff.html
│ ├── STdiff_volcano.html
│ ├── STenrich.html
│ ├── STgradient.html
│ ├── SThet.html
│ ├── SThet_invdist_test.html
│ ├── STlist-class.html
│ ├── STlist.html
│ ├── STplot-1.png
│ ├── STplot.html
│ ├── STplot_interpolation-1.png
│ ├── STplot_interpolation.html
│ ├── compare_SThet.html
│ ├── dim-STlist-method.html
│ ├── distribution_plots.html
│ ├── filter_data.html
│ ├── gene_interpolation-1.png
│ ├── gene_interpolation.html
│ ├── get_gene_meta.html
│ ├── index.html
│ ├── load_images.html
│ ├── old_SThet.html
│ ├── plot_counts-1.png
│ ├── plot_counts.html
│ ├── plot_image.html
│ ├── pseudobulk_dim_plot-1.png
│ ├── pseudobulk_dim_plot.html
│ ├── pseudobulk_heatmap-1.png
│ ├── pseudobulk_heatmap-2.png
│ ├── pseudobulk_heatmap.html
│ ├── pseudobulk_pca_plot-1.png
│ ├── pseudobulk_pca_plot.html
│ ├── pseudobulk_samples-1.png
│ ├── pseudobulk_samples.html
│ ├── show-STlist-method.html
│ ├── spatial_metadata.html
│ ├── summarize_STlist.html
│ ├── summary-STlist-method.html
│ ├── tissue_names.html
│ └── transform_data.html
├── sitemap.xml
├── smi_dir_structure.png
├── spatialGE_workflow_v3.png
└── spatial_gradients.png
├── inst
├── .DS_Store
└── err.csv
├── logo.png
├── man
├── STclust.Rd
├── STdiff.Rd
├── STdiff_volcano.Rd
├── STenrich.Rd
├── STgradient.Rd
├── SThet.Rd
├── STlist-class.Rd
├── STlist.Rd
├── STplot.Rd
├── STplot_interpolation.Rd
├── compare_SThet.Rd
├── dim-STlist-method.Rd
├── distribution_plots.Rd
├── filter_data.Rd
├── gene_interpolation.Rd
├── get_gene_meta.Rd
├── load_images.Rd
├── plot_counts.Rd
├── plot_image.Rd
├── pseudobulk_dim_plot.Rd
├── pseudobulk_heatmap.Rd
├── pseudobulk_samples.Rd
├── show-STlist-method.Rd
├── spatial_metadata.Rd
├── summarize_STlist.Rd
├── summary-STlist-method.Rd
├── tissue_names.Rd
└── transform_data.Rd
├── pkgdown
└── favicon
│ ├── apple-touch-icon-120x120.png
│ ├── apple-touch-icon-152x152.png
│ ├── apple-touch-icon-180x180.png
│ ├── apple-touch-icon-60x60.png
│ ├── apple-touch-icon-76x76.png
│ ├── apple-touch-icon.png
│ ├── favicon-16x16.png
│ ├── favicon-32x32.png
│ └── favicon.ico
├── spatialGE.Rproj
├── spatialGE_workflow_v3.png
├── src
├── RcppExports.cpp
├── seurat_helpers.cpp
└── spatialge_helpers.cpp
├── tests
├── testthat.R
└── testthat
│ └── test-tests.R
└── vignettes
├── basic_functions_vignette.Rmd
├── img
├── diagram_stenrich.png
├── dir_structure.png
├── metadata_file.png
├── smi_dir_structure.png
└── spatial_gradients.png
├── spatial_differential_expression.Rmd
├── spatial_enrichment_gradients_smi.Rmd
├── troubleshooting.Rmd
└── vignette_references.bib
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .Rproj.user
3 | .Rhistory
4 | .RData
5 | .Ruserdata
6 | inst/doc
7 | docs/.DS_Store
8 | inst/.DS_Store
9 | R/.DS_Store
10 | R/STdiff_2.R
11 | vignettes/.Rhistory
12 | .Rbuildignore
13 | src/*.o
14 | src/*.so
15 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: spatialGE
2 | Title: Visualization and Analysis of Spatial Heterogeneity in Spatially-Resolved Gene Expression
3 | Version: 1.2.0
4 | Authors@R: c(
5 | person(given = "Oscar",
6 | family = "Ospina",
7 | role = c("aut", "cre"),
8 | email = "ospina.oe@gmail.com",
9 | comment = c(ORCID = "0000-0001-5986-4207")),
10 | person(given = "Alex",
11 | family = "Soupir",
12 | role = c("aut"),
13 | email = "Alex.Soupir@moffitt.org",
14 | comment = c(ORCID = "0000-0003-1251-9179")),
15 | person(given = "Brooke",
16 | family = "Fridley",
17 | role = c("aut"),
18 | email = "Brooke.Fridley@Moffitt.org",
19 | comment = c(ORCID = "0000-0001-7739-7956"))
20 | )
21 | Description: Visualization and analysis of spatially resolved transcriptomics data. The functions in spatialGE allow for the study of gene expression in tissue sections via spatial interpolation, RNA-Seq data deconvolution, and autocorrelation statistics, as well as correlation with clinical/phenotype data.
22 | License: MIT + file LICENSE
23 | Encoding: UTF-8
24 | LazyData: true
25 | Roxygen: list(markdown = TRUE)
26 | RoxygenNote: 7.3.2
27 | Imports:
28 | arrow,
29 | BiocParallel,
30 | concaveman,
31 | ComplexHeatmap,
32 | data.table,
33 | DelayedArray,
34 | DelayedMatrixStats,
35 | dynamicTreeCut,
36 | dplyr,
37 | EBImage,
38 | ggforce,
39 | ggplot2,
40 | ggpolypath,
41 | ggrepel,
42 | gstat,
43 | GSVA,
44 | hdf5r,
45 | jpeg,
46 | jsonlite,
47 | khroma (>= 1.6.0),
48 | magrittr,
49 | Matrix,
50 | MASS,
51 | methods,
52 | parallel,
53 | png,
54 | RColorBrewer,
55 | Rcpp (>= 1.0.7),
56 | readr,
57 | readxl,
58 | rlang,
59 | scales,
60 | sctransform,
61 | sfsmisc,
62 | sf,
63 | sp,
64 | spaMM,
65 | spdep,
66 | stats,
67 | stringr,
68 | tibble,
69 | tidyr,
70 | utils,
71 | uwot,
72 | wordspace
73 | LinkingTo:
74 | Rcpp,
75 | RcppEigen,
76 | RcppProgress
77 | Suggests:
78 | geoR,
79 | ggpubr,
80 | janitor,
81 | kableExtra,
82 | knitr,
83 | msigdbr,
84 | progress,
85 | rmarkdown,
86 | rpart,
87 | R.utils,
88 | scSpatialSIM,
89 | spatstat,
90 | SeuratObject,
91 | tidyverse,
92 | testthat (>= 3.0.0)
93 | VignetteBuilder:
94 | knitr
95 | Config/testthat/edition: 3
96 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | YEAR: 2023
2 | COPYRIGHT HOLDER: Moffitt Cancer Center
3 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # MIT License
2 |
3 | Copyright (c) 2023 Moffitt Cancer Center
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export(STclust)
4 | export(STdiff)
5 | export(STdiff_volcano)
6 | export(STenrich)
7 | export(STgradient)
8 | export(SThet)
9 | export(STlist)
10 | export(STplot)
11 | export(STplot_interpolation)
12 | export(compare_SThet)
13 | export(distribution_plots)
14 | export(filter_data)
15 | export(gene_interpolation)
16 | export(get_gene_meta)
17 | export(load_images)
18 | export(plot_counts)
19 | export(plot_image)
20 | export(pseudobulk_dim_plot)
21 | export(pseudobulk_heatmap)
22 | export(pseudobulk_samples)
23 | export(spatial_metadata)
24 | export(summarize_STlist)
25 | export(tissue_names)
26 | export(transform_data)
27 | import(Matrix)
28 | import(ggplot2)
29 | import(ggpolypath)
30 | importFrom(Rcpp,evalCpp)
31 | importFrom(grDevices,colorRampPalette)
32 | importFrom(magrittr,"%>%")
33 | importFrom(methods,as)
34 | importFrom(methods,is)
35 | importFrom(methods,new)
36 | importFrom(rlang,":=")
37 | importFrom(stats,IQR)
38 | importFrom(stats,as.dist)
39 | importFrom(stats,as.formula)
40 | importFrom(stats,complete.cases)
41 | importFrom(stats,cor.test)
42 | importFrom(stats,cutree)
43 | importFrom(stats,dist)
44 | importFrom(stats,hclust)
45 | importFrom(stats,lm)
46 | importFrom(stats,loess)
47 | importFrom(stats,na.omit)
48 | importFrom(stats,p.adjust)
49 | importFrom(stats,prcomp)
50 | importFrom(stats,quantile)
51 | importFrom(stats,sd)
52 | importFrom(stats,setNames)
53 | useDynLib(spatialGE)
54 |
--------------------------------------------------------------------------------
/NEWS.md:
--------------------------------------------------------------------------------
1 | # spatialGE 1.2.1
2 |
3 | * Added C++ code to speed up `STenrich` computations (Thank you, Dr. Soupir!).
4 | * The `STenrich` function can now calculate gene set enrichment scores (via `GSVA`) in addition to gene set average expression.
5 | * The `STenrich` function can now test for gene set "hot-spots" within an specific tissue domain (`annot` and `domain` arguments).
6 | * Spatial distances can now be log-transformed for `STgradient` (`log` argument).
7 | * Example data sets are now in the spatialGE_Data GitHub repository.
8 | * New functions `spatial_metadata` and `tissue_names` to quickly access the names of spot/cell annotations and sample names.
9 | * Re-assessed package dependencies.
10 | * Use of `DelayedArray` for some calculations.
11 | * Several bug fixes.
12 |
13 | # spatialGE 1.2.0
14 |
15 | * Functions `STenrich`, `STgradient`, and `STdiff` available.
16 | * Added C++ code for Seurat's implementation of `FindVariableFeatures`
17 |
18 | # spatialGE 1.1.0
19 |
20 | * Multiple, significant changes to STlist (incompatible with STlist objects from version 1.0).
21 |
--------------------------------------------------------------------------------
/R/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/R/.DS_Store
--------------------------------------------------------------------------------
/R/RcppExports.R:
--------------------------------------------------------------------------------
1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand
2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3 |
4 | SparseRowVar2 <- function(mat, mu) {
5 | .Call('_spatialGE_SparseRowVar2', PACKAGE = 'spatialGE', mat, mu)
6 | }
7 |
8 | SparseRowVarStd <- function(mat, mu, sd, vmax) {
9 | .Call('_spatialGE_SparseRowVarStd', PACKAGE = 'spatialGE', mat, mu, sd, vmax)
10 | }
11 |
12 | computeSubsampleSums <- function(coords, n_subsample, n_samples) {
13 | .Call('_spatialGE_computeSubsampleSums', PACKAGE = 'spatialGE', coords, n_subsample, n_samples)
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/R/STdiff_volcano.R:
--------------------------------------------------------------------------------
1 | ##
2 | #' @title STdiff_volcano: Generates volcano plots from STdiff results
3 | #' @description Generates volcano plots of differential expression results from STdiff
4 | #' @details The function generated volcano plots (p-value vs. log-fold change) for
5 | #' genes tested with `STdiff`. Colors can be customized to show significance from
6 | #' spatial and non-spatial models
7 | #'
8 | #' @param x the output of `STdiff`
9 | #' @param samples samples to create plots
10 | #' @param clusters names of the clusters to generate comparisons
11 | #' @param pval_thr the p-value threshold to color genes with differential expression
12 | #' @param color_pal the palette to color genes by significance
13 | #' @return a list of ggplot objects
14 | #'
15 | #' @export
16 | #'
17 | #' @import ggplot2
18 | #
19 | #
20 | STdiff_volcano = function(x=NULL, samples=NULL, clusters=NULL, pval_thr=0.05, color_pal=NULL){
21 | # Define samples to plot if NULL or numeric
22 | if(is.null(samples)){
23 | samples = names(x)
24 | } else if(is.numeric(x)){
25 | samples = names(x)[samples]
26 | }
27 |
28 | # Loop through samples
29 | plist = list()
30 | for(i in samples){
31 | df_tmp = x[[i]]
32 | # Find out if test was pairwise
33 | pairwise = F
34 | if(any(grepl('^cluster_2$', colnames(x[[i]])))){
35 | pairwise = T
36 | }
37 | # Define which clusters to plot
38 | if(!is.null(clusters)){
39 | if(pairwise){
40 | df_tmp = df_tmp %>% dplyr::filter(cluster_1 %in% clusters | cluster_2 %in% clusters)
41 | } else{
42 | df_tmp = df_tmp %>% dplyr::filter(cluster_1 %in% clusters)
43 | }
44 | }
45 |
46 | # Define combination of clusters to plot
47 | combo = df_tmp %>% dplyr::select(dplyr::any_of(c('cluster_1', 'cluster_2'))) %>% dplyr::distinct()
48 | # Create plots
49 | for(cb in 1:nrow(combo)){
50 | cl1 = as.vector(unlist(combo[cb, 'cluster_1']))
51 | pl_name = paste0(i, '_', cl1)
52 | pl_title = paste0('Sample: ', i, '\nCluster ', cl1)
53 | if(pairwise){
54 | cl2 = as.vector(unlist(combo[cb, 'cluster_2']))
55 | pl_name = paste0(i, '_', cl1, '_vs_', cl2)
56 | pl_title = paste0('Sample: ', i, '\nCluster ', cl1, ' vs. ', cl2)
57 | #df_plot = df_tmp %>% dplyr::filter(cluster_1 == cl1 | cluster_2 == cl2)
58 | df_plot = df_tmp %>% dplyr::filter( (cluster_1 == cl1 & cluster_2 == cl2) | (cluster_1 == cl2 & cluster_2 == cl1) )
59 | } else{
60 | df_plot = df_tmp %>% dplyr::filter(cluster_1 == cl1)
61 | }
62 |
63 | # Detect test performed
64 | pval_col = grep("mm_p_val|ttest_p_val|wilcox_p_val", colnames(df_plot), value=T)
65 | if(pval_col == 'mm_p_val'){
66 | testname = 'Mixed models'
67 | } else if(pval_col == 'ttest_p_val'){
68 | testname = 't-test'
69 | } else if(pval_col == 'wilcox_p_val'){
70 | testname = "Wilcoxon's test"
71 | }
72 |
73 | # Classify p-values
74 | df_plot = df_plot %>%
75 | dplyr::mutate(signif=dplyr::case_when(adj_p_val < pval_thr & avg_log2fc < 0 ~ 'Down',
76 | adj_p_val < pval_thr & avg_log2fc > 0 ~ 'Up',
77 | TRUE ~ 'Not DE')) %>%
78 | dplyr::rename(plot_pval := !!pval_col)
79 |
80 | # Check if results for spatial model tests are available
81 | if(any(colnames(df_plot) == 'exp_adj_p_val')){
82 | df_plot = df_plot %>%
83 | dplyr::mutate(signif=dplyr::case_when(exp_adj_p_val < pval_thr & avg_log2fc < 0 ~ 'Down (spatial)',
84 | exp_adj_p_val < pval_thr & avg_log2fc > 0 ~ 'Up (spatial)',
85 | TRUE ~ signif))
86 | }
87 |
88 | # Re-order values in significance legend
89 | df_plot = df_plot %>%
90 | dplyr::mutate(signif=factor(signif, levels=c('Not DE', 'Down', 'Up', 'Down (spatial)', 'Up (spatial)')))
91 |
92 | # Define color palette
93 | if(is.null(color_pal)){
94 | cat_cols = c('black', 'cornflowerblue', 'lightpink', 'blue', 'red')
95 | names(cat_cols) = c('Not DE', 'Down', 'Up', 'Down (spatial)', 'Up (spatial)')
96 | } else{
97 | if(length(color_pal) == 3){
98 | cat_cols = color_pal
99 | names(cat_cols) = c('Not DE', 'Down', 'Up')
100 | } else if(length(color_pal) == 5){
101 | cat_cols = color_pal
102 | names(cat_cols) = c('Not DE', 'Down', 'Up', 'Down (spatial)', 'Up (spatial)')
103 | } else{
104 | cat(paste0('Custom color palettes must have three (non-spatial results) or five colors (spatial results). Setting to default palette.'))
105 | cat_cols = c('black', 'cornflowerblue', 'lightpink', 'blue', 'red')
106 | names(cat_cols) = c('Not DE', 'Down', 'Up', 'Down (spatial)', 'Up (spatial)')
107 | }
108 | }
109 |
110 | plist[[pl_name]] = ggplot2::ggplot(df_plot, aes(x=avg_log2fc, y=-log10(plot_pval))) +
111 | ggplot2::geom_point(aes(color=signif)) +
112 | ggrepel::geom_text_repel(aes(label=gene), size=3, verbose=F, force=0, max.iter=100, max.overlaps=1, nudge_x=0.2) +
113 | ggplot2::xlab('Average log fold-change') +
114 | ggplot2::ylab(paste0('-log10(nominal p-value)\n', testname)) +
115 | labs(color='FDR\nsignif.') +
116 | ggplot2::ggtitle(pl_title) +
117 | ggplot2::scale_color_manual(values=cat_cols[ names(cat_cols) %in% unique(df_plot[['signif']]) ]) +
118 | ggplot2::theme(panel.background=element_rect(color='black', fill=NULL))
119 |
120 | rm(list=grep("df_plot|cl1|cl2|pl_name|pl_title|testname|pval_col", ls(), value=T)) # Clean env
121 | }
122 | rm(combo, df_tmp, pairwise) # Clean env
123 | }
124 | return(plist)
125 | }
126 |
127 |
--------------------------------------------------------------------------------
/R/STplot.R:
--------------------------------------------------------------------------------
1 | ##
2 | #' @title STplot: Plots of gene expression, cluster memberships, and metadata in spatial context
3 | #' @description Generates a plot of the location of spots/cells within an spatial
4 | #' sample, and colors them according to gene expression levels or spot/cell-level
5 | #' metadata
6 | #' @details
7 | #' The function takes an STlist and plots the cells or spots in their spatial context.
8 | #' The users can color the spots/cells according to the expression of selected genes,
9 | #' cluster memberships, or any spot/cell level metadata included in `x@spatial_meta`.
10 | #' The function also can average expression of gene sets.
11 | #'
12 | #' @param x an STlist
13 | #' @param samples a vector of numbers indicating the ST samples to plot, or their
14 | #' sample names. If vector of numbers, it follow the order of samples in `names(x@counts)`.
15 | #' If NULL, the function plots all samples
16 | #' @param genes a vector of gene names or a named list of gene sets. In the latter
17 | #' case, the averaged expression of genes within the sets is plotted
18 | #' @param plot_meta a column name in `x@spatial_meta` to plot
19 | #' @param data_type one of 'tr' or 'raw', to plot transformed or raw counts
20 | #' respectively
21 | #' @param ks the k values to plot or 'dtc' to plot results from `dynamicTreeCut`
22 | #' clustering solutions. Requires previous analysis with `STclust`
23 | #' @param ws the spatial weights to plot samples if `STclust` was used
24 | #' @param deepSplit a logical or positive number indicating the `deepSplit`, if
25 | #' samples were analyzed with `STclust`
26 | #' @param color_pal a string of a color palette from `khroma` or `RColorBrewer`, or a
27 | #' vector with enough color names or HEX values
28 | #' @param ptsize a number specifying the size of the points. Passed to the `size`
29 | #' @param txsize a number controlling the size of the text in the plot title and legend title. Passed to the `element_text`
30 | #' aesthetic.
31 | #' @return a list of plots
32 | #'
33 | #' @examples
34 | ##'
35 | #' # Using included melanoma example (Thrane et al.)
36 | #' # Download example data set from spatialGE_Data
37 | #' thrane_tmp = tempdir()
38 | #' unlink(thrane_tmp, recursive=TRUE)
39 | #' dir.create(thrane_tmp)
40 | #' lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
41 | #' download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
42 | #' zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
43 | #' unzip(zipfile=zip_tmp, exdir=thrane_tmp)
44 | #' # Generate the file paths to be passed to the STlist function
45 | #' count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
46 | #' full.names=TRUE, pattern='counts')
47 | #' coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
48 | #' full.names=TRUE, pattern='mapping')
49 | #' clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
50 | #' full.names=TRUE, pattern='clinical')
51 | #' # Create STlist
52 | #' library('spatialGE')
53 | #' melanoma <- STlist(rnacounts=count_files[c(1,2)],
54 | #' spotcoords=coord_files[c(1,2)],
55 | #' samples=clin_file) # Only first two samples
56 | #' melanoma <- transform_data(melanoma)
57 | #' STplot(melanoma, gene='MLANA', samples='ST_mel1_rep2', ptsize=1)
58 | #'
59 | #' @export
60 | #'
61 | #' @import ggplot2
62 | #' @importFrom magrittr %>%
63 | #
64 | STplot = function(x, samples=NULL, genes=NULL, plot_meta=NULL, ks='dtc', ws=NULL, deepSplit=NULL, color_pal=NULL, data_type='tr', ptsize=NULL, txsize=NULL){
65 |
66 | # Check if data set is Visium to flip y axis (may consider remove this in the future)
67 | visium = F
68 | if(x@misc[['platform']] == 'visium'){
69 | visium = T
70 | }
71 |
72 | # Define if expression or metadata is to be plotted
73 | if(!is.null(genes)){
74 | # Set default color if NULL input
75 | if(is.null(color_pal)){
76 | color_pal = 'BuRd'
77 | }
78 | if(is.list(genes)){
79 | plot_list = plot_spatial_geneset(x=x, genes=genes, samples=samples, color_pal=color_pal, visium=visium, ptsize=ptsize)
80 | } else{
81 | plot_list = plot_spatial_expression(x=x, genes=genes, samples=samples, color_pal=color_pal, data_type=data_type, visium=visium, ptsize=ptsize)
82 | }
83 | } else{
84 | plot_list = plot_spatial_meta(x=x, samples=samples, ks=ks, ws=ws, deepSplit=deepSplit, plot_meta=plot_meta, color_pal=color_pal, visium=visium, ptsize=ptsize, txsize=txsize)
85 | }
86 |
87 | return(plot_list)
88 | }
89 |
90 |
--------------------------------------------------------------------------------
/R/classDefinitions.R:
--------------------------------------------------------------------------------
1 | ## Class definitions -----------------------------------------------------------
2 |
3 | ##
4 | #' Definition of an STlist object class.
5 | #'
6 | #' @slot counts per spot RNA counts
7 | #' @slot spatial_meta per spot x,y coordinates
8 | #' @slot gene_meta per gene statistics (e.g., average expression, variance, Moran's I)
9 | #' @slot sample_meta dataframe with metadata per sample
10 | #' @slot tr_counts transfromed per spot counts
11 | #' @slot gene_krige results from kriging on gene expression
12 | #' @slot misc Parameters and images from ST data
13 | #
14 | #
15 | setClass(Class="STlist",
16 | slots=list(counts="list",
17 | spatial_meta="list",
18 | gene_meta="list",
19 | sample_meta=class(tibble::tibble())[1],
20 | tr_counts="list",
21 | gene_krige="list",
22 | misc="list"
23 | )
24 | )
25 |
26 |
27 | # STList Methods ---------------------------------------------------------------
28 |
29 | ##
30 | #' @title show: Prints overview of STList oject.
31 | #' @description Prints overview/summary of STList oject.
32 | #' @details
33 | #' This function takes an STList and prints a the number of spatial arrays in that
34 | #' object and other information about the object.
35 | #'
36 | #' @param object an STList object to show summary from.
37 | #
38 | #
39 | setMethod("show", signature="STlist",
40 | function(object){
41 | cat("Spatial Transcriptomics List (STlist).\n")
42 | cat(length(object@counts), "spatial array(s):\n")
43 | if(length(names(object@counts)) >= 11){
44 | counter = 1
45 | while(counter < 11){
46 | i = names(object@counts)[counter]
47 | cat(paste0('\t', i, " (", ncol(object@counts[[i]]), ' ROIs|spots|cells x ', nrow(object@counts[[i]]), ' genes)\n'))
48 | counter = counter + 1
49 | }
50 | } else{
51 | for(i in names(object@counts)){
52 | cat(paste0('\t', i, " (", ncol(object@counts[[i]]), ' ROIs|spots|cells x ', nrow(object@counts[[i]]), ' genes)\n'))
53 | }
54 | }
55 | if(length(names(object@counts)) > 10){
56 | cat(paste0('\tPlus ', (length(names(object@counts))-10), ' additional spatial array(s)\n'))
57 | }
58 | cat('\n')
59 | if(!rlang::is_empty(object@sample_meta)){
60 | cat(paste0((ncol(object@sample_meta)-1), " variables in sample-level data:\n"))
61 | cat('\t', paste0(colnames(object@sample_meta[, -1]), collapse = ', '), '\n')
62 | }
63 | }
64 | )
65 |
66 | ##
67 | #' @title summary: Prints overview of STList oject.
68 | #' @description Prints overview/summary of STList oject.
69 | #' @details
70 | #' This function takes an STList and prints a the number of spatial arrays in that
71 | #' object and other information about the object.
72 | #'
73 | #' @param object an STList object to show summary from.
74 | #
75 | #
76 | setMethod("summary", signature="STlist",
77 | function(object){
78 | cat("Spatial Transcriptomics List (STlist).\n")
79 | cat(length(object@counts), "spatial array(s):\n")
80 | if(length(names(object@counts)) >= 11){
81 | counter = 1
82 | while(counter < 11){
83 | i = names(object@counts)[counter]
84 | cat(paste0('\t', i, " (", ncol(object@counts[[i]]), ' ROIs|spots|cells x ', nrow(object@counts[[i]]), ' genes)\n'))
85 | counter = counter + 1
86 | }
87 | } else{
88 | for(i in names(object@counts)){
89 | cat(paste0('\t', i, " (", ncol(object@counts[[i]]), ' ROIs|spots|cells x ', nrow(object@counts[[i]]), ' genes)\n'))
90 | }
91 | }
92 | if(length(names(object@counts)) > 10){
93 | cat(paste0('\tPlus ', (length(names(object@counts))-10), ' additional spatial array(s)\n'))
94 | }
95 | cat('\n')
96 | if(!rlang::is_empty(object@sample_meta)){
97 | cat(paste0((ncol(object@sample_meta)-1), " variables in sample-level data:\n"))
98 | cat('\t', paste0(colnames(object@sample_meta[, -1]), collapse = ', '), '\n')
99 | }
100 | }
101 | )
102 |
103 | ##
104 | #' @title dim: Prints the dimensions of count arrays within an STList object.
105 | #' @description Returns the number of genes and spots for each array within an STList object
106 | #' @details
107 | #' This function takes an STList and prints the number of genes (rows) and spots (columns) of
108 | #' each spatial array within that object.
109 | #'
110 | #' @param x an STList object to show summary from.
111 | #
112 | #
113 | setMethod(f="dim", signature="STlist",
114 | definition=function(x){
115 | dim_res = list()
116 | for(i in seq(x@counts)){
117 | dim_res[[i]] = c(base::nrow(x@counts[[i]]), base::ncol(x@counts[[i]]))
118 | }
119 | names(dim_res) = names(x@counts)
120 | return(dim_res)
121 | }
122 | )
123 |
124 |
--------------------------------------------------------------------------------
/R/globals.R:
--------------------------------------------------------------------------------
1 | utils::globalVariables(c('.', 'CenterX_local_px', 'CenterY_local_px', 'adj_p_val', 'as.sparse', 'avg_log2fc',
2 | 'barcode', 'cell_ID', 'cell_id', 'cluster_1', 'cluster_2', 'coded_annot', 'comments',
3 | 'comments_spatial', 'contains', 'counts', 'dist2ref', 'dtype', 'emsb', 'exp_adj_p_val',
4 | 'exp_p_val', 'expr_values', 'exprval', 'feat_n', 'fov', 'geary', 'geary_c', 'gene', 'gene_mean',
5 | 'gene_stdevs', 'h5_file', 'imagecol', 'imagerow', 'import_xenium_mex', 'intissue',
6 | 'krige', 'libname', 'meta', 'meta1', 'meta2', 'moran', 'moran_i', 'orig_annot', 'pca_labs',
7 | 'plot_pval', 'prop_size_test', 'sample_name', 'samplename', 'spatialmod',
8 | 'spearman_r_pval', 'spearman_r_pval_adj', 'spot_n', 'total_counts', 'total_genes',
9 | 'values', 'vst.variance.standardized', 'x_centroid', 'x_pos', 'xpos', 'y_centroid',
10 | 'y_pos', 'ypos'))
--------------------------------------------------------------------------------
/R/import_smi.R:
--------------------------------------------------------------------------------
1 | ##
2 | # @title import_smi: Prepare CosMx-SMI inputs to be formated into an STlist
3 | #
4 | # @param counts_fp the path to a CosMx *exprMat* file
5 | # @param coords_fp the path to a CosMx *metadata* file
6 | # @return x a list of processed counts and coordinates
7 | #
8 | #
9 | import_smi = function(counts_fp=NULL, coords_fp=NULL, slidename=NULL){
10 | # Read gene counts
11 | counts_read = data.table::fread(counts_fp)
12 |
13 | # Separate FOVs and create sparse matrices
14 | # FILTER CELLS WITH ID ZERO (0)
15 | fov_ls = list()
16 | for(fovid in unique(counts_read[['fov']])){
17 | fov_ls[[paste0(slidename, '_fov_', fovid)]] = counts_read[counts_read[['fov']] == fovid, ] %>%
18 | dplyr::filter(cell_ID != 0) %>%
19 | dplyr::mutate(libname=paste0('fov_', fov, '_', 'cell_',cell_ID)) %>%
20 | #dplyr::select(-c('fov', 'cell_ID', 'cell')) %>% # NANOSTRING BRAIN CORTEX DATA SET HAD BOTH 'cell_ID' and 'cell'
21 | dplyr::select(!dplyr::matches('^fov$|^cell_ID$|^cell$')) %>% # NANOSTRING BRAIN CORTEX DATA SET HAD BOTH 'cell_ID' and 'cell'
22 | dplyr::relocate(libname, .before=1) %>%
23 | tibble::column_to_rownames('libname') %>%
24 | t() %>% as.data.frame() %>% tibble::rownames_to_column('gene_name')
25 |
26 | # Remove zero-count genes
27 | fov_ls[[paste0(slidename, '_fov_', fovid)]] = fov_ls[[paste0(slidename, '_fov_', fovid)]][rowSums(fov_ls[[paste0(slidename, '_fov_', fovid)]][, -1]) > 0, ]
28 | }
29 | rm(counts_read) # Clean env
30 |
31 | # Read in coordinate data
32 | spotcoords_df = data.table::fread(coords_fp)
33 |
34 | # Separate FOV coordinates
35 | fov_coord_ls = list()
36 | for(fovid in unique(spotcoords_df[['fov']])){
37 | fov_coord_ls[[paste0(slidename, '_fov_', fovid)]] = spotcoords_df[spotcoords_df[['fov']] == fovid, ] %>%
38 | dplyr::select(c('fov', 'cell_ID', 'CenterX_local_px', 'CenterY_local_px')) %>%
39 | dplyr::mutate(libname=paste0('fov_', fov, '_', 'cell_', cell_ID)) %>%
40 | dplyr::select(c('libname', 'CenterX_local_px', 'CenterY_local_px')) %>%
41 | dplyr::rename(ypos=CenterY_local_px, xpos=CenterX_local_px) %>%
42 | tibble::as_tibble()
43 |
44 | # Error if number of spot coordinate do not match number of spots in counts
45 | if(nrow(fov_coord_ls[[paste0(slidename, '_fov_', fovid)]]) != ncol(fov_ls[[paste0(slidename, '_fov_', fovid)]][, -1])){
46 | stop(paste0('Number of cells in FOV ', fovid, ' do not match between count and coordinate data.'))
47 | }
48 |
49 | }
50 | rm(spotcoords_df) # Clean env
51 |
52 | smi_list = list(rawcounts=fov_ls, coords=fov_coord_ls)
53 | return(smi_list)
54 | }
55 |
--------------------------------------------------------------------------------
/R/old_SThet.R:
--------------------------------------------------------------------------------
1 | ##
2 | # @title SThet: Computes global spatial autocorrelation statistics on gene expression
3 | # @description Computes the global spatial autocorrelation statistics Moran's I and/or
4 | # Geary's C for a set of genes
5 | # @details The function computes global spatial autocorrelation statistics (Moran's I and/or
6 | # Geary's C) for the requested genes and samples. Then computation uses the
7 | # package `spdep`. The calculated statistics are stored in the STlist, which can
8 | # be accessed with the `get_gene_meta` function.
9 | #
10 | # @param x an STlist
11 | # @param genes a vector of gene names to compute statistics
12 | # @param samples the samples to compute statistics
13 | # @param method The spatial statistic(s) to estimate. It can be set to 'moran',
14 | # 'geary' or both. Default is 'moran'
15 | # @param overwrite logical indicating if previous statistics should be overwritten.
16 | # Default to TRUE (overwrite)
17 | # @return an STlist containing spatial statistics
18 | #
19 | # @export
20 | #
21 | old_SThet = function(x=NULL, genes=NULL, samples=NULL, method='moran', overwrite=T){
22 | # Select sample names if NULL or if number entered
23 | if (is.null(samples)){
24 | samples = names(x@tr_counts)
25 | } else{
26 | if(is.numeric(samples)){
27 | samples = names(x@tr_counts)[samples]
28 | }
29 | }
30 |
31 | # Generate combination of sample x gene to for.
32 | combo_tmp = tibble::tibble()
33 | for(i in samples){
34 | # Check if gene names are in the data set
35 | subsetgenes = genes[genes %in% rownames(x@tr_counts[[i]])]
36 | combo_tmp = dplyr::bind_rows(combo_tmp, expand.grid(i, subsetgenes))
37 |
38 | # Get genes not present.
39 | notgenes = genes[!(genes %in% rownames(x@tr_counts[[i]]))]
40 |
41 | if(!rlang::is_empty(notgenes)){
42 | cat(paste0(paste(notgenes, collapse=', '), ": Not present in the transformed counts for sample ", i), ".\n")
43 | }
44 |
45 | rm(subsetgenes, notgenes) # Clean env
46 |
47 | # Add columns in gene meta data if not already present
48 | if(!('moran_i' %in% colnames(x@gene_meta[[i]]))){
49 | x@gene_meta[[i]][['moran_i']] = NA
50 | }
51 | if(!('geary_c' %in% colnames(x@gene_meta[[i]]))){
52 | x@gene_meta[[i]][['geary_c']] = NA
53 | }
54 | }
55 |
56 | # Check whether or not a list of weights have been created
57 | if(overwrite | is.null(x@misc[['sthet']][['listws']])){
58 | x@misc[['sthet']][['listws']] = create_listw(x)
59 | }
60 |
61 | # Perform calculations
62 | if('moran' %in% method){
63 | x = gene_moran_i(x=x, combo=combo_tmp, overwrite=overwrite)
64 | }
65 | if('geary' %in% method){
66 | x = gene_geary_c(x=x, combo=combo_tmp, overwrite=overwrite)
67 | }
68 |
69 | return(x)
70 | }
71 |
72 |
73 | # Helpers ----------------------------------------------------------------------
74 |
75 | ##
76 | # @title gene_moran_I
77 | # @description Calculates Moran's I from ST data.
78 | #
79 | # @param x an STlist with normalized gene counts.
80 | # @param combo a table with combinations of samples and genes to calculate statistics
81 | # @return x a STlist including the calculated Moran's I
82 | #
83 | #
84 | gene_moran_i <- function(x=NULL, combo=NULL, overwrite=T) {
85 |
86 | genes = as.vector(unique(combo[[2]]))
87 |
88 | # Define cores available
89 | cores = count_cores(nrow(combo))
90 | # Loop through combinations of samples x genes
91 | stat_list = parallel::mclapply(seq_along(1:nrow(combo)), function(i_combo){
92 | i = as.vector(unlist(combo[i_combo, 1]))
93 | j = as.vector(unlist(combo[i_combo, 2]))
94 |
95 | # Extract expression data for a given gene.
96 | gene_expr = x@tr_counts[[i]][j, ]
97 |
98 | # Estimate statistic.
99 | stat_est = spdep::moran.test(x=gene_expr, listw=x@misc[['sthet']][['listws']][[i]])
100 |
101 | return(stat_est)
102 | }, mc.cores=cores, mc.preschedule=F)
103 | names(stat_list) = paste(combo[[1]], combo[[2]], sep='&&')
104 |
105 | # Store kriging results in STList.
106 | for(i in 1:nrow(combo)){
107 | combo_name = unlist(strsplit(names(stat_list)[i], split = '&&'))
108 | if(overwrite | is.na(as.vector(x@gene_meta[[combo_name[1]]][x@gene_meta[[combo_name[1]]][['gene']] == combo_name[2], 'moran_i']))){
109 | x@gene_meta[[combo_name[1]]][x@gene_meta[[combo_name[1]]][['gene']] == combo_name[2], 'moran_i'] = as.vector(stat_list[[i]]$estimate[1])
110 | #print(as.vector(stat_list[[i]]$estimate[1]))
111 | }
112 | }
113 |
114 | return(x)
115 | }
116 |
117 |
118 | ##
119 | # @title gene_geary_C
120 | # @description Calculates Geary's C from ST data.
121 | #
122 | # @param x an STlist with normalized gene counts.
123 | # @param combo a table with combinations of samples and genes to calculate statistics
124 | # @return x a STlist including the calculated Geary's I
125 | #
126 | #
127 | gene_geary_c <- function(x=NULL, combo=NULL, overwrite=T) {
128 |
129 | genes = as.vector(unique(combo[[2]]))
130 |
131 | # Define cores available
132 | cores = count_cores(nrow(combo))
133 | # Loop through combinations of samples x genes
134 | stat_list = parallel::mclapply(seq_along(1:nrow(combo)), function(i_combo){
135 | i = as.vector(unlist(combo[i_combo, 1]))
136 | j = as.vector(unlist(combo[i_combo, 2]))
137 |
138 | # Extract expression data for a given gene.
139 | gene_expr = x@tr_counts[[i]][j, ]
140 |
141 | # Estimate statistic.
142 | stat_est = spdep::geary.test(x=gene_expr, listw=x@misc[['sthet']][['listws']][[i]])
143 |
144 | return(stat_est)
145 | }, mc.cores=cores, mc.preschedule=F)
146 | names(stat_list) = paste(combo[[1]], combo[[2]], sep='&&')
147 |
148 | # Store results in STList.
149 | for(i in 1:nrow(combo)){
150 | combo_name = unlist(strsplit(names(stat_list)[i], split = '&&'))
151 | if(overwrite | is.na(as.vector(x@gene_meta[[combo_name[1]]][x@gene_meta[[combo_name[1]]][['gene']] == combo_name[2], 'geary_c']))){
152 | x@gene_meta[[combo_name[1]]][x@gene_meta[[combo_name[1]]][['gene']] == combo_name[2], 'geary_c'] = as.vector(stat_list[[i]]$estimate[1])
153 | #print(as.vector(stat_list[[i]]$estimate[1]))
154 | }
155 | }
156 |
157 | return(x)
158 | }
159 |
160 |
--------------------------------------------------------------------------------
/R/plot_helpers.R:
--------------------------------------------------------------------------------
1 | ##
2 | # @title quilt_p
3 | # @description Creates a quilt plot from ST data.
4 | # @details
5 | # Function to produce a "quilt plot" from a data frame with three columns:
6 | # x coordinates, y coordinates, and values (expression or cell scores). The data
7 | # frame has column names 'x_pos', 'y_pos', and 'values'. It also takes a color
8 | # palette name from the 'khroma' or RColorBrewer packages. Finally, it takes a
9 | # name for the color legend title.
10 | #
11 | # @param data_f, a data with three columns: x coordinates, y coordinates, and
12 | # the values to be plotted.
13 | # @param color_pal, a scheme from 'khroma'. Default is 'YlOrBr'.
14 | # @param leg_name, a short name for the legend title.
15 | # @param title_name, a short name for the plot title.
16 | # @param minvalue, the minimum value of gene expression or cell score. Used for
17 | # standardization.
18 | # @param maxvalue, the maximum value of gene expression or cell score. Used for
19 | # standardization.
20 | # @param visium, whether or not to reverse axes for Visium slides.
21 | # @param ptsize, a number specifying the size of the points. Passed to `size` aesthetic.
22 | # @return, a ggplot object.
23 | #
24 | #' @import ggplot2
25 | #
26 | #
27 | quilt_p <- function(data_f=NULL, color_pal="BuRd", leg_name='', title_name='', minvalue=minvalue, maxvalue=maxvalue, visium=T, ptsize=0.5){
28 |
29 | # Creates color palette function.
30 | p_palette = color_parse(color_pal)
31 |
32 | # Create plot.
33 | p <- ggplot2::ggplot(data=data_f, ggplot2::aes(x=xpos, y=ypos, color=values)) +
34 | ggplot2::geom_point(size=ptsize) +
35 | ggplot2::scale_color_gradientn(colours=p_palette, limits=c(minvalue, maxvalue)) +
36 | ggplot2::xlab("X Position") +
37 | ggplot2::ylab("Y Position") +
38 | ggplot2::labs(color=leg_name, title=title_name) +
39 | ggplot2::theme_void()
40 | #ggplot2::theme_classic()
41 |
42 | if(visium){
43 | p <- p + ggplot2::scale_y_reverse()
44 | # scale_y_reverse() + coord_fixed(ratio=1.7)
45 | }
46 |
47 | p <- p + ggplot2::coord_equal() + theme(legend.position="right")
48 |
49 | return(p)
50 | }
51 |
52 |
53 | ##
54 | # @title krige_p
55 | # @description Creates a kriging plot from ST data.
56 | # @details
57 | # Function to produce a "kriging plot" from a data frame with three columns:
58 | # x coordinates, y coordinates, and predicted kriging values. The data frame has
59 | # column names 'x_pos', 'y_pos', and 'krige'. The function also takes a
60 | # SpatialPolygons object to mask the predicted grid to the area of the tissue.
61 | # It also takes a color palette name from the 'khroma' package. Finally, it
62 | # takes a name for the color legend title.
63 | #
64 | # @param data_f, a data with three columns: x coordinates, y coordinates, and
65 | # the kriging prediction values to be plotted.
66 | # @param mask, an object of class SpatialPolygons containing a large polygon
67 | # encasing all the predicted grid, and a smaller polygon drawing the concave hull
68 | # of the tissue shape.
69 | # @param color_pal, a scheme from 'khroma'. Default is 'YlOrBr'.
70 | # @param leg_name, a short name for the legend title.
71 | # @param title_name, a short name for the plot title.
72 | # @param minvalue, the minimum value of gene expression or cell score. Used for
73 | # standardization.
74 | # @param maxvalue, the maximum value of gene expression or cell score. Used for
75 | # standardization.
76 | # @param visium, whether or not to reverse axes for Visium slides.
77 | # @return, a ggplot object.
78 | #
79 | #' @import ggplot2
80 | #' @import ggpolypath
81 | #
82 | #
83 | krige_p <- function(data_f=NULL, mask=NULL, color_pal="YlOrBr", leg_name='',
84 | title_name='', minvalue=minvalue, maxvalue=maxvalue,
85 | visium=T){
86 |
87 | #requireNamespace('sf')
88 |
89 | # Creates color palette function.
90 | p_palette = color_parse(color_pal)
91 |
92 | # Convert the SpatialPolygon mask into a data frame.
93 | #mask_df <- ggplot2::fortify(mask)
94 |
95 | # Create plot.
96 | p <- ggplot2::ggplot(data=data_f, ggplot2::aes(x=x_pos, y=y_pos)) +
97 | ggplot2::geom_raster(ggplot2::aes(fill=krige), interpolate=F) +
98 | ggplot2::scale_fill_gradientn(colors=p_palette, limits=c(minvalue, maxvalue), oob=scales::squish) +
99 | ggplot2::xlab("X Position") +
100 | ggplot2::ylab("Y Position") +
101 | ggplot2::labs(fill=leg_name, title=title_name) +
102 | ggplot2::geom_sf(data=mask, color='white', fill="white", linewidth=2, inherit.aes=F) +
103 | ggplot2::theme_void()
104 |
105 | # if(visium){
106 | # p <- p + ggplot2::scale_y_reverse() #+ scale_x_reverse() +
107 | # #coord_fixed(ratio=1.7)
108 | # }
109 |
110 | p <- p + #ggplot2::coord_equal() +
111 | ggplot2::theme(legend.position="right")
112 |
113 | return(p)
114 | }
115 |
116 |
--------------------------------------------------------------------------------
/R/plot_image.R:
--------------------------------------------------------------------------------
1 | ##
2 | #' @title plot_image: Generate a ggplot object of the tissue image
3 | #' @description Creates ggplot objects of the tissue images when available within the STlist
4 | #' @details
5 | #' If the STlist contains tissue images in the `@misc` slot, the `plot_image` function
6 | #' can be used to generate ggplot objects. These ggplot objects can be plotted next to
7 | #' quilt plots (`STplot` function) for comparative analysis.
8 | #'
9 | #' @param x an STlist
10 | #' @param samples a vector of numbers indicating the ST samples to plot, or their
11 | #' sample names. If vector of numbers, it follow the order of `names(x@counts)`.
12 | #' If NULL, the function plots all samples
13 | #' @return a list of plots
14 | #'
15 | #' @import ggplot2
16 | #
17 | #' @export
18 | #
19 | #
20 | plot_image = function(x=NULL, samples=NULL){
21 |
22 | # Define which samples to plot
23 | if(is.null(samples)){
24 | samples = names(x@spatial_meta)
25 | } else{
26 | if(is.numeric(samples)){
27 | samples = names(x@spatial_meta)[samples]
28 | }
29 | if(length(grep(paste0(samples, collapse='|'), names(x@spatial_meta))) == 0){
30 | stop('The requested samples are not present in the STlist.')
31 | }
32 | }
33 |
34 | # Find which samples have images
35 | samples_tmp = samples
36 | for(i in samples){
37 | if(!(i %in% names(x@misc[['sp_images']]))){
38 | samples_tmp = grep(i, samples_tmp, value=T, invert=T)
39 | }
40 | }
41 | samples = samples_tmp
42 | rm(samples_tmp)
43 |
44 | # Create list of plots.
45 | rp_list <- list()
46 |
47 | if(length(samples) < 1){
48 | warning('No tissue images available for the samples in this STlist.')
49 | } else{
50 | # Loop through each normalized count matrix.
51 | for (i in samples){
52 | # Read image if available
53 | if(!is.null(x@misc[['sp_images']][[i]])){
54 | img_obj = grid::rasterGrob(x@misc[['sp_images']][[i]])
55 | }
56 |
57 | # Get scaling factor is available (VISIUM)
58 | if(is.list(x@misc[['image_scaling']][[i]]) & x@misc[['platform']] == 'visium'){
59 | scl_info_hires = x@misc[['image_scaling']][[i]][['tissue_hires_scalef']]
60 | scl_info_lores = x@misc[['image_scaling']][[i]][['tissue_lowres_scalef']]
61 |
62 | # Attempt scaling with hires scaling factor
63 | image_test_hires = tryCatch({
64 | raster_extext = list(ymin=round(min(x@spatial_meta[[i]][['ypos']]) * scl_info_hires, 0),
65 | ymax=round(max(x@spatial_meta[[i]][['ypos']]) * scl_info_hires, 0),
66 | xmin=round(min(x@spatial_meta[[i]][['xpos']]) * scl_info_hires, 0),
67 | xmax=round(max(x@spatial_meta[[i]][['xpos']]) * scl_info_hires, 0))
68 | image_tmp = img_obj
69 | image_tmp[['raster']] = image_tmp[['raster']][raster_extext$ymin:raster_extext$ymax, raster_extext$xmin:raster_extext$xmax]
70 | },
71 | error=function(e){
72 | return(e)
73 | })
74 |
75 | # Attempt scaling with lowres scaling factor
76 | if(any(class(image_test_hires) == 'simpleError')){
77 | image_test_lores = tryCatch({
78 | raster_extext = list(ymin=round(min(x@spatial_meta[[i]][['ypos']]) * scl_info_hires, 0),
79 | ymax=round(max(x@spatial_meta[[i]][['ypos']]) * scl_info_hires, 0),
80 | xmin=round(min(x@spatial_meta[[i]][['xpos']]) * scl_info_hires, 0),
81 | xmax=round(max(x@spatial_meta[[i]][['xpos']]) * scl_info_hires, 0))
82 | image_tmp = img_obj
83 | image_tmp = image_tmp[raster_extext$ymin:raster_extext$ymax, raster_extext$xmin:raster_extext$xmax]
84 | },
85 | error=function(e){
86 | return(e)
87 | })
88 | }
89 |
90 | # Re-assign object whether modified or not
91 | img_obj = image_tmp
92 | }
93 |
94 | rp_list[[paste0('image_', i)]] = ggplot() +
95 | annotation_custom(img_obj)
96 | }
97 | }
98 | return(rp_list)
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/R/plot_spatial_expression.R:
--------------------------------------------------------------------------------
1 | ##
2 | # @title plot_spatial_expression: Plots gene expression at each spot from an ST sample
3 | # @description Plot raw or transformed gene expression levels at each spot (quilt plot)
4 | # within a spatially-resolved transcriptomic sample
5 | # @details
6 | # This function produces a 'quilt plot' for a one or several genes and ST samples
7 | # within an STlist. A quilt plot shows the levels of gene expression at each spot r
8 | # cell as a color gradient
9 | #
10 | # @param x an STlist
11 | # @param genes a vector of one or more gene names to plot.
12 | # @param samples a vector of numbers indicating the ST samples to plot, or their
13 | # sample names. If vector of numbers, it follow the order of `names(x@counts)`.
14 | # If NULL, the function plots all samples
15 | # @param color_pal a color scheme from 'khroma' or RColorBrewer.
16 | # @param data_type one of 'tr' or 'raw', to plot transformed or raw counts
17 | # respectively
18 | # @param visium logical, whether or not the samples are from a Visium experiment.
19 | # @param ptsize a number specifying the size of the points in the quilt plot.
20 | # Passed to the `size` aesthetic
21 | # @return a list with plots
22 | #
23 | # @importFrom methods as is new
24 | #' @import ggplot2
25 | #' @importFrom magrittr %>%
26 | #
27 | #
28 | plot_spatial_expression = function(x=NULL, genes=NULL, samples=NULL, color_pal='BuRd', data_type='tr', visium=T, ptsize=NULL){
29 |
30 | # To prevent NOTES in R CMD check
31 | . = NULL
32 |
33 | # Test that a gene name was entered.
34 | if (is.null(genes)) {
35 | stop("Please, enter one or more genes to plot.")
36 | }
37 |
38 | # Remove duplicated genes entered by user.
39 | genes = unique(genes)
40 |
41 | # Define which samples to plot
42 | if(is.null(samples)){
43 | samples = names(x@spatial_meta)
44 | } else{
45 | if(is.numeric(samples)){
46 | samples = names(x@spatial_meta)[samples]
47 | }
48 | if(length(grep(paste0(samples, collapse='|'), names(x@spatial_meta))) == 0){
49 | stop('The requested samples are not present in the STList spatial metadata.')
50 | }
51 | }
52 |
53 | # Select appropriate slot to take counts from
54 | if(data_type == 'tr'){
55 | counts = x@tr_counts[samples]
56 | }else if(data_type == 'raw'){
57 | counts = x@counts[samples]
58 | # Expand sparse matrices
59 | # for(i in samples){
60 | # counts[[i]] = expandSparse(counts[[i]])
61 | # counts[[i]] = tibble::as_tibble(tibble::rownames_to_column(counts[[i]], var='gene'))
62 | # }
63 | } else(
64 | stop('Please, select one of transformed (tr) or raw (raw) counts')
65 | )
66 |
67 | # Extract genes and trasnpose expression data (to get genes in columns)
68 | # Also join coordinate data
69 | for(i in samples){
70 | if(any(rownames(counts[[i]]) %in% genes)){
71 | # counts[[i]] = counts[[i]] %>%
72 | # expandSparse() %>%
73 | # tibble::rownames_to_column(var='gene') %>%
74 | # dplyr::filter(gene %in% genes) %>%
75 | # tibble::column_to_rownames(var='gene') %>%
76 | # t() %>%
77 | # as.data.frame() %>%
78 | # tibble::rownames_to_column(var='libname') %>%
79 | # dplyr::left_join(x@spatial_meta[[i]] %>%
80 | # dplyr::select(libname, xpos, ypos), ., by='libname')
81 |
82 | counts[[i]] = expandSparse(counts[[i]][rownames(counts[[i]]) %in% genes, , drop=FALSE]) %>%
83 | t() %>%
84 | as.data.frame() %>%
85 | tibble::rownames_to_column(var='libname') %>%
86 | dplyr::left_join(x@spatial_meta[[i]] %>%
87 | dplyr::select(libname, xpos, ypos), ., by='libname')
88 | } else{
89 | # Remove samples for which none of the requested genes are available
90 | counts = counts[grep(i, names(counts), value=T, invert=T)]
91 | warning(paste0('None of the requested genes are available for sample ', i, '.'))
92 | }
93 | }
94 |
95 | # Test if requested data slot is available.
96 | if(rlang::is_empty(counts)) {
97 | stop("Data was not found in the specified slot of this STList.")
98 | }
99 |
100 | # Store maximum expression value to standardize color legend.
101 | maxvalue <- c()
102 | minvalue <- c()
103 | for(i in names(counts)){
104 | for(gene in genes){
105 | # Test if gene name exists in normalized count matrix.
106 | if(any(colnames(counts[[i]]) == gene)){
107 | # Find maximum expression value for each spatial array.
108 | values_tmp <- unlist(counts[[i]][[gene]])
109 | maxvalue <- append(maxvalue, max(values_tmp))
110 | minvalue <- append(minvalue, min(values_tmp))
111 | rm(values_tmp) # Clean environment
112 | }
113 | }
114 | }
115 | # Find maximum value among selected spatial arrays.
116 | maxvalue <- max(maxvalue)
117 | minvalue <- min(minvalue)
118 |
119 | # Define size of points
120 | if(is.null(ptsize)){
121 | ptsize = 0.5
122 | }
123 |
124 | # Create list of plots.
125 | qp_list <- list()
126 | # Loop through each normalized count matrix.
127 | for (i in names(counts)) {
128 | # Loop though genes to plot.
129 | for (gene in colnames(counts[[i]] %>% dplyr::select(-libname, -xpos, -ypos))){
130 | # Extract relevant data frame
131 | df_tmp = counts[[i]] %>%
132 | #dplyr::rename(values:=!!gene)
133 | dplyr::select(xpos, ypos, values:=!!gene)
134 |
135 | # Set legend title
136 | if(data_type == 'tr'){
137 | qlegname = paste0(x@misc[['transform']], "\nexpr")
138 | } else{
139 | qlegname = 'raw\nexpr'
140 | }
141 |
142 | # The color palette function in khroma is created by quilt_p() function.
143 | qp <- quilt_p(data_f=df_tmp, leg_name=qlegname, color_pal=color_pal,
144 | title_name=paste0(gene, "\n", "sample: ", i),
145 | minvalue=minvalue, maxvalue=maxvalue, visium=visium, ptsize=ptsize)
146 |
147 | # Append plot to list.
148 | qp_list[[paste0(gene, "_", i)]] = qp
149 |
150 | rm(df_tmp) # Clean environment
151 | }
152 | }
153 |
154 | return(qp_list)
155 | }
156 |
157 |
--------------------------------------------------------------------------------
/R/plot_spatial_geneset.R:
--------------------------------------------------------------------------------
1 | ##
2 | # @title plot_spatial_geneset: Averaged gene set expression at each spot in an ST sample
3 | # @description Plot averaged transformed expression of gene within a gene set (quilt plot).
4 | # @details
5 | # This function produces a quilt plot for a series of gene sets and spatial
6 | # arrays within an STlist.
7 | #
8 | # @param x an STlist
9 | # @param genes a named list of gene sets to plot
10 | # @param samples a vector of numbers indicating the ST samples to plot, or their
11 | # sample names. If vector of numbers, it follow the order of `names(x@counts)`.
12 | # If NULL, the function plots all samples
13 | # @param color_pal a color scheme from 'khroma' or RColorBrewer
14 | # @param visium logical, whether or not the samples are from a Visium experiment
15 | # @param ptsize a number specifying the size of the points in the quilt plot.
16 | # Passed to the `size` aesthetic
17 | # @return a list with plots
18 | #
19 | # @importFrom methods as is new
20 | #' @import ggplot2
21 | #' @importFrom magrittr %>%
22 | #
23 | #
24 | plot_spatial_geneset = function(x=NULL, genes=NULL, samples=NULL, color_pal='BuRd', visium=T, ptsize=NULL){
25 |
26 | # To prevent NOTES in R CMD check
27 | . = NULL
28 |
29 | # May implement later
30 | image = F
31 |
32 | # Test that a gene name was entered.
33 | if (is.null(genes)) {
34 | stop("Please, enter one or more genes to plot.")
35 | }
36 |
37 | # Define which samples to plot
38 | if(is.null(samples)){
39 | samples = names(x@spatial_meta)
40 | } else{
41 | if(is.numeric(samples)){
42 | samples = names(x@spatial_meta)[samples]
43 | }
44 | if(length(grep(paste0(samples, collapse='|'), names(x@spatial_meta))) == 0){
45 | stop('The requested samples are not present in the STList spatial metadata.')
46 | }
47 | }
48 |
49 | # Test if no transformed counts are available
50 | if(rlang::is_empty(x@tr_counts)){
51 | stop('There are no transformed counts in the STlist.')
52 | }
53 |
54 | avg_expr = list()
55 | # Loop through samples
56 | for(i in samples){
57 | counts = expandSparse(x@tr_counts[[i]])
58 | # Loop through gene sets
59 | avg_expr[[i]] = list()
60 | for(set in names(genes)){
61 | if(any(rownames(counts) %in% genes[[set]])){
62 | # Get averaged expression
63 | avg_expr[[i]][[set]] = as.data.frame(t(data.frame(colMeans(counts[rownames(counts) %in% genes[[set]], ]))))
64 | rownames(avg_expr[[i]][[set]]) = set
65 | }
66 | }
67 | # Make data frame from lots of averaged expression
68 | avg_expr[[i]] = dplyr::bind_rows(avg_expr[[i]])
69 |
70 | # Merge coordinate data
71 | avg_expr[[i]] = t(avg_expr[[i]]) %>%
72 | as.data.frame() %>%
73 | tibble::rownames_to_column(var='libname') %>%
74 | dplyr::left_join(x@spatial_meta[[i]] %>%
75 | dplyr::select(libname, xpos, ypos), ., by='libname')
76 | }
77 |
78 | # Store maximum expression value to standardize color legend.
79 | maxvalue <- c()
80 | minvalue <- c()
81 | for(i in names(avg_expr)){
82 | for(set in names(genes)){
83 | # Test if gene name exists in normalized count matrix.
84 | if(any(colnames(avg_expr[[i]]) == set)){
85 | # Find maximum expression value for each spatial array.
86 | values_tmp <- unlist(avg_expr[[i]][[set]])
87 | maxvalue <- append(maxvalue, max(values_tmp))
88 | minvalue <- append(minvalue, min(values_tmp))
89 | rm(values_tmp) # Clean environment
90 | }
91 | }
92 | }
93 | # Find maximum value among selected spatial arrays.
94 | maxvalue <- max(maxvalue)
95 | minvalue <- min(minvalue)
96 |
97 | # Define size of points
98 | if(is.null(ptsize)){
99 | ptsize = 0.5
100 | }
101 |
102 | # Create list of plots.
103 | qp_list <- list()
104 | # Loop through each normalized count matrix.
105 | for (i in names(avg_expr)) {
106 | # Loop though genes to plot.
107 | for (set in colnames(avg_expr[[i]] %>% dplyr::select(-libname, -xpos, -ypos))){
108 | # Extract relevant data frame
109 | df_tmp = avg_expr[[i]] %>%
110 | dplyr::select(xpos, ypos, values:=!!set)
111 |
112 | # The color palette function in khroma is created by quilt_p() function.
113 | qp <- quilt_p(data_f=df_tmp, leg_name='averaged\ngene_expr', color_pal=color_pal,
114 | title_name=paste0(set, "\n", "sample: ", i),
115 | minvalue=minvalue, maxvalue=maxvalue, visium=visium, ptsize=ptsize)
116 |
117 | # Append plot to list.
118 | qp_list[[paste0(set, "_", i)]] = qp
119 |
120 | rm(df_tmp) # Clean environment
121 | }
122 |
123 | if(image && !is.null(x@misc[['sp_images']][[i]])){
124 | img_obj = grid::rasterGrob(x@misc[['sp_images']][[i]])
125 | qp_list[[paste0('image', names(counts[i]))]] = ggplot() +
126 | annotation_custom(img_obj)
127 | }
128 | }
129 |
130 | return(qp_list)
131 | }
132 |
133 |
--------------------------------------------------------------------------------
/R/seurat_helpers.R:
--------------------------------------------------------------------------------
1 | ##
2 | # UTILITY AND HELPER FUNCTIONS MODIFIED FROM THE SEURAT PACKAGE
3 | #
4 | # Copyright (c) 2021 Seurat authors
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy of this
6 | # software and associated documentation files (the "Software"), to deal in the Software
7 | # without restriction, including without limitation the rights to use, copy, modify, merge,
8 | # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9 | # to whom the Software is furnished to do so, subject to the following conditions:
10 | # The above copyright notice and this permission notice shall be included in all copies or
11 | # substantial portions of the Software.
12 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 | # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 | # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15 | # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16 | # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 | # DEALINGS IN THE SOFTWARE.
18 |
19 | ##
20 | # @title Seurat_FindVariableFeatures
21 | # @description Extracted from the Seurat package
22 | #
23 | #' @importFrom stats loess setNames
24 | #
25 | #
26 | Seurat_FindVariableFeatures = function(object=NULL){
27 | # Fixed arguments
28 | selection.method="vst"
29 | loess.span=0.3
30 | clip.max='auto'
31 |
32 | if (!inherits(x=object, 'Matrix')) {
33 | object <- as(object = as.matrix(x = object), Class = 'Matrix')
34 | }
35 | if (!inherits(x = object, what = 'dgCMatrix')) {
36 | object <- as.sparse(x = object)
37 | }
38 | if (selection.method == "vst") {
39 | if (clip.max == 'auto') {
40 | clip.max <- sqrt(x = ncol(x = object))
41 | }
42 | hvf.info <- data.frame(mean = rowMeans(x = object))
43 | hvf.info$variance <- SparseRowVar2(
44 | mat = object,
45 | mu = hvf.info$mean
46 | )
47 | hvf.info$variance.expected <- 0
48 | hvf.info$variance.standardized <- 0
49 | not.const <- hvf.info$variance > 0
50 | fit <- loess(
51 | formula = log10(x = variance) ~ log10(x = mean),
52 | data = hvf.info[not.const, ],
53 | span = loess.span
54 | )
55 | hvf.info$variance.expected[not.const] <- 10 ^ fit$fitted
56 | # use c function to get variance after feature standardization
57 | hvf.info$variance.standardized <- SparseRowVarStd(
58 | mat = object,
59 | mu = hvf.info$mean,
60 | sd = sqrt(hvf.info$variance.expected),
61 | vmax = clip.max
62 | )
63 | colnames(x = hvf.info) <- paste0('vst.', colnames(x = hvf.info))
64 | }
65 | return(hvf.info)
66 | }
67 |
68 |
--------------------------------------------------------------------------------
/R/summarize_STlist.R:
--------------------------------------------------------------------------------
1 | ##
2 | #' @title summarize_STlist: Generates a data frame with summary statistics
3 | #' @description Produces a data frame with counts per gene and counts per ROI/spot/cell
4 | #' @details
5 | #' The function creates a table with counts per gene and counts per region of interest (ROI),
6 | #' spot, or cell in the samples stored in the STlist
7 | #'
8 | #' @param x an STlist
9 | #' @return a data frame
10 | #'
11 | #' @examples
12 | ##'
13 | #' # Using included melanoma example (Thrane et al.)
14 | #' # Download example data set from spatialGE_Data
15 | #' thrane_tmp = tempdir()
16 | #' unlink(thrane_tmp, recursive=TRUE)
17 | #' dir.create(thrane_tmp)
18 | #' lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
19 | #' download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
20 | #' zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
21 | #' unzip(zipfile=zip_tmp, exdir=thrane_tmp)
22 | #' # Generate the file paths to be passed to the STlist function
23 | #' count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
24 | #' full.names=TRUE, pattern='counts')
25 | #' coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
26 | #' full.names=TRUE, pattern='mapping')
27 | #' clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
28 | #' full.names=TRUE, pattern='clinical')
29 | #' # Create STlist
30 | #' library('spatialGE')
31 | #' melanoma <- STlist(rnacounts=count_files[c(1,2)],
32 | #' spotcoords=coord_files[c(1,2)],
33 | #' samples=clin_file) # Only first two samples
34 | #' summarize_STlist(melanoma)
35 | #'
36 | #' @export summarize_STlist
37 | #
38 | summarize_STlist = function(x=NULL){
39 | if(!is(x, 'STlist')){
40 | stop('The input is not an STlist')
41 | }
42 |
43 | # Get statistics per sample
44 | df_tmp = lapply(names(x@counts), function(i){
45 | total_counts = sum(x@counts[[i]])
46 | # Count stats
47 | mean_counts_spot = mean(Matrix::colSums(x@counts[[i]]), na.rm=T)
48 | min_counts_spot = min(Matrix::colSums(x@counts[[i]]))
49 | max_counts_spot = max(Matrix::colSums(x@counts[[i]]))
50 | # Gene stats (non-zero counts)
51 | mtx_mask = x@counts[[i]] > 0
52 | mean_genes_spot = mean(Matrix::colSums(mtx_mask))
53 | min_genes_spot = min(Matrix::colSums(mtx_mask))
54 | max_genes_spot = max(Matrix::colSums(mtx_mask))
55 |
56 | # Make a row with stats
57 | df_row_tmp = tibble::tibble(sample_name=i,
58 | spotscells=ncol(x@counts[[i]]),
59 | genes=nrow(x@counts[[i]]),
60 | min_counts_per_spotcell=min_counts_spot,
61 | mean_counts_per_spotcell=mean_counts_spot,
62 | max_counts_per_spotcell=max_counts_spot,
63 | min_genes_per_spotcell=min_genes_spot,
64 | mean_genes_per_spotcell=mean_genes_spot,
65 | max_genes_per_spotcell=max_genes_spot)
66 | return(df_row_tmp)
67 | })
68 |
69 | # Compile all rows into table
70 | df_tmp = dplyr::bind_rows(df_tmp)
71 |
72 | return(df_tmp)
73 | }
74 |
75 |
--------------------------------------------------------------------------------
/R/zzz.R:
--------------------------------------------------------------------------------
1 | #' @useDynLib spatialGE
2 | #' @importFrom Rcpp evalCpp
3 | NULL
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spatialGE
2 |
3 |
4 |
5 | An R package for the visualization and analysis of spatially-resolved transcriptomics data,
6 | such as those generated with 10X Visium. The **spatialGE** package features a data object
7 | (STlist: Spatial Transctiptomics List) to store data and results from multiple tissue sections,
8 | as well as associated analytical methods for:
9 |
10 | - Visualization: `STplot`, `gene_interpolation`, `STplot_interpolation` to explore gene
11 | expression in spatial context.
12 | - Spatial autocorrelation: `SThet`, `compare_SThet` to assess the level of spatial uniformity in
13 | gene expression by calculating Moran's I and/or Geary's C and qualitatively explore correlations with
14 | sample-level metadata (i.e., tissue type, therapy, disease status).
15 | - Tissue domain/niche detection: `STclust` to perform spatially-informed hierarchical clustering for
16 | prediction of tissue domains in samples.
17 | - Gene set spatial enrichment: `STenrich` to detect gene sets with indications of spatial
18 | patterns (i.e., non-spatially uniform gene set expression).
19 | - Gene expression spatial gradients: `STgradient` to detect genes with evidence of variation in
20 | expression with respect to a tissue domain.
21 | - Spatially-informed differential expression: `STdiff` to test for differentially expressed
22 | genes using mixed models with spatial covariance structures to account of spatial dependency
23 | among spots/cells. It also supports non-spatial tests (Wilcoxon's and T-test).
24 |
25 | The methods in the initial spatialGE release, technical details, and their utility are presented in
26 | this publication: https://doi.org/10.1093/bioinformatics/btac145. For details on the recently
27 | developed methods `STenrich`, `STgradient`, and `STdiff` please refer to the spatialGE documentation.
28 |
29 |
30 |
31 |
32 |
33 | ## Installation
34 |
35 | The `spatialGE` repository is available at GitHub and can be installed via `devtools`.
36 | ```
37 | options(timeout=9999999) # To avoid R closing connection with GitHub
38 | devtools::install_github("fridleylab/spatialGE")
39 | ```
40 |
41 | ## How to use spatialGE
42 |
43 | For tutorials on how to use `spatialGE`, please go to:
44 | https://fridleylab.github.io/spatialGE/
45 |
46 | The code for `spatialGE` can be found here:
47 | https://github.com/FridleyLab/spatialGE
48 |
49 | ## [spatialGE-Web](https://spatialge.moffitt.org)
50 |
51 | A point-and-click web application that allows using spatialGE without coding/scripting is
52 | available at https://spatialge.moffitt.org . The web app currently supports Visium outputs and
53 | csv/tsv gene expression files paired with csv/tsv coordinate files.
54 |
55 | ## How to cite
56 |
57 | When using spatialGE, please cite the following publication:
58 |
59 | Ospina, O. E., Wilson C. M., Soupir, A. C., Berglund, A. Smalley, I., Tsai, K. Y., Fridley, B. L. 2022.
60 | spatialGE: quantification and visualization of the tumor microenvironment heterogeneity using spatial
61 | transcriptomics. Bioinformatics, 38:2645-2647. https://doi.org/10.1093/bioinformatics/btac145
62 |
63 |
64 |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66 |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
67 |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
`, etc.) that has more than one element. Defaults to 1 (for `
`).
76 | getTopLevel: function($scope) {
77 | for (var i = 1; i <= 6; i++) {
78 | var $headings = this.findOrFilter($scope, 'h' + i);
79 | if ($headings.length > 1) {
80 | return i;
81 | }
82 | }
83 |
84 | return 1;
85 | },
86 |
87 | // returns the elements for the top level, and the next below it
88 | getHeadings: function($scope, topLevel) {
89 | var topSelector = 'h' + topLevel;
90 |
91 | var secondaryLevel = topLevel + 1;
92 | var secondarySelector = 'h' + secondaryLevel;
93 |
94 | return this.findOrFilter($scope, topSelector + ',' + secondarySelector);
95 | },
96 |
97 | getNavLevel: function(el) {
98 | return parseInt(el.tagName.charAt(1), 10);
99 | },
100 |
101 | populateNav: function($topContext, topLevel, $headings) {
102 | var $context = $topContext;
103 | var $prevNav;
104 |
105 | var helpers = this;
106 | $headings.each(function(i, el) {
107 | var $newNav = helpers.generateNavItem(el);
108 | var navLevel = helpers.getNavLevel(el);
109 |
110 | // determine the proper $context
111 | if (navLevel === topLevel) {
112 | // use top level
113 | $context = $topContext;
114 | } else if ($prevNav && $context === $topContext) {
115 | // create a new level of the tree and switch to it
116 | $context = helpers.createChildNavList($prevNav);
117 | } // else use the current $context
118 |
119 | $context.append($newNav);
120 |
121 | $prevNav = $newNav;
122 | });
123 | },
124 |
125 | parseOps: function(arg) {
126 | var opts;
127 | if (arg.jquery) {
128 | opts = {
129 | $nav: arg
130 | };
131 | } else {
132 | opts = arg;
133 | }
134 | opts.$scope = opts.$scope || $(document.body);
135 | return opts;
136 | }
137 | },
138 |
139 | // accepts a jQuery object, or an options object
140 | init: function(opts) {
141 | opts = this.helpers.parseOps(opts);
142 |
143 | // ensure that the data attribute is in place for styling
144 | opts.$nav.attr('data-toggle', 'toc');
145 |
146 | var $topContext = this.helpers.createChildNavList(opts.$nav);
147 | var topLevel = this.helpers.getTopLevel(opts.$scope);
148 | var $headings = this.helpers.getHeadings(opts.$scope, topLevel);
149 | this.helpers.populateNav($topContext, topLevel, $headings);
150 | }
151 | };
152 |
153 | $(function() {
154 | $('nav[data-toggle="toc"]').each(function(i, el) {
155 | var $nav = $(el);
156 | Toc.init($nav);
157 | });
158 | });
159 | })();
160 |
--------------------------------------------------------------------------------
/docs/deps/data-deps.txt:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/docs/diagram_stenrich.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/diagram_stenrich.png
--------------------------------------------------------------------------------
/docs/dir_structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/dir_structure.png
--------------------------------------------------------------------------------
/docs/docsearch.js:
--------------------------------------------------------------------------------
1 | $(function() {
2 |
3 | // register a handler to move the focus to the search bar
4 | // upon pressing shift + "/" (i.e. "?")
5 | $(document).on('keydown', function(e) {
6 | if (e.shiftKey && e.keyCode == 191) {
7 | e.preventDefault();
8 | $("#search-input").focus();
9 | }
10 | });
11 |
12 | $(document).ready(function() {
13 | // do keyword highlighting
14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */
15 | var mark = function() {
16 |
17 | var referrer = document.URL ;
18 | var paramKey = "q" ;
19 |
20 | if (referrer.indexOf("?") !== -1) {
21 | var qs = referrer.substr(referrer.indexOf('?') + 1);
22 | var qs_noanchor = qs.split('#')[0];
23 | var qsa = qs_noanchor.split('&');
24 | var keyword = "";
25 |
26 | for (var i = 0; i < qsa.length; i++) {
27 | var currentParam = qsa[i].split('=');
28 |
29 | if (currentParam.length !== 2) {
30 | continue;
31 | }
32 |
33 | if (currentParam[0] == paramKey) {
34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20"));
35 | }
36 | }
37 |
38 | if (keyword !== "") {
39 | $(".contents").unmark({
40 | done: function() {
41 | $(".contents").mark(keyword);
42 | }
43 | });
44 | }
45 | }
46 | };
47 |
48 | mark();
49 | });
50 | });
51 |
52 | /* Search term highlighting ------------------------------*/
53 |
54 | function matchedWords(hit) {
55 | var words = [];
56 |
57 | var hierarchy = hit._highlightResult.hierarchy;
58 | // loop to fetch from lvl0, lvl1, etc.
59 | for (var idx in hierarchy) {
60 | words = words.concat(hierarchy[idx].matchedWords);
61 | }
62 |
63 | var content = hit._highlightResult.content;
64 | if (content) {
65 | words = words.concat(content.matchedWords);
66 | }
67 |
68 | // return unique words
69 | var words_uniq = [...new Set(words)];
70 | return words_uniq;
71 | }
72 |
73 | function updateHitURL(hit) {
74 |
75 | var words = matchedWords(hit);
76 | var url = "";
77 |
78 | if (hit.anchor) {
79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor;
80 | } else {
81 | url = hit.url + '?q=' + escape(words.join(" "));
82 | }
83 |
84 | return url;
85 | }
86 |
--------------------------------------------------------------------------------
/docs/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/favicon-16x16.png
--------------------------------------------------------------------------------
/docs/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/favicon-32x32.png
--------------------------------------------------------------------------------
/docs/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/favicon.ico
--------------------------------------------------------------------------------
/docs/link.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 |
--------------------------------------------------------------------------------
/docs/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/logo.png
--------------------------------------------------------------------------------
/docs/metadata_file.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/metadata_file.png
--------------------------------------------------------------------------------
/docs/pkgdown.js:
--------------------------------------------------------------------------------
1 | /* http://gregfranko.com/blog/jquery-best-practices/ */
2 | (function($) {
3 | $(function() {
4 |
5 | $('.navbar-fixed-top').headroom();
6 |
7 | $('body').css('padding-top', $('.navbar').height() + 10);
8 | $(window).resize(function(){
9 | $('body').css('padding-top', $('.navbar').height() + 10);
10 | });
11 |
12 | $('[data-toggle="tooltip"]').tooltip();
13 |
14 | var cur_path = paths(location.pathname);
15 | var links = $("#navbar ul li a");
16 | var max_length = -1;
17 | var pos = -1;
18 | for (var i = 0; i < links.length; i++) {
19 | if (links[i].getAttribute("href") === "#")
20 | continue;
21 | // Ignore external links
22 | if (links[i].host !== location.host)
23 | continue;
24 |
25 | var nav_path = paths(links[i].pathname);
26 |
27 | var length = prefix_length(nav_path, cur_path);
28 | if (length > max_length) {
29 | max_length = length;
30 | pos = i;
31 | }
32 | }
33 |
34 | // Add class to parent
, and enclosing
if in dropdown
35 | if (pos >= 0) {
36 | var menu_anchor = $(links[pos]);
37 | menu_anchor.parent().addClass("active");
38 | menu_anchor.closest("li.dropdown").addClass("active");
39 | }
40 | });
41 |
42 | function paths(pathname) {
43 | var pieces = pathname.split("/");
44 | pieces.shift(); // always starts with /
45 |
46 | var end = pieces[pieces.length - 1];
47 | if (end === "index.html" || end === "")
48 | pieces.pop();
49 | return(pieces);
50 | }
51 |
52 | // Returns -1 if not found
53 | function prefix_length(needle, haystack) {
54 | if (needle.length > haystack.length)
55 | return(-1);
56 |
57 | // Special case for length-0 haystack, since for loop won't run
58 | if (haystack.length === 0) {
59 | return(needle.length === 0 ? 0 : -1);
60 | }
61 |
62 | for (var i = 0; i < haystack.length; i++) {
63 | if (needle[i] != haystack[i])
64 | return(i);
65 | }
66 |
67 | return(haystack.length);
68 | }
69 |
70 | /* Clipboard --------------------------*/
71 |
72 | function changeTooltipMessage(element, msg) {
73 | var tooltipOriginalTitle=element.getAttribute('data-original-title');
74 | element.setAttribute('data-original-title', msg);
75 | $(element).tooltip('show');
76 | element.setAttribute('data-original-title', tooltipOriginalTitle);
77 | }
78 |
79 | if(ClipboardJS.isSupported()) {
80 | $(document).ready(function() {
81 | var copyButton = "";
82 |
83 | $("div.sourceCode").addClass("hasCopyButton");
84 |
85 | // Insert copy buttons:
86 | $(copyButton).prependTo(".hasCopyButton");
87 |
88 | // Initialize tooltips:
89 | $('.btn-copy-ex').tooltip({container: 'body'});
90 |
91 | // Initialize clipboard:
92 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', {
93 | text: function(trigger) {
94 | return trigger.parentNode.textContent.replace(/\n#>[^\n]*/g, "");
95 | }
96 | });
97 |
98 | clipboardBtnCopies.on('success', function(e) {
99 | changeTooltipMessage(e.trigger, 'Copied!');
100 | e.clearSelection();
101 | });
102 |
103 | clipboardBtnCopies.on('error', function() {
104 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy');
105 | });
106 | });
107 | }
108 | })(window.jQuery || window.$)
109 |
--------------------------------------------------------------------------------
/docs/pkgdown.yml:
--------------------------------------------------------------------------------
1 | pandoc: 3.6.3
2 | pkgdown: 2.1.1
3 | pkgdown_sha: ~
4 | articles:
5 | basic_functions_vignette: basic_functions_vignette.html
6 | spatial_differential_expression: spatial_differential_expression.html
7 | spatial_enrichment_gradients_smi: spatial_enrichment_gradients_smi.html
8 | troubleshooting: troubleshooting.html
9 | last_built: 2025-04-08T02:33Z
10 |
--------------------------------------------------------------------------------
/docs/reference/Rplot001.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/Rplot001.png
--------------------------------------------------------------------------------
/docs/reference/Rplot002.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/Rplot002.png
--------------------------------------------------------------------------------
/docs/reference/STclust-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/STclust-1.png
--------------------------------------------------------------------------------
/docs/reference/STplot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/STplot-1.png
--------------------------------------------------------------------------------
/docs/reference/STplot_interpolation-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/STplot_interpolation-1.png
--------------------------------------------------------------------------------
/docs/reference/gene_interpolation-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/gene_interpolation-1.png
--------------------------------------------------------------------------------
/docs/reference/plot_counts-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/plot_counts-1.png
--------------------------------------------------------------------------------
/docs/reference/pseudobulk_dim_plot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/pseudobulk_dim_plot-1.png
--------------------------------------------------------------------------------
/docs/reference/pseudobulk_heatmap-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/pseudobulk_heatmap-1.png
--------------------------------------------------------------------------------
/docs/reference/pseudobulk_heatmap-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/pseudobulk_heatmap-2.png
--------------------------------------------------------------------------------
/docs/reference/pseudobulk_pca_plot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/pseudobulk_pca_plot-1.png
--------------------------------------------------------------------------------
/docs/reference/pseudobulk_samples-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/reference/pseudobulk_samples-1.png
--------------------------------------------------------------------------------
/docs/sitemap.xml:
--------------------------------------------------------------------------------
1 |
2 | /404.html
3 | /LICENSE-text.html
4 | /LICENSE.html
5 | /articles/basic_functions_vignette.html
6 | /articles/index.html
7 | /articles/spatial_differential_expression.html
8 | /articles/spatial_enrichment_gradients_smi.html
9 | /articles/troubleshooting.html
10 | /authors.html
11 | /index.html
12 | /news/index.html
13 | /reference/STclust.html
14 | /reference/STdiff.html
15 | /reference/STdiff_volcano.html
16 | /reference/STenrich.html
17 | /reference/STgradient.html
18 | /reference/SThet.html
19 | /reference/SThet_invdist_test.html
20 | /reference/STlist-class.html
21 | /reference/STlist.html
22 | /reference/STplot.html
23 | /reference/STplot_interpolation.html
24 | /reference/compare_SThet.html
25 | /reference/dim-STlist-method.html
26 | /reference/distribution_plots.html
27 | /reference/filter_data.html
28 | /reference/gene_interpolation.html
29 | /reference/get_gene_meta.html
30 | /reference/index.html
31 | /reference/load_images.html
32 | /reference/old_SThet.html
33 | /reference/plot_counts.html
34 | /reference/plot_image.html
35 | /reference/pseudobulk_dim_plot.html
36 | /reference/pseudobulk_heatmap.html
37 | /reference/pseudobulk_pca_plot.html
38 | /reference/pseudobulk_samples.html
39 | /reference/show-STlist-method.html
40 | /reference/spatial_metadata.html
41 | /reference/summarize_STlist.html
42 | /reference/summary-STlist-method.html
43 | /reference/tissue_names.html
44 | /reference/transform_data.html
45 |
46 |
47 |
--------------------------------------------------------------------------------
/docs/smi_dir_structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/smi_dir_structure.png
--------------------------------------------------------------------------------
/docs/spatialGE_workflow_v3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/spatialGE_workflow_v3.png
--------------------------------------------------------------------------------
/docs/spatial_gradients.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/docs/spatial_gradients.png
--------------------------------------------------------------------------------
/inst/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/inst/.DS_Store
--------------------------------------------------------------------------------
/inst/err.csv:
--------------------------------------------------------------------------------
1 | error0001,'STlist -> process_sample_names: A sample name matches more than one expression path.'
2 | error0002,'STlist -> process_lists: The ROI/spot/cell IDs in the count data (columns) and coordinate data (rows) do not match.'
3 | error0003,'STlist -> detect_input: The input lists are not named. Please input named lists.'
4 | error0004,'STlist: Unless input is Seurat or data frames, the samples argument requires a file path to sample metadata table or a vector with sample IDs.'
5 | error0005,'pseudobulk_samples: At least two variable genes are necessary to calculate principal components.'
6 | error0006,'STplot -> plot_spatial_meta: Could not find the requested spatial metadata in any sample.'
7 | error0007,'STclust: Could not find normalized counts. Please use transform data before using STclust.'
8 | error0008,'STdiff: If pairwise tests requested, at least two clusters are required.'
9 | error0009,'STdiff: topgenes or sp_topgenes contain invalid values.'
10 | error0010,'STdiff: None of the requested samples are present in the STlist.'
11 | error0011,'STdiff: The argument annot is empty. Please specify both k and w (STclust parameters).'
12 | error0012,'STdiff -> get_annot_from_kw: If k=\"dtc\", then specify deepSplit.'
13 | error0013,'STdiff -> get_annot_from_kw: Please enter a valid deepSplit value.'
14 | error0014,'STdiff -> get_annot_from_kw: The specified k value is not numeric.'
15 | error0015,'STdiff: No samples left to test. Are the requested annotations/clusters present in at least one sample?'
16 | error0016,'STclust: Refusing to generate < 2 clusters.'
17 | error0017,'pseudobulk_samples: Refusing to make PCA/UMAP containing less than four samples!'
18 | error0018,'pseudobulk_samples: The input must be an STList.'
19 | error0019,'pseudobulk_samples: Please, remove samples containing zero reads.'
20 | error0020,'STenrich: None of the requested samples are present in the STlist.'
21 | error0021,'STenrich: No samples available to test after verifying annotations/domains.'
22 | error0022,'STlist -> detect_input_rnacounts: At least one of the elements in the rnacounts list is not a dataframe.'
23 | error0023,'STlist -> detect_input_rnacounts: The list provided to rnacounts is not a named list.'
24 | error0024,'STlist -> detect_input_rnacounts: Input files were not recognized as tab- OR comma-delimited.'
25 | error0025,'STlist -> detect_input_rnacounts: If intended input is a Visium/Xenium (Space Ranger/ Xenium Ranger) output, the paths to directories could not be found.'
26 | error0026,'STlist -> detect_input_rnacounts: Unable to locate .h5 or MEX expression files.'
27 | error0027,'STlist -> detect_input_samples: Could not recognize the delimiter in the sample file.'
28 | error0028,'STlist -> detect_input_samples: Sample names must not begin with a number, and contain only alphanumerics, dash, underscore, or spaces.'
29 | error0029,'STlist -> detect_input_spotcoords: Input files were not recognized as tab- OR comma-delimited.'
30 | error0030,'STlist -> detect_input_spotcoords: At least one of the elements in the spotcoords list is not a dataframe.'
31 | error0031,'STlist -> detect_input_spotcoords: The list provided to spotcoords is not a named list.'
32 | error0032,'STlist: Could not recognize number of cores requested.'
33 | error0033,'STlist: An input to the rnacounts argument must be provided.'
34 | error0034,'STlist -> process_sample_names: Could not match samples names to file paths.'
35 | error0035,'STlist -> read_visium_outs: One of the Visium output folders does not have all necessary files.'
36 | error0036,'STlist -> read_visium_outs: Could not recognize number of cores requested.'
37 | error0037,'STlist -> read_xenium_outs: One of the Xenium output folders does not have all necessary files.'
38 | error0038,'STlist -> read_xenium_outs: Could not recognize number of cores requested.'
39 | error0039,'STlist -> read_xenium_outs: Could not recognize the file containing coordinates (csv or parquet).'
40 | error0040,'STlist -> read_xenium_outs: Could not find cell_id, y_centroid, and x_centroid in coordinates file.'
41 | error0041,'STgradient: None of the requested samples are present in the STlist.'
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/logo.png
--------------------------------------------------------------------------------
/man/STclust.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STclust.R
3 | \name{STclust}
4 | \alias{STclust}
5 | \title{STclust: Detect clusters of spots/cells}
6 | \usage{
7 | STclust(
8 | x = NULL,
9 | samples = NULL,
10 | ws = 0.025,
11 | dist_metric = "euclidean",
12 | linkage = "ward.D2",
13 | ks = "dtc",
14 | topgenes = 2000,
15 | deepSplit = F,
16 | cores = NULL
17 | )
18 | }
19 | \arguments{
20 | \item{x}{an STlist with normalized expression data}
21 |
22 | \item{samples}{a vector with strings or a vector with integers indicating the samples
23 | to run STclust}
24 |
25 | \item{ws}{a double (0-1) indicating the weight to be applied to spatial distances.
26 | Defaults to 0.025}
27 |
28 | \item{dist_metric}{the distance metric to be used. Defaults to 'euclidean'. Other
29 | options are the same as in \code{wordspace::dist.matrix}}
30 |
31 | \item{linkage}{the linkage method applied to hierarchical clustering. Passed to
32 | \code{hclust} and defaults to 'ward.D'}
33 |
34 | \item{ks}{the range of k values to assess. Defaults to \code{dtc}, meaning \code{cutreeDynamic}
35 | is applied}
36 |
37 | \item{topgenes}{the number of genes with highest spot-to-spot expression variation. The
38 | variance is calculated via \code{Seurat::FindVariableFeatures}.}
39 |
40 | \item{deepSplit}{a logical or integer (1-4), to be passed to \code{cutreeDynamic} and
41 | control cluster resolution}
42 |
43 | \item{cores}{an integer indicating the number of cores to use in parallelization (Unix only)}
44 | }
45 | \value{
46 | an STlist with cluster assignments
47 | }
48 | \description{
49 | Perform unsupervised spatially-informed clustering on the spots/cells of a
50 | ST sample
51 | }
52 | \details{
53 | The function takes an STlist and calculates euclidean distances between cells or spots
54 | based on the x,y spatial locations, and the expression of the top variable genes
55 | (\code{Seurat::FindVariableFeatures}). The resulting distances are weighted by
56 | applying 1-\code{ws} to the gene expression distances and \code{ws} to the spatial distances.
57 | Hierarchical clustering is performed on the sum of the weighted distance matrices.
58 | The \code{STclust} method allows for identification of tissue niches/domains that are
59 | spatially cohesive.
60 | }
61 | \examples{
62 |
63 | # Using included melanoma example (Thrane et al.)
64 | # Download example data set from spatialGE_Data
65 | thrane_tmp = tempdir()
66 | unlink(thrane_tmp, recursive=TRUE)
67 | dir.create(thrane_tmp)
68 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
69 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
70 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
71 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
72 | # Generate the file paths to be passed to the STlist function
73 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
74 | full.names=TRUE, pattern='counts')
75 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
76 | full.names=TRUE, pattern='mapping')
77 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
78 | full.names=TRUE, pattern='clinical')
79 | # Create STlist
80 | library('spatialGE')
81 | melanoma <- STlist(rnacounts=count_files[c(1,3)], spotcoords=coord_files[c(1,3)], samples=clin_file)
82 | melanoma <- transform_data(melanoma, method='log')
83 | melanoma <- STclust(melanoma, ws=c(0, 0.025))
84 | STplot(melanoma, ws=0.025, samples='ST_mel1_rep2', ptsize=1)
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/man/STdiff.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STdiff.R
3 | \name{STdiff}
4 | \alias{STdiff}
5 | \title{STdiff: Differential gene expression analysis for spatial transcriptomics data}
6 | \usage{
7 | STdiff(
8 | x = NULL,
9 | samples = NULL,
10 | annot = NULL,
11 | w = NULL,
12 | k = NULL,
13 | deepSplit = NULL,
14 | topgenes = 5000,
15 | pval_thr = 0.05,
16 | pval_adj = "fdr",
17 | test_type = "mm",
18 | sp_topgenes = 0.2,
19 | clusters = NULL,
20 | pairwise = F,
21 | verbose = 1L,
22 | cores = NULL
23 | )
24 | }
25 | \arguments{
26 | \item{x}{an STlist}
27 |
28 | \item{samples}{an integer indicating the spatial samples to be included in the DE tests.
29 | Numbers follow the order in \code{names(x@counts)}. Sample names are also allowed.
30 | If NULL, performs tests on all samples}
31 |
32 | \item{annot}{a column name in \code{x@spatial_meta} containing the groups/clusters to
33 | be tested. Required if \code{k} and \code{w} are empty.}
34 |
35 | \item{w}{the spatial weight used in STclust. Required if \code{annot} is empty.}
36 |
37 | \item{k}{the k value used in STclust, or \code{dtc} for dynamicTreeCut clusters. Required if \code{annot} is empty.}
38 |
39 | \item{deepSplit}{the deepSplit value if used in STclust. Required if \code{k='dtc'}.}
40 |
41 | \item{topgenes}{an integer indicating the top variable genes to select from each sample
42 | based on variance (default=5000). If NULL, all genes are selected.}
43 |
44 | \item{pval_thr}{cut-off of adjusted p-values to define differentially expressed genes from
45 | non-spatial linear models. A proportion of genes (\code{sp_topgenes}) under this cut-off
46 | will be applied the spatial models. Default=0.05}
47 |
48 | \item{pval_adj}{Method to adjust p-values. Defaults to \code{FDR}. Other options as
49 | available from \code{p.adjust}}
50 |
51 | \item{test_type}{one of \code{mm}, \code{t_test}, or \code{wilcoxon}. Specifies the type of
52 | test performed.}
53 |
54 | \item{sp_topgenes}{Proportion of differentially expressed genes from non-spatial
55 | linear models (and controlled by \code{pval_thr}) to use in differential gene expression
56 | analysis with spatial linear models. If 0 (zero), no spatial models are fit. Default=0.2}
57 |
58 | \item{clusters}{cluster name(s) to test DE genes, as opposed to all clusters.}
59 |
60 | \item{pairwise}{whether or not to carry tests on a pairwise manner. The default is
61 | \code{pairwise=F}, meaning that DE genes are tested by comparing each cluster to the
62 | rest of the pooled cell/spots.}
63 |
64 | \item{verbose}{output progress indicators. If \code{verbose=0}, no text is shown in console.
65 | Other values are 1 and 2 indicating increasing level of verbosity. Default is
66 | \code{verbose=1}}
67 |
68 | \item{cores}{Number of cores to use in parallelization. If \code{NULL}, the number of
69 | cores to use is detected automatically}
70 | }
71 | \value{
72 | a list with one data frame per sample with results of differential gene
73 | expression analysis
74 | }
75 | \description{
76 | Tests for differentially expressed genes using linear models with or
77 | without spatial covariance structures
78 | }
79 | \details{
80 | The method tests for differentially expressed genes between groups of spots/cells
81 | (e.g., clusters) in a spatial transcriptomics sample. Specifically, the function
82 | tests for genes with significantly higher or lower gene expression in one group of
83 | spots/cells with respect to the rest of spots/cells in the sample. The method first
84 | runs non-spatial linear models on the genes to detect differentially expressed genes.
85 | Then spatial linear models with exponential covariance structure are fit on a
86 | subset of genes detected as differentially expressed by the non-linear models (\code{sp_topgenes}).
87 | If running on clusters detected via STclust, the user can specify the assignments
88 | using the same parameters (\code{w}, \code{k}, \code{deepSplit}). Otherwise, the assignments are
89 | specified by indicating one of the column names in \code{x@spatial_meta}. The function
90 | uses \code{spaMM::fitme} and is computationally expensive even on HPC environments.
91 | To run the STdiff using the non-spatial approach (faster), set \code{sp_topgenes=0}.
92 | }
93 |
--------------------------------------------------------------------------------
/man/STdiff_volcano.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STdiff_volcano.R
3 | \name{STdiff_volcano}
4 | \alias{STdiff_volcano}
5 | \title{STdiff_volcano: Generates volcano plots from STdiff results}
6 | \usage{
7 | STdiff_volcano(
8 | x = NULL,
9 | samples = NULL,
10 | clusters = NULL,
11 | pval_thr = 0.05,
12 | color_pal = NULL
13 | )
14 | }
15 | \arguments{
16 | \item{x}{the output of \code{STdiff}}
17 |
18 | \item{samples}{samples to create plots}
19 |
20 | \item{clusters}{names of the clusters to generate comparisons}
21 |
22 | \item{pval_thr}{the p-value threshold to color genes with differential expression}
23 |
24 | \item{color_pal}{the palette to color genes by significance}
25 | }
26 | \value{
27 | a list of ggplot objects
28 | }
29 | \description{
30 | Generates volcano plots of differential expression results from STdiff
31 | }
32 | \details{
33 | The function generated volcano plots (p-value vs. log-fold change) for
34 | genes tested with \code{STdiff}. Colors can be customized to show significance from
35 | spatial and non-spatial models
36 | }
37 |
--------------------------------------------------------------------------------
/man/STenrich.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STenrich.R
3 | \name{STenrich}
4 | \alias{STenrich}
5 | \title{STenrich}
6 | \usage{
7 | STenrich(
8 | x = NULL,
9 | samples = NULL,
10 | gene_sets = NULL,
11 | score_type = "avg",
12 | reps = 1000,
13 | annot = NULL,
14 | domain = NULL,
15 | num_sds = 1,
16 | min_units = 20,
17 | min_genes = 5,
18 | pval_adj_method = "BH",
19 | seed = 12345,
20 | cores = NULL
21 | )
22 | }
23 | \arguments{
24 | \item{x}{an STlist with transformed gene expression}
25 |
26 | \item{samples}{a vector with sample names or indexes to run analysis}
27 |
28 | \item{gene_sets}{a named list of gene sets to test. The names of the list should
29 | identify the gene sets to be tested}
30 |
31 | \item{score_type}{Controls how gene set expression is calculated. The options are
32 | the average expression among genes in a set ('avg'), or a GSEA score ('gsva'). The
33 | default is 'avg'}
34 |
35 | \item{reps}{the number of random samples to be extracted. Default is 1000 replicates}
36 |
37 | \item{annot}{name of the annotation within \code{x@spatial_meta} containing the spot/cell
38 | categories. Needs to be used in conjunction with \code{domain}}
39 |
40 | \item{domain}{the domain to restrict the analysis. Must exist within the spot/cell
41 | categories included in the selected annotation (i.e., \code{annot})}
42 |
43 | \item{num_sds}{the number of standard deviations to set the minimum gene set
44 | expression threshold. Default is one (1) standard deviation}
45 |
46 | \item{min_units}{Minimum number of spots with high expression of a pathway for
47 | that gene set to be considered in the analysis. Defaults to 20 spots or cells}
48 |
49 | \item{min_genes}{the minimum number of genes of a gene set present in the data set
50 | for that gene set to be included. Default is 5 genes}
51 |
52 | \item{pval_adj_method}{the method for multiple comparison adjustment of p-values.
53 | Options are the same as that of \code{p.adjust}. Default is 'BH'}
54 |
55 | \item{seed}{the seed number for the selection of random samples. Default is 12345}
56 |
57 | \item{cores}{the number of cores used during parallelization. If NULL (default),
58 | the number of cores is defined automatically}
59 | }
60 | \value{
61 | a list of data frames with the results of the test
62 | }
63 | \description{
64 | Test for spatial enrichment of gene expression sets in ST data sets
65 | }
66 | \details{
67 | The function performs a randomization test to assess if the sum of
68 | distances between cells/spots with high expression of a gene set is lower than
69 | the sum of distances among randomly selected cells/spots. The cells/spots are
70 | considered as having high gene set expression if the average expression of genes in a
71 | set is higher than the average expression plus \code{num_sds} times the standard deviation.
72 | Control over the size of regions with high expression is provided by setting the
73 | minimum number of cells/spots (\code{min_units}). This method is a modification of
74 | the method devised by Hunter et al. 2021 (zebrafish melanoma study).
75 | }
76 |
--------------------------------------------------------------------------------
/man/STgradient.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STgradient.R
3 | \name{STgradient}
4 | \alias{STgradient}
5 | \title{STgradient: Tests of gene expression spatial gradients}
6 | \usage{
7 | STgradient(
8 | x = NULL,
9 | samples = NULL,
10 | topgenes = 2000,
11 | annot = NULL,
12 | ref = NULL,
13 | exclude = NULL,
14 | out_rm = F,
15 | limit = NULL,
16 | distsumm = "min",
17 | min_nb = 3,
18 | robust = T,
19 | nb_dist_thr = NULL,
20 | log_dist = F,
21 | cores = NULL
22 | )
23 | }
24 | \arguments{
25 | \item{x}{an STlist with transformed gene expression}
26 |
27 | \item{samples}{the samples on which the test should be executed}
28 |
29 | \item{topgenes}{the number of high-variance genes to be tested. These genes are
30 | selected in descending order of variance as caclulated using Seurat's vst method}
31 |
32 | \item{annot}{the name of a column in \verb{@spatial_meta} containing the tissue domain
33 | assignments for each spot or cell. These assignments can be generated using the
34 | \code{STclust} function}
35 |
36 | \item{ref}{one of the tissue domains in the column specified in \code{annot},
37 | corresponding to the "reference" cluster or domain. Spearman's correlations will
38 | be calculated using spots assigned to domains other than this reference domain
39 | (or domains specified in \code{exclude}).}
40 |
41 | \item{exclude}{optional, a cluster/domain to exclude from the analysis}
42 |
43 | \item{out_rm}{logical (optional), remove gene expression outliers defined by
44 | the interquartile method. This option is only valid when \code{robust=F}}
45 |
46 | \item{limit}{limite the analysis to spots/cells with distances to \code{ref} shorther
47 | than the value specified here. Useful when gradients might occur at smaller scales
48 | or when the domain in \code{ref} is scattered through the tissue. Caution must be used
49 | due to difficult interpretation of imposed limits. It is suggested to run analysis
50 | without restricted distances in addition for comparison.}
51 |
52 | \item{distsumm}{the distance summary metric to use in correlations. One of \code{min} or \code{avg}}
53 |
54 | \item{min_nb}{the minimum number of immediate neighbors a spot or cell has to
55 | have in order to be included in the analysis. This parameter seeks to reduce the
56 | effect of isolated \code{ref} spots on the correlation}
57 |
58 | \item{robust}{logical, whether to use robust regression (\code{MASS} and \code{sfsmisc} packages)}
59 |
60 | \item{nb_dist_thr}{a numeric vector of length two indicating the tolerance interval to assign
61 | spots/cells to neighborhoods. The wider the range of the interval, the more likely
62 | distinct neighbors to be considered. If NULL, \code{c(0.75, 1.25)} and \code{c(0.25, 3)} is assigned
63 | for Visium and CosMx respectively.}
64 |
65 | \item{log_dist}{logical, whether to apply the natural logarithm to the spot/cell
66 | distances. It applies to all distances a constant (1e-200) to avoid log(0)}
67 |
68 | \item{cores}{the number of cores used during parallelization. If NULL (default),
69 | the number of cores is defined automatically}
70 | }
71 | \value{
72 | a list of data frames with the results of the test
73 | }
74 | \description{
75 | Calculates Spearman's coefficients to detect genes showing expression spatial gradients
76 | }
77 | \details{
78 | The \code{STgradient} function fits linear models and calculates Spearman coefficients
79 | between the expression of a gene and the minimum or average distance of spots or
80 | cells to a reference tissue domain. In other wordsm the \code{STgradient} function
81 | can be used to investigate if a gene is expressed higher in spots/cells closer to
82 | a specific reference tissue domain, compared to spots/cells farther from the
83 | reference domain (or viceversa as indicated by the Spearman's cofficient).
84 | }
85 |
--------------------------------------------------------------------------------
/man/SThet.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/SThet.R
3 | \name{SThet}
4 | \alias{SThet}
5 | \title{SThet: Computes global spatial autocorrelation statistics on gene expression}
6 | \usage{
7 | SThet(
8 | x = NULL,
9 | genes = NULL,
10 | samples = NULL,
11 | method = "moran",
12 | k = NULL,
13 | overwrite = T,
14 | cores = NULL
15 | )
16 | }
17 | \arguments{
18 | \item{x}{an STlist}
19 |
20 | \item{genes}{a vector of gene names to compute statistics}
21 |
22 | \item{samples}{the samples to compute statistics}
23 |
24 | \item{method}{The spatial statistic(s) to estimate. It can be set to 'moran',
25 | 'geary' or both. Default is 'moran'}
26 |
27 | \item{k}{the number of neighbors to estimate weights. By default NULL, meaning that
28 | spatial weights will be estimated from Euclidean distances. If an positive integer is
29 | entered, then the faster k nearest-neighbors approach is used. Please keep in mind
30 | that estimates are not as accurate as when using the default distance-based method.}
31 |
32 | \item{overwrite}{logical indicating if previous statistics should be overwritten.
33 | Default to FALSE (do not overwrite)}
34 |
35 | \item{cores}{integer indicating the number of cores to use during parallelization.
36 | If NULL, the function uses half of the available cores at a maximum. The parallelization
37 | uses \code{parallel::mclapply} and works only in Unix systems.}
38 | }
39 | \value{
40 | an STlist containing spatial statistics
41 | }
42 | \description{
43 | Computes the global spatial autocorrelation statistics Moran's I and/or
44 | Geary's C for a set of genes
45 | }
46 | \details{
47 | The function computes global spatial autocorrelation statistics (Moran's I and/or
48 | Geary's C) for the requested genes and samples. Then computation uses the
49 | package \code{spdep}. The calculated statistics are stored in the STlist, which can
50 | be accessed with the \code{get_gene_meta} function. For visual comparative analysis,
51 | the function \code{compare_SThet} can be used afterwards.
52 | }
53 | \examples{
54 |
55 | # Using included melanoma example (Thrane et al.)
56 | # Download example data set from spatialGE_Data
57 | thrane_tmp = tempdir()
58 | unlink(thrane_tmp, recursive=TRUE)
59 | dir.create(thrane_tmp)
60 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
61 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
62 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
63 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
64 | # Generate the file paths to be passed to the STlist function
65 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
66 | full.names=TRUE, pattern='counts')
67 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
68 | full.names=TRUE, pattern='mapping')
69 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
70 | full.names=TRUE, pattern='clinical')
71 | # Create STlist
72 | library('spatialGE')
73 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
74 | spotcoords=coord_files[c(1,2)],
75 | samples=clin_file) # Only first two samples
76 | melanoma <- transform_data(melanoma, method='log')
77 | melanoma <- SThet(melanoma, genes=c('MLANA', 'TP53'), method='moran')
78 | get_gene_meta(melanoma, sthet_only=TRUE)
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/man/STlist-class.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/classDefinitions.R
3 | \docType{class}
4 | \name{STlist-class}
5 | \alias{STlist-class}
6 | \title{Definition of an STlist object class.}
7 | \description{
8 | Definition of an STlist object class.
9 | }
10 | \section{Slots}{
11 |
12 | \describe{
13 | \item{\code{counts}}{per spot RNA counts}
14 |
15 | \item{\code{spatial_meta}}{per spot x,y coordinates}
16 |
17 | \item{\code{gene_meta}}{per gene statistics (e.g., average expression, variance, Moran's I)}
18 |
19 | \item{\code{sample_meta}}{dataframe with metadata per sample}
20 |
21 | \item{\code{tr_counts}}{transfromed per spot counts}
22 |
23 | \item{\code{gene_krige}}{results from kriging on gene expression}
24 |
25 | \item{\code{misc}}{Parameters and images from ST data}
26 | }}
27 |
28 |
--------------------------------------------------------------------------------
/man/STplot.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STplot.R
3 | \name{STplot}
4 | \alias{STplot}
5 | \title{STplot: Plots of gene expression, cluster memberships, and metadata in spatial context}
6 | \usage{
7 | STplot(
8 | x,
9 | samples = NULL,
10 | genes = NULL,
11 | plot_meta = NULL,
12 | ks = "dtc",
13 | ws = NULL,
14 | deepSplit = NULL,
15 | color_pal = NULL,
16 | data_type = "tr",
17 | ptsize = NULL,
18 | txsize = NULL
19 | )
20 | }
21 | \arguments{
22 | \item{x}{an STlist}
23 |
24 | \item{samples}{a vector of numbers indicating the ST samples to plot, or their
25 | sample names. If vector of numbers, it follow the order of samples in \code{names(x@counts)}.
26 | If NULL, the function plots all samples}
27 |
28 | \item{genes}{a vector of gene names or a named list of gene sets. In the latter
29 | case, the averaged expression of genes within the sets is plotted}
30 |
31 | \item{plot_meta}{a column name in \code{x@spatial_meta} to plot}
32 |
33 | \item{ks}{the k values to plot or 'dtc' to plot results from \code{dynamicTreeCut}
34 | clustering solutions. Requires previous analysis with \code{STclust}}
35 |
36 | \item{ws}{the spatial weights to plot samples if \code{STclust} was used}
37 |
38 | \item{deepSplit}{a logical or positive number indicating the \code{deepSplit}, if
39 | samples were analyzed with \code{STclust}}
40 |
41 | \item{color_pal}{a string of a color palette from \code{khroma} or \code{RColorBrewer}, or a
42 | vector with enough color names or HEX values}
43 |
44 | \item{data_type}{one of 'tr' or 'raw', to plot transformed or raw counts
45 | respectively}
46 |
47 | \item{ptsize}{a number specifying the size of the points. Passed to the \code{size}}
48 |
49 | \item{txsize}{a number controlling the size of the text in the plot title and legend title. Passed to the \code{element_text}
50 | aesthetic.}
51 | }
52 | \value{
53 | a list of plots
54 | }
55 | \description{
56 | Generates a plot of the location of spots/cells within an spatial
57 | sample, and colors them according to gene expression levels or spot/cell-level
58 | metadata
59 | }
60 | \details{
61 | The function takes an STlist and plots the cells or spots in their spatial context.
62 | The users can color the spots/cells according to the expression of selected genes,
63 | cluster memberships, or any spot/cell level metadata included in \code{x@spatial_meta}.
64 | The function also can average expression of gene sets.
65 | }
66 | \examples{
67 |
68 | # Using included melanoma example (Thrane et al.)
69 | # Download example data set from spatialGE_Data
70 | thrane_tmp = tempdir()
71 | unlink(thrane_tmp, recursive=TRUE)
72 | dir.create(thrane_tmp)
73 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
74 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
75 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
76 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
77 | # Generate the file paths to be passed to the STlist function
78 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
79 | full.names=TRUE, pattern='counts')
80 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
81 | full.names=TRUE, pattern='mapping')
82 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
83 | full.names=TRUE, pattern='clinical')
84 | # Create STlist
85 | library('spatialGE')
86 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
87 | spotcoords=coord_files[c(1,2)],
88 | samples=clin_file) # Only first two samples
89 | melanoma <- transform_data(melanoma)
90 | STplot(melanoma, gene='MLANA', samples='ST_mel1_rep2', ptsize=1)
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/man/STplot_interpolation.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/STplot_interpolation.R
3 | \name{STplot_interpolation}
4 | \alias{STplot_interpolation}
5 | \title{STplot_interpolation: Visualize gene expression surfaces}
6 | \usage{
7 | STplot_interpolation(
8 | x = NULL,
9 | genes = NULL,
10 | top_n = 10,
11 | samples = NULL,
12 | color_pal = "BuRd"
13 | )
14 | }
15 | \arguments{
16 | \item{x}{an STlist containing results from \code{gene_krige} for the genes selected.}
17 |
18 | \item{genes}{a vector of gene names (one or several) to plot. If 'top', the 10
19 | genes with highest standard deviation from each spatial sample are plotted.}
20 |
21 | \item{top_n}{an integer indicating how many top genes to perform kriging. Default is 10.}
22 |
23 | \item{samples}{a vector indicating the spatial samples to plot. If vector of numbers,
24 | it follows the order of \code{names(x@counts)}. If NULL, the function plots all samples}
25 |
26 | \item{color_pal}{a color scheme from \code{khroma} or \code{RColorBrewer}.}
27 | }
28 | \value{
29 | a list of plots
30 | }
31 | \description{
32 | Produces a gene expression surface from kriging interpolation of ST data.
33 | }
34 | \details{
35 | This function produces a gene expression surface plot via kriging for one or several
36 | genes and spatial samples
37 | }
38 | \examples{
39 |
40 | # Using included melanoma example (Thrane et al.)
41 | # Download example data set from spatialGE_Data
42 | thrane_tmp = tempdir()
43 | unlink(thrane_tmp, recursive=TRUE)
44 | dir.create(thrane_tmp)
45 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
46 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
47 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
48 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
49 | # Generate the file paths to be passed to the STlist function
50 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
51 | full.names=TRUE, pattern='counts')
52 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
53 | full.names=TRUE, pattern='mapping')
54 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
55 | full.names=TRUE, pattern='clinical')
56 | # Create STlist
57 | library('spatialGE')
58 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
59 | spotcoords=coord_files[c(1,2)],
60 | samples=clin_file) # Only first two samples
61 | melanoma <- transform_data(melanoma)
62 | melanoma <- gene_interpolation(melanoma, genes=c('MLANA', 'COL1A1'), samples='ST_mel1_rep2')
63 | kp = STplot_interpolation(melanoma, genes=c('MLANA', 'COL1A1'), samples='ST_mel1_rep2')
64 | ggpubr::ggarrange(plotlist=kp)
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/man/compare_SThet.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/compare_SThet.R
3 | \name{compare_SThet}
4 | \alias{compare_SThet}
5 | \title{compare_SThet: Compares spatial autocorrelation statistics across samples}
6 | \usage{
7 | compare_SThet(
8 | x = NULL,
9 | samplemeta = NULL,
10 | genes = NULL,
11 | color_by = NULL,
12 | categorical = T,
13 | color_pal = "muted",
14 | ptsize = 1
15 | )
16 | }
17 | \arguments{
18 | \item{x}{an STlist.}
19 |
20 | \item{samplemeta}{a string indicating the name of the variable in the clinical
21 | data frame. If NULL, uses sample names}
22 |
23 | \item{genes}{the name(s) of the gene(s) to plot.}
24 |
25 | \item{color_by}{the variable in \code{x@spatial_meta} used to color points in the plot.
26 | If NULL, each sample is assigned a different color}
27 |
28 | \item{categorical}{logical indicating whether or not to treat \code{color_by} as a
29 | categorical variable. Default is TRUE}
30 |
31 | \item{color_pal}{a string of a color palette from khroma or RColorBrewer, or a
32 | vector with colors with enough elements to plot categories.}
33 |
34 | \item{ptsize}{a number specifying the size of the points. Passed to the \code{size}
35 | aesthetic.}
36 | }
37 | \value{
38 | a list of plots
39 | }
40 | \description{
41 | Plots the spatial autocorrelation statistics of genes across samples and
42 | colors samples acording to sample metadata.
43 | }
44 | \details{
45 | This function takes the names of genes and their Moran's I or Geary's C computed for
46 | multiple samples and to provide a muti-sample comparison. Samples in the plot can
47 | be colored according to sample metadata to explore potential associations between
48 | spatial distribution of gene expression and sample-level data.
49 | }
50 |
--------------------------------------------------------------------------------
/man/dim-STlist-method.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/classDefinitions.R
3 | \name{dim,STlist-method}
4 | \alias{dim,STlist-method}
5 | \title{dim: Prints the dimensions of count arrays within an STList object.}
6 | \usage{
7 | \S4method{dim}{STlist}(x)
8 | }
9 | \arguments{
10 | \item{x}{an STList object to show summary from.}
11 | }
12 | \description{
13 | Returns the number of genes and spots for each array within an STList object
14 | }
15 | \details{
16 | This function takes an STList and prints the number of genes (rows) and spots (columns) of
17 | each spatial array within that object.
18 | }
19 |
--------------------------------------------------------------------------------
/man/distribution_plots.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/distribution_plots.R
3 | \name{distribution_plots}
4 | \alias{distribution_plots}
5 | \title{per_unit_counts: Generates distribution plots of spot/cell meta data or gene expression}
6 | \usage{
7 | distribution_plots(
8 | x = NULL,
9 | plot_meta = NULL,
10 | genes = NULL,
11 | samples = NULL,
12 | data_type = "tr",
13 | color_pal = "okabeito",
14 | plot_type = "violin",
15 | ptsize = 0.5,
16 | ptalpha = 0.5
17 | )
18 | }
19 | \arguments{
20 | \item{x}{an STlist}
21 |
22 | \item{plot_meta}{vector of variables in \code{x@spatial_meta} to plot distributions.
23 | If 'total_counts', the function plots the counts per spot/cell. If 'total_genes',
24 | the function plots the number of genes per spot/cell are plotted}
25 |
26 | \item{genes}{vector of genes to plot expression distribution. If used in conjunction
27 | with \code{plot_meta}, the expression values are grouped using that variable}
28 |
29 | \item{samples}{samples to include in the plot. Default (NULL) includes all samples}
30 |
31 | \item{data_type}{one of 'tr' or 'raw', to plot transformed or raw counts}
32 |
33 | \item{color_pal}{a string of a color palette from \code{khroma} or \code{RColorBrewer}, or a
34 | vector with colors}
35 |
36 | \item{plot_type}{one of "violin", "box", or "density" (violin plots, box plots, or
37 | density plots respectively). If \code{plot_meta} and \code{gene} are used together, then
38 | density plots are disabled}
39 |
40 | \item{ptsize}{the size of points in the plots}
41 |
42 | \item{ptalpha}{the transparency of points (violin/box plot) or curves (density plots)}
43 | }
44 | \description{
45 | Generates violin plots, boxplots, or density plots of variables in the
46 | spatial meta data or of gene expression
47 | }
48 | \details{
49 | The function allows to visualize the distribution of spot/cell total
50 | counts, total genes, or expression of specific genes across all samples for
51 | comparative purposes. It also allows grouping of gene expression values by
52 | categorical variables (e.g., clusters).
53 | }
54 |
--------------------------------------------------------------------------------
/man/filter_data.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/filter_data.R
3 | \name{filter_data}
4 | \alias{filter_data}
5 | \title{filter_data: Filters cells/spots, genes, or samples}
6 | \usage{
7 | filter_data(
8 | x = NULL,
9 | spot_minreads = 0,
10 | spot_maxreads = NULL,
11 | spot_mingenes = 0,
12 | spot_maxgenes = NULL,
13 | spot_minpct = 0,
14 | spot_maxpct = NULL,
15 | gene_minreads = 0,
16 | gene_maxreads = NULL,
17 | gene_minspots = 0,
18 | gene_maxspots = NULL,
19 | gene_minpct = 0,
20 | gene_maxpct = NULL,
21 | samples = NULL,
22 | rm_tissue = NULL,
23 | rm_spots = NULL,
24 | rm_genes = NULL,
25 | rm_genes_expr = NULL,
26 | spot_pct_expr = "^MT-"
27 | )
28 | }
29 | \arguments{
30 | \item{x}{an STlist}
31 |
32 | \item{spot_minreads}{the minimum number of total reads for a spot to be retained}
33 |
34 | \item{spot_maxreads}{the maximum number of total reads for a spot to be retained}
35 |
36 | \item{spot_mingenes}{the minimum number of non-zero counts for a spot to be retained}
37 |
38 | \item{spot_maxgenes}{the maximum number of non-zero counts for a spot to be retained}
39 |
40 | \item{spot_minpct}{the minimum percentage of counts for features defined by \code{spot_pct_expr} for a spot to be retained.}
41 |
42 | \item{spot_maxpct}{the maximum percentage of counts for features defined by \code{spot_pct_expr} for a spot to be retained.}
43 |
44 | \item{gene_minreads}{the minimum number of total reads for a gene to be retained}
45 |
46 | \item{gene_maxreads}{the maximum number of total reads for a gene to be retained}
47 |
48 | \item{gene_minspots}{he minimum number of spots with non-zero counts for a gene to be retained}
49 |
50 | \item{gene_maxspots}{the maximum number of spots with non-zero counts for a gene to be retained}
51 |
52 | \item{gene_minpct}{the minimum percentage of spots with non-zero counts for a gene to be retained}
53 |
54 | \item{gene_maxpct}{the maximum percentage of spots with non-zero counts for a gene to be retained}
55 |
56 | \item{samples}{samples (as in \code{names(x@counts)}) to perform filtering.}
57 |
58 | \item{rm_tissue}{sample (as in \code{names(x@counts)}) to remove from STlist. Removes samples in \code{x@counts}, \code{x@tr_counts}, \code{x@spatial_meta}, \code{x@gene_meta}, and \code{x@sample_meta}}
59 |
60 | \item{rm_spots}{vector of spot/cell IDs to remove. Removes spots/cells in \code{x@counts}, \code{x@tr_counts}, and \code{x@spatial_meta}}
61 |
62 | \item{rm_genes}{vector of gene names to remove from STlist. Removes genes in \code{x@counts}, \code{x@tr_counts}, and \code{x@gene_meta}}
63 |
64 | \item{rm_genes_expr}{a regular expression that matches genes to remove. Removes genes in \code{x@counts}, \code{x@tr_counts}, and \code{x@gene_meta}}
65 |
66 | \item{spot_pct_expr}{a expression to use with \code{spot_minpct} and \code{spot_maxpct}. By default '^MT-'.}
67 | }
68 | \value{
69 | an STlist containing the filtered data
70 | }
71 | \description{
72 | Filtering of spots/cells, genes or samples, as well as count-based
73 | filtering
74 | }
75 | \details{
76 | This function provides options to filter elements in an STlist. It can remove
77 | cells/spots or genes based on raw counts (\code{x@counts}). Users can input an
78 | regular expression to query gene names and calculate percentages (for example \%
79 | mtDNA genes). The function also can filter entire samples. Note that the function
80 | removes cells/spots, genes, and/or samples in the raw counts, transformed counts,
81 | spatial variables, gene variables, and sample metadata. Also note that the function
82 | filters in the following order:
83 | \enumerate{
84 | \item Samples (\code{rm_tissue})
85 | \item Spots (\code{rm_spots})
86 | \item Genes (\code{rm_genes})
87 | \item Genes matching \code{rm_genes_expr}
88 | \item Min and max counts
89 | }
90 | }
91 | \examples{
92 |
93 | # Using included melanoma example (Thrane et al.)
94 | # Download example data set from spatialGE_Data
95 | thrane_tmp = tempdir()
96 | unlink(thrane_tmp, recursive=TRUE)
97 | dir.create(thrane_tmp)
98 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
99 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
100 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
101 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
102 | # Generate the file paths to be passed to the STlist function
103 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
104 | full.names=TRUE, pattern='counts')
105 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
106 | full.names=TRUE, pattern='mapping')
107 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
108 | full.names=TRUE, pattern='clinical')
109 | # Create STlist
110 | library('spatialGE')
111 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
112 | spotcoords=coord_files[c(1,2)],
113 | samples=clin_file) # Only first two samples
114 | melanoma <- filter_data(melanoma, spot_minreads=2000)
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/man/gene_interpolation.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/gene_interpolation.R
3 | \name{gene_interpolation}
4 | \alias{gene_interpolation}
5 | \title{gene_interpolation: Spatial interpolation of gene expression}
6 | \usage{
7 | gene_interpolation(
8 | x = NULL,
9 | genes = "top",
10 | top_n = 10,
11 | samples = NULL,
12 | cores = NULL
13 | )
14 | }
15 | \arguments{
16 | \item{x}{an STlist with transformed RNA counts}
17 |
18 | \item{genes}{a vector of gene names or 'top'. If 'top' (default), interpolation of
19 | the 10 genes (\code{top_n} default) with highest standard deviation in each ST sample
20 | is estimated.}
21 |
22 | \item{top_n}{an integer indicating how many top genes to perform interpolation.
23 | Default is 10.}
24 |
25 | \item{samples}{the spatial samples for which interpolations will be performed.
26 | If NULL (Default), all samples are interpolated.}
27 |
28 | \item{cores}{integer indicating the number of cores to use during parallelization.
29 | If NULL, the function uses half of the available cores at a maximum. The parallelization
30 | uses \code{parallel::mclapply} and works only in Unix systems.}
31 | }
32 | \value{
33 | x a STlist including spatial interpolations.
34 | }
35 | \description{
36 | Performs spatial interpolation ("kriging") of transformed gene counts
37 | }
38 | \details{
39 | This function takes an STlist and a vector of gene names and generates spatial
40 | interpolation of gene expression values via "kriging". If genes='top', then
41 | the 10 genes (default) with the highest standard deviation for each ST sample
42 | are interpolated. The resulting interpolations can be visualized via the
43 | \code{STplot_interpolation} function
44 | }
45 | \examples{
46 |
47 | # Using included melanoma example (Thrane et al.)
48 | # Download example data set from spatialGE_Data
49 | thrane_tmp = tempdir()
50 | unlink(thrane_tmp, recursive=TRUE)
51 | dir.create(thrane_tmp)
52 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
53 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
54 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
55 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
56 | # Generate the file paths to be passed to the STlist function
57 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
58 | full.names=TRUE, pattern='counts')
59 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
60 | full.names=TRUE, pattern='mapping')
61 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
62 | full.names=TRUE, pattern='clinical')
63 | # Create STlist
64 | library('spatialGE')
65 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
66 | spotcoords=coord_files[c(1,2)],
67 | samples=clin_file) # Only first two samples
68 | melanoma <- transform_data(melanoma)
69 | melanoma <- gene_interpolation(melanoma, genes=c('MLANA', 'COL1A1'), samples='ST_mel1_rep2')
70 | kp = STplot_interpolation(melanoma, genes=c('MLANA', 'COL1A1'))
71 | ggpubr::ggarrange(plotlist=kp)
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/man/get_gene_meta.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/utils.R
3 | \name{get_gene_meta}
4 | \alias{get_gene_meta}
5 | \title{get_gene_meta: Extract gene-level metadata and statistics}
6 | \usage{
7 | get_gene_meta(x = NULL, sthet_only = F)
8 | }
9 | \arguments{
10 | \item{x}{an STlist}
11 |
12 | \item{sthet_only}{logical, return only genes with spatial statistics}
13 | }
14 | \value{
15 | a data frame with gene-level data
16 | }
17 | \description{
18 | Extracts gene-level metadata and spatial statistics (if already computed)
19 | }
20 | \details{
21 | This function extracts data from the \code{x@gene_meta} slot, optionally subsetting
22 | only to those genes for which spatial statistics (Moran's I or Geary's C, see \code{SThet})
23 | have been calculated. The output is a data frame with data from all samples in the
24 | STlist
25 | }
26 | \examples{
27 |
28 | # Using included melanoma example (Thrane et al.)
29 | # Download example data set from spatialGE_Data
30 | thrane_tmp = tempdir()
31 | unlink(thrane_tmp, recursive=TRUE)
32 | dir.create(thrane_tmp)
33 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
34 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
35 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
36 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
37 | # Generate the file paths to be passed to the STlist function
38 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
39 | full.names=TRUE, pattern='counts')
40 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
41 | full.names=TRUE, pattern='mapping')
42 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
43 | full.names=TRUE, pattern='clinical')
44 | # Create STlist
45 | library('spatialGE')
46 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
47 | spotcoords=coord_files[c(1,2)],
48 | samples=clin_file) # Only first two samples
49 | melanoma <- transform_data(melanoma, method='log')
50 | melanoma <- SThet(melanoma, genes=c('MLANA', 'TP53'), method='moran')
51 | get_gene_meta(melanoma, sthet_only=TRUE)
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/man/load_images.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/utils.R
3 | \name{load_images}
4 | \alias{load_images}
5 | \title{load_images: Place tissue images within STlist}
6 | \usage{
7 | load_images(x = NULL, images = NULL)
8 | }
9 | \arguments{
10 | \item{x}{an STlist}
11 |
12 | \item{images}{a string indicating a folder to load images from}
13 | }
14 | \value{
15 | an STlist with images
16 | }
17 | \description{
18 | Loads the images from tissues to the appropriate STlist slot.
19 | }
20 | \details{
21 | This function looks for \code{.PNG} or \code{.JPG} files within a folder matching the
22 | sample names in an existing STlist. Then, loads the images to the STlist which
23 | can be used for plotting along with other spatialGE plots.
24 | }
25 |
--------------------------------------------------------------------------------
/man/plot_counts.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/plot_counts.R
3 | \name{plot_counts}
4 | \alias{plot_counts}
5 | \title{plot_counts: Generates plots for the distribution of counts}
6 | \usage{
7 | plot_counts(
8 | x = NULL,
9 | samples = NULL,
10 | data_type = "tr",
11 | plot_type = "density",
12 | color_pal = "okabeito",
13 | cvalpha = 0.5,
14 | distrib_subset = 0.5,
15 | subset_seed = 12345
16 | )
17 | }
18 | \arguments{
19 | \item{x}{an STlist}
20 |
21 | \item{samples}{samples to include in the plot. Default (NULL) includes all samples}
22 |
23 | \item{data_type}{one of \code{tr} or \code{raw}, to plot transformed or raw counts}
24 |
25 | \item{plot_type}{one or several of \code{density}, \code{violin}, and \code{box}, to generate
26 | density plots, violin plots, and/or boxplots}
27 |
28 | \item{color_pal}{a string of a color palette from \code{khroma} or \code{RColorBrewer}, or a
29 | vector with colors}
30 |
31 | \item{cvalpha}{the transparency of the density plots}
32 |
33 | \item{distrib_subset}{the proportion of spots/cells to plot. Generating these
34 | plots can be time consuming due to the large amount of elements to plot. This
35 | argument provides control on how many randomly values to show to speed plotting}
36 |
37 | \item{subset_seed}{related to \code{distrib_subset}. Sets the seed number to ensure
38 | the same subset of values is selected for plotting}
39 | }
40 | \value{
41 | a list of ggplot objects
42 | }
43 | \description{
44 | Generates density plots, violin plots, and/or boxplots for the
45 | distribution of count values
46 | }
47 | \details{
48 | The function allows to visualize the distribution counts across all genes and spots
49 | in the STlist. The user can select between density plots, violin plots, or box
50 | plots as visualization options. Useful for assessment of the effect of filtering and
51 | data transformations and to assess zero-inflation. To plot counts or genes per
52 | spot/cell, the function \code{distribution_plots} should be used instead.
53 | }
54 | \examples{
55 |
56 | # Using included melanoma example (Thrane et al.)
57 | # Download example data set from spatialGE_Data
58 | thrane_tmp = tempdir()
59 | unlink(thrane_tmp, recursive=TRUE)
60 | dir.create(thrane_tmp)
61 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
62 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
63 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
64 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
65 | # Generate the file paths to be passed to the STlist function
66 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
67 | full.names=TRUE, pattern='counts')
68 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
69 | full.names=TRUE, pattern='mapping')
70 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
71 | full.names=TRUE, pattern='clinical')
72 | # Create STlist
73 | library('spatialGE')
74 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
75 | spotcoords=coord_files[c(1,2)],
76 | samples=clin_file) # Only first two samples
77 | cp <- plot_counts(melanoma, data_type='raw', plot_type=c('violin', 'box'))
78 | ggpubr::ggarrange(plotlist=cp)
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/man/plot_image.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/plot_image.R
3 | \name{plot_image}
4 | \alias{plot_image}
5 | \title{plot_image: Generate a ggplot object of the tissue image}
6 | \usage{
7 | plot_image(x = NULL, samples = NULL)
8 | }
9 | \arguments{
10 | \item{x}{an STlist}
11 |
12 | \item{samples}{a vector of numbers indicating the ST samples to plot, or their
13 | sample names. If vector of numbers, it follow the order of \code{names(x@counts)}.
14 | If NULL, the function plots all samples}
15 | }
16 | \value{
17 | a list of plots
18 | }
19 | \description{
20 | Creates ggplot objects of the tissue images when available within the STlist
21 | }
22 | \details{
23 | If the STlist contains tissue images in the \verb{@misc} slot, the \code{plot_image} function
24 | can be used to generate ggplot objects. These ggplot objects can be plotted next to
25 | quilt plots (\code{STplot} function) for comparative analysis.
26 | }
27 |
--------------------------------------------------------------------------------
/man/pseudobulk_dim_plot.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pseudobulk_samples.R
3 | \name{pseudobulk_dim_plot}
4 | \alias{pseudobulk_dim_plot}
5 | \title{pseudobulk_dim_plot: Plot PCA of pseudobulk samples}
6 | \usage{
7 | pseudobulk_dim_plot(
8 | x = NULL,
9 | color_pal = "muted",
10 | plot_meta = NULL,
11 | dim = "pca",
12 | pcx = 1,
13 | pcy = 2,
14 | ptsize = 5
15 | )
16 | }
17 | \arguments{
18 | \item{x}{an STlist with pseudobulk PCA results in the \verb{@misc} slot (generated by
19 | \code{pseudobulk_samples})}
20 |
21 | \item{color_pal}{a string of a color palette from khroma or RColorBrewer, or a
22 | vector of color names or HEX values. Each color represents a category in the
23 | variable specified in \code{plot_meta}}
24 |
25 | \item{plot_meta}{a string indicating the name of the variable in the sample
26 | metadata to color points in the PCA plot}
27 |
28 | \item{dim}{one of \code{umap} or \code{pca}. The dimension reduction to plot}
29 |
30 | \item{pcx}{integer indicating the principal component to plot in the x axis}
31 |
32 | \item{pcy}{integer indicating the principal component to plot in the y axis}
33 |
34 | \item{ptsize}{the size of the points in the PCA plot. Passed to the \code{size}
35 | aesthetic from \code{ggplot2}}
36 | }
37 | \value{
38 | a ggplot object
39 | }
40 | \description{
41 | Generates a PCA plot after computation of "pseudobulk" counts
42 | }
43 | \details{
44 | Generates a Principal Components Analysis plot to help in initial data exploration of
45 | differences among samples. The points in the plot represent "pseudobulk" samples.
46 | This function follows after usage of \code{pseudobulk_samples}.
47 | }
48 | \examples{
49 |
50 | # Using included melanoma example (Thrane et al.)
51 | # Download example data set from spatialGE_Data
52 | thrane_tmp = tempdir()
53 | unlink(thrane_tmp, recursive=TRUE)
54 | dir.create(thrane_tmp)
55 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
56 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
57 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
58 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
59 | # Generate the file paths to be passed to the STlist function
60 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
61 | full.names=TRUE, pattern='counts')
62 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
63 | full.names=TRUE, pattern='mapping')
64 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
65 | full.names=TRUE, pattern='clinical')
66 | # Create STlist
67 | library('spatialGE')
68 | melanoma <- STlist(rnacounts=count_files,
69 | spotcoords=coord_files,
70 | samples=clin_file, cores=2)
71 | melanoma <- pseudobulk_samples(melanoma)
72 | pseudobulk_dim_plot(melanoma, plot_meta='patient')
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/man/pseudobulk_heatmap.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pseudobulk_samples.R
3 | \name{pseudobulk_heatmap}
4 | \alias{pseudobulk_heatmap}
5 | \title{pseudobulk_heatmap: Heatmap of pseudobulk samples}
6 | \usage{
7 | pseudobulk_heatmap(
8 | x = NULL,
9 | color_pal = "muted",
10 | plot_meta = NULL,
11 | hm_display_genes = 30
12 | )
13 | }
14 | \arguments{
15 | \item{x}{an STlist with pseudobulk counts in the \verb{@misc} slot (generated by
16 | \code{pseudobulk_samples})}
17 |
18 | \item{color_pal}{a string of a color palette from khroma or RColorBrewer, or a
19 | vector of color names or HEX values. Each color represents a category in the
20 | variable specified in \code{plot_meta}}
21 |
22 | \item{plot_meta}{a string indicating the name of the variable in the sample
23 | metadata to annotate heatmap columns}
24 |
25 | \item{hm_display_genes}{number of genes to display in heatmap, selected based on
26 | decreasing order of standard deviation across samples}
27 | }
28 | \value{
29 | a ggplot object
30 | }
31 | \description{
32 | Generates a heatmap plot after computation of "pseudobulk" counts
33 | }
34 | \details{
35 | Generates a heatmap of transformed "pseudobulk" counts to help in initial data
36 | exploration of differences among samples. Each column in the heatmap represents a
37 | "pseudobulk" sample. Rows are genes, with the number of genes displayed controlled by
38 | the \code{hm_display_genes} argument. This function follows after usage of \code{pseudobulk_samples}.
39 | }
40 | \examples{
41 |
42 | # Using included melanoma example (Thrane et al.)
43 | # Download example data set from spatialGE_Data
44 | thrane_tmp = tempdir()
45 | unlink(thrane_tmp, recursive=TRUE)
46 | dir.create(thrane_tmp)
47 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
48 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
49 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
50 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
51 | # Generate the file paths to be passed to the STlist function
52 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
53 | full.names=TRUE, pattern='counts')
54 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
55 | full.names=TRUE, pattern='mapping')
56 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
57 | full.names=TRUE, pattern='clinical')
58 | # Create STlist
59 | library('spatialGE')
60 | melanoma <- STlist(rnacounts=count_files,
61 | spotcoords=coord_files,
62 | samples=clin_file, cores=2)
63 | melanoma <- pseudobulk_samples(melanoma)
64 | hm <- pseudobulk_heatmap(melanoma, plot_meta='BRAF_status', hm_display_genes=30)
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/man/pseudobulk_samples.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/pseudobulk_samples.R
3 | \name{pseudobulk_samples}
4 | \alias{pseudobulk_samples}
5 | \title{pseudobulk_samples: Aggregates counts into "pseudo bulk" samples}
6 | \usage{
7 | pseudobulk_samples(x = NULL, max_var_genes = 5000, calc_umap = F)
8 | }
9 | \arguments{
10 | \item{x}{an STlist.}
11 |
12 | \item{max_var_genes}{number of most variable genes (standard deviation) to use in
13 | pseudobulk analysis}
14 |
15 | \item{calc_umap}{logical, whether to calculate UMAP embeddings in addition to PCs}
16 | }
17 | \value{
18 | an STlist with appended pseudobulk counts and PCA coordinates
19 | }
20 | \description{
21 | Aggregates spot/cell counts into "pseudo bulk" samples for data exploration
22 | }
23 | \details{
24 | This function takes an STlist and aggregates the spot/cell counts into "pseudo bulk"
25 | counts by summing all counts from all cell/spots for each gene. Then performs
26 | Principal Component Analysis (PCA) to explore non-spatial sample-to-sample
27 | variation
28 | }
29 | \examples{
30 |
31 | # Using included melanoma example (Thrane et al.)
32 | # Download example data set from spatialGE_Data
33 | thrane_tmp = tempdir()
34 | unlink(thrane_tmp, recursive=TRUE)
35 | dir.create(thrane_tmp)
36 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
37 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
38 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
39 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
40 | # Generate the file paths to be passed to the STlist function
41 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
42 | full.names=TRUE, pattern='counts')
43 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
44 | full.names=TRUE, pattern='mapping')
45 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
46 | full.names=TRUE, pattern='clinical')
47 | # Create STlist
48 | library('spatialGE')
49 | melanoma <- STlist(rnacounts=count_files,
50 | spotcoords=coord_files,
51 | samples=clin_file, cores=2)
52 | melanoma <- pseudobulk_samples(melanoma)
53 | pseudobulk_dim_plot(melanoma)
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/man/show-STlist-method.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/classDefinitions.R
3 | \name{show,STlist-method}
4 | \alias{show,STlist-method}
5 | \title{show: Prints overview of STList oject.}
6 | \usage{
7 | \S4method{show}{STlist}(object)
8 | }
9 | \arguments{
10 | \item{object}{an STList object to show summary from.}
11 | }
12 | \description{
13 | Prints overview/summary of STList oject.
14 | }
15 | \details{
16 | This function takes an STList and prints a the number of spatial arrays in that
17 | object and other information about the object.
18 | }
19 |
--------------------------------------------------------------------------------
/man/spatial_metadata.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/utils.R
3 | \name{spatial_metadata}
4 | \alias{spatial_metadata}
5 | \title{spatial_metadata: Prints the names of the available spot/cell annotations}
6 | \usage{
7 | spatial_metadata(x)
8 | }
9 | \arguments{
10 | \item{x}{an STList object}
11 | }
12 | \description{
13 | returns a character vector with the names of the annotations in the
14 | \code{x@spatial_meta} slot.
15 | }
16 |
--------------------------------------------------------------------------------
/man/summarize_STlist.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/summarize_STlist.R
3 | \name{summarize_STlist}
4 | \alias{summarize_STlist}
5 | \title{summarize_STlist: Generates a data frame with summary statistics}
6 | \usage{
7 | summarize_STlist(x = NULL)
8 | }
9 | \arguments{
10 | \item{x}{an STlist}
11 | }
12 | \value{
13 | a data frame
14 | }
15 | \description{
16 | Produces a data frame with counts per gene and counts per ROI/spot/cell
17 | }
18 | \details{
19 | The function creates a table with counts per gene and counts per region of interest (ROI),
20 | spot, or cell in the samples stored in the STlist
21 | }
22 | \examples{
23 |
24 | # Using included melanoma example (Thrane et al.)
25 | # Download example data set from spatialGE_Data
26 | thrane_tmp = tempdir()
27 | unlink(thrane_tmp, recursive=TRUE)
28 | dir.create(thrane_tmp)
29 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
30 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
31 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
32 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
33 | # Generate the file paths to be passed to the STlist function
34 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
35 | full.names=TRUE, pattern='counts')
36 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
37 | full.names=TRUE, pattern='mapping')
38 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
39 | full.names=TRUE, pattern='clinical')
40 | # Create STlist
41 | library('spatialGE')
42 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
43 | spotcoords=coord_files[c(1,2)],
44 | samples=clin_file) # Only first two samples
45 | summarize_STlist(melanoma)
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/man/summary-STlist-method.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/classDefinitions.R
3 | \name{summary,STlist-method}
4 | \alias{summary,STlist-method}
5 | \title{summary: Prints overview of STList oject.}
6 | \usage{
7 | \S4method{summary}{STlist}(object)
8 | }
9 | \arguments{
10 | \item{object}{an STList object to show summary from.}
11 | }
12 | \description{
13 | Prints overview/summary of STList oject.
14 | }
15 | \details{
16 | This function takes an STList and prints a the number of spatial arrays in that
17 | object and other information about the object.
18 | }
19 |
--------------------------------------------------------------------------------
/man/tissue_names.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/utils.R
3 | \name{tissue_names}
4 | \alias{tissue_names}
5 | \title{tissue_names: Prints the names of the tissue samples in the STlist}
6 | \usage{
7 | tissue_names(x)
8 | }
9 | \arguments{
10 | \item{x}{an STList object}
11 | }
12 | \description{
13 | returns a character vector with the names of tissue samples in the
14 | STlist.
15 | }
16 |
--------------------------------------------------------------------------------
/man/transform_data.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/transform_data.R
3 | \name{transform_data}
4 | \alias{transform_data}
5 | \title{transform_data: Transformation of spatial transcriptomics data}
6 | \usage{
7 | transform_data(
8 | x = NULL,
9 | method = "log",
10 | scale_f = 10000,
11 | sct_n_regr_genes = 3000,
12 | sct_min_cells = 5,
13 | cores = NULL
14 | )
15 | }
16 | \arguments{
17 | \item{x}{an STlist with raw count matrices.}
18 |
19 | \item{method}{one of \code{log} or \code{sct}. If \code{log}, log-normalization is performed.
20 | If \code{sct}, then the SCTransform method is applied by calling \code{sctransform::vst}}
21 |
22 | \item{scale_f}{the scale factor used in logarithmic transformation}
23 |
24 | \item{sct_n_regr_genes}{the number of genes to be used in the regression model
25 | during SCTransform. The function \code{sctransform::vst} makes a random gene selection
26 | based on this number}
27 |
28 | \item{sct_min_cells}{The minimum number of spots/cells to be used in the regression
29 | model fit by \code{sctransform::vst}}
30 |
31 | \item{cores}{integer indicating the number of cores to use during parallelization.
32 | If NULL, the function uses half of the available cores at a maximum. The parallelization
33 | uses \code{parallel::mclapply} and works only in Unix systems.}
34 | }
35 | \value{
36 | x an updated STlist with transformed counts.
37 | }
38 | \description{
39 | Applies data transformation methods to spatial transcriptomics
40 | samples within an STlist
41 | }
42 | \details{
43 | This function takes an STlist with raw counts and performs data transformation.
44 | The user has the option to select between log transformation after library size
45 | normalization (\code{method='log'}), or SCTransform (\code{method='sct'}). In the case of
46 | logarithmic transformation, a scaling factor (10^4 by default) is applied. The
47 | function uses parallelization using "forking" (not available in Windows OS).
48 | Note that the method \code{sct} returns a matrix with less genes as filtering is
49 | done for low expression genes.
50 | }
51 | \examples{
52 |
53 | # Using included melanoma example (Thrane et al.)
54 | # Download example data set from spatialGE_Data
55 | thrane_tmp = tempdir()
56 | unlink(thrane_tmp, recursive=TRUE)
57 | dir.create(thrane_tmp)
58 | lk='https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download='
59 | download.file(lk, destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
60 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
61 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
62 | # Generate the file paths to be passed to the STlist function
63 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
64 | full.names=TRUE, pattern='counts')
65 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
66 | full.names=TRUE, pattern='mapping')
67 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'),
68 | full.names=TRUE, pattern='clinical')
69 | # Create STlist
70 | library('spatialGE')
71 | melanoma <- STlist(rnacounts=count_files[c(1,2)],
72 | spotcoords=coord_files[c(1,2)],
73 | samples=clin_file) # Only first two samples
74 | melanoma <- transform_data(melanoma, method='log')
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-120x120.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon-120x120.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-152x152.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon-152x152.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-180x180.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon-180x180.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-60x60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon-60x60.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-76x76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon-76x76.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/apple-touch-icon.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/favicon-16x16.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/favicon-32x32.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/pkgdown/favicon/favicon.ico
--------------------------------------------------------------------------------
/spatialGE.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 | ProjectId: 5526cd1e-c4b4-4870-a0f9-6612763ad421
3 |
4 | RestoreWorkspace: No
5 | SaveWorkspace: No
6 | AlwaysSaveHistory: Default
7 |
8 | EnableCodeIndexing: Yes
9 | UseSpacesForTab: Yes
10 | NumSpacesForTab: 2
11 | Encoding: UTF-8
12 |
13 | RnwWeave: Sweave
14 | LaTeX: pdfLaTeX
15 |
16 | AutoAppendNewline: Yes
17 | StripTrailingWhitespace: Yes
18 | LineEndingConversion: Posix
19 |
20 | BuildType: Package
21 | PackageUseDevtools: Yes
22 | PackageInstallArgs: --no-multiarch --with-keep.source
23 | PackageRoxygenize: rd,collate,namespace
24 |
--------------------------------------------------------------------------------
/spatialGE_workflow_v3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/spatialGE_workflow_v3.png
--------------------------------------------------------------------------------
/src/RcppExports.cpp:
--------------------------------------------------------------------------------
1 | // Generated by using Rcpp::compileAttributes() -> do not edit by hand
2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3 |
4 | #include
5 | #include
6 |
7 | using namespace Rcpp;
8 |
9 | #ifdef RCPP_USE_GLOBAL_ROSTREAM
10 | Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
11 | Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
12 | #endif
13 |
14 | // SparseRowVar2
15 | NumericVector SparseRowVar2(Eigen::SparseMatrix mat, NumericVector mu);
16 | RcppExport SEXP _spatialGE_SparseRowVar2(SEXP matSEXP, SEXP muSEXP) {
17 | BEGIN_RCPP
18 | Rcpp::RObject rcpp_result_gen;
19 | Rcpp::traits::input_parameter< Eigen::SparseMatrix >::type mat(matSEXP);
20 | Rcpp::traits::input_parameter< NumericVector >::type mu(muSEXP);
21 | rcpp_result_gen = Rcpp::wrap(SparseRowVar2(mat, mu));
22 | return rcpp_result_gen;
23 | END_RCPP
24 | }
25 | // SparseRowVarStd
26 | NumericVector SparseRowVarStd(Eigen::SparseMatrix mat, NumericVector mu, NumericVector sd, double vmax);
27 | RcppExport SEXP _spatialGE_SparseRowVarStd(SEXP matSEXP, SEXP muSEXP, SEXP sdSEXP, SEXP vmaxSEXP) {
28 | BEGIN_RCPP
29 | Rcpp::RObject rcpp_result_gen;
30 | Rcpp::traits::input_parameter< Eigen::SparseMatrix >::type mat(matSEXP);
31 | Rcpp::traits::input_parameter< NumericVector >::type mu(muSEXP);
32 | Rcpp::traits::input_parameter< NumericVector >::type sd(sdSEXP);
33 | Rcpp::traits::input_parameter< double >::type vmax(vmaxSEXP);
34 | rcpp_result_gen = Rcpp::wrap(SparseRowVarStd(mat, mu, sd, vmax));
35 | return rcpp_result_gen;
36 | END_RCPP
37 | }
38 | // computeSubsampleSums
39 | NumericVector computeSubsampleSums(NumericMatrix coords, int n_subsample, int n_samples);
40 | RcppExport SEXP _spatialGE_computeSubsampleSums(SEXP coordsSEXP, SEXP n_subsampleSEXP, SEXP n_samplesSEXP) {
41 | BEGIN_RCPP
42 | Rcpp::RObject rcpp_result_gen;
43 | Rcpp::RNGScope rcpp_rngScope_gen;
44 | Rcpp::traits::input_parameter< NumericMatrix >::type coords(coordsSEXP);
45 | Rcpp::traits::input_parameter< int >::type n_subsample(n_subsampleSEXP);
46 | Rcpp::traits::input_parameter< int >::type n_samples(n_samplesSEXP);
47 | rcpp_result_gen = Rcpp::wrap(computeSubsampleSums(coords, n_subsample, n_samples));
48 | return rcpp_result_gen;
49 | END_RCPP
50 | }
51 |
52 | static const R_CallMethodDef CallEntries[] = {
53 | {"_spatialGE_SparseRowVar2", (DL_FUNC) &_spatialGE_SparseRowVar2, 2},
54 | {"_spatialGE_SparseRowVarStd", (DL_FUNC) &_spatialGE_SparseRowVarStd, 4},
55 | {"_spatialGE_computeSubsampleSums", (DL_FUNC) &_spatialGE_computeSubsampleSums, 3},
56 | {NULL, NULL, 0}
57 | };
58 |
59 | RcppExport void R_init_spatialGE(DllInfo *dll) {
60 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
61 | R_useDynamicSymbols(dll, FALSE);
62 | }
63 |
--------------------------------------------------------------------------------
/src/seurat_helpers.cpp:
--------------------------------------------------------------------------------
1 | // CODE TAKEN MODIFIED FROM THE SEURAT PACKAGE
2 | //
3 | // Copyright (c) 2021 Seurat authors
4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this
5 | // software and associated documentation files (the "Software"), to deal in the Software
6 | // without restriction, including without limitation the rights to use, copy, modify, merge,
7 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
8 | // to whom the Software is furnished to do so, subject to the following conditions:
9 | // The above copyright notice and this permission notice shall be included in all copies or
10 | // substantial portions of the Software.
11 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
12 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
13 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
14 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
16 | // DEALINGS IN THE SOFTWARE.
17 |
18 |
19 | #include
20 |
21 | using namespace Rcpp;
22 | // [[Rcpp::depends(RcppEigen)]]
23 | // [[Rcpp::depends(RcppProgress)]]
24 |
25 | /* use this if you know the row means */
26 | // [[Rcpp::export(rng = false)]]
27 | NumericVector SparseRowVar2(Eigen::SparseMatrix mat, NumericVector mu){
28 | mat = mat.transpose();
29 | NumericVector allVars = no_init(mat.cols());
30 | for (int k=0; k::InnerIterator it(mat,k); it; ++it) {
34 | nZero -= 1;
35 | colSum += pow(it.value() - mu[k], 2);
36 | }
37 | colSum += pow(mu[k], 2) * nZero;
38 | allVars[k] = colSum / (mat.rows() - 1);
39 | }
40 | return(allVars);
41 | }
42 |
43 | /* standardize matrix rows using given mean and standard deviation,
44 | clip values larger than vmax to vmax,
45 | then return variance for each row */
46 | // [[Rcpp::export(rng = false)]]
47 | NumericVector SparseRowVarStd(Eigen::SparseMatrix mat, NumericVector mu, NumericVector sd, double vmax){
48 | mat = mat.transpose();
49 | NumericVector allVars(mat.cols());
50 | for (int k=0; k::InnerIterator it(mat,k); it; ++it)
55 | {
56 | nZero -= 1;
57 | colSum += pow(std::min(vmax, (it.value() - mu[k]) / sd[k]), 2);
58 | }
59 | colSum += pow((0 - mu[k]) / sd[k], 2) * nZero;
60 | allVars[k] = colSum / (mat.rows() - 1);
61 | }
62 | return(allVars);
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/src/spatialge_helpers.cpp:
--------------------------------------------------------------------------------
1 | //
2 |
3 |
4 | #include
5 | // #include
6 | using namespace Rcpp;
7 |
8 | // computeSubsampleSums
9 | // Calculates sum of distances among spots or cells randomly selected
10 | // Arguments:
11 | // coords is a matrix with x locations in the first column and y locations in the second
12 | // n_subsample is the number of cells that are positive or above a thresold
13 | // n_samples is the number of permutations ot perform to estimate the distribution
14 | // Code by Dr. Alex "The Lab Warrior" Soupir, slightly modified
15 | // [[Rcpp::export]]
16 | NumericVector computeSubsampleSums(
17 | NumericMatrix coords,
18 | int n_subsample,
19 | int n_samples) {
20 |
21 | NumericVector x_coords = coords.column(0);
22 | NumericVector y_coords = coords.column(1);
23 | int N = coords.nrow();
24 |
25 | // vector for ith cell
26 | IntegerVector all_indices(N);
27 | std::iota(all_indices.begin(), all_indices.end(), 0);
28 |
29 | NumericVector sums(n_samples);
30 |
31 | for (int s = 0; s < n_samples; ++s) {
32 | // dont select same cell twice for permutations
33 | IntegerVector sample_indices = Rcpp::sample(all_indices, n_subsample, false);
34 |
35 | double total_dist = 0;
36 | int ns = sample_indices.size();
37 |
38 | // sum i,j only, not j,i/ upper triangle
39 | for (int i = 0; i < ns; ++i) {
40 | int idx_i = sample_indices[i];
41 | double xi = x_coords[idx_i], yi = y_coords[idx_i];
42 |
43 | for (int j = i + 1; j < ns; ++j) {
44 | int idx_j = sample_indices[j];
45 | double dx = xi - x_coords[idx_j];
46 | double dy = yi - y_coords[idx_j];
47 |
48 | total_dist += std::sqrt((dx * dx) + (dy * dy));
49 | //std::cout << total_dist << std::endl;
50 | }
51 | }
52 |
53 | sums[s] = total_dist;
54 | }
55 | return sums;
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | # This file is part of the standard setup for testthat.
2 | # It is recommended that you do not modify it.
3 | #
4 | # Where should you do additional test configuration?
5 | # Learn more about the roles of various files in:
6 | # * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7 | # * https://testthat.r-lib.org/articles/special-files.html
8 |
9 | library(testthat)
10 | library(spatialGE)
11 |
12 | test_check("spatialGE")
13 |
--------------------------------------------------------------------------------
/tests/testthat/test-tests.R:
--------------------------------------------------------------------------------
1 | ##
2 | # Unit tests via testthat
3 | #
4 |
5 | # Create STlist from Thrane et al. data
6 | thrane_tmp = tempdir()
7 | unlink(thrane_tmp, recursive=TRUE)
8 | dir.create(thrane_tmp)
9 | download.file('https://github.com/FridleyLab/spatialGE_Data/raw/refs/heads/main/melanoma_thrane.zip?download=',
10 | destfile=paste0(thrane_tmp, '/', 'melanoma_thrane.zip'), mode='wb')
11 | zip_tmp = list.files(thrane_tmp, pattern='melanoma_thrane.zip$', full.names=TRUE)
12 | unzip(zipfile=zip_tmp, exdir=thrane_tmp)
13 | count_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'), full.names=TRUE, pattern='counts')
14 | coord_files <- list.files(paste0(thrane_tmp, '/melanoma_thrane'), full.names=TRUE, pattern='mapping')
15 | clin_file <- list.files(paste0(thrane_tmp, '/melanoma_thrane'), full.names=TRUE, pattern='clinical')
16 | # library('spatialGE')
17 | melanoma = STlist(rnacounts=count_files[c(1,2)], spotcoords=coord_files[c(1,2)], samples=clin_file) # Only first two samples
18 |
19 | # Test that resulting STlist is an S4 object
20 | testthat::test_that("Data input checks output single character string", {
21 | testthat::expect_s4_class(melanoma, 'STlist')
22 | })
23 |
--------------------------------------------------------------------------------
/vignettes/img/diagram_stenrich.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/vignettes/img/diagram_stenrich.png
--------------------------------------------------------------------------------
/vignettes/img/dir_structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/vignettes/img/dir_structure.png
--------------------------------------------------------------------------------
/vignettes/img/metadata_file.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/vignettes/img/metadata_file.png
--------------------------------------------------------------------------------
/vignettes/img/smi_dir_structure.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/vignettes/img/smi_dir_structure.png
--------------------------------------------------------------------------------
/vignettes/img/spatial_gradients.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FridleyLab/spatialGE/51f988b2620d5236c335a0168c82f4147a23cfd0/vignettes/img/spatial_gradients.png
--------------------------------------------------------------------------------
/vignettes/troubleshooting.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Troubleshooting"
3 | date: '`r format(Sys.Date())`'
4 | output: rmarkdown::html_vignette
5 | bibliography: vignette_references.bib
6 | vignette: >
7 | %\VignetteIndexEntry{troubleshooting_page}
8 | %\VignetteEngine{knitr::rmarkdown}
9 | %\VignetteEncoding{UTF-8}
10 | ---
11 |
12 | ```{r vig_settings, include=F}
13 | knitr::opts_chunk$set(
14 | collapse=TRUE,
15 | comment="#>",
16 | fig.width=10, fig.height=7
17 | )
18 | options(rmarkdown.html_vignette.check_title=F)
19 | ```
20 |
21 |
22 | **When installing `spatialGE`, I get `Error in utils::download.file(url, path, method = method, quiet = quiet`. How can I solve this error?**
23 |
24 | This error might appear when installing `spatialGE`:
25 |
26 | ```
27 | Downloading GitHub repo FridleyLab/spatialGE@oospina_dev
28 | Error in utils::download.file(url, path, method = method, quiet = quiet, :
29 | download from 'https://api.github.com/repos/FridleyLab/spatialGE/tarball/oospina_dev' failed
30 | ```
31 |
32 | A workaround is to set `options(timeout)` to a large number:
33 |
34 | ```
35 | options(timeout=9999999) # To avoid R closing connection with GitHub
36 | devtools::install_github("fridleylab/spatialGE")
37 | ```
38 |
39 |
40 |
41 |
42 | **The `gene_interpolation` function outputs an error: `Error in solve.default(qr.R(qr.VT))`. What can be done about it?**
43 |
44 | When running the `gene_interpolation` function with `REML=TRUE`, you may get this error:
45 |
46 | ```
47 | Error in solve.default(qr.R(qr.VT)) :
48 | Lapack routine dgesv: system is exactly singular: U[1,1] = 0
49 | Error in call to optim
50 | spatialProcess: Problems with optim in mKrigMLEJoint
51 | returned object includes the likelihood evaluations up to the error
52 | ```
53 |
54 | This error occurs during REML parameter estimation, and it means that the gene expression surface estimated might not be accurate. Nonetheless, if you run the `STplot_interpolation` on the STlist resulting from `gene_interpolation`, you will notice that gene expression surfaces have actually been generated for the other genes and samples despite the error being presented.
55 |
56 | If you still want to obtain gene expression surfaces for the other genes for which `gene_interpolation` could not produce a surface, you can try `REML=F`, which is a slower procedure.
57 |
58 |
59 |
60 |
61 |
62 | **When using readRDS to load an STList, I get ‘Error: vector memory exhausted’. What can I do?**
63 |
64 | This error results from R’s default memoery limits. Provided your computer has enough memory to hold an STList in memory, this error message can be overridden by typing in a Terminal (Mac OS):
65 |
66 | ```
67 | echo 'R_MAX_VSIZE=100Gb' >> ~/.Renviron
68 | ```
69 |
70 | Then, close and re-open R and try to load the STList again.
71 |
72 |
73 |
--------------------------------------------------------------------------------
/vignettes/vignette_references.bib:
--------------------------------------------------------------------------------
1 | @article{bassiouni_2023,
2 | author = {Bassiouni, Rania and Idowu, Michael O. and Gibbs, Lee D. and Robila, Valentina and Grizzard, Pamela J. and Webb, Michelle G. and Song, Jiarong and Noriega, Ashley and Craig, David W. and Carpten, John D.},
3 | title = {Spatial transcriptomic analysis of a diverse patient cohort reveals a conserved architecture in triple-negative breast cancer},
4 | journal = {Cancer Research},
5 | volume = {83},
6 | pages = {34-48},
7 | year = {2023},
8 | url = {https://doi.org/10.1158/0008-5472.CAN-22-2682}
9 | }
10 |
11 | @article{hafemeister_2019,
12 | title={Normalization and variance stabilization of single-cell RNA-seq data using regularized negative binomial regression},
13 | author={Hafemeister, Christoph and Satija, Rahul},
14 | journal={Genome biology},
15 | volume={20},
16 | pages={296},
17 | year={2019},
18 | url={https://doi.org/10.1186/s13059-019-1874-1}
19 | }
20 |
21 | @article{langfelder_2008,
22 | author = {Langfelder, Peter and Zhang, Bin and Horvath, Steve},
23 | title = "{Defining clusters from a hierarchical cluster tree: the Dynamic Tree Cut package for R}",
24 | journal = {Bioinformatics},
25 | volume = {24},
26 | pages = {719-720},
27 | year = {2007},
28 | url = {https://doi.org/10.1093/bioinformatics/btm563}}
29 |
30 | @article{ospina_2022,
31 | author = {Ospina, Oscar E and Wilson, Christopher M and Soupir, Alex C and Berglund, Anders and Smalley, Inna and Tsai, Kenneth Y and Fridley, Brooke L},
32 | title = "{spatialGE: quantification and visualization of the tumor microenvironment heterogeneity using spatial transcriptomics}",
33 | journal = {Bioinformatics},
34 | volume = {38},
35 | pages = {2645-2647},
36 | year = {2022},
37 | url = {https://doi.org/10.1093/bioinformatics/btac145}
38 | }
39 |
40 | @misc{website:visium_faq,
41 | author = {10X Genomics},
42 | title = {General FAQ},
43 | month = {July},
44 | year = {2023},
45 | url = {https://kb.10xgenomics.com/hc/en-us/sections/360007215051-General}
46 | }
47 |
48 | @article{thrane2018spatially,
49 | title={Spatially resolved transcriptomics enables dissection of genetic heterogeneity in stage III cutaneous malignant melanoma},
50 | author={Thrane, Kim and Eriksson, Hanna and Maaskola, Jonas and Hansson, Johan and Lundeberg, Joakim},
51 | journal={Cancer research},
52 | volume={78},
53 | pages={5970-5979},
54 | year={2018}
55 | }
56 |
57 | @article{hunter2021spatially,
58 | title={Spatially resolved transcriptomics reveals the architecture of the tumor-microenvironment interface},
59 | author={Hunter, Miranda V and Moncada, Reuben and Weiss, Joshua M and Yanai, Itai and White, Richard M},
60 | journal={Nature communications},
61 | volume={12},
62 | number={1},
63 | pages={6278},
64 | year={2021},
65 | publisher={Nature Publishing Group UK London}
66 | }
67 |
68 | @article{ospina_2024,
69 | author = {Ospina, Oscar E and Soupir, Alex C and Manjarres-Betancur, Roberto and Gonzalez-Calderon, Guillermo and Yu, Xiaoqing and Fridley, Brooke L},
70 | title = "{Differential gene expression analysis of spatial transcriptomic experiments using spatial mixed models}",
71 | journal = {Scientific reports},
72 | volume = {14},
73 | pages = {10967},
74 | year = {2024},
75 | url = {https://doi.org/10.1038/s41598-024-61758-0}
76 | }
77 |
78 |
--------------------------------------------------------------------------------