-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
144 lines (122 loc) · 6.42 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# scBSC (single cell Bivariate Spatial Correlation)
<!-- badges: start -->
<!-- badges: end -->
scBSC allows users calculate bivariate spatial correlation statistics as applied from [Lee S., J Geography Syst (2001)](https://link.springer.com/article/10.1007/s101090100064) to this wonderful spatial single cell RNA-Seq domain.
## Installation
You can install the released version of scBSC from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("scBSC")
```
And the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("soymintc/scBSC")
```
## Example
The following calculates spatial correlation between expression of two genes, Penk and Cck, from a Seurat-format mouse brain spatial scRNA-seq data from Visium 10x.
#### Using pre-made SeuratData
```R
# Import libraries
library(Seurat)
library(SeuratData)
library(scBSC)
# Import Seurat-format mouse brain data
InstallData("stxBrain")
brain <- LoadData("stxBrain", type = "anterior1")
brain <- SCTransform(brain, assay = "Spatial", verbose = FALSE)
# Calculate bivariate spatial correlation statistics
conn_mat <- make_conn_mat(brain) # creating connectivity matrix; only required once per dataset
bsc <- calc_bsc("Penk", "Cck", brain, conn_mat) # calculate statistics for any pair of genes in the data
print_ <- sprintf("%.3f %.3f %.3f %.3f %.3f",
bsc$L_XX, # How well is Penk expression spatially clustered
bsc$L_YY, # How well is Cck expression spatially clustered
bsc$r_sm, # Correlation between spatially smoothened expression of two genes
bsc$r, # Correlation between expression of two genes
bsc$L_XY) # Bivariate spatial correlation (clustering effect & expression correlation)
print(print_) # result: 0.869 0.865 -0.809 -0.729 -0.702
```
#### Using Visium 10x data, imported by Seurat Load10X_Spatial
Of course scBSC also supports your own spatial scRNA-seq, as long as the data is in Seurat spatial object format.
Link to raw data: [Visium 10x Mouse Brain Serial Section](https://support.10xgenomics.com/spatial-gene-expression/datasets/1.1.0/V1_Mouse_Brain_Sagittal_Anterior)
```R
# Import libraries
library(Seurat)
library(scBSC)
# Import Visium 10x data using Seurat
setwd('/path/to/mouse_brain_data')
# Use Load10X_Spatial to load data from the following file structure.
# Data used was Visium 10x Mouse Brain Serial Section 1 (Anterior-Sagittal)
# anterior1 (directory under /path/to/mouse_brain_data; any name would suffice)
# ├── filtered_feature_bc_matrix
# │ ├── barcodes.tsv.gz # cell barcodes
# │ ├── features.tsv.gz # gene info
# │ └── matrix.mtx.gz # gene count per cell
# ├── filtered_feature_bc_matrix.h5 # [req] sparse matrix including the info above
# └── spatial
# ├── aligned_fiducials.jpg
# ├── detected_tissue_image.jpg
# ├── scalefactors_json.json # [req] high -> low-res image scaling info
# ├── tissue_hires_image.png
# ├── tissue_lowres_image.png # [req]
# └── tissue_positions_list.csv # [req] the coordinates for each spot
brain <- Load10X_Spatial(data.dir="./anterior1",
filename="filtered_feature_bc_matrix.h5",
assay='Spatial',
slice="anterior1",
filter.matrix = TRUE)
brain@meta.data$orig.ident <- "anterior1" # give cells identity; default: SeuratProject
Idents(object = brain) <- "anterior1" # label experiment
Project(object = brain) <- "brain" # label project
# Remove some unwanted genes for your downstream experiments
brain <- PercentageFeatureSet(brain, "^mt-", col.name = "percent_mito")
brain <- PercentageFeatureSet(brain, "^Hb.*-", col.name = "percent_hb")
brain = brain[, brain$nFeature_Spatial > 500 &
brain$percent_mito < 25 &
brain$percent_hb < 20]
brain <- brain[!grepl("Bc1", rownames(brain)), ] # filter Bc1
brain <- brain[!grepl("^mt-", rownames(brain)), ] # filter Mitocondrial
brain <- brain[!grepl("^Hb.*-", rownames(brain)), ] # filter Hemoglobin gene (optional if Hb genes needed)
# Normalize expression values by SCTransform
# Required for later calculations based on assay="SCT"; uses brain@assays[["SCT"]]@data as default
# Not required if the assay property in calc_bsc is to be changed to "Spatial" (then only requires brain@assays[["Spatial"]])
brain <- SCTransform(brain, assay = "Spatial", verbose = TRUE, method = "poisson")
# Calculate bivariate spatial correlation statistics
conn_mat <- make_conn_mat(brain) # creating connectivity matrix; only required once per dataset
bsc <- calc_bsc("Penk", "Cck", brain, conn_mat) # calculate statistics for any pair of genes in the data
print_ <- sprintf("%.3f %.3f %.3f %.3f %.3f",
bsc$L_XX, # How well is Penk expression spatially clustered
bsc$L_YY, # How well is Cck expression spatially clustered
bsc$r_sm, # Correlation between spatially smoothened expression of two genes
bsc$r, # Correlation between expression of two genes
bsc$L_XY) # Bivariate spatial correlation (clustering effect & expression correlation)
print(print_) # result: 0.869 0.865 -0.809 -0.729 -0.702
```
Plotting the relative gene expression between the two genes will show the following result.
Below includes a highly negative, highly positive, and close-to-zero Lxy cases.
```r
# Highly positive spatial correlation example with Lxx 0.869, Lyy 0.864, rsm 0.936, r 0.855, Lxy 0.813
SpatialFeaturePlot(brain, features = c("Gpr88", "Ppp1r1b"), ncol = 2, alpha = c(0.1, 1), max.cutoff = 5)
```
![Expression between two genes](man/figures/README-1.png?raw=true "Highly positive spatial correlation")
```r
# Highly negative spatial correlation with Lxx 0.869, Lyy 0.865, rsm -0.809, r -0.729, Lxy -0.702
SpatialFeaturePlot(brain, features = c("Penk", "Cck"), ncol = 2, alpha = c(0.1, 1), max.cutoff = 5)
```
![Expression between two genes](man/figures/README-2.png?raw=true "Highly negative spatial correlation")
```r
# Close-to-zero spatial correlation with Lxx 0.461, Lyy 0.569, rsm 0.075, r 0.059, Lxy 0.038
SpatialFeaturePlot(brain, features = c("Rpl34", "Ptn"), ncol = 2, alpha = c(0.1, 1), max.cutoff = 5)
```
![Expression between two genes](man/figures/README-3.png?raw=true "Close-to-zero spatial correlation")