-
Notifications
You must be signed in to change notification settings - Fork 0
/
paul-dim-red.Rmd
227 lines (195 loc) · 6.58 KB
/
paul-dim-red.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: "Paul Gene Expression Data: Dimension Reduction"
author: "Michelle Li and Gianna Wu with Professor de Pillis and Dr. Park"
date: "7/20/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(destiny)
library(Biobase)
library(tsne)
library(rgl)
options(rgl.useNULL = TRUE)
r3dDefaults$windowRect <- c(0,0,1000,1000)
```
```{r load-paul, include=FALSE}
## load .Rdata file into environment so you have all calculations done (located in our Drive)
## if you have the .RData file to load, use eval=FALSE for pca, diffmap, tsne
load("~/GitHub/cell-diff-reu-2018/paul-rmd-data.RData")
```
----
Analyzing Paul gene expression data using the following dimension reduction techniques in R: PCA (handwritten version), diffusion mapping (destiny package), and t-SNE (tsne package). The original Paul data comes in as a matrix of 8716 genes x 2730 cells.
Paul examines stem cell differentiation in the bone marrow. Using single-cell RNA sequencing they examine the heterogeneity of myeloid progenitor cells to better understand and define the differentiation of hematopoietic progenitors into distinct
lineages. They identify 19 transcriptionally homogeneous subpopulations of cells, which we use for our coloring of plots.
```{r prepare-paul, eval=FALSE, include=FALSE}
## read in data
load("~/GitHub/cell-diff-reu-2018/data/Paul_Cell_MARSseq_GSE72857.RData")
## create factor for grouping
groupsf <- factor(cluster.id) # paul data
nclusters <- nlevels(groupsf) # number of groups to cluster
colors <- topo.colors(nclusters) # choose colors
```
```{r heatmap-grover, eval=FALSE, include=FALSE}
hmap <- heatmap(data, Rowv=NA, Colv=NA)
```
## PCA (Handwritten)
```{r pca-paul, eval=FALSE, include=FALSE}
## PCA DIMENSIONS:
## cells = rows, genes = columns
## Paul: use TRANSPOSE of data matrix (want cells x genes)
data <- t(data)
M1centered <- scale(data, center=TRUE, scale=FALSE) # subtracts column means from each column
## svd
svd <- svd(M1centered, nu=3, nv=3) # only first 3 left/right singular vectors for efficiency
U <- svd$u # dim: nrowx3
V <- svd$v # dim: ncolx3
# Note: U = M1centered %*% V %*% S^-1
## calculate scores
# weights <- t(M1centered) %*% U # dim: ncolx3
scores <- M1centered %*% V # dim: nrowx3
```
#### Plots of scores
Scores were calculated by: scores <- data %*% V. V are the right singular vectors from SVD whose columns contain the eigenvectors of the covariance matrix. Scores give the coordinates of the individuals on the principal components (coefficients of the linear combination of original variables for each principal component).
2D plot of scores
```{r pca-plot2dscores}
plot(x=scores[,1], y=scores[,2],
xlab="PC1 scores", ylab="PC2 scores",
col = colors[groupsf],
pch=20,
main = "PCA Scores, Paul")
legend("bottomright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=0.4, pt.cex=1)
```
3D plot of scores
```{r pca-plot3dscores}
plot3d(x=scores[,1], y=scores[,2], z=scores[,3],
xlab="PC1 scores", ylab="PC2 scores", zlab="PC3 scores",
col = colors[groupsf],
pch=20,
main = "PCA Scores, Paul")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=1.5)
rglwidget()
```
#### Plots of principal components
2D plot of PCs (eigenvectors of covariance matrix)
```{r pca-plot2dpcs}
## plot PCs 2d (eigenvectors of covariance matrix)
plot(x=V[,1], y=V[,2],
xlab="PC1", ylab="PC2",
col = colors[groupsf],
pch=20,
main = "Principal Components, Paul")
legend("bottomright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=0.4, pt.cex=1)
```
3D plot of PCs (eigenvectors of covariance matrix)
```{r pca-plot3dpcs}
## plot PCs 3d
plot3d(x=V[,1], y=V[,2], z=V[,3],
xlab="PC1", ylab="PC2", zlab="PC3",
col = colors[groupsf],
pch=20,
main = "Principal Components, Paul")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=1.5)
rglwidget()
```
----
## Diffusion Mapping (destiny)
Diffusion mapping using destiny package.
```{r diffmap-paul, eval=FALSE, include=FALSE}
## convert to expression set
## EXPRESSION SET DIMENSIONS: cells = columns ; genes = rows
## Paul needs the original formatting (we just took a transpose, must transpose back)
data <- t(data)
dataES <- ExpressionSet(assayData=data) # you must include the 'assayData=' for this to come out correctly
## diffusion map
dm <- DiffusionMap(dataES)
## sigmas
sigmas <- find_sigmas(dataES)
```
2D Diffusion Map
```{r diffmap-plot2d}
## plot 2d
plot(x=eigenvectors(dm)[,1], y=eigenvectors(dm)[,2],
xlab="DC1", ylab="DC2",
col = colors[groupsf],
pch=20,
main = "Diffusion Map, Paul")
legend("bottomright", inset=c(0,0),
legend=levels(groupsf),
col=colors,
pch=20,
ncol=3, cex=0.4, pt.cex=1)
```
3D Diffusion Map
```{r diffmap-plot3d}
## plot 3d
plot3d(x=eigenvectors(dm)[,1], y=eigenvectors(dm)[,2], z=eigenvectors(dm)[,3],
xlab="DC1", ylab="DC2", zlab="DC3",
col = colors[groupsf],
pch=20,
main = "Diffusion Map, Paul")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=1.5)
rglwidget()
```
Sigmas
```{r diffmap-sigmas}
plot(sigmas)
```
----
## t-SNE (tsne package)
t-SNE using tsne package (from jdonaldson, linked on van der Maaten's website).
```{r tsne-paul, eval=FALSE, include=FALSE}
## already read in data and converted to matrix
# Note: can take very, very long with this dataset
# t-SNE DIMENSIONS: rows=cells, columns=genes
# Paul data needs transpose to have correct dimensions
data <- t(data)
ydata3d <- tsne(data, k=3, max_iter = 200) # ydata = matrix(rnorm(k * n),n)
```
2D t-SNE plot
```{r tsne-plot2d}
plot(x=ydata3d[,1], y=ydata3d[,2],
xlab="", ylab="",
col = colors[groupsf],
pch=20,
main = "t-SNE, Paul")
legend("bottomleft", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=0.4, pt.cex=1)
```
3D t-SNE plot
```{r tsne-plot3d}
plot3d(x=ydata3d[,1], y=ydata3d[,2], z=ydata3d[,3],
xlab="", ylab="", zlab="",
col=colors[groupsf],
pch=20,
main = "t-SNE, Paul")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=3, cex=1.5)
rglwidget()
```