-
Notifications
You must be signed in to change notification settings - Fork 2
/
monocle_pseudotime.R
238 lines (171 loc) · 10 KB
/
monocle_pseudotime.R
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
library(monocle)
library(Seurat)
library(ggplot2)
integrated = readRDS("integrated_3TIP_new.RDS")
DimPlot(integrated)
integrated$CellType = Idents(integrated)
var_genes <- integrated[["integrated"]]@var.features
#Extract data, phenotype data, and feature data from the SeuratObject
data <- as(as.matrix(integrated@assays$RNA@data), 'sparseMatrix')
pd <- new('AnnotatedDataFrame', data = integrated@meta.data)
fData <- data.frame(gene_short_name = row.names(data), row.names = row.names(data))
fd <- new('AnnotatedDataFrame', data = fData)
#Construct monocle cds
i_monocle2 <- newCellDataSet(data,
phenoData = pd,
featureData = fd,
#lowerDetectionLimit = 0.5,
expressionFamily = uninormal())# since I have already normalized, thresholded and scaled data
var_genes <- integrated[["integrated"]]@var.features
ordering_genes <- var_genes
i_monocle2 <- setOrderingFilter(i_monocle2, ordering_genes)
i_monocle2 <- reduceDimension(i_monocle2, max_components = 2,
method = 'DDRTree', norm_method="none", pseudo_expr=0,scaling=TRUE)
## black and white heatmap State vs CellType
i_monocle2 <- orderCells(i_monocle2)
st_ce_table = pData(i_monocle2)[, c('State', 'CellType')]
st_ce_table$CellType <- factor(st_ce_table$CellType)
state_cluster_stat = table(st_ce_table)
state_cluster_stat <- apply(state_cluster_stat, 2, function(x) x / sum(x))
state_cluster_stat_ordered <- t(state_cluster_stat)
PH <- pheatmap::pheatmap(state_cluster_stat_ordered,
cluster_cols = F,
cluster_rows = F,
color = colorRampPalette(RColorBrewer::brewer.pal(n=9, name='Greys'))(100),
fontsize = 30, fontsize_row = 30, fontsize_col = 30,cellwidth=30,cellheight = 30 )
pdf("Trajectory_HeatMap_BW.pdf", 20,10)
PH
dev.off()
#saveRDS(i_monocle2, "Bonanomi_trajectory.rds")
#i_monocle2 = readRDS("Bonanomi_trajectory.rds")
#color by celltype
P1 = plot_cell_trajectory(i_monocle2, color_by = "CellType", cell_name_size = 8) +
guides(color = guide_legend(override.aes = list(size = 4))) +
theme(legend.text = element_text(size=10)) +
theme(legend.title = element_text(size=10, face="bold")) + scale_color_manual(values=c('ARTERIAL' = '#0066FF',
'BARR_END_CAP' = '#336666',
'CAPILLARY_PLVAP-' = '#399933',
'CAPILLARY_PLVAP+' = '#99CC33',
'IMMATURE' = '#6600CC',
'PROLIFERATING' = '#FF99CC',
'TIP' = '#FF00FF',
'INTERMEDIATE' = 'grey',
'VENOUS_PLVAP-' = '#990000',
'VENOUS_PLVAP+' = '#FF6666'))
#Fig1 O
pdf("Trajectory_CellType.pdf")
P1
dev.off()
#color by State
P3 = plot_cell_trajectory(i_monocle2, color_by = "State")+
guides(color = guide_legend(override.aes = list(size = 6))) +
theme(legend.text = element_text(size=20)) +
theme(legend.title = element_text(size=20,face="bold")) + scale_color_manual(values=c('1' = '#535D87',
'2' = '#7C5D67',
'3' = '#808B96'))
#Fig1 O
pdf("Trajectory_state.pdf")
P3
dev.off()
#color and wrap by CellType
P4 = plot_cell_trajectory(i_monocle2, color_by = "CellType") + facet_wrap(~CellType, nrow = 1)+
guides(color = guide_legend(override.aes = list(size = 6))) +
theme(legend.text = element_text(size=20)) +
theme(text = element_text(size=20)) +
theme(legend.title = element_text(size=20,face="bold")) + scale_color_manual(values=c('ARTERIAL' = '#0066FF',
'BARR_END_CAP' = '#336666',
'CAPILLARY_PLVAP-' = '#399933',
'IMMATURE' = '#6600CC',
'PROLIFERATING' = '#FF99CC',
'TIP' = '#FF00FF',
'INTERMEDIATE' = 'grey',
'VENOUS_PLVAP-' = '#990000',
'VENOUS_PLVAP+' = '#FF6666'))
pdf("Bonanomi_trajectory_CellType_wrap.pdf", 30, 10)
P4
dev.off()
# impose trajectory start in proliferating cells for pseudotime
GM_state <- function(cds){
if (length(unique(cds@phenoData@data$State)) > 1){
T0_counts <- table(cds@phenoData@data$State, cds@phenoData@data$CellType)[,"PROLIFERATING"]
return(as.numeric(names(T0_counts)[which
(T0_counts == max(T0_counts))]))
} else {
return (1)
}
}
i_monocle2 <- orderCells(i_monocle2, root_state = GM_state(i_monocle2))
# color by pseudotime
P6 = plot_cell_trajectory(i_monocle2, color_by = "Pseudotime") +
guides(color = guide_legend(override.aes = list(size = 6))) +
theme(legend.text = element_text(size=20)) +
theme(text = element_text(size=20)) +
theme(legend.title = element_text(size=20,face="bold"))
#Fig1 O
pdf("Trajectory_Pseudotime.pdf")
P6
dev.off()
#Fig1 R
my_genes <- row.names(subset(fData(i_monocle2),
gene_short_name %in% c("Car4", "Mfsd2a", "Mki67", "Apln", "Itgb4", "Ackr1")))
cds_subset <- i_monocle2[my_genes,]
pdf("plot_genes_branched_pseudotime_Clust6.pdf")
plot_genes_branched_pseudotime(cds_subset,
branch_point = 1,
color_by = "CellType",
branch_labels = c("Arterial-Barrier","Venous"),
ncol = 1) + scale_color_manual(values=c('ARTERIAL' = '#0066FF',
'BARR_END_CAP' = '#336666',
'CAPILLARY_PLVAP-' = '#399933',
'CAPILLARY_PLVAP+' = '#99CC33',
'TIP_1' = '#6600CC',
'TIP_2' = '#FF99CC',
'TIP_3' = '#FF00FF',
'eight' = 'grey',
'VENOUS_PLVAP-' = '#990000',
'VENOUS_PLVAP+' = '#FF6666'))+
theme(legend.text = element_text(size=20)) +
theme(text = element_text(size=20)) +
theme(legend.title = element_text(size=20,face="bold"))
dev.off()
# use the beam function to generate a table of significance scores for each gene.
# Genes that score significant are said to be branch-dependent in their expression.
i_monocle2_res <- BEAM(i_monocle2, branch_point = 1, cores = 32)
i_monocle2_res <- i_monocle2_res[order(i_monocle2_res$qval),]
i_monocle2_res <- i_monocle2_res[,c("gene_short_name", "pval", "qval")]
write.table(i_monocle2_res, "Bonanomi_BEAM.txt")
#save.image(file = "my_work_space_bonanomi.RData")
#load("my_work_space_bonanomi.RData")
# write a table with gene names reported in heatmap rows
plot_table <- plot_genes_branched_heatmap(i_monocle2[var_genes,],
branch_point = 1,
cores = 32,
num_clusters = 6,
use_gene_short_name = T,
return_heatmap = TRUE,
show_rownames = T)
write.table(plot_table$annotation_row, 'Bonanomi_trajectory_Heatmap_branch_point_small_2pc_bp1_2000variablegenes_6clust.txt')
# generate heatmap
#Fig1 P
# small without gene names
pdf("Bonanomi_trajectory_Heatmap_branch_point_2pc_bp1_2000variablegenes_6clust.pdf", 20, 220)
plot_genes_branched_heatmap(i_monocle2[var_genes,],
branch_point = 1,
cores = 32,
num_clusters = 5,
branch_colors = c("#619CFF","#F8766D","#00BA38"),
use_gene_short_name = T,
show_rownames = T)
dev.off()
# big with gene names
pdf("Bonanomi_trajectory_Heatmap_branch_point_2pc_bp1_2000variablegenes_6clust.pdf", 20, 220)
plot_genes_branched_heatmap(i_monocle2[var_genes,],
branch_point = 1,
cores = 32,
num_clusters = 3,
branch_colors = c("#619CFF","#F8766D","#00BA38"),
use_gene_short_name = T,
show_rownames = T)
dev.off()
#saveRDS(br_monocle2, "br_monocle2.RDS")
#write.table(plot_table$annotation_row, 'plot_newheatmap_br_subset_2pc_bp2_Cd34.txt')