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