a b/Figures/clonevol/clonevol_P21_081118.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
20
21
###P21, 2 biopies, nFL
22
#T1:LN_neck_unknown
23
#T2:LN_left_inguinal
24
25
26
27
setwd("G:/FL_resequncing/FL_exome_final/fl_latest/11_pyclone/BED_bam/counts_new/pyclone_output_301018")
28
29
pyclone_out<- read.table(file="ouput_pyclone/output_200X_i2/P21_200X_i2/tables/loci.tsv",sep="\t",header=T)
30
31
mutations<-cast(pyclone_out[,1:4], mutation_id~sample_id, value="cellular_prevalence")
32
id<-unique(pyclone_out[, c(1,3)] )
33
34
P_case<- merge(id, mutations,by="mutation_id")
35
36
vafs = data.frame(cluster=P_case$cluster_id,
37
                  T1_vaf=(P_case$`GCF0150-0021-T01`/2)*100,
38
                  T2_vaf=(P_case$`GCF0150-0021-T02`/2)*100,
39
                  stringsAsFactors=F)
40
41
vafs$cluster<- vafs$cluster+1
42
43
###needs manully check density plot to identify which cluster is founding cluster
44
##clonevol requires founding cluster=1
45
46
max_id<- max(vafs$cluster)+1
47
48
vafs$cluster[vafs$cluster==1]<-max_id
49
vafs$cluster[vafs$cluster==3]<-1
50
vafs$cluster[vafs$cluster==max_id]<-3
51
52
53
samples<-c("P21_1","P21_2")
54
samples1<-c("P21_1\nPretreat1","P21_2\nPretreat2")
55
56
57
58
names(vafs)[2:3] = samples 
59
##step 2: run infer.clonal.models, run twice: 1. include all cluster. 
60
###########2. manual review density plot and exlcude cluster with small number of mutations
61
62
63
dir.create("./clonevol/P21")
64
##
65
66
67
##first: use all clusters, no consensus models
68
69
vafs$P21_2[vafs$cluster=="2"]<-vafs$P21_2[vafs$cluster=="2"]-1
70
71
res = infer.clonal.models(variants=vafs, cluster.col.name="cluster", vaf.col.names=samples,
72
                          
73
                          subclonal.test="bootstrap", subclonal.test.model="non-parametric",
74
                          founding.cluster=1,
75
                          cluster.center="mean", num.boots=1000,
76
                          min.cluster.vaf=0.01, sum.p=0.01, alpha=0.01)
77
78
res = infer.clonal.models(variants=vafs, cluster.col.name="cluster", vaf.col.names=samples,
79
                          
80
                          subclonal.test="bootstrap", subclonal.test.model="non-parametric",
81
                          founding.cluster=1,ignore.clusters = c(5,6,8:10),
82
                          cluster.center="mean", num.boots=1000,
83
                          min.cluster.vaf=0.01, sum.p=0.01, alpha=0.01)
84
85
86
#Finding consensus models across samples...
87
#Found  0 consensus model(s)
88
#Found 0 consensus model(s)
89
90
91
##second: ignore cluster 5:7 consensus model based on manual review
92
93
94
vafs_used<- subset(vafs, !cluster %in% c(5,6,8:10))
95
96
vafs_used$cluster[vafs_used$cluster=="7"]<-5
97
98
99
res = infer.clonal.models(variants=vafs_used, cluster.col.name="cluster", vaf.col.names=samples,
100
                          subclonal.test="bootstrap", subclonal.test.model="non-parametric",
101
                          founding.cluster=1, 
102
                          cluster.center="mean", num.boots=1000,
103
                          min.cluster.vaf=0.01, sum.p=0.01, alpha=0.01)
