a b/examples/walkthrough_simulation.Rmd
1
2
---
3
title: "Walkthrough with a simulated data"
4
author: "Suoqin Jin, Lihua Zhang"
5
output: html_document
6
mainfont: Arial
7
vignette: >
8
  %\VignetteIndexEntry{Integrative analysis of single cell multi-omics data using scAI}
9
  %\VignetteEngine{knitr::rmarkdown}
10
  %\VignetteEncoding{UTF-8}
11
---
12
13
```{r setup, include = FALSE}
14
knitr::opts_chunk$set(
15
  collapse = TRUE,
16
  comment = "#>",
17
  root.dir = './'
18
)
19
```
20
21
22
This walkthrough outlines the key steps of scAI using a simulated data. This simulated data consist of paired single-cell RNA-seq and ATAC-seq data, which were generated based on bulk RNA-seq and DNase-seq profiles from the same sample using MOSim package. 
23
24
Load the required libraries
25
```{r message=FALSE,warning=FALSE}
26
library(scAI)
27
library(dplyr)
28
library(cowplot)
29
library(ggplot2)
30
```
31
32
## Load data
33
The algorithm takes a list of two digital data matrices as input. Genes/loci should be in rows and cells in columns. rownames and colnames should be included. Before running the scAI model, we need to normalize the data to account for library size and select highly variable features.
34
35
```{r}
36
load("/Users/suoqinjin/Documents/scAI/data/data_simulation.rda")
37
X <- data_simulation$data # List of data matrix
38
labels <- data_simulation$labels # the true labels of cells, which is used for validation 
39
```
40
41
## Create a scAI object
42
```{r}
43
scAI_outs <- create_scAIobject(raw.data = X)
44
```
45
## Preprocess data
46
Perform quality control to remove low-quality cells and genes, and normalize the data. 
47
Since this is a simulated data, we do not need to normalize the data. Thus we set `assay = NULL`. 
48
49
```{r, results='asis'}
50
scAI_outs <- preprocessing(scAI_outs, assay = NULL, minFeatures = 200, minCells = 1, 
51
                            libararyflag = F, logNormalize = F)
52
```
53
Add cell information into *pData* slot of the object
54
```{r}
55
scAI_outs <- addpData(scAI_outs, pdata = labels, pdata.name = "labels")
56
```
57
58
## Run scAI model
59
As depending on the random initilization the results might differ, we run scAI multiple times (e.g. nrun = 5) and output the best result. User can also output results from all runs by setting *keep_all = TRUE*. The key parameters here are the number of factors/clusters (k). The `selectK` function can aid in selecting k. A suitable k is the one at which the magnitude of cophenetic correlation begins to fall.
60
```{r}
61
scAI_outs <- run_scAI(scAI_outs, K = 5, nrun = 5)
62
```
63
64
## Visualize the inferred biologically relevant components
65
We plot the heatmap of the three learned low-rank matrices using hierarchical clustering. The ground truth labels of cells are used for validation (not necessary). 
66
```{r, fig.width=7,fig.height = 8, fig.wide = TRUE, fig.align = "center"}
67
lmHeatmap(scAI_outs, color.by = "labels")
68
```
69
70
## Visualize cells onto the low-dimensional space
71
We can visualize cells onto the low-dimensional space using t-SNE, FIt-sne or UMAP. 
72
Here, we perform comparison of the visualization of raw ATAC-seq data with the aggregated data. Cells are colored by the true labels.
73
```{r, fig.width=7,fig.height = 3.5,  fig.wide = TRUE, fig.align = "center"}
74
cell_coords.ori <- reducedDims(scAI_outs, data.use = scAI_outs@norm.data$ATAC, do.scale = F, method = "umap", return.object = F)
75
cell_coords.agg <- reducedDims(scAI_outs, data.use = scAI_outs@agg.data, do.scale = F, method = "umap", return.object = F)
76
77
gg1 <- cellVisualization(scAI_outs, cell_coords.ori, color.by = "labels",show.legend = F, title = "scATAC-seq")
78
gg2 <- cellVisualization(scAI_outs, cell_coords.agg, color.by = "labels", ylabel = NULL, title = "Aggregated scATAC-seq")
79
cowplot::plot_grid(gg1, gg2)
80
```
81
82
## Identify enriched features in each factor
83
```{r}
84
markers_RNA <- identifyFactorMarkers(scAI_outs, assay = 'RNA', n.top = 5)
85
markers_ATAC <- identifyFactorMarkers(scAI_outs, assay = 'ATAC',  n.top = 5)
86
```
87
88
### Ranking the features (genes/loci) and show the top markers in each factor
89
```{r, fig.width=8, fig.height=3,  fig.wide = TRUE, fig.align = "center"}
90
featureRankingPlot(scAI_outs, assay = 'RNA', feature.show = markers_RNA$markers.top$features, top.p = 0.1, ylabel = "Gene score", ncol = 5)
91
featureRankingPlot(scAI_outs, assay = 'ATAC', feature.show = markers_ATAC$markers.top$features, top.p = 0.1, ylabel = "Locus score", ncol = 5)
92
93
```
94
95
## Embedding cells, genes, loci and factors into 2D-dimensions using our new visualization method VscAI
96
97
```{r message=FALSE,warning=FALSE}
98
scAI_outs <- getEmbeddings(scAI_outs)
99
100
```
101
102
### Visualization of the embedding using VscAI
103
User can provide a vector of the features (e.g., key marker genes/loci) to explore the biological meaning of the cell groups and enhance the interpretation of the data. Here, we select the top two features of each factor. 
104
```{r, fig.width=10,fig.height=4, fig.align = "center"}
105
genes.embed <- markers_RNA$markers.top %>% group_by(factors) %>% slice(1:2)
106
genes.embed <- as.character(genes.embed$features)
107
loci.embed <- markers_ATAC$markers.top %>% group_by(factors) %>% slice(1:2)
108
loci.embed <- as.character(loci.embed$features)
109
110
gg1 <- VscAIplot(scAI_outs, gene.use = genes.embed, loci.use = NULL, loci.use.names = NULL, color.by = "labels") 
111
gg2 <- VscAIplot(scAI_outs, gene.use = NULL, loci.use = loci.embed, loci.use.names = loci.embed, color.by = "labels")
112
cowplot::plot_grid(gg1, gg2)
113
114
```
115
116
## Feature plot
117
We can overlay the expression of features, or the cell loading values onto the low-dimensional space, e.g., VscAI, tsne, umap
118
```{r, fig.width=9, fig.height=5,  fig.wide = TRUE, fig.align = "center"}
119
featureScoreVisualization(scAI_outs, feature.scores = t(scAI_outs@fit$H), feature.use = c('factor1','factor2','factor3','factor4','factor5'),  method = "VscAI", nCol = 3, cell.size = 0.1, show.legend = T, show.legend.combined = F)
120
```
121
122
## Identify cell clusters
123
We can also identify cell clusters based on the inferred cell loading matrix using Leiden algorithm.
124
```{r}
125
scAI_outs <- identifyClusters(scAI_outs, resolution = 0.05)
126
127
```
128
## Visualize cells onto the low-dimensional space
129
We can visualize cells onto the low-dimensional space generated by t-SNE, FIt-sne or UMAP.
130
Here, we perform UMAP dimension reduction. Cells are colored by the clustering inferred by scAI.
131
```{r, fig.width=4.5,fig.height = 3.5,  fig.wide = TRUE, fig.align = "center"}
132
scAI_outs <- reducedDims(scAI_outs, method = "umap")
133
cellVisualization(scAI_outs, scAI_outs@embed$umap, color.by = "cluster")
134
135
```
136