-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltering_kraken_output.R
executable file
·491 lines (378 loc) · 18 KB
/
filtering_kraken_output.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env Rscript
################################################################################
#
# filtering kraken output
# PostKraken
# Cameron Ferguson, 06-12-22
#
################################################################################
########################## preparing the workspace #############################
#importing the different library's
require(data.table, quietly = T) -> dt_package
require(optparse, quietly = T) -> opt_package
require(stringi, quietly = T) -> stri_package
############################ creating the flags ################################
option_list <- list(
make_option(c('--input_type'),
dest = 'input_type',
action = 'store',
default = NA,
type = 'character',
metavar = '[PE, SE, C]',
help = 'A flag to specify the input type: PE = paired end, SE = single end, C = assembly file containing contigs, this is a required flag'),
make_option(c('--i'),
dest = 'input_type',
action = 'store',
default = NA,
type = 'character',
metavar = '[PE, SE, C]',
help = 'A flag to specify the input type: PE = paired end, SE = single end, C = assembly file containing contigs, this is a required flag'),
make_option(c('--kraken_output'),
dest = 'kraken_File',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 output file]',
help = 'The file name of the kraken output file you want to filter, this is a required flag'),
make_option(c('--k'),
dest = 'kraken_File',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 output file]',
help = 'The file name of the kraken output file you want to filter, this is a required flag'),
make_option(c('--kraken_output_2'),
dest = 'kraken_File_2',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 output file]',
help = 'The file name of the kraken output file for your reverse reads'),
make_option(c('--k2'),
dest = 'kraken_File_2',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 output file]',
help = 'The file name of the kraken output file for your reverse reads'),
make_option(c('--output_filename'),
dest = 'output_FileName',
action = 'store',
default = NA,
type = 'character',
metavar = '[name of output file]',
help = 'The file name you want to give the filtered reads'),
make_option(c('--o'),
dest = 'output_FileName',
action = 'store',
default = NA,
type = 'character',
metavar = '[name of output file]',
help = 'The file name you want to give the filtered reads'),
make_option(c('--output_filename_2'),
dest = 'output_FileName_2',
action = 'store',
default = NA,
type = 'character',
metavar = '[name of output file]',
help = 'The file name you want to give the filtered reverse reads'),
make_option(c('--o2'),
dest = 'output_FileName_2',
action = 'store',
default = NA,
type = 'character',
metavar = '[name of output file]',
help = 'The file name you want to give the filtered reverse reads'),
make_option(c('--method'),
dest = 'method',
action = 'store',
default = 'filter',
type = 'character',
metavar = '[F, E]',
help = 'F = filter, E = exclude. Filter finds reads that match specified taxa IDs. \n\t\tExclude finds reads that do not match the specified taxa ID, this is a required flag'),
make_option(c('--m'),
dest = 'method',
action = 'store',
default = 'filter',
type = 'character',
metavar = '[F, E]',
help = 'F = filter, E = exclude. Filter finds reads that match specified taxa IDs. \n\t\tExclude finds reads that do not match the specified taxa ID, this is a required flag'),
make_option(c('--taxa'),
dest = 'taxa_ID',
action = 'store',
default = NA,
type = 'character',
metavar = '[taxa ID]',
help = 'The taxa ID you want to filter/exclude by, this is a required flag'),
make_option(c('--t'),
dest = 'taxa_ID',
action = 'store',
default = NA,
type = 'character',
metavar = '[taxa ID]',
help = 'The taxa ID you want to filter/exclude by, this is a required flag'),
make_option(c('--include_lower_taxa'),
dest = 'include_lower_taxa',
action = 'store',
default = F,
type = 'logical',
metavar = '[F, T]',
help = 'whether to include lower taxa or not: F = do not include lower taxa, T = include lower taxa. \n\t\tDefualt is F if the T value is passed you will need to include the --r flag for it to work'),
make_option(c('--r'),
dest = 'kraken_Report_Filename',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 report file]',
help = 'The report file from your kraken run'),
make_option(c('--kraken_report'),
dest = 'kraken_Report_Filename',
action = 'store',
default = NA,
type = 'character',
metavar = '[kraken2 report file]',
help = 'The report file from your kraken run')
)
opt <- parse_args(OptionParser(option_list = option_list,
description = 'Description: Extracting reads identified as a specific taxa(s)',
usage = 'filtering_kraken_output.R --i [PE,SE,C] --k KRAKEN_FILE [--k2 KRAKEN_FILE_2] --o OUTPUT_FILENAME [--o2 OUTPUT_FILENAME_2] \n\t\t\t\t --m [F,E] --t TAXA_ID [--include_lower_taxa [T,F]] [--r KRAKEN_REPORT_FILENAME]\n',
epilogue = 'If you have any issues or suggestions for improvements please headover too: https://github.com/theHatIsBack/PostKraken\n'))
#################### creating the functions for the flags ######################
#creating a function to pull out all the reads that match the IDs
filtReads <- function(ID, dataframe, in_filename){
#creating the regular exspression search term
regID <- paste('\\b', ID, '\\b', sep = '')
#converting the list of kraken output to a data.table
if (stri_detect(in_filename, fixed = '.fasta') == T | stri_detect(in_filename, regex = '.fa\\b') == T) {
table <- as.data.table(matrix(dataframe,
ncol = 2,
nrow = length(dataframe)/2,
byrow = T))
} else if (stri_detect(in_filename, fixed = '.fastq') == T | stri_detect(in_filename, regex = '.fq\\b') == T) {
table <- as.data.table(matrix(dataframe,
ncol = 4,
nrow = length(dataframe)/4,
byrow = T))
}
#using lapply to loop in c and find the header lines that match our ID and
#return a vector of positions
index <- sort(unlist(lapply(regID, function(x){ grep(pattern = x, table$V1) })))
#filtering the table for the sequences of interest
fTable <- table[index, ,]
#converting the data.table back to a list
fReads <- unlist(as.list(t(fTable)))
return(fReads)
}
#creating a function to exclude all the reads that match the IDs
exReads <- function(ID, dataframe, in_filename){
#creating the regular exspression search term
regID <- paste('\\b', ID, '\\b', sep = '')
#converting the list of kraken output to a data.table
if (stri_detect(in_filename, fixed = '.fasta') == T | stri_detect(in_filename, regex = '.fa\\b') == T) {
table <- as.data.table(matrix(dataframe,
ncol = 2,
nrow = length(dataframe)/2,
byrow = T))
} else if (stri_detect(in_filename, fixed = '.fastq') == T | stri_detect(in_filename, regex = '.fq\\b') == T) {
table <- as.data.table(matrix(dataframe,
ncol = 4,
nrow = length(dataframe)/4,
byrow = T))
}
#using lapply to loop in c and find the header lines that match our ID and
#return a vector of positions
index <- sort(unlist(lapply(regID, function(x){ grep(pattern = x, table$V1) })))
#excluding specified sequences from the table
excluTable <- table[!index, ,]
#converting the data.table back to a list
excluReads <- unlist(as.list(t(excluTable)))
return(excluReads)
}
#creating a function to pull out all taxonomic ID's bellow
findIDsBellow <- function(ID, dataframe){
#creating the regular exspression search term
regID <- paste('\\b', ID, '\\b', sep = '')
#removing any levels above the supplied taxa ID
lower <- grep(pattern = regID, dataframe$V5)
upper <- length(dataframe$V5)
trimmedReport <- dataframe[lower:upper, , ]
#creating a list of kraken taxa codes
taxaSymbols <- c('R', 'D', 'K', 'P', 'C', 'O', 'F', 'G', 'S')
#pulling out the taxa symbol and splitting it up into characters
pat <- stri_split_boundaries(trimmedReport$V4[1], type = 'character')
#identifying level of taxa code supplied in list
level <- grep(pattern = pat[[1]][1], taxaSymbols)
#removing the supplied taxa ID from the df so it doesn't return a null
trimmedReport2 <- trimmedReport[2:length(V4), ,]
#creating the list to populate with taxa codes
lowerTaxa <- c()
#the index serves two perpouses to act as a way to access elements of the list and to keep
#track of the row of the data.table
index <- 1
#using a for loop to output a list of lower taxa ID's
for (x in trimmedReport2$V4) {
#pulling out the taxa symbol and splitting it up into characters
pat2 <- stri_split_boundaries(x, type = 'character')
#checking if the position of the next taxa symbol is higher or lower then the supplied taxa
#in the taxaSymbols list
if (level < grep(pattern = pat2[[1]][1], taxaSymbols)){
results <- trimmedReport2[index, .(V5),]
lowerTaxa[index] <- results$V5
index <- index + 1
} else if (level == grep(pattern = pat2[[1]][1], taxaSymbols) & is.na(pat2[[1]][2]) != is.na(pat[[1]][2])) {
results <- trimmedReport2[index, .(V5),]
lowerTaxa[index] <- results$V5
index <- index + 1
} else {
break
}
}
#combining the new list of taxa with the old supplied taxa
listOfTaxa <- append(lowerTaxa, ID)
return(listOfTaxa)
}
############################## data workflow ###################################
workflow <- function(ID, seqFile, outputFile, meth, includeOtherTaxa, inputFile){
#creating a list to collect the output
filteredReads <- c()
if (meth == 'F') {
#running the findIDsBellow function and filtering all ID's returned
if (includeOtherTaxa == T) {
#importing the data
krakenReport <- fread(opt$kraken_Report_Filename, header = F)
#pulling out all the need ID's from the report
taxaList <- findIDsBellow(ID, krakenReport)
#looping through all of the taxa ID's
filteredReads <- filtReads(taxaList, seqFile, inputFile)
#just running the filtering for the ID provided
} else {
taxaList <- c(ID)
filteredReads <- filtReads(taxaList, seqFile, inputFile)
}
} else if (meth == 'E'){
#running the findIDsBellow function and removing all ID's returned
if (includeOtherTaxa == T) {
#importing the data
krakenReport <- fread(opt$kraken_Report_Filename, header = F)
#pulling out all the need ID's from the report
taxaList <- findIDsBellow(ID, krakenReport)
#removing all of the ID's that where found
filteredReads <- exReads(taxaList, seqFile, inputFile)
#just running the filtering for the ID provided
} else {
taxaList <- c(ID)
filteredReads <- exReads(taxaList, seqFile, inputFile)
}
}
#writing the output of the filtering to a file
stri_write_lines(filteredReads, outputFile, sep = '\n')
#outputting info to the user
if (stri_detect(inputFile, fixed = '.fasta') == T | stri_detect(inputFile, regex = '.fa\\b') == T) {
if (meth == 'F'){
print(inputFile)
print(paste('number of taxa IDs found:', length(taxaList)))
print(paste('number of reads found:', length(filteredReads)/2))
print(paste('percentage of reads retained:', (length(filteredReads)/length(seqFile)*100), '%'))
print(paste('filtered reads have been written to:', outputFile))
print('')
}else if(meth == 'E'){
print(inputFile)
print(paste('number of taxa IDs found:', length(taxaList)))
print(paste('number of reads found:', ((length(seqFile)/2) - length(filteredReads)/2)))
print(paste('percentage of reads retained:', (length(filteredReads)/length(seqFile)*100), '%'))
print(paste('filtered reads have been written to:', outputFile))
print('')
}
} else if (stri_detect(inputFile, fixed = '.fastq') == T | stri_detect(inputFile, regex = '.fq\\b') == T) {
if (meth == 'F'){
print(inputFile)
print(paste('number of taxa IDs found:', length(taxaList)))
print(paste('number of reads found:', length(filteredReads)/4))
print(paste('percentage of reads retained:', (length(filteredReads)/length(seqFile)*100), '%'))
print(paste('filtered reads have been written to:', outputFile))
print('')
}else if(meth == 'E'){
print(inputFile)
print(paste('number of taxa IDs found:', length(taxaList)))
print(paste('number of reads found:', ((length(seqFile)/4) - length(filteredReads)/4)))
print(paste('percentage of reads retained:', (length(filteredReads)/length(seqFile)*100), '%'))
print(paste('filtered reads have been written to:', outputFile))
print('')
}
}
}
############################## main function ###################################
main <- function(){
#checking for the required packages and if not installed prompting the user to install
if (dt_package == F) {
install <- readline(prompt = 'Required package data.table not installed, would you like to install it? (y/n)')
if (install == 'y' | install == 'Y') {
install.packages('data.table')
} else {
stop('Required package not installed')
}
} else if (opt_package == F) {
install <- readline(prompt = 'Required package optparse not installed, would you like to install it? (y/n)')
if (install == 'y' | install == 'Y') {
install.packages('data.table')
} else {
stop('Required package not installed')
}
} else if (stri_package == F) {
install <- readline(prompt = 'Required package stringi not installed, would you like to install it? (y/n)')
if (install == 'y' | install == 'Y') {
install.packages('data.table')
} else {
stop('Required package not installed')
}
}
#checking for required flags
if (is.na(opt$input_type) == T) {
stop('Required flag --i, --input_type not specified')
} else if (is.na(opt$kraken_File) == T) {
stop('Required flag --k, --kraken_output not specified')
} else if (is.na(opt$method) == T) {
stop('Required flag --m, --method not specified')
} else if (is.na(opt$taxa_ID) == T) {
stop('Required flag --t, --taxa not specified')
}
#passing data from the flags to variables:
#method variables
taxID <- opt$taxa_ID
include_taxa_levels_bellow <- opt$include_lower_taxa
Method <- opt$method
inputType <- opt$input_type
#input/output file variables
inputFilename <- opt$kraken_File
krakenOut <- stri_read_lines(inputFilename)
outputFilename <- opt$output_FileName
if (inputType == 'SE' | inputType == 'C') {
workflow(taxID, krakenOut, outputFilename, Method, include_taxa_levels_bellow, inputFilename)
} else if (inputType == 'PE') {
#passing data from the flags to variables for additional data need for PE
inputFilename2 <- opt$kraken_File_2
krakenOut2 <- stri_read_lines(inputFilename2)
outputFilename2 <- opt$output_FileName_2
#using forking to parallelise the paired end workflow
#importing mclapply function from the base package parallel
invisible(parallel::mclapply(1:2,
function(x) {
if (x == 1) {
workflow(taxID,
krakenOut,
outputFilename,
Method,
include_taxa_levels_bellow,
inputFilename)
} else if (x == 2) {
workflow(taxID,
krakenOut2,
outputFilename2,
Method,
include_taxa_levels_bellow,
inputFilename2)
}
}, mc.cores = 2))
}
}
main()