a b/Figures/clonevol/clonevol_P2_301018.R
1
library(dplyr)
2
library(plyr)
3
library(clonevol)
4
library(fishplot)
5
library(reshape)
6
7
####coverage: from reseq BAM files 
8
####step1: Using mpileup to extract coverage for each site. (Prepared 27/10/18), default parameter
9
10
####step2: Prepare input for pyclone, using segments from ASCAT
11
12
####step3: Run Pyclone twice. The first run is to identify founding cluster, 
13
####       the second run adding --tumour_content based on the cellular prevalence for each biopsy (29/10/18)
14
15
###step4: using clonevol to build evolution tree and model (in this script) (30/10/18)
16
17
###       using Fishplot to visulaize
18
19
##P1: 2 biopsies, clinic transformed
20
21
#T1:LN_right_neck
22
#T2:LN_left_inguinal
23
24
setwd("F:/FL_exome_final/fl_latest/11_pyclone/BED_bam/counts_new/pyclone_output_301018/")
25
26
pyclone_out<- read.table(file="ouput_pyclone/output_200X_i2/GCF0150-0002-N01_200X_i2/tables/loci.tsv",sep="\t",header=T)
27
28
mutations<-cast(pyclone_out[,1:4], mutation_id~sample_id, value="cellular_prevalence")
29
id<-unique(pyclone_out[, c(1,3)] )
30
31
P_case<- merge(id, mutations,by="mutation_id")
32
33
vafs = data.frame(cluster=P_case$cluster_id,
34
                  T1_vaf=(P_case$`GCF0150-0002-T01`/2)*100,
35
                  T2_vaf=(P_case$`GCF0150-0002-T02`/2)*100,
36
                  stringsAsFactors=F)
37
38
vafs$cluster<- vafs$cluster+1
39
40
###needs manully check density plot to identify which cluster is founding cluster
41
##clonevol requires founding cluster=1
42
##in this case founding cluster is #3
43
44
max_id<- max(vafs$cluster)+1
45
46
vafs$cluster[vafs$cluster==1]<-max_id
47
vafs$cluster[vafs$cluster==2]<-1
48
vafs$cluster[vafs$cluster==max_id]<-2
49
50
51
52
samples<-c("P2_1","P2_2")
53
samples2<- c("P2_1\nPrimary\nLN_right_neck", "P2_2\nRelapsed\nLN_right_inguinal")
54
55
names(vafs)[2:3] = samples 
56
##step 2: run infer.clonal.models, run twice: 1. include all cluster. 
57
###########2. manual review density plot and exlcude cluster with small number of mutations
58
59
60
dir.create("./clonevol/P2")
61
##
62
63
64
##first: use all clusters, no consensus models
65
66
res = infer.clonal.models(variants=vafs, cluster.col.name="cluster", vaf.col.names=samples,
67
                          
68
                          subclonal.test="bootstrap", subclonal.test.model="non-parametric",
69
                          founding.cluster=1,
70
                          cluster.center="mean", num.boots=1000,
71
                          min.cluster.vaf=0.01, sum.p=0.01, alpha=0.01)
72
73
74
#Finding consensus models across samples...
75
#Found  0 consensus model(s)
76
#Found 0 consensus model(s)
77
78
79
##second: ignore cluster 5:7 consensus model based on manual review
80
81
##to be able to run clonevol, have to change the cellular_prevalance
82
83
vafs_used<- subset(vafs, !cluster %in% c(4:6))
84
85
86
87
res = infer.clonal.models(variants=vafs_used, cluster.col.name="cluster", vaf.col.names=samples,
88
                          subclonal.test="bootstrap", subclonal.test.model="non-parametric",
89
                          founding.cluster=1,
90
                          cluster.center="mean", num.boots=1000,
91
                          min.cluster.vaf=0.02, sum.p=0.01, alpha=0.01)
