-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracken_runner.py
More file actions
58 lines (48 loc) · 2.71 KB
/
bracken_runner.py
File metadata and controls
58 lines (48 loc) · 2.71 KB
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
# Script that runs Bracken default on strain, species and genus levels on all kreport files in a dir
# Make sure Bracken is installed in your environment and the Bracken database is build
import os
import subprocess
import argparse
# Create parser
parser = argparse.ArgumentParser(
description=('Run Bracken on all Kraken2 reports in a directory.')
)
# Add arguments
parser.add_argument('--kr_dir', '-k', type=str, required=True,
help='Path to directory containing Kraken2 reports')
parser.add_argument('--db_path', '-d', type=str, required=True,
help='Path to Bracken database')
parser.add_argument('--output_dir', '-o', type=str, required=True,
help = 'Path to output directory')
# Parse
args = parser.parse_args()
# Output and report directories
output_dir_reports = os.path.join(args.output_dir, 'bracken_reports')
# Make dirs if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
if not os.path.exists(output_dir_reports):
os.makedirs(output_dir_reports)
# Iterate over files
for filename in os.listdir(args.kr_dir):
if filename.endswith("kraken2_report.txt"):
# Full krakenrrport path
kreport = os.path.join(args.kr_dir, filename)
# Full bracken kreport path species
br_kreport_S = os.path.join(output_dir_reports, filename.replace("kraken2_report.txt", "bracken_report_S.txt"))
# Full bracken default output path species
br_output_S = os.path.join(args.output_dir, filename.replace("kraken2_report.txt", "bracken_out_S.txt"))
# Full bracken kreport path genus
br_kreport_G = os.path.join(output_dir_reports, filename.replace("kraken2_report.txt", "bracken_report_G.txt"))
# Full bracken default output path genus
br_output_G = os.path.join(args.output_dir, filename.replace("kraken2_report.txt", "bracken_out_G.txt"))
# Full bracken kreport path strain
br_kreport_S1 = os.path.join(output_dir_reports, filename.replace("kraken2_report.txt", "bracken_report_S1.txt"))
# Full bracken default output path genus
br_output_S1 = os.path.join(args.output_dir, filename.replace("kraken2_report.txt", "bracken_out_S1.txt"))
# Run bracken genus
subprocess.run(["bracken", "-d", args.db_path, "-i", kreport, "-o", br_output_G, "-w", br_kreport_G, "-l", "G"])
# Run bracken species
subprocess.run(["bracken", "-d", args.db_path, "-i", kreport, "-o", br_output_S, "-w", br_kreport_S, "-l", "S"])
# Run bracken strain
subprocess.run(["bracken", "-d", args.db_path, "-i", kreport, "-o", br_output_S1, "-w", br_kreport_S1, "-l", "S1"])