-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonocle Projections.Rmd
318 lines (223 loc) · 10.7 KB
/
Monocle Projections.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
---
title: "Monocle Trajectories"
author: "D. Ford Hannum"
date: "6/17/2020"
output:
html_document:
toc: true
toc_depth: 3
number_sections: false
theme: united
highlight: tango
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(Seurat)
library(ggplot2)
library(data.table)
library(monocle)
```
Looking at monocle trajectories for the scWBM project, experiment 1. Going to look at three different trajectories:
* all data
* MKs, erythrocytes and MEPs
* myeloid
* myeloblast (granulocytes, monocytes, and macrophages)
```{r loading data}
# NOT SURE WHY THE IMPORT DOESN'T WORK, JUST GOING TO IMPORT THE RAW DATA INTO MONOCLE
# ?importCDS()
# seuratWBM <- readRDS('./data/wbm_clustered_filtered_named.rds')
# x <- importCDS(seuratWBM)
mtx <- readMM('./data/filtered_feature_bc_matrix/matrix.mtx')
features <- read.table('./data/filtered_feature_bc_matrix/features.tsv')
colnames(features)[2] <- 'gene_short_name'
barcodes <- read.table('./data/filtered_feature_bc_matrix/barcodes.tsv')
pd <- new("AnnotatedDataFrame", data = barcodes)
fd <- new("AnnotatedDataFrame", data = features)
library(Matrix)
cds <- newCellDataSet(as(as.matrix(mtx),"sparseMatrix"),
phenoData = pd,
featureData = fd,
lowerDetectionLimit = 0.5,
expressionFamily = negbinomial.size())
# HAVING ISSUES RUNNING estimateDispresions SO GOING TO FILTER TO THE CELLS AND GENES FROM THE SEURAT ANALYSIS
swbm <- readRDS('./data/wbm_clustered_filtered_named.rds')
valid_cells <- colnames(swbm)
# there is a -1 in front of the monocle cells
#summary(as.factor(tstrsplit(pData(cds)$V1, '-', keep = 2)[[1]]))
valid_cells <- paste0(valid_cells,'-1')
valid_cells <- rownames(subset(pData(cds),
V1 %in% valid_cells))
#head(valid_cells)
cds <- cds[,valid_cells]
```
```{r doing all cells}
cds <- estimateSizeFactors(cds)
cds <- estimateDispersions(cds)
cds <- detectGenes(cds, min_expr = 0.1)
#summary(fData(cds)$num_cells_expressed)
expressed_genes <- rownames(subset(fData(cds), num_cells_expressed >= 10))
```
```{r adding info to the pData from Seurat}
# DIDN'T WANT TO MESS UP THE MONOCLE OBJECT AND START OVER, SO cds1 IS A BACKUP
#cds1 <- cds
#summary(pData(cds)$V1 == paste0(rownames(swbm@meta.data),'-1'))
pData(cds)[,c('nCountRNA', 'nFeature_RNA', 'celltype','condition', 'percent.mt',
'seurat_clusters', 'cluster_IDs')] <- swbm@meta.data[,c(2,3,5,6,8,11,15)]
colnames(pData(cds))[1] <- 'cell'
#head(pData(cds))
```
# All Data
```{r ordering genes}
cds <- detectGenes(cds, min_expr = 0.1)
fData(cds)$use_for_ordering <- fData(cds)$num_cells_expressed > 0.05 * ncol(cds)
# THE BELOW CODE TOOK FOREVER TO RUN SO I STOPPED IT
#plot_pc_variance_explained(cds, return_all = F, max_components = 10)
cds <- reduceDimension(cds,
max_components = 2,
norm_method = 'log',
num_dim = 3,
reduction_method = 'tSNE',
verbose = T)
cds <- clusterCells(cds, verbose = F)
# checking clustering results
# not sure what to read into these figures
plot_cell_clusters(cds, color_by = 'Cluster')
plot_cell_clusters(cds, color_by = 'cluster_IDs')
plot_cell_clusters(cds, color_by = 'condition')
plot_rho_delta(cds, rho_threshold = 2, delta_threshold = 4)
cds <- clusterCells(cds,
rho_threshold = 2,
delta_threshold = 4,
sskip_rho_sigma = T,
verbose = F)
plot_cell_clusters(cds, color_by = 'Cluster')
plot_cell_clusters(cds, color_by = 'condition')
plot_cell_clusters(cds, color_by = 'cluster_IDs')
clust_DEG_genes <- differentialGeneTest(cds[expressed_genes,],
fullModelFormulaStr = "~Cluster",
cores = 1)
cds_ordering_genes <- row.names(clust_DEG_genes)[order(clust_DEG_genes$qval)][1:1000]
cds <- setOrderingFilter(cds,
ordering_genes = cds_ordering_genes)
cds <- reduceDimension(cds, method = 'DDRTree')
cds <- orderCells(cds)
plot_cell_trajectory(cds, color_by = "cluster_IDs")
#summary(as.factor(pData(cds)$cluster_IDs))
pData(cds)$cell_type_broad <- ifelse(pData(cds)$cluster_IDs %in% c(paste0("Granulocyte",1:4), 'Stem Cell'), 'Granulocyte',
ifelse(pData(cds)$cluster_IDs %in% c('Monocyte','Macrophage'), 'Mono/Macro',
ifelse(pData(cds)$cluster_IDs %in% c('MK','Erythroid','MEP'), 'MK/Ery/MEP',
'Lymphocyte')))
table(pData(cds)$cluster_IDs, pData(cds)$cell_type_broad)
# pData(cds)$pot_progenitors <- ifelse(cds$cluster_IDs %in% c('MEP', 'B-cell pro','Stem Cell'), 'Potential Progenitors',
# 'Further Cell States')
cds$prog <- ifelse(cds$cluster_IDs %in% c('MEP', 'B-cell pro','Stem Cell'), as.character(cds$cluster_IDs), 'Further States')
cds$type <- ifelse(cds$cluster_IDs %in% c('MEP', 'Erythroid','MEP'), 'MK/Ery/MEP',
ifelse(cds$cluster_IDs %in% c('B cell', 'B-cell pro','T-cell/NK'), 'Lymphocyte','Myeloid'))
plot_cell_trajectory(cds, color_by = "cell_type_broad")
#plot_cell_trajectory(cds, color_by = 'pot_progenitors')
plot_cell_trajectory(cds, color_by = 'prog')
plot_cell_trajectory(cds, color_by = 'condition')
plot_cell_trajectory(cds, color_by = 'type')
```
This is the cell trajectory using unsupervised methods. Looks pretty good overall, with the exception of monocytes and macrophages being at the end of the MK/Ery/MEP trajectory. Had low expectations for using all these cells. The clusters we had labeled in Seurat as progenitor populations had differing results. The stem cells, which we've decided is a poor name as in more likely a granulocyte progenitor/neutrophil, showed close to the base, whereas MEPs were disperesed and B-cell prog were actually at the end of the trajectory. Would be more useful to look at the separated populations, which is what will come next.
# MK, Erythroid and MEP Trajectory
```{r mep trajectory}
Mcds <- cds[,cds$cluster_IDs %in% c('MEP',"MK", "Erythroid")]
#dim(Mcds)
Mcds <- estimateSizeFactors(Mcds)
Mcds <- estimateDispersions(Mcds)
Mcds <- detectGenes(Mcds, min_expr = 0.1)
fData(Mcds)$use_for_ordering <- fData(Mcds)$num_cells_expressed > 0.05 * ncol(Mcds)
Mcds <- reduceDimension(Mcds,
max_compongents = 2,
norm_method = 'log',
num_dim = 3,
reduction_method = 'tSNE',
verbose = F)
Mcds <- clusterCells(Mcds, verbose = F)
plot_cell_clusters(Mcds, color_by = 'Cluster')
plot_cell_clusters(Mcds, color_by = 'cluster_IDs')
Mcds_expresssed_genes <- row.names(subset(fData(Mcds),
num_cells_expressed >=10))
# cluster_DEG <- differentialGeneTest(Mcds[Mcds_expresssed_genes,],
# fullModelFormulaStr = '~seurat_clusters',
# cores = 2)
#
# Mcds_ordering_genes <- row.names(cluster_DEG)[order(cluster_DEG$qval)][1:1000]
#
# Mcds <- setOrderingFilter(Mcds, ordering_genes = Mcds_ordering_genes)
Mcds <- reduceDimension(Mcds, method = "DDRTree")
Mcds <- orderCells(Mcds)
plot_cell_trajectory(Mcds, color_by = "cluster_IDs")
plot_cell_trajectory(Mcds, color_by = 'condition')
plot_cell_trajectory(Mcds, color_by = 'celltype')
```
```{r gran trajectory}
gcds <- cds[,cds$cluster_IDs %in% c('Stem Cell', paste0('Granulocyte',1:4))]
#dim(Mcds)
gcds <- detectGenes(gcds, min_expr = 0.1)
fData(gcds)$use_for_ordering <- fData(gcds)$num_cells_expressed > 0.05 * ncol(gcds)
gcds <- reduceDimension(gcds,
max_compongents = 2,
norm_method = 'log',
num_dim = 3,
reduction_method = 'tSNE',
verbose = F)
gcds <- clusterCells(gcds, verbose = F)
plot_cell_clusters(gcds, color_by = 'Cluster')
plot_cell_clusters(gcds, color_by = 'cluster_IDs')
gcds_expresssed_genes <- row.names(subset(fData(gcds),
num_cells_expressed >=10))
# cluster_DEG <- differentialGeneTest(gcds[gcds_expresssed_genes,],
# fullModelFormulaStr = '~seurat_clusters',
# cores = 2)
#
# gcds_ordering_genes <- row.names(cluster_DEG)[order(cluster_DEG$qval)][1:1000]
#
# gcds <- setOrderingFilter(gcds, ordering_genes = gcds_ordering_genes)
gcds <- reduceDimension(gcds, method = "DDRTree")
gcds <- orderCells(gcds)
plot_cell_trajectory(gcds, color_by = "cluster_IDs")
plot_cell_trajectory(gcds, color_by = 'condition')
plot_cell_trajectory(gcds, color_by = 'celltype')
DimPlot(swbm, reduction = 'umap', label = T, repel = T) + NoLegend()
DimPlot(swbm, reduction = 'umap', label = T, repel = T, split.by = 'condition') + NoLegend()
```
```{r running with just MEPs}
MEPcds <- cds[,cds$cluster_IDs %in% c('MK')]
MEPcds <- estimateSizeFactors(MEPcds)
MEPcds <- estimateDispersions(MEPcds)
MEPcds <- detectGenes(MEPcds, min_expr = 0.1)
fData(MEPcds)$use_for_ordering <- fData(MEPcds)$num_cells_expressed > 0.05 * ncol(MEPcds)
MEPcds <- reduceDimension(MEPcds,
max_components = 2,
norm_method = 'log',
num_dim = 3,
reduction_method = 'tSNE',
verbose = F)
MEPcds <- clusterCells(MEPcds, verbose = F)
plot_cell_clusters(MEPcds, color_by = 'Cluster')
plot_cell_clusters(MEPcds, color_by = 'cluster_IDs')
MEPcds_expresssed_genes <- row.names(subset(fData(MEPcds),
num_cells_expressed >=10))
# cluster_DEG <- differentialGeneTest(MEPcds[MEPcds_expresssed_genes,],
# fullModelFormulaStr = '~seurat_clusters',
# cores = 2)
#
# MEPcds_ordering_genes <- row.names(cluster_DEG)[order(cluster_DEG$qval)][1:1000]
#
# MEPcds <- setOrderingFilter(MEPcds, ordering_genes = MEPcds_ordering_genes)
MEPcds <- reduceDimension(MEPcds, method = "DDRTree")
MEPcds <- orderCells(MEPcds)
plot_cell_trajectory(MEPcds, color_by = "cluster_IDs")
plot_cell_trajectory(MEPcds, color_by = 'condition')
plot_cell_trajectory(MEPcds, color_by = 'celltype')
```
```{r adding seurat subclusters}
mks <- readRDS('data/MK-MEP_subclustering.rds')
# Checking to see if the cells are ordered the same way
summary(tstrsplit(pData(MEPcds)$cell, '-', keep = 1)[[1]] == rownames(mks@meta.data))
# Adding a column for the seurat subclusters
pData(MEPcds)[,'seurat_clusters'] <- mks$seurat_clusters
plot_cell_trajectory(MEPcds, color_by = 'seurat_clusters')
```