92
93
94
95
res<-convert.consensus.tree.clone.to.branch(res, branch.scale = 'sqrt')
96
97
pdf("./clonevol/P2/P2_trees.pdf", useDingbats = FALSE)
98
plot.all.trees.clone.as.branch(res, branch.width = 0.5, node.size = 1, node.label.size = 0.5)
99
dev.off()
100
101
102
103
plot.clonal.models(res,
104
                   # box plot parameters
105
                   box.plot = TRUE,
106
                   fancy.boxplot = TRUE,
107
                   fancy.variant.boxplot.highlight = 'is.driver',
108
                   fancy.variant.boxplot.highlight.shape = 21,
109
                   fancy.variant.boxplot.highlight.fill.color = 'red',
110
                   fancy.variant.boxplot.highlight.color = 'black',
111
                   fancy.variant.boxplot.highlight.note.col.name = 'gene',
112
                   fancy.variant.boxplot.highlight.note.color = 'blue',
113
                   fancy.variant.boxplot.highlight.note.size = 2,
114
                   fancy.variant.boxplot.jitter.alpha = 1,
115
                   fancy.variant.boxplot.jitter.center.color = 'grey50',
116
                   fancy.variant.boxplot.base_size = 12,
117
                   fancy.variant.boxplot.plot.margin = 1,
118
                   fancy.variant.boxplot.vaf.suffix = '.VAF',
119
                   # bell plot parameters
120
                   clone.shape = 'bell',
121
                   bell.event = TRUE,
122
                   bell.event.label.color = 'blue',
123
                   bell.event.label.angle = 60,
124
                   clone.time.step.scale = 1,
125
                   bell.curve.step = 2,
126
                   # node-based consensus tree parameters
127
                   merged.tree.plot = TRUE,
128
                   tree.node.label.split.character = NULL,
129
                   tree.node.shape = 'circle',
130
                   tree.node.size = 30,
131
                   tree.node.text.size = 0.5,
132
                   merged.tree.node.size.scale = 1.25,
133
                   merged.tree.node.text.size.scale = 2,
134
                   merged.tree.cell.frac.ci = FALSE,
135
                   # branch-based consensus tree parameters
136
                   merged.tree.clone.as.branch = TRUE,
137
                   mtcab.event.sep.char = ',',
138
                   mtcab.branch.text.size = 1,
139
                   mtcab.branch.width = 0.75,
140
                   mtcab.node.size = 3,
141
                   mtcab.node.label.size = 1,
142
                   mtcab.node.text.size = 1.5,
143
                   # cellular population parameters
144
                   cell.plot = TRUE,
145
                   num.cells = 100,
146
                   cell.border.size = 0.25,
147
                   cell.border.color = 'black',
148
                   clone.grouping = 'horizontal',
149
                   #meta-parameters
150
                   scale.monoclonal.cell.frac = TRUE,
151
                   show.score = FALSE,
152
                   cell.frac.ci = TRUE,
153
                   disable.cell.frac = TRUE,
154
                   # output figure parameters
155
                   out.dir = './clonevol/P2/',
156
                   out.format = 'pdf',
157
                   overwrite.output = TRUE,
158
                   width = 10,
159
                   height = 4,
160
                   # vector of width scales for each panel from left to right
161
                   panel.widths = c(1.5,2.5,1.5,2.5,2))
