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