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