162
163
164
165
plot.clonal.models(res,
166
                   # box plot parameters
167
                   box.plot = TRUE,
168
                   fancy.boxplot = TRUE,
169
                   fancy.variant.boxplot.highlight = 'is.driver',
170
                   fancy.variant.boxplot.highlight.shape = 21,
171
                   fancy.variant.boxplot.highlight.fill.color = 'red',
172
                   fancy.variant.boxplot.highlight.color = 'black',
173
                   fancy.variant.boxplot.highlight.note.col.name = 'gene',
174
                   fancy.variant.boxplot.highlight.note.color = 'blue',
175
                   fancy.variant.boxplot.highlight.note.size = 2,
176
                   fancy.variant.boxplot.jitter.alpha = 1,
177
                   fancy.variant.boxplot.jitter.center.color = 'grey50',
178
                   fancy.variant.boxplot.base_size = 12,
179
                   fancy.variant.boxplot.plot.margin = 1,
180
                   fancy.variant.boxplot.vaf.suffix = '.VAF',
181
                   # bell plot parameters
182
                   clone.shape = 'bell',
183
                   bell.event = TRUE,
184
                   bell.event.label.color = 'blue',
185
                   bell.event.label.angle = 60,
186
                   clone.time.step.scale = 1,
187
                   bell.curve.step = 2,
188
                   # node-based consensus tree parameters
189
                   merged.tree.plot = TRUE,
190
                   tree.node.label.split.character = NULL,
191
                   tree.node.shape = 'circle',
192
                   tree.node.size = 30,
193
                   tree.node.text.size = 0.5,
194
                   merged.tree.node.size.scale = 1.25,
195
                   merged.tree.node.text.size.scale = 2,
196
                   merged.tree.cell.frac.ci = FALSE,
197
                   # branch-based consensus tree parameters
198
                   merged.tree.clone.as.branch = TRUE,
199
                   mtcab.event.sep.char = ',',
200
                   mtcab.branch.text.size = 1,
201
                   mtcab.branch.width = 0.75,
202
                   mtcab.node.size = 3,
203
                   mtcab.node.label.size = 1,
204
                   mtcab.node.text.size = 1.5,
205
                   # cellular population parameters
206
                   cell.plot = TRUE,
207
                   num.cells = 100,
208
                   cell.border.size = 0.25,
209
                   cell.border.color = 'black',
210
                   clone.grouping = 'horizontal',
211
                   #meta-parameters
212
                   scale.monoclonal.cell.frac = TRUE,
213
                   show.score = FALSE,
214
                   cell.frac.ci = TRUE,
215
                   disable.cell.frac = FALSE,
216
                   # output figure parameters
217
                   out.dir = './clonevol/P2/',
218
                   out.format = 'pdf',
219
                   overwrite.output = TRUE,
220
                   width = 10,
221
                   height = 4,
222
                   # vector of width scales for each panel from left to right
223
                   panel.widths = c(1.5,2.5,1.5,2.5,2))
224
225
###removing cell.frac annotation
226
227
228
229
plot.clonal.models(res,
230
                   # box plot parameters
231
                   box.plot = TRUE,
232
                   fancy.boxplot = TRUE,
233
                   fancy.variant.boxplot.highlight = 'is.driver',
234
                   fancy.variant.boxplot.highlight.shape = 21,
235
                   fancy.variant.boxplot.highlight.fill.color = 'red',
236
                   fancy.variant.boxplot.highlight.color = 'black',
237
                   fancy.variant.boxplot.highlight.note.col.name = 'gene',
238
                   fancy.variant.boxplot.highlight.note.color = 'blue',
239
                   fancy.variant.boxplot.highlight.note.size = 2,
240
                   fancy.variant.boxplot.jitter.alpha = 1,
241
                   fancy.variant.boxplot.jitter.center.color = 'grey50',
242
                   fancy.variant.boxplot.base_size = 12,
243
                   fancy.variant.boxplot.plot.margin = 1,
244
                   fancy.variant.boxplot.vaf.suffix = '.VAF',
245
                   # bell plot parameters
246
                   clone.shape = 'bell',
247
                   bell.event = TRUE,
248
                   bell.event.label.color = 'blue',
249
                   bell.event.label.angle = 60,
250
                   clone.time.step.scale = 1,
251
                   bell.curve.step = 2,
252
                   # node-based consensus tree parameters
253
                   merged.tree.plot = TRUE,
254
                   tree.node.label.split.character = NULL,
255
                   tree.node.shape = 'circle',
256
                   tree.node.size = 30,
257
                   tree.node.text.size = 0.5,
258
                   merged.tree.node.size.scale = 1.25,
259
                   merged.tree.node.text.size.scale = 2,
260
                   merged.tree.cell.frac.ci = FALSE,
261
                   # branch-based consensus tree parameters
262
                   merged.tree.clone.as.branch = TRUE,
263
                   mtcab.event.sep.char = ',',
264
                   mtcab.branch.text.size = 1,
265
                   mtcab.branch.width = 0.75,
266
                   mtcab.node.size = 3,
267
                   mtcab.node.label.size = 1,
268
                   mtcab.node.text.size = 1.5,
269
                   # cellular population parameters
270
                   cell.plot = TRUE,
271
                   num.cells = 100,
272
                   cell.border.size = 0.25,
273
                   cell.border.color = 'black',
274
                   clone.grouping = 'horizontal',
275
                   #meta-parameters
276
                   scale.monoclonal.cell.frac = TRUE,
277
                   show.score = FALSE,
278
                   cell.frac.ci = TRUE,
279
                   disable.cell.frac = TRUE,
280
                   # output figure parameters
281
                   out.dir = './clonevol/P1/',
282
                   out.format = 'pdf',
283
                   overwrite.output = TRUE,
284
                   width = 10,
285
                   height = 4,
286
                   # vector of width scales for each panel from left to right
287
                   panel.widths = c(1.5,2.5,1.5,2.5,2))
