-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestorowa-dim-red.Rmd
253 lines (218 loc) · 7.34 KB
/
nestorowa-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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
---
title: 'Nestorowa 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: default
---
```{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-nestorowa, 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/nestorowa-rmd-data.RData")
```
----
Analyzing Nestorowa 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 Nestorowa data comes as a dataframe of 1645 cells x 4290 genes.
Nestorowa examines differentiation of hematopoeitic stem and progenitor cells (HSPCs). They examine 1656 single HSPC transcriptomes using single-cell RNA sequencing and aim to better understand and visualize HSC-to-progenitor transitions.
```{r prepare-nestorowa, eval=FALSE, include=FALSE}
## read in data, convert to matrix
rawdata <- read.delim("~/GitHub/cell-diff-reu-2018/data/coordinates_gene_counts_flow_cytometry.txt", row.names=1)
cleandata <- na.omit(rawdata) # remove rows with NA
data <- cleandata[,14:ncol(cleandata)] # choose relevant gene exp data
data <- as.matrix(data)
dataname <- "Nestorowa Gene Expression Data"
## create factor for grouping
rownames <- row.names(data)
groups <- as.character(rownames)
groups <- gsub(".*HSPC.*", "HSPC", groups)
groups <- gsub(".*LT.*", "LT.HSC", groups)
groups <- gsub(".*Prog.*", "PROG", groups)
groupsf <- factor(groups)
nclusters <- nlevels(groupsf) # number of groups to cluster
# colors <- topo.colors(nclusters) # choose colors
colors <- c("limegreen", "dodgerblue", "gold")
```
```{r heatmap-nestorowa, eval=FALSE, include=FALSE}
hmap <- heatmap(data, Rowv=NA, Colv=NA)
```
## PCA (Handwritten)
This is the handwritten version of PCA in R, I used SVD for everything (not eig).
```{r pca-nestorowa, eval=FALSE, include=FALSE}
## PCA DIMENSIONS:
## cells = rows, genes = columns
## Nestorowa: use original data matrix, NO transpose
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, Nestorowa")
legend("topright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=0.6, pt.cex=1)
```
3D plot of scores
```{r pca-plot3dscores}
## plot scores 3d
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, Nestorowa")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=1.5)
rglwidget()
```
#### Plots of principal components
2D plot of PCs (eigenvectors of covariance matrix)
```{r pca-plot2dpcs}
## plot PCs 2d (eigenvectors of original covariance matrix)
plot(x=V[,1], y=V[,2],
xlab="PC1", ylab="PC2",
col = colors[groupsf],
pch=20,
main = "Principal Components, Nestorowa")
legend("topright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=0.6, pt.cex=1)
```
3D plot of PCs (eigenvectors of actual covariance matrix)
```{r pca-plot3d-pcs}
## 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, Nestorowa")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=1.5)
rglwidget()
```
----
## Diffusion Mapping (destiny)
Diffusion mapping using destiny package.
```{r diffmap-nestorowa, eval=FALSE, include=FALSE}
## convert to expression set
## EXPRESSION SET DIMENSIONS: cells = columns ; genes = rows
## Nestorowa needs transpose
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, Nestorowa")
legend("bottomleft", inset=c(0,0),
legend=levels(groupsf),
col=colors,
pch=20,
ncol=1, cex=0.6, 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, Nestorowa")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, 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-nestorowa, eval=FALSE, include=FALSE}
## already read in data and converted to matrix
# t-SNE DIMENSIONS: rows=cells, columns=genes
# Nestorowa requires a transpose back to original data setup
data <- t(data)
ydata3d <- tsne(data, k=3, max_iter = 200)
```
2D t-SNE plot (dimensions 1 vs. 2)
```{r tsne-plot2d-1v2}
plot(x=ydata3d[,1], y=ydata3d[,2],
xlab="", ylab="",
col = colors[groupsf],
pch=20,
main = "t-SNE, Nestorowa")
legend("topright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=0.6, pt.cex=1)
```
2D t-SNE plot (dimensions 2 vs. 3)
```{r tsne-plot2d-2v3}
plot(x=ydata3d[,2], y=ydata3d[,3],
xlab="", ylab="",
col = colors[groupsf],
pch=20,
main = "t-SNE, Nestorowa")
legend("topright", inset = c(0,0),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=0.6, 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, Nestorowa")
legend3d("bottomright", inset=c(0.1,0.1),
legend = levels(groupsf),
pch = 20,
col = colors,
ncol=1, cex=1.5)
rglwidget()
```