104
105
106
107
res<-convert.consensus.tree.clone.to.branch(res, branch.scale = 'sqrt')
108
109
pdf("./clonevol/P21/P21_trees.pdf", useDingbats = FALSE)
110
plot.all.trees.clone.as.branch(res, branch.width = 0.5, node.size = 1, node.label.size = 0.5)
111
dev.off()
112
113
114
plot.clonal.models(res,
115
                   # box plot parameters
116
                   box.plot = TRUE,
117
                   fancy.boxplot = TRUE,
118
                   fancy.variant.boxplot.highlight = 'is.driver',
119
                   fancy.variant.boxplot.highlight.shape = 21,
120
                   fancy.variant.boxplot.highlight.fill.color = 'red',
121
                   fancy.variant.boxplot.highlight.color = 'black',
122
                   fancy.variant.boxplot.highlight.note.col.name = 'gene',
123
                   fancy.variant.boxplot.highlight.note.color = 'blue',
124
                   fancy.variant.boxplot.highlight.note.size = 2,
125
                   fancy.variant.boxplot.jitter.alpha = 1,
126
                   fancy.variant.boxplot.jitter.center.color = 'grey50',
127
                   fancy.variant.boxplot.base_size = 12,
128
                   fancy.variant.boxplot.plot.margin = 1,
129
                   fancy.variant.boxplot.vaf.suffix = '.VAF',
130
                   # bell plot parameters
131
                   clone.shape = 'bell',
132
                   bell.event = TRUE,
133
                   bell.event.label.color = 'blue',
134
                   bell.event.label.angle = 60,
135
                   clone.time.step.scale = 1,
136
                   bell.curve.step = 2,
137
                   # node-based consensus tree parameters
138
                   merged.tree.plot = TRUE,
139
                   tree.node.label.split.character = NULL,
140
                   tree.node.shape = 'circle',
141
                   tree.node.size = 30,
142
                   tree.node.text.size = 0.5,
143
                   merged.tree.node.size.scale = 1.25,
144
                   merged.tree.node.text.size.scale = 2,
145
                   merged.tree.cell.frac.ci = FALSE,
146
                   # branch-based consensus tree parameters
147
                   merged.tree.clone.as.branch = TRUE,
148
                   mtcab.event.sep.char = ',',
149
                   mtcab.branch.text.size = 1,
150
                   mtcab.branch.width = 0.75,
151
                   mtcab.node.size = 3,
152
                   mtcab.node.label.size = 1,
153
                   mtcab.node.text.size = 1.5,
154
                   # cellular population parameters
155
                   cell.plot = TRUE,
156
                   num.cells = 100,
157
                   cell.border.size = 0.25,
158
                   cell.border.color = 'black',
159
                   clone.grouping = 'horizontal',
160
                   #meta-parameters
161
                   scale.monoclonal.cell.frac = TRUE,
162
                   show.score = FALSE,
163
                   cell.frac.ci = TRUE,
164
                   disable.cell.frac = FALSE,
165
                   # output figure parameters
166
                   out.dir = './clonevol/P21/',
167
                   out.format = 'pdf',
168
                   overwrite.output = TRUE,
169
                   width = 10,
170
                   height = 4,
171
                   # vector of width scales for each panel from left to right
172
                   panel.widths = c(1.5,2.5,1.5,2.5,2))
173
174
###removing cell.frac annotation
175
176
plot.clonal.models(res,
177
                   # box plot parameters
178
                   box.plot = TRUE,
179
                   fancy.boxplot = TRUE,
180
                   fancy.variant.boxplot.highlight = 'is.driver',
181
                   fancy.variant.boxplot.highlight.shape = 21,
182
                   fancy.variant.boxplot.highlight.fill.color = 'red',
183
                   fancy.variant.boxplot.highlight.color = 'black',
184
                   fancy.variant.boxplot.highlight.note.col.name = 'gene',
185
                   fancy.variant.boxplot.highlight.note.color = 'blue',
186
                   fancy.variant.boxplot.highlight.note.size = 2,
187
                   fancy.variant.boxplot.jitter.alpha = 1,
188
                   fancy.variant.boxplot.jitter.center.color = 'grey50',
189
                   fancy.variant.boxplot.base_size = 12,
190
                   fancy.variant.boxplot.plot.margin = 1,
191
                   fancy.variant.boxplot.vaf.suffix = '.VAF',
192
                   # bell plot parameters
193
                   clone.shape = 'bell',
194
                   bell.event = TRUE,
195
                   bell.event.label.color = 'blue',
196
                   bell.event.label.angle = 60,
197
                   clone.time.step.scale = 1,
198
                   bell.curve.step = 2,
199
                   # node-based consensus tree parameters
200
                   merged.tree.plot = TRUE,
201
                   tree.node.label.split.character = NULL,
202
                   tree.node.shape = 'circle',
203
                   tree.node.size = 30,
204
                   tree.node.text.size = 0.5,
205
                   merged.tree.node.size.scale = 1.25,
206
                   merged.tree.node.text.size.scale = 2,
207
                   merged.tree.cell.frac.ci = FALSE,
208
                   # branch-based consensus tree parameters
209
                   merged.tree.clone.as.branch = TRUE,
210
                   mtcab.event.sep.char = ',',
211
                   mtcab.branch.text.size = 1,
212
                   mtcab.branch.width = 0.75,
213
                   mtcab.node.size = 3,
214
                   mtcab.node.label.size = 1,
215
                   mtcab.node.text.size = 1.5,
216
                   # cellular population parameters
217
                   cell.plot = TRUE,
218
                   num.cells = 100,
219
                   cell.border.size = 0.25,
220
                   cell.border.color = 'black',
221
                   clone.grouping = 'horizontal',
222
                   #meta-parameters
223
                   scale.monoclonal.cell.frac = TRUE,
224
                   show.score = FALSE,
225
                   cell.frac.ci = TRUE,
226
                   disable.cell.frac = TRUE,
227
                   # output figure parameters
228
                   out.dir = './clonevol/P21/',
229
                   out.format = 'pdf',
230
                   overwrite.output = TRUE,
231
                   width = 10,
232
                   height = 4,
233
                   # vector of width scales for each panel from left to right
234
                   panel.widths = c(1.5,2.5,1.5,2.5,2))