288
289
290
##generating fish plot
291
f<- generateFishplotInputs(results = res)
292
fishes=createFishPlotObjects(f)
293
294
295
296
297
298
299
pdf('./clonevol/P2/P2_fish_200x_pyclone_anno_new.pdf', width=14, height=7)
300
for (i in 1:length(fishes)){
301
  
302
  fish = layoutClones(fishes[[i]])
303
  fish = setCol(fish,f$clonevol.clone.colors)
304
  fishPlot(fish,shape="spline", title.btm="P1", cex.title=0.7,cex.vlab = 1.4,
305
           vlines=seq(1, length(samples2)), vlab=samples2, pad.left=0.5)
306
}
307
308
dev<-dev.off()
309
310
##other plots
311
312
pdf("./clonevol/P2/P2_box.pdf", width=3, height=3,useDingbats = FALSE, title='')
313
pp<-plot.variant.clusters(vafs_used,
314
                          cluster.col.name = 'cluster',
315
                          show.cluster.size = FALSE,
316
                          cluster.size.text.color = 'blue',
317
                          vaf.col.names = samples,
318
                          vaf.limits = 70,
319
                          sample.title.size = 20,
320
                          violin = FALSE,
321
                          box = FALSE,
322
                          jitter = TRUE,
323
                          jitter.shape = 1,
324
                          
325
                          jitter.size = 3,
326
                          jitter.alpha = 1,
327
                          jitter.center.method = 'median',
328
                          jitter.center.size = 1,
329
                          jitter.center.color = 'darkgray',
330
                          jitter.center.display.value = 'none',
331
                          highlight = 'is.driver',
332
                          highlight.shape = 21,
333
                          highlight.color = 'blue',
334
                          highlight.fill.color = 'green',
335
                          highlight.note.col.name = 'gene',
336
                          highlight.note.size = 2,
337
                          order.by.total.vaf = FALSE)
338
339
dev.off()
340
341
342
343
pdf('./clonevol/P2/P2_flow.pdf')
344
plot.cluster.flow(vafs_used, vaf.col.names = samples,
345
                  sample.names = c('Primary', 'Relapse'))
346
dev.off()
347
348
####checking coverage 
349
350
351
352
P2_1<- read.table(file="input_pyclone_271018_newpara/200X/GCF0150-0002-N01_200X/GCF0150-0002-T01.txt",sep="\t",header=T)
353
P2_2<- read.table(file="input_pyclone_271018_newpara/200X/GCF0150-0002-N01_200X/GCF0150-0002-T02.txt",sep="\t",header=T)
354
P2<- rbind(P2_1, P2_2)
355
356
c1inP1_1<- subset(P1_1, mutation_id %in% unique(c1$mutation_id))
357
358
########
359
#min (dp) =min(c1inP1_1$var_counts+c1inP1_1$ref_counts)=206
360
361
#max (dp) =max(c1inP1_1$var_counts+c1inP1_1$ref_counts)=1500
362
#median (dp) =median(c1inP1_1$var_counts+c1inP1_1$ref_counts)=463
363
#mean (dp) =mean(c1inP1_1$var_counts+c1inP1_1$ref_counts)=544.9
364
365
library(ggplot2)
366
367
ggplot(P1, aes(x=var_counts+ref_counts), group_by=sample)+geom_histogram()
368
369
pdf("clonevol/P2/coverage.pdf")
370
ggplot(P2, aes(x=var_counts+ref_counts, fill=sample))+geom_histogram()
371
372
dev.off()
373