2
|
1 ---
|
15
|
2 title: 'Short reads evaluation with [FastQC](https://www.bioinformatics.babraham.ac.uk/projects/fastqc/)'
|
14
|
3 output:
|
|
4 html_document:
|
|
5 number_sections: true
|
|
6 toc: true
|
|
7 theme: cosmo
|
|
8 highlight: tango
|
2
|
9 ---
|
|
10
|
14
|
11 ```{r setup, include=FALSE, warning=FALSE, message=FALSE}
|
|
12 knitr::opts_chunk$set(
|
16
|
13 echo = ECHO,
|
|
14 error = TRUE
|
14
|
15 )
|
2
|
16 ```
|
|
17
|
|
18
|
16
|
19 # Fastqc Evaluation
|
14
|
20
|
16
|
21 ## Evaluation of reads before trimming
|
2
|
22
|
16
|
23 ```{r}
|
|
24 if ('READS_1' == 'None') {
|
|
25 stop("No pre-trimming reads provided!")
|
|
26 } else {
|
|
27 ## run fastqc evaluation
|
|
28 fastqc_command = paste0('fastqc ') %>%
|
|
29 (function(x) {
|
|
30 ifelse('CONTAMINANTS' != 'None', paste0(x, '-c CONTAMINANTS '), x)
|
|
31 }) %>%
|
|
32 (function(x) {
|
|
33 ifelse('LIMITS' != 'None', paste0(x, '-l LIMITS '), x)
|
|
34 }) %>%
|
|
35 (function(x) {
|
|
36 paste0(x, '-o REPORT_DIR ')
|
|
37 })
|
|
38 fastqc_command_reads_1 = paste0(fastqc_command, 'READS_1 > /dev/null 2>&1')
|
|
39 system(fastqc_command_reads_1, intern = TRUE)
|
|
40
|
|
41 # Original html report
|
|
42 reads_1_base = tail(strsplit('READS_1', '/')[[1]], 1)
|
|
43 original_html = tags$a(href=paste0(reads_1_base, '_fastqc.html'), paste0('HTML report: ', opt$name_1))
|
|
44
|
|
45 unzip(paste0('REPORT_DIR/', reads_1_base, '_fastqc.zip'), exdir = 'REPORT_DIR')
|
|
46 reads_1_unzip = paste0('REPORT_DIR/', reads_1_base, '_fastqc/')
|
|
47 # fastqc_data.txt
|
|
48 file.copy(paste0(reads_1_unzip, 'fastqc_data.txt'), 'REPORT_DIR/reads_1_fastqc_data.txt')
|
|
49 fastqc_data = tags$a(href='reads_1_fastqc_data.txt', paste0('fastqc_data.txt: ', opt$name_1))
|
|
50 # summary.txt
|
|
51 file.copy(paste0(reads_1_unzip, 'summary.txt'), 'REPORT_DIR/reads_1_summary.txt')
|
|
52 summary_data = tags$a(href='reads_1_summary.txt', paste0('summary.txt: ', opt$name_1))
|
|
53
|
|
54 tags$ul(
|
|
55 tags$li(original_html),
|
|
56 tags$li(fastqc_data),
|
|
57 tags$li(summary_data)
|
|
58 )
|
|
59 }
|
15
|
60 ```
|
|
61
|
|
62
|
16
|
63 ## Evaluation of reads after trimming
|
15
|
64
|
16
|
65 ```{r}
|
|
66 if ('READS_2' == 'None') {
|
|
67 stop("No pre-trimming reads provided!")
|
|
68 } else {
|
|
69 ## run fastqc evaluation
|
|
70 fastqc_command = paste0('fastqc ') %>%
|
|
71 (function(x) {
|
|
72 ifelse('CONTAMINANTS' != 'None', paste0(x, '-c CONTAMINANTS '), x)
|
|
73 }) %>%
|
|
74 (function(x) {
|
|
75 ifelse('LIMITS' != 'None', paste0(x, '-l LIMITS '), x)
|
|
76 }) %>%
|
|
77 (function(x) {
|
|
78 paste0(x, '-o REPORT_DIR ')
|
|
79 })
|
|
80 fastqc_command_reads_2 = paste0(fastqc_command, 'READS_2 > /dev/null 2>&1')
|
|
81 system(fastqc_command_reads_2, intern = TRUE)
|
|
82
|
|
83 # Original html report
|
|
84 reads_2_base = tail(strsplit('READS_2', '/')[[1]], 1)
|
|
85 original_html = tags$a(href=paste0(reads_2_base, '_fastqc.html'), paste0('HTML report: ', opt$name_2))
|
|
86
|
|
87 unzip(paste0('REPORT_DIR/', reads_2_base, '_fastqc.zip'), exdir = 'REPORT_DIR')
|
|
88 reads_2_unzip = paste0('REPORT_DIR/', reads_2_base, '_fastqc/')
|
|
89 # fastqc_data.txt
|
|
90 file.copy(paste0(reads_2_unzip, 'fastqc_data.txt'), 'REPORT_DIR/reads_2_fastqc_data.txt')
|
|
91 fastqc_data = tags$a(href='reads_2_fastqc_data.txt', paste0('fastqc_data.txt: ', opt$name_2))
|
|
92 # summary.txt
|
|
93 file.copy(paste0(reads_2_unzip, 'summary.txt'), 'REPORT_DIR/reads_2_summary.txt')
|
|
94 summary_data = tags$a(href='reads_2_summary.txt', paste0('summary.txt: ', opt$name_2))
|
|
95
|
|
96 tags$ul(
|
|
97 tags$li(original_html),
|
|
98 tags$li(fastqc_data),
|
|
99 tags$li(summary_data)
|
|
100 )
|
|
101 }
|
2
|
102 ```
|
|
103
|
15
|
104
|
|
105
|
|
106 # Fastqc output visualization
|
|
107
|
|
108 ## Overview
|
|
109
|
|
110 ```{r}
|
16
|
111 reads_1_summary = read.csv('REPORT_DIR/reads_1_summary.txt', header = FALSE, sep = '\t')[, 2:1]
|
17
|
112 reads_2_summary = read.csv('REPORT_DIR/reads_2_summary.txt', header = FALSE, sep = '\t')[, 1]
|
16
|
113 combined_summary = cbind(reads_1_summary, reads_2_summary)
|
|
114 names(combined_summary) = c('MODULE', paste0(opt$name_1, '(before)'), paste0(opt$name_2, '(after)'))
|
17
|
115 combined_summary[combined_summary == 'FAIL'] = 'FAIL (X)'
|
|
116 combined_summary[combined_summary == 'WARN'] = 'WARN (!)'
|
16
|
117 knitr::kable(combined_summary)
|
15
|
118 ```
|
|
119
|
17
|
120 ## Visualization by data module {.tabset}
|
2
|
121
|
14
|
122 * Define a function to extract outputs for each module from fastqc output
|
2
|
123
|
14
|
124 ```{r 'function definition'}
|
17
|
125 extract_data_module = function(fastqc_data, module_name, header = TRUE, comment.char = "") {
|
14
|
126 f = readLines(fastqc_data)
|
|
127 start_line = grep(module_name, f)
|
|
128 end_module_lines = grep('END_MODULE', f)
|
|
129 end_line = end_module_lines[which(end_module_lines > start_line)[1]]
|
|
130 module_data = f[(start_line+1):(end_line-1)]
|
|
131 writeLines(module_data, 'temp.txt')
|
17
|
132 read.csv('temp.txt', sep = '\t', header = header, comment.char = comment.char)
|
2
|
133 }
|
|
134 ```
|
|
135
|
15
|
136 ### Per base sequence quality
|
|
137
|
16
|
138 ```{r 'per base sequence quality', fig.width=10}
|
|
139 ## reads 1
|
|
140 pbsq_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per base sequence quality')
|
|
141 pbsq_1$id = 1:length(pbsq_1$X.Base)
|
18
|
142 pbsq_1$trim = 'before'
|
16
|
143
|
|
144 ## reads 2
|
|
145 pbsq_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per base sequence quality')
|
|
146 pbsq_2$id = 1:length(pbsq_2$X.Base)
|
18
|
147 pbsq_2$trim = 'after'
|
16
|
148
|
18
|
149 comb_pbsq = rbind(pbsq_1, pbsq_2)
|
16
|
150 comb_pbsq$trim = factor(levels = c('before', 'after'), comb_pbsq$trim)
|
17
|
151
|
16
|
152 p = ggplot(data = comb_pbsq) +
|
18
|
153 geom_boxplot(mapping = aes(x = id,
|
|
154 lower = Lower.Quartile,
|
|
155 upper = Upper.Quartile,
|
|
156 middle = Median,
|
|
157 ymin = X10th.Percentile,
|
|
158 ymax = X90th.Percentile,
|
|
159 fill = "yellow"),
|
|
160 stat = 'identity') +
|
|
161 geom_line(mapping = aes(x = id, y = Mean, color = "red")) +
|
|
162 scale_x_continuous(breaks = pbsq_2$id, labels = pbsq_2$X.Base) +
|
|
163 scale_fill_identity() +
|
|
164 scale_color_identity() +
|
|
165 ylim(0, max(comb_pbsq$Upper.Quartile) + 5) +
|
|
166 facet_grid(. ~ trim) +
|
16
|
167 theme(axis.text.x = element_text(angle=45))
|
18
|
168 p
|
16
|
169
|
15
|
170 ```
|
|
171
|
|
172 ### Per tile sequence quality
|
|
173
|
17
|
174 ```{r 'per tile sequence quality', fig.width=10}
|
|
175 ## check if 'per tile sequence quality' module exits or not
|
|
176 check_ptsq = grep('Per tile sequence quality', readLines('REPORT_DIR/reads_1_fastqc_data.txt'))
|
|
177 if (length(check_ptsq) > 0) {
|
|
178 ## reads 1
|
|
179 ptsq_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per tile sequence quality')
|
|
180 ptsq_1$trim = 'before'
|
|
181
|
|
182 ## reads 2
|
|
183 ptsq_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per tile sequence quality')
|
|
184 ptsq_2$trim = 'after'
|
|
185
|
|
186 comb_ptsq = rbind(ptsq_1, ptsq_2)
|
|
187 comb_ptsq$trim = factor(levels = c('before', 'after'), comb_ptsq$trim)
|
|
188 comb_ptsq$Base = factor(levels = unique(comb_ptsq$Base), comb_ptsq$Base)
|
|
189
|
|
190 # convert integers to charaters
|
|
191 comb_ptsq$Tile = as.character(comb_ptsq$X.Tile)
|
|
192
|
|
193 p = ggplot(data = comb_ptsq, aes(x = Base, y = Tile, fill = Mean)) +
|
|
194 geom_raster() +
|
|
195 facet_grid(. ~ trim) +
|
|
196 xlab('Position in read (bp)') +
|
|
197 ylab('') +
|
|
198 theme(axis.text.x = element_text(angle=45))
|
|
199 ggplotly(p)
|
|
200 } else {
|
|
201 print('No "per tile sequence quality" data')
|
|
202 }
|
|
203
|
|
204
|
|
205 ```
|
|
206
|
|
207 ### Per sequence quality score
|
|
208
|
|
209 ```{r 'Per sequence quality score', fig.width=10}
|
16
|
210 ## reads 1
|
17
|
211 psqs_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per sequence quality scores')
|
|
212 psqs_1$trim = 'before'
|
16
|
213
|
|
214 ## reads 2
|
17
|
215 psqs_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per sequence quality scores')
|
|
216 psqs_2$trim = 'after'
|
|
217
|
|
218 comb_psqs = rbind(psqs_1, psqs_2)
|
|
219 comb_psqs$trim = factor(levels = c('before', 'after'), comb_psqs$trim)
|
|
220
|
|
221 p = ggplot(data = comb_psqs, aes(x = X.Quality, y = Count)) +
|
|
222 geom_line(color = 'red') +
|
|
223 facet_grid(. ~ trim) +
|
|
224 xlim(min(comb_psqs$X.Quality), max(comb_psqs$X.Quality)) +
|
|
225 xlab('Mean Sequence Qaulity (Phred Score)') +
|
|
226 ylab('')
|
|
227 ggplotly(p)
|
|
228 ```
|
|
229
|
|
230
|
|
231 ### Per base sequence content
|
|
232
|
|
233 ```{r 'Per base sequence content', fig.width=10}
|
|
234 ## reads 1
|
|
235 pbsc_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per base sequence content')
|
|
236 pbsc_1$id = 1:length(pbsc_1$X.Base)
|
|
237
|
|
238 melt_pbsc_1 = melt(pbsc_1, id=c('X.Base', 'id'))
|
|
239 melt_pbsc_1$trim = 'before'
|
|
240
|
|
241
|
|
242 ## reads 2
|
|
243 pbsc_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per base sequence content')
|
|
244 pbsc_2$id = 1:length(pbsc_2$X.Base)
|
|
245
|
|
246 melt_pbsc_2 = melt(pbsc_2, id=c('X.Base', 'id'))
|
|
247 melt_pbsc_2$trim = 'after'
|
|
248
|
|
249 comb_pbsc = rbind(melt_pbsc_1, melt_pbsc_2)
|
|
250 comb_pbsc$trim = factor(levels = c('before', 'after'), comb_pbsc$trim)
|
|
251
|
|
252 p = ggplot(data = comb_pbsc, aes(x = id, y = value, color = variable)) +
|
|
253 geom_line() +
|
|
254 facet_grid(. ~ trim) +
|
|
255 xlim(min(comb_pbsc$id), max(comb_pbsc$id)) +
|
|
256 ylim(0, 100) +
|
|
257 xlab('Position in read (bp)') +
|
|
258 ylab('')
|
|
259 ggplotly(p)
|
|
260 ```
|
16
|
261
|
17
|
262 ### Per sequence GC content
|
|
263
|
|
264 ```{r 'Per sequence GC content', fig.width=10}
|
|
265 ## reads 1
|
|
266 psGCc_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per sequence GC content')
|
|
267 psGCc_1$trim = 'before'
|
|
268
|
|
269 ## reads 2
|
|
270 psGCc_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per sequence GC content')
|
|
271 psGCc_2$trim = 'after'
|
|
272
|
|
273 comb_psGCc = rbind(psGCc_1, psGCc_2)
|
|
274 comb_psGCc$trim = factor(levels = c('before', 'after'), comb_psGCc$trim)
|
|
275
|
|
276 p = ggplot(data = comb_psGCc, aes(x = X.GC.Content, y = Count)) +
|
|
277 geom_line(color = 'red') +
|
|
278 facet_grid(. ~ trim) +
|
|
279 xlab('Mean Sequence Qaulity (Phred Score)') +
|
|
280 ylab('')
|
|
281 ggplotly(p)
|
|
282 ```
|
|
283
|
16
|
284
|
17
|
285 ### Per base N content
|
|
286
|
|
287 ```{r 'Per base N content', fig.width=10}
|
|
288 ## reads 1
|
|
289 pbNc_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Per base N content')
|
|
290 pbNc_1$id = 1:length(pbNc_1$X.Base)
|
|
291 pbNc_1$trim = 'before'
|
|
292
|
|
293 ## reads 2
|
|
294 pbNc_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Per base N content')
|
|
295 pbNc_2$id = 1:length(pbNc_2$X.Base)
|
|
296 pbNc_2$trim = 'after'
|
|
297
|
|
298 comb_pbNc = rbind(pbNc_1, pbNc_2)
|
|
299 comb_pbNc$trim = factor(levels = c('before', 'after'), comb_pbNc$trim)
|
|
300
|
|
301 p = ggplot(data = comb_pbNc, aes(x = id, y = N.Count)) +
|
|
302 geom_line(color = 'red') +
|
|
303 scale_x_continuous(breaks = pbNc_2$id, labels = pbNc_2$X.Base) +
|
|
304 facet_grid(. ~ trim) +
|
|
305 ylim(0, 1) +
|
|
306 xlab('N-Count') +
|
|
307 ylab('') +
|
16
|
308 theme(axis.text.x = element_text(angle=45))
|
|
309 ggplotly(p)
|
15
|
310 ```
|
|
311
|
|
312
|
17
|
313 ### Sequence Length Distribution
|
|
314
|
|
315 ```{r 'Sequence Length Distribution', fig.width=10}
|
|
316 ## reads 1
|
|
317 sld_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Sequence Length Distribution')
|
|
318 sld_1$id = 1:length(sld_1$X.Length)
|
|
319 sld_1$trim = 'before'
|
|
320
|
|
321 ## reads 2
|
|
322 sld_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Sequence Length Distribution')
|
|
323 sld_2$id = 1:length(sld_2$X.Length)
|
|
324 sld_2$trim = 'after'
|
|
325
|
|
326 comb_sld = rbind(sld_1, sld_2)
|
|
327 comb_sld$trim = factor(levels = c('before', 'after'), comb_sld$trim)
|
|
328
|
|
329 p = ggplot(data = comb_sld, aes(x = id, y = Count)) +
|
|
330 geom_line(color = 'red') +
|
|
331 scale_x_continuous(breaks = sld_2$id, labels = sld_2$X.Length) +
|
|
332 facet_grid(. ~ trim) +
|
|
333 xlab('Sequence Length (bp)') +
|
|
334 ylab('') +
|
|
335 theme(axis.text.x = element_text(angle=45))
|
|
336 ggplotly(p)
|
|
337 ```
|
|
338
|
|
339 ### Sequence Duplication Levels
|
|
340
|
|
341 ```{r 'Sequence Duplication Levels', fig.width=10}
|
|
342 ## reads 1
|
|
343 sdl_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Sequence Duplication Levels', header = FALSE, comment.char = '#')
|
|
344 names(sdl_1) = c('Duplication_Level', 'Percentage_of_deduplicated', 'Percentage_of_total')
|
|
345 sdl_1$id = 1:length(sdl_1$Duplication_Level)
|
|
346
|
|
347 melt_sdl_1 = melt(sdl_1, id=c('Duplication_Level', 'id'))
|
|
348 melt_sdl_1$trim = 'before'
|
|
349
|
|
350
|
|
351 ## reads 2
|
|
352 sdl_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Sequence Duplication Levels', header = FALSE, comment.char = '#')
|
|
353 names(sdl_2) = c('Duplication_Level', 'Percentage_of_deduplicated', 'Percentage_of_total')
|
|
354 sdl_2$id = 1:length(sdl_2$Duplication_Level)
|
|
355
|
|
356 melt_sdl_2 = melt(sdl_2, id=c('Duplication_Level', 'id'))
|
|
357 melt_sdl_2$trim = 'after'
|
|
358
|
|
359 comb_sdl = rbind(melt_sdl_1, melt_sdl_2)
|
|
360 comb_sdl$trim = factor(levels = c('before', 'after'), comb_sdl$trim)
|
|
361
|
|
362 p = ggplot(data = comb_sdl, aes(x = id, y = value, color = variable)) +
|
|
363 geom_line() +
|
|
364 scale_x_continuous(breaks = sdl_2$id, labels = sdl_2$Duplication_Level) +
|
|
365 facet_grid(. ~ trim) +
|
|
366 xlab('Sequence Duplication Level') +
|
|
367 ylab('') +
|
|
368 theme(axis.text.x = element_text(angle=45))
|
|
369 ggplotly(p)
|
|
370 ```
|
|
371
|
|
372 ### Adapter Content
|
|
373
|
|
374 ```{r 'Adapter Content', fig.width=10}
|
|
375 ## reads 1
|
|
376 ac_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Adapter Content')
|
|
377 ac_1$id = 1:length(ac_1$X.Position)
|
|
378
|
|
379 melt_ac_1 = melt(ac_1, id=c('X.Position', 'id'))
|
|
380 melt_ac_1$trim = 'before'
|
|
381
|
|
382 ## reads 2
|
|
383 ac_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Adapter Content')
|
|
384 ac_2$id = 1:length(ac_2$X.Position)
|
|
385
|
|
386 melt_ac_2 = melt(ac_2, id=c('X.Position', 'id'))
|
|
387 melt_ac_2$trim = 'after'
|
|
388
|
|
389 comb_ac = rbind(melt_ac_1, melt_ac_2)
|
|
390 comb_ac$trim = factor(levels = c('before', 'after'), comb_ac$trim)
|
|
391
|
|
392 p = ggplot(data = comb_ac, aes(x = id, y = value, color = variable)) +
|
|
393 geom_line() +
|
|
394 facet_grid(. ~ trim) +
|
|
395 xlim(min(comb_ac$id), max(comb_ac$id)) +
|
|
396 ylim(0, 1) +
|
|
397 xlab('Position in read (bp)') +
|
|
398 ylab('')
|
|
399 ggplotly(p)
|
|
400 ```
|
|
401
|
|
402 ### Kmer Content {.tabset}
|
|
403
|
|
404 #### Before
|
|
405
|
|
406 ```{r 'Kmer Content (before)', fig.width=10}
|
|
407 kc_1 = extract_data_module('REPORT_DIR/reads_1_fastqc_data.txt', 'Kmer Content')
|
|
408 knitr::kable(kc_1)
|
|
409 ```
|
|
410
|
|
411 #### After
|
|
412 ```{r 'Kmer Content (after)', fig.width=10}
|
|
413 kc_2 = extract_data_module('REPORT_DIR/reads_2_fastqc_data.txt', 'Kmer Content')
|
|
414 knitr::kable(kc_2)
|
|
415 ```
|
|
416
|
2
|
417
|
14
|
418 # Session Info
|
2
|
419
|
14
|
420 ```{r 'session info'}
|
|
421 sessionInfo()
|
2
|
422 ```
|
|
423
|