-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot_error_rate_22_11_2018.R
319 lines (251 loc) · 10.8 KB
/
Plot_error_rate_22_11_2018.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(argparse))
suppressPackageStartupMessages(library(gridExtra))
suppressPackageStartupMessages(library(grid))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(seqinr))
parser <- ArgumentParser()
# setting parameters
parser$add_argument("-bc1", "--bc1_file", type="character", help="Barcode corrected 1", metavar="file", nargs=1, required=TRUE)
parser$add_argument("-bc2", "--bc2_file", type="character", help="Barcode corrected 2", metavar="file", nargs=1, required=TRUE)
parser$add_argument("-bc3", "--bc3_file", type="character", help="Barcode corrected 3", metavar="file", nargs=1, required=TRUE)
parser$add_argument("-bc4", "--bc4_file", type="character", help="Barcode corrected 4 or more", metavar="file", nargs=1, required=TRUE)
parser$add_argument("-out", "--out_folder", type="character", help="out_folder", nargs=1, required=TRUE)
# reading parameters
args <- parser$parse_args()
bc1 <- args$bc1_file
bc2 <- args$bc2_file
bc3 <- args$bc3_file
bc4 <- args$bc4_file
OUTF <- args$out_folder
#### SOME FUNCTIONS
ER_NT <- function(x){
NT <- c("A","C","T","G")
TAB <- NULL
DP <- sum(as.numeric(x$DP))
for (i in NT){
X <- x[x$REF == i,]
Z <- NT[!(NT == i)]
for (y in Z){
COUNT <- tryCatch(as.numeric(sum(X[y])), error = function(e) NA)
TAB2 <- data.frame(i,y,sum(X$DP),COUNT,DP,COUNT/DP,COUNT/sum(X$DP))
colnames(TAB2) <- c("REF","ALT","DP_REF","ALT_COUNT","DP_TOTAL", "ER_TOTAL","ER_REF")
TAB <- rbind(TAB,TAB2)
}
}
return(TAB)
}
NT_CHANGE <- function(x){
x$Change <- apply(x,1,function(x) ifelse(x["REF"] %in% c("A","C"), paste(x["REF"],x["ALT"],sep='>'),paste(comp(x['REF'],forceToLower = F),comp(x['ALT'],forceToLower = F),sep='>')) )
return(x)
}
LOADING <- function(DATA){
NUCLEOTIDES <- c('A','C','T','G', 'INS', 'INSo', 'DEL', 'DELo')
DATA <- as.data.frame(fread(DATA))
DATA$ALT_COUNT <- rowSums(DATA[,NUCLEOTIDES])-DATA$REFf-DATA$REFr
DATA$DP <- rowSums(DATA[,NUCLEOTIDES])
DATA$AB <- DATA$ALT_COUNT/DATA$DP_HQ
#DATA <- DATA[DATA$AB < 0.05 & DATA$DP > 20,]
DATA <- DATA[!(is.na(DATA$ALT_COUNT)),]
return(DATA)
}
ERROR_RATE <- function(DATA){
#DATA$AB <- DATA$ALT_COUNT/DATA$DP
DATA <- DATA[DATA$DP > 20,]
DATA <- DATA[!(is.na(DATA$ALT_COUNT)),]
MEDIAN_DP <- median(DATA$DP)
SUM_DP <- sum(as.numeric(DATA$DP))
SUM_ALT <- sum(as.numeric(DATA$ALT_COUNT))
ER <- SUM_ALT/SUM_DP
OUT <- data.frame(ER, nrow(DATA), SUM_ALT, SUM_DP)
colnames(OUT) <- c('ER', 'Sites', 'ALT_COUNT', 'DP')
return(OUT)
}
COLLAPSED_ER <- function(DATA){
DATA[is.na(DATA)] <- 0
COLLAPSED <- aggregate(cbind(ALT_COUNT,DP_REF) ~ Change, data = DATA, sum)
COLLAPSED$ER <- COLLAPSED$ALT_COUNT/COLLAPSED$DP_REF
return(COLLAPSED)
}
give.n <- function(x){
return(c(y = 0, label = length(x)))
}
## Loading data
# Barcode 1x1
BC1 <- LOADING(bc1)
BC1$Correction_level <- '1x'
# Barcode 1x2
BC2 <- LOADING(bc2)
BC2$Correction_level <- '2x'
# Barcode 2x1
BC3 <- LOADING(bc3)
BC3$Correction_level <- '3x'
# Barcode 2x2
BC4 <- LOADING(bc4)
BC4$Correction_level <- '4x'
## Coverage plots
ALL <- rbind(BC1, BC2, BC3, BC4)
COV_PLOT <- ggplot(ALL, aes(x = Correction_level, y = DP))+
geom_boxplot(outlier.size = -1, color = "dodgerblue3", fill = "lightskyblue2") +
scale_fill_grey() +
stat_summary(aes(x = Correction_level),position=position_dodge(0.90),
fun.data = give.n, geom = "text",
vjust = +1, size = 3.5) +
theme_bw() +
coord_cartesian(ylim=c(0,max((aggregate(DP ~ Correction_level, data = ALL, function(x) quantile(x,0.75) + 1.5* IQR(x)))$DP)))+
labs(x='Correction level', y='Depth of coverage', col='Correction level')+
ggtitle("Depth of coverage per correction level")
## Finding potential germline calls
ALL2 <- aggregate(cbind(DP,ALT_COUNT) ~ CHROM + POS,
data = ALL, sum)
ALL2$AB <- ALL2$ALT_COUNT/ALL2$DP
EXCLUDE <- ALL2[(ALL2$AB >= 0.05 & ALL2$DP > 100) | ALL2$AB >= 0.1,]
rm(ALL)
rm(ALL2)
## Error Rates
# Barcode 1
BC1 <- BC1[!paste(BC1$CHROM, BC1$POS, sep = ';') %in% paste(EXCLUDE$CHROM, EXCLUDE$POS, sep = ';') & BC1$DP > 20 & BC1$AB < 0.1,]
ER1 <- ERROR_RATE(BC1)
ER1$Correction_level <- '1x'
ER1_NT <- ER_NT(BC1)
ER1_NT <- NT_CHANGE(ER1_NT)
ER1_NT <- COLLAPSED_ER(ER1_NT)
ER1_NT$Correction_level <- '1x'
# Barcode 2
BC2 <- BC2[!paste(BC2$CHROM, BC2$POS, sep = ';') %in% paste(EXCLUDE$CHROM, EXCLUDE$POS, sep = ';') & BC2$DP > 20 & BC2$AB < 0.1,]
ER2 <- ERROR_RATE(BC2)
ER2$Correction_level <- '2x'
ER2_NT <- ER_NT(BC2)
ER2_NT <- NT_CHANGE(ER2_NT)
ER2_NT <- COLLAPSED_ER(ER2_NT)
ER2_NT$Correction_level <- '2x'
# Barcode 3
BC3 <- BC3[!paste(BC3$CHROM, BC3$POS, sep = ';') %in% paste(EXCLUDE$CHROM, EXCLUDE$POS, sep = ';') & BC3$DP > 20 & BC3$AB < 0.1,]
ER3 <- ERROR_RATE(BC3)
ER3$Correction_level <- '3x'
ER3_NT <- ER_NT(BC3)
ER3_NT <- NT_CHANGE(ER3_NT)
ER3_NT <- COLLAPSED_ER(ER3_NT)
ER3_NT$Correction_level <- '3x'
# Barcode 4
BC4 <- BC4[!paste(BC4$CHROM, BC4$POS, sep = ';') %in% paste(EXCLUDE$CHROM, EXCLUDE$POS, sep = ';') & BC4$DP > 20 & BC4$AB < 0.1,]
ER4 <- ERROR_RATE(BC4)
ER4$Correction_level <- '4x'
ER4_NT <- ER_NT(BC4)
ER4_NT <- NT_CHANGE(ER4_NT)
ER4_NT <- COLLAPSED_ER(ER4_NT)
ER4_NT$Correction_level <- '4x'
### Getting error rates
ER <- rbind(ER1,ER2,ER3,ER4)
ER$FILTER <- (ER$DP > 1e5)*1
FILTER <- ER[,c('Correction_level', 'FILTER')]
ER_PASS <- ER[ER$FILTER == 1,]
ymax <- round(max(ER_PASS$ER)*1.25,digits = 5)+2E-5
ymax <- ifelse(is.finite(ymax), ymax, 1e-2)
PLOT_ER <- ggplot(ER_PASS,aes(x=Correction_level,y=ER))+
geom_point(color="blue",size=2)+
scale_x_discrete(limits=ER$Correction_level)+
theme_bw()+
#ylim(min(ER$Error_rate)/5,max(ER$Error_rate)*1.25)+
scale_y_continuous(breaks = seq(0,ymax, 2.5E-5))+
ggtitle("Error rate per analysis type")+
#theme(axis.text.x = element_text(angle = 45, hjust = 1))+
xlab("Error correction type")+
ylab("Error rate")
### Error rates per nucleotide
### Grouping all together
ER_NT_GROUP <- rbind(ER1_NT,ER2_NT,
ER3_NT,ER4_NT)
ER_NT_GROUP$FILTER <- (ER_NT_GROUP$DP_REF > 1e5)*1
ER_NT_GROUP_PASS <- ER_NT_GROUP[ER_NT_GROUP$FILTER == 1,]
ymax <- round(max(ER_NT_GROUP_PASS$ER),digits = 5)+2E-5
ymax <- ifelse(is.finite(ymax), ymax, 1e-2)
PLOT_ER_NT <- ggplot(ER_NT_GROUP_PASS,aes(x=Correction_level,y=ER, color = Change, group = Change))+
geom_point(size = 2) + geom_line()+
scale_y_continuous(breaks = seq(0,ymax, 5E-5))+
scale_x_discrete(limits=ER$Correction_level) +
theme_bw()+
ggtitle("Error rate per analysis type")+
#theme(axis.text.x = element_text(angle = 45, hjust = 1))+
xlab("Error correction level")+
ylab("Error rate")
### PLOTING INFORMATION ERROR RATES
setwd(OUTF)
ggsave(file="Coverage.pdf",plot=COV_PLOT, useDingbats=FALSE)
ggsave(file="Error_rates_per_nucleotide.pdf",plot=PLOT_ER_NT, useDingbats=FALSE)
ggsave(file='Error_rates.pdf',plot=PLOT_ER, useDingbats=FALSE)
write.table(ER,"Error_rates.txt",row.names = F,col.names = T,sep='\t', quote = F)
write.table(ER_NT_GROUP,"Error_rates_per_nucleotide.txt",row.names = F,col.names = T,sep='\t', quote = F)
# ### COUNT DISTRIBUTION PLOTS
### COUNT DISTRIBUTION PLOTS
BC1_1000 <- rbinom(n = 100000, 1000, ER[ER$Correction_level == '1x',]$ER)
BC2_1000 <- rbinom(n = 100000, 1000, ER[ER$Correction_level == '2x',]$ER)
BC3_1000 <- rbinom(n = 100000, 1000, ER[ER$Correction_level == '3x',]$ER)
BC4_1000 <- rbinom(n = 100000, 1000, ER[ER$Correction_level == '4x',]$ER)
DATA <- data.frame(c(BC1_1000, BC2_1000, BC3_1000, BC4_1000),
c(rep('1x',length(BC1_1000)), rep('2x',length(BC2_1000)), rep('3x',length(BC3_1000)),rep('4x',length(BC4_1000))))
colnames(DATA) <- c('ALT_COUNT_1000', 'Correction_type')
ALT_COUNT_PLOT <- ggplot(DATA, aes(x=as.numeric(ALT_COUNT_1000), group=Correction_type)) +
geom_histogram(binwidth = 1, size = 0.25,
aes(y = ..density..), color="black", fill="lightblue") +
facet_grid(~Correction_type) +
scale_y_continuous(labels = percent_format()) +
theme_bw()+
coord_cartesian(xlim=c(-1, 5), ylim=c(0, 1)) +
#scale_x_discrete(limits=seq(0, 8)) +
labs(title = "Alt counts per num. of duplicates. Coverage: 1000", x = "Alt count", y = "Fraction", fill = "Alt count")
ALT_COUNT_PLOT_ZOOM <- ggplot(DATA, aes(x=as.numeric(ALT_COUNT_1000), group=Correction_type)) +
geom_histogram(binwidth = 1, size = 0.25,
aes(y = ..density..), color="black", fill="lightblue") +
facet_grid(~Correction_type) +
scale_y_continuous(labels = percent_format()) +
theme_bw()+
coord_cartesian(xlim=c(-1, 5), ylim=c(0,0.25)) +
labs(title = "Alt counts per num. of duplicates. Coverage: 1000", x = "Alt count", y = "Fraction", fill = "Alt count")
#scale_x_discrete(limits=seq(0, 8))
## SAVING LAST PLOTS
ggsave(file='Alternative_Count.pdf',plot=ALT_COUNT_PLOT,useDingbats=FALSE)
ggsave(file='Alternative_Count_zoom.pdf',plot=ALT_COUNT_PLOT_ZOOM,useDingbats=FALSE)
# # ### COUNT DISTRIBUTION PLOTS
# ### COUNT DISTRIBUTION PLOTS
# BC1$Correction_type <- "1x"
# BC1$ALT_COUNT_1000 <- (BC1$ALT_COUNT/BC1$DP)
#
# BC2$Correction_type <- "2x"
# BC2$ALT_COUNT_1000 <- (BC2$ALT_COUNT/BC2$DP)
#
# BC3$Correction_type <- "3x"
# BC3$ALT_COUNT_1000 <- (BC3$ALT_COUNT/BC3$DP)
#
# BC4$ALT_COUNT_1000 <- (BC4$ALT_COUNT/BC4$DP)
# BC4$Correction_type <- "4x"
#
#
# DATA <- rbind(BC1,BC2,BC3,BC4)
#
# ALT_COUNT_PLOT <- ggplot(DATA, aes(x=as.numeric(ALT_COUNT_1000), group=Correction_type)) +
# geom_histogram(binwidth = 0.005, size = 0.25,
# aes(y = ..density..), color="black", fill="lightblue") +
# facet_grid(~Correction_type) +
# scale_y_continuous(labels = percent_format()) +
# theme_bw()+
# coord_cartesian(xlim=c(-0.001, 0.05)) +
# scale_x_discrete(limits=seq(0, 8)) +
# labs(title = "Alt counts per num. of duplicates. Coverage: 1000", x = "Alt count", y = "Fraction", fill = "Alt count")
#
#
#
# ALT_COUNT_PLOT_ZOOM <- ggplot(DATA, aes(x=as.numeric(ALT_COUNT_1000), group=Correction_type)) +
# geom_histogram(binwidth = 1, size = 0.25,
# aes(y = ..density..), color="black", fill="lightblue") +
# facet_grid(~Correction_type) +
# scale_y_continuous(labels = percent_format()) +
# theme_bw()+
# coord_cartesian(xlim=c(-1, 8), ylim=c(0,0.15)) +
# scale_x_discrete(limits=seq(0, 8))
#
# ## SAVING LAST PLOTS
# ggsave(file='Alternative_Count.pdf',plot=ALT_COUNT_PLOT,useDingbats=FALSE)
# ggsave(file='Alternative_Count_zoom.pdf',plot=ALT_COUNT_PLOT_ZOOM,useDingbats=FALSE)
#