-
Notifications
You must be signed in to change notification settings - Fork 2
/
germlineR.R
278 lines (225 loc) · 8.88 KB
/
germlineR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# plot germline vars
library(tidyverse)
library(cowplot)
infile <- 'snps_dirty_combined.txt'
infile <- 'snps_filtered_combined.txt'
snps <- read.delim(infile, header = T)
total <- nrow(snps)
dark_blue <- '#21608A'
mint_green <- '#26AD7E'
red <- '#D9564A'
purple <- '#B57CE6'
colours <- c(dark_blue, mint_green, red, purple)
types_count <- snps %>%
dplyr::group_by(genotype) %>%
dplyr::summarise(count = n(), perc = count/total * 100)
# Percentage contribution of genotypes
# dirty_genotype_contributions
types_count %>% ggplot(.) +
geom_bar(aes(fct_reorder(genotype, -perc), perc), fill=mint_green, alpha=0.6, stat = 'identity') +
scale_y_continuous("Percent contribution") +
theme_bw()
dfsObj <- paste0( "~/Desktop/script_test/Riddiford_et_al_2020/muts.RData")
load(dfsObj)
# Count SNVs per sample
snv_count <- male_snvs %>%
filter(af > 0) %>%
droplevels() %>%
dplyr::mutate(sample = sample_paper) %>%
group_by(sample) %>%
dplyr::summarise(somatic_tumour = n())
# Count SNPs per sample
snp_count <- snps %>%
dplyr::filter(genotype == 'germline_private') %>%
droplevels() %>%
dplyr::mutate(sample = samples) %>%
dplyr::group_by(sample) %>%
dplyr::summarise(germline_private = n())
normal_count <- snps %>%
dplyr::filter(genotype == 'somatic_normal') %>%
droplevels() %>%
dplyr::mutate(sample = samples) %>%
dplyr::group_by(sample) %>%
dplyr::summarise(normal_only = n())
tumour_count <- snps %>%
dplyr::filter(genotype == 'somatic_tumour') %>%
droplevels() %>%
dplyr::mutate(sample = samples) %>%
dplyr::group_by(sample) %>%
dplyr::summarise(tumour_only = n())
gr_depth <- snps %>%
dplyr::filter(genotype == 'germline_private') %>%
droplevels() %>%
dplyr::mutate(depth = as.numeric(as.character(depth)),
sample = samples) %>%
dplyr::select(sample, depth)
# dplyr::group_by(sample) %>%
# summarise(count = n(), av_depth = mean(depth))
order <- levels(fct_reorder(snp_count$sample, -snp_count$germline_private))
gr_depth$sample <- factor(gr_depth$sample, levels = order, labels=order)
depth_p <- gr_depth %>%
dplyr::filter(depth < 100) %>%
ggplot(.) +
geom_boxplot(aes(sample, depth), fill='grey', alpha=0.6) +
scale_y_continuous("Depth") +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
legend.position = 'none'
)
# combined <- plyr::join_all(list(snp_count, normal_count, tumour_count, snv_count), by='sample')
combined <- plyr::join_all(list(snp_count, snv_count), by='sample')
df_long <- tidyr::gather(combined, type, val, germline_private:somatic_tumour, factor_key=TRUE) %>%
dplyr::group_by(sample) %>%
tidyr::complete(type, fill = list(n=0, perc=0))
df_long$val[is.na(df_long$val)] <- 0
df_long$sample <- factor(df_long$sample, levels = order, labels=order)
# Fig 1 - private SNPs vs somatic SNVs
threshold = 200
count_p <- df_long %>%
ggplot(.) +
geom_bar(aes(sample, val, fill=type), alpha=0.6, stat = 'identity') +
geom_hline(aes(yintercept=threshold), color = red, linetype = "solid") +
geom_text(aes(20, threshold,label = threshold, vjust = -1)) +
scale_y_continuous("Mutation count") +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_blank(),
axis.title.y = element_text(size=15),
legend.position = 'none'
) +
scale_fill_manual(values = colours) +
facet_wrap(~type, ncol = 1, scales = 'free_y')
count_p
plot_grid(count_p, depth_p, ncol = 1, rel_heights = c(2,1))
# # plot SNVs per sample
# snv_p <- combined %>%
# ggplot(.) +
# geom_bar(aes(fct_reorder(sample, -snp), snv), fill=mint_green, alpha=0.6, stat = 'identity') +
# scale_y_continuous("Somatic tumour SNV count") +
# theme_bw() +
# theme(
# panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
# axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
# axis.text.y = element_text(size=12),
# axis.title.x = element_blank(),
# axis.title.y = element_blank()
# )
#
# # plot gerSNPs per sample
# snp_p <- combined %>%
# ggplot(.) +
# geom_bar(aes(fct_reorder(sample, -snp), snp), fill=dark_blue, alpha=0.6, stat = 'identity') +
# scale_y_continuous("Private germline SNP count") +
# theme_bw() +
# theme(
# panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
# axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
# axis.text.y = element_text(size=12),
# axis.title.x = element_blank(),
# axis.title.y = element_blank()
# )
#
# normal_p <- combined %>%
# ggplot(.) +
# geom_bar(aes(fct_reorder(sample, -snp), normal), fill=red, alpha=0.6, stat = 'identity') +
# scale_y_continuous("Somatic normal SNV count") +
# theme_bw() +
# theme(
# panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
# axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
# axis.text.y = element_text(size=12),
# axis.title.x = element_blank(),
# axis.title.y = element_blank()
# )
# library(cowplot)
#
# labels = c('Tumour-only', 'Germline-private', 'Normal-only')
# plot_grid(snv_p, snp_p, normal_p, labels=labels, ncol = 1)
# Germline events shared by multiple samples
snps %>%
dplyr::filter(genotype %in% c('germline_private', 'germline_recurrent')) %>%
dplyr::group_by(sharedby) %>%
dplyr::tally() %>%
dplyr::mutate(sharedby = as.factor(sharedby)) %>%
ggplot(.) +
geom_bar(aes(sharedby, n), fill=dark_blue, alpha=0.6, stat = 'identity') +
scale_y_continuous("Number of SNPS") +
scale_x_discrete("Shared between samples") +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1, size=12),
axis.text.y = element_text(size=12),
axis.title =element_text(size=15)
)
# Average depth VS count for germline_private events
snps %>%
dplyr::filter(genotype == 'germline_private') %>%
droplevels() %>%
dplyr::mutate(depth = as.numeric(as.character(depth))) %>%
dplyr::group_by(samples) %>%
summarise(count = n(), av_depth = mean(depth)) %>%
ggplot(.,aes(av_depth, count)) +
stat_summary(fun.data=mean_cl_normal) +
geom_smooth(method='lm', formula= y~x) +
scale_x_continuous("Average depth") +
scale_y_continuous("Germline private SNP count") +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text = element_text(size=12),
axis.title = element_text(size=15)
)
## VAF of SNPs/SNVs
hum_1 <- read.delim('filtered/B241R59_snps.txt', header=T)
gl_p <- hum_1 %>%
dplyr::filter(genotype %in% c('germline')) %>%
ggplot(.) +
geom_density(aes(tumour_vaf, fill=purple), alpha=.6) +
scale_x_continuous("Allele Frequency") +
scale_y_continuous("Density") +
scale_fill_manual(values = purple) +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text = element_text(size=12),
axis.title = element_text(size=15),
legend.position = 'none'
) +
ggtitle("Germline")
snv_p <- male_snvs %>%
ggplot(.) +
geom_density(aes(af, fill=mint_green), alpha=.6) +
scale_x_continuous("Allele Frequency") +
scale_y_continuous("Density") +
scale_fill_manual(values = mint_green) +
theme_bw() +
theme(
panel.grid.major.y = element_line(color = "grey80", size = 0.5, linetype = "dotted"),
axis.text = element_text(size=12),
axis.title = element_text(size=15),
legend.position = 'none'
) +
ggtitle("Somatic")
plot_grid(gl_p, snv_p, ncol = 1)
# Write somatic calls for indels + snvs
male_snvs %>%
dplyr::mutate(genotype = 'somatic_tumour') %>%
dplyr::select(sample_paper, chrom, pos, ref, alt, af, caller, gene, feature, id, fpkm) %>%
dplyr::mutate(sample = sample_paper, allele_frequency = af, calledby = caller) %>%
dplyr::select(sample, chrom, pos, ref, alt, allele_frequency, calledby, gene, feature, id, fpkm) %>%
write.table(., file = 'somatic_snvs.txt', quote=FALSE, sep='\t', row.names = FALSE)
male_indels %>%
dplyr::mutate(genotype = 'somatic_tumour') %>%
dplyr::select(sample_paper, chrom, pos, ref, alt, af, type, caller, gene, feature, id, fpkm) %>%
dplyr::mutate(sample = sample_paper, allele_frequency = af, calledby = caller) %>%
dplyr::select(sample, chrom, pos, ref, alt, type, allele_frequency, calledby, gene, feature, id, fpkm) %>%
write.table(., file = 'somatic_indels.txt', quote=FALSE, sep='\t', row.names = FALSE)