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