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