-
Notifications
You must be signed in to change notification settings - Fork 71
/
merfish_nonparametric.Rmd
140 lines (116 loc) · 8.01 KB
/
merfish_nonparametric.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
---
title: "Applying CSIDE to Spatial Transcriptomics Data: Smooth spatial patterns in MERFISH"
author: "Dylan Cable"
date: "December 21st, 2021"
output:
html_document:
keep_md: yes
pdf_document: default
rmarkdown::html_vignette:
keep_md: yes
vignette: |
%\VignetteIndexEntry{merfish-nonparametric}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
```{r setup}
library(spacexr)
library(Matrix)
library(doParallel)
library(ggplot2)
datadir <- system.file("extdata",'SpatialRNA/MerfishVignette',package = 'spacexr') # directory for sample MERFISH dataset
if(!dir.exists(datadir))
dir.create(datadir)
savedir <- 'RCTD_results'
if(!dir.exists(savedir))
dir.create(savedir)
```
```{r, include = TRUE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
cache = TRUE,
out.width = "100%"
)
```
## Introduction
Cell type-Specific Inference of Differential Expression, or CSIDE, is part of the *spacexr* R package for learning cell type-specific differential expression from spatial transcriptomics data. In this Vignette, we will use CSIDE to test for nonparametric differential expression in a toy hypothalamus MERFISH dataset. First, we will first use RCTD to assign cell types to a hypothalamus MERFISH dataset. We will define cell type profiles using an annotated single cell RNA-sequencing (scRNA-seq) hypothalamus dataset. We will detect differential expression as nonparametric smooth spatial patterns.
## Data Preprocessing and running RCTD
First, we run RCTD on the data to annotate cell types. Please note that this follows exactly the content of the spatial transcriptomics RCTD vignette, except that we use RCTD on doublet mode. Since doublet mode can discover 1-2 cell types per pixel, it is a reasonable choice for technologies such as MERFISH that can have precise spatial resolution. Please refer to the [spatial transcriptomics vignette](https://raw.githack.com/dmcable/spacexr/master/vignettes/spatial-transcriptomics.html) for more explanation on the RCTD algorithm.
```{r RCTD}
### Load in/preprocess your data, this might vary based on your file type
if(!file.exists(file.path(savedir,'myRCTD_merfish.rds'))) {
counts <- as.data.frame(readr::read_csv(file.path(datadir,"counts.csv"))) # load in counts matrix
coords <- read.csv(file.path(datadir,"coords.csv"))
rownames(counts) <- counts[,1]; counts[,1] <- NULL # Move first column to rownames
rownames(coords) <- coords[,1]; coords[,1] <- NULL # Move first column to rownames
nUMI <- colSums(counts) # In this case, total counts per pixel is nUMI
puck <- SpatialRNA(coords, counts, nUMI)
barcodes <- colnames(puck@counts) # pixels to be used (a list of barcode names).
plot_puck_continuous(puck, barcodes, puck@nUMI, ylimit = c(0,round(quantile(puck@nUMI,0.9))),
title ='plot of nUMI')
refdir <- system.file("extdata",'Reference/Merfish_Ref',package = 'spacexr') # directory for the reference
counts <- as.data.frame(readr::read_csv(file.path(refdir,"counts.csv"))) # load in cell types
rownames(counts) <- counts[,1]; counts[,1] <- NULL # Move first column to rownames
cell_types <- read.csv(file.path(refdir,"cell_types.csv")) # load in cell types
cell_types <- setNames(cell_types[[2]], cell_types[[1]])
cell_types <- as.factor(cell_types) # convert to factor data type
nUMI <- read.csv(file.path(refdir,"nUMI.csv")) # load in cell types
nUMI <- setNames(nUMI[[2]], nUMI[[1]])
reference <- Reference(counts, cell_types, nUMI)
myRCTD <- create.RCTD(puck, reference, max_cores = 2)
myRCTD <- run.RCTD(myRCTD, doublet_mode = 'doublet')
saveRDS(myRCTD,file.path(savedir,'myRCTD_merfish.rds'))
}
```
## Running CSIDE
We are now ready to run CSIDE using the `run.CSIDE.nonparametric` function. We will use two cores, and a false discovery rate of 0.25. Next, we will set a gene threshold (i.e. minimum gene expression) of 0.01, and we will set a cell_type_threshold (minimum instances per cell type) of 10.
**Warning**: On this toy dataset, we have made several choices of parameters that are not recommended for regular use. On real datasets, we recommend first consulting the CSIDE default parameters. This includes `gene_threshold` (default 5e-5), `cell_type_threshold` (default 125), `fdr` (default 0.01), and `weight_threshold` (default NULL). Please see `?run.CSIDE.nonparametric` for more information on these parameters.
```{r DEgenes}
#de
myRCTD <- readRDS(file.path(savedir,'myRCTD_merfish.rds'))
myRCTD@config$max_cores <- 2
cell_types <- c('Excitatory', 'Inhibitory', 'Astrocytes')
myRCTD <- run.CSIDE.nonparam(myRCTD, df = 6, cell_types = cell_types, gene_threshold = .001, cell_type_threshold = 10, fdr = 0.25)
saveRDS(myRCTD,file.path(savedir,'myRCTDde_merfish.rds'))
```
Equivalently to using the `run.CSIDE.nonparametric` function, those who want to have more precise control can build the design matrix directly. In this case, we use the `build.designmatrix.nonparametric` function, which constructs the design matrix given `df` the number of basis functions. After constructing the design matrix, one can use the `run.CSIDE` function to run CSIDE with this general design matrix. We also plot the third basis function below.
```{r design matrix}
X <- build.designmatrix.nonparam(myRCTD,df = 6)
print(head(X))
barcodes <- rownames(X)
myRCTD_identical <- run.CSIDE(myRCTD, X, barcodes, cell_types, gene_threshold = .001,
cell_type_threshold = 10, fdr = 0.25)
plot_puck_continuous(myRCTD@spatialRNA, myRCTD@internal_vars_de$barcodes, X[,3], ylimit = c(0,1),
title ='plot of third basis function')
```
## CSIDE results
After running CSIDE, it is time to examine CSIDE results. We will focus on cell type Excitatory. Furthermore, we will examine the Irs4 gene. Notice below that the largest coefficient in magnitude is the third basis function, which was plotted above. This implies that Irs4 has large change in expression along the y-axis, which we will verify with spatial plots next.
```{r CSIDE_results, fig.width = 8, fig.height=8}
#print results for cell type 'Excitatory'
cell_type <- 'Excitatory'
gene <- 'Irs4'
print(paste("following results hold for", gene))
print("check for covergence of each cell type")
print(myRCTD@de_results$gene_fits$con_mat[gene, ])
print('estimated coefficients for Excitatory in each basis function')
cell_type_ind <- which(cell_type == cell_types)
print(myRCTD@de_results$gene_fits$all_vals[gene, , cell_type_ind])
```
## Look at significant genes.
We will now examine the `myRCTD@de_results$sig_gene_list` object. `sig_gene_list` is a list, for each cell type, of data.frames that contain the hypothesis testing results for significant genes. For nonparametric CSIDE, each coefficient is tested for significance. In particular, notice the following columns: `p_val` (p value), `log_fc` (log-fold-change along that coefficient axis), `Z_score` (Z-score of testing that coefficient), `se` (standard error), `conv` (convergence), and `paramindex_best` (index of the parameter that achieved significance). For example, for Irs4, the third basis function, as discussed above, achieved signifcance.
```{r load_testes}
print(head(myRCTD@de_results$sig_gene_list[[cell_type]]))
```
Finally, we will plot CSIDE results in the `savedir` directory!
The following plot shows a spatial visualization of the Irs4 gene, which was determined by CSIDE to be significantly differentially expressed in excitatory neurons. We below use the `plot_gene_raw` function to observe the raw gene values, whereas `plot_prediction_gene` plots the CSIDE fitted spatial patterns. Observe how the CSIDE fit smooths over the noisy single-pixel gene expression values.
The function `make_all_de_plots` will automatically generate several types of plots displaying the CSIDE results across all genes and cell types.
```{r results, fig.width = 8, fig.height=8}
myRCTD <- readRDS(file.path(savedir,'myRCTDde_merfish.rds'))
gene <- 'Irs4'
plot_prediction_gene(myRCTD, cell_type, gene)
plot_gene_raw(myRCTD, gene, cell_type, ymax = 40)
```
```{r plots, results = 'hide', warning = FALSE}
make_all_de_plots(myRCTD, savedir)
```