Mercurial > repos > public-health-bioinformatics > screen_abricate_report
comparison screen_abricate_report.py @ 0:2c6153023368 draft
"planemo upload for repository https://github.com/public-health-bioinformatics/galaxy_tools/blob/master/tools/screen_abricate_report"
author | public-health-bioinformatics |
---|---|
date | Wed, 30 Oct 2019 23:01:01 -0400 |
parents | |
children | 45542cd244ad |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2c6153023368 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 from __future__ import print_function | |
4 | |
5 import argparse | |
6 import os | |
7 import re | |
8 import sys | |
9 import csv | |
10 from pprint import pprint | |
11 | |
12 def parse_screen_file(screen_file): | |
13 screen = [] | |
14 with open(screen_file) as f: | |
15 reader = csv.DictReader(f, delimiter="\t", quotechar='"') | |
16 for row in reader: | |
17 screen.append(row) | |
18 return screen | |
19 | |
20 def get_fieldnames(input_file): | |
21 with open(input_file) as f: | |
22 reader = csv.DictReader(f, delimiter="\t", quotechar='"') | |
23 row = next(reader) | |
24 fieldnames = row.keys() | |
25 return fieldnames | |
26 | |
27 def main(args): | |
28 screen = parse_screen_file(args.screening_file) | |
29 abricate_report_fieldnames = get_fieldnames(args.abricate_report) | |
30 gene_detection_status_fieldnames = ['gene_name', 'detected'] | |
31 with open(args.abricate_report, 'r') as f1, open(args.screened_report, 'w') as f2, open(args.gene_detection_status, 'w') as f3: | |
32 abricate_report_reader = csv.DictReader(f1, delimiter="\t", quotechar='"') | |
33 screened_report_writer = csv.DictWriter(f2, delimiter="\t", quotechar='"', fieldnames=abricate_report_fieldnames) | |
34 gene_detection_status_writer = csv.DictWriter(f3, delimiter="\t", quotechar='"', fieldnames=gene_detection_status_fieldnames) | |
35 screened_report_writer.writeheader() | |
36 gene_detection_status_writer.writeheader() | |
37 | |
38 for gene in screen: | |
39 gene_detection_status = { | |
40 'gene_name': gene['gene_name'], | |
41 'detected': False | |
42 } | |
43 for abricate_report_row in abricate_report_reader: | |
44 if re.search(gene['regex'], abricate_report_row['GENE']): | |
45 gene_detection_status['detected'] = True | |
46 screened_report_writer.writerow(abricate_report_row) | |
47 gene_detection_status_writer.writerow(gene_detection_status) | |
48 f1.seek(0) # return file pointer to start of abricate report | |
49 | |
50 | |
51 if __name__ == '__main__': | |
52 parser = argparse.ArgumentParser() | |
53 parser.add_argument("abricate_report", help="Input: Abricate report to screen (tsv)") | |
54 parser.add_argument("--screening_file", help="Input: List of genes to screen for (tsv)") | |
55 parser.add_argument("--screened_report", help="Output: Screened abricate report including only genes of interest (tsv)") | |
56 parser.add_argument("--gene_detection_status", help="Output: detection status for all genes listed in the screening file (tsv)") | |
57 args = parser.parse_args() | |
58 main(args) |