235
236
237
##generating fish plot
238
f<- generateFishplotInputs(results = res)
239
fishes=createFishPlotObjects(f)
240
241
samples2<-c("P21_1\nPrimary\nLN_neck_unknown","P21_2\nPretreat\nLN_left_inguinal")
242
243
244
pdf('./clonevol/P21/P21_fish_200x_pyclone_anno_loc.pdf', width=14, height=7)
245
for (i in 1:length(fishes)){
246
  
247
  fish = layoutClones(fishes[[i]])
248
  fish = setCol(fish,f$clonevol.clone.colors)
249
  fishPlot(fish,shape="spline", title.btm="P1", cex.title=0.7,cex.vlab = 1.4,
250
           vlines=seq(1, length(samples2)), vlab=samples2, pad.left=0.5)
251
}
252
dev.off()
253
254
255
256
pdf("./clonevol/P21/P21_box.pdf", width=3, height=3,useDingbats = FALSE, title='')
257
pp<-plot.variant.clusters(vafs_used,
258
                          cluster.col.name = 'cluster',
259
                          show.cluster.size = FALSE,
260
                          cluster.size.text.color = 'blue',
261
                          vaf.col.names = samples,
262
                          vaf.limits = 70,
263
                          sample.title.size = 20,
264
                          violin = FALSE,
265
                          box = FALSE,
266
                          jitter = TRUE,
267
                          jitter.shape = 1,
268
                          
269
                          jitter.size = 3,
270
                          jitter.alpha = 1,
271
                          jitter.center.method = 'median',
272
                          jitter.center.size = 1,
273
                          jitter.center.color = 'darkgray',
274
                          jitter.center.display.value = 'none',
275
                          highlight = 'is.driver',
276
                          highlight.shape = 21,
277
                          highlight.color = 'blue',
278
                          highlight.fill.color = 'green',
279
                          highlight.note.col.name = 'gene',
280
                          highlight.note.size = 2,
281
                          order.by.total.vaf = FALSE)
282
283
dev.off()
284
285
286
plot.pairwise(vafs_used, col.names = samples,
287
              out.prefix = './clonevol/P21/P21_variants.pairwise.plot')
288
289
pdf('./clonevol/P21/P21_flow.pdf')
290
plot.cluster.flow(vafs_used, vaf.col.names = samples,
291
                  sample.names = c('Pretreat1', 'Pretreat2'))
292
dev.off()
293
294
####checking coverage f
295
296
Pcase<-do.call("rbind", lapply( list.files("input_pyclone_271018_newpara/200X/GCF0150-0021-N01_200X/",full=TRUE),
297
                                read.table, header=TRUE, sep="\t"))
298
299
300
#P21_1<- read.table(file="input_pyclone_271018_newpara/200X/GCF0150-0021-N01_200X/GCF0150-0021-T01.txt",sep="\t",header=T)
301
#P21_2<- read.table(file="input_pyclone_271018_newpara/200X/GCF0150-0021-N01_200X/GCF0150-0021-T02.txt",sep="\t",header=T)
302
#Pcase<- rbind(P21_1, P21_2)
303
304
305
306
########
307
#min (dp) =min(c1inP4_1$var_counts+c1inP4_1$ref_counts)=273
308
309
#max (dp) =max(c1inP4_1$var_counts+c1inP4_1$ref_counts)=648
310
#median (dp) =median(c1inP4_1$var_counts+c1inP4_1$ref_counts)=380
311
#mean (dp) =mean(c1inP4_1$var_counts+c1inP4_1$ref_counts)=417
312
313
library(ggplot2)
314
315
316
317
pdf("clonevol/P21/coverage.pdf")
318
ggplot(Pcase, aes(x=(var_counts+ref_counts)))+
319
  geom_histogram(position="dodge")+
320
  facet_grid(~sample)
321
dev.off()
322
323
324