Skip to content

Commit

Permalink
Add generic hamronize.py CLI parser that can parse any tool
Browse files Browse the repository at this point in the history
- Reorganise the repo to make it pip installable directly from git/conda
- Gets rid of now redundant parsers
- Updates test.sh to new parser format
  • Loading branch information
fmaguire committed Sep 25, 2020
1 parent 34e8b8f commit 0bf0538
Show file tree
Hide file tree
Showing 39 changed files with 143 additions and 180 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ cd hAMRonization
pip install -e .
```

## Usage

```
python hamronize.py abricate ../test/data/raw_outputs/abricate/report.tsv --reference_database_version db_v_1 --analysis_software_version tool_v_1 --format json
```

## Parsers

Parsers needing tested (both automated and just sanity checking output), see [test.sh](parsers/test.sh) for example invocations.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,73 @@ def write(self, output_location=None, output_format='tsv'):


def cli_parser(analysis_tool):
parser = argparse.ArgumentParser(description=f"hAMRonization parser for {analysis_tool}")
parser.add_argument("report", help="Path to tool report")
parser.add_argument("--format", default="tsv", help="Output format (tsv or json)")
parser.add_argument("--output", default=None, help="Output location")
"""
Generate a command line hAMRonization tool for a specific tool
"""
parser = generic_parser(analysis_tool)
args = parser.parse_args()
metadata = {field: getattr(args, field) for field in required_mandatory_metadata}

# parse report and write to specified
parsed_report = hAMRonization.parse(args.report, metadata, analysis_tool)
parsed_report.write(output_location=args.output,
output_format=args.format)

def generate_tool_subparser(subparser, analysis_tool):
"""
Build the argument parser for a specific tool (used to generate a tool-specific
cli-parser and a generic tool parser)
"""
report_file = hAMRonization._ReportFileToUse[analysis_tool]
tool_parser = subparser.add_parser(analysis_tool,
description=f"Applies hAMRonization specification to output from {analysis_tool} ({report_file})",
usage=f"hamronize.py {analysis_tool} <options>",
help=f"hAMRonize {analysis_tool}'s output report i.e., {report_file}")

tool_parser.add_argument("report", help="Path to tool report")
tool_parser.add_argument("--format", default="tsv", help="Output format (tsv or json)")
tool_parser.add_argument("--output", default=None, help="Output location")

# any missing mandatory fields need supplied as CLI argument
required_mandatory_metadata = hAMRonization._RequiredToolMetadata[analysis_tool]
for field in required_mandatory_metadata:
parser.add_argument(f"--{field}", required=True,
tool_parser.add_argument(f"--{field}", required=True,
help="Input string containing the "
f"{field} "
f"for {analysis_tool}")
return subparser


def generic_cli_interface():
"""
Generate a generic tool report parser that passes to the tool specific
parser
"""
parser = argparse.ArgumentParser(description="Convert AMR gene detection tool output to hAMRonization specification format",
prog='hamronize',
usage='hamronize.py <tool> <options>')

parser.add_argument('-v', '--version', action='version',
version=f"%(prog)s {hAMRonization.__version__}")


subparser = parser.add_subparsers(title="Tools with hAMRonizable reports",
help='', dest='analysis_tool')

for analysis_tool in hAMRonization._RequiredToolMetadata.keys():
subparser = generate_tool_subparser(subparser, analysis_tool)

args = parser.parse_args()
metadata = {field: getattr(args, field) for field in required_mandatory_metadata}

# parse report and write to specified
parsed_report = hAMRonization.parse(args.report, metadata, analysis_tool)
parsed_report.write(output_location=args.output,
output_format=args.format)
if args.analysis_tool:
required_mandatory_metadata = hAMRonization._RequiredToolMetadata[args.analysis_tool]
metadata = {field: getattr(args, field) for field in required_mandatory_metadata}

# parse report and write to specified
parsed_report = hAMRonization.parse(args.report, metadata, args.analysis_tool)
parsed_report.write(output_location=args.output,
output_format=args.format)
else:
parser.print_help()
exit(1)

File renamed without changes.
Empty file removed hAMRonization/README.md
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

__version__ = "1.0.0"

from hAMRonization.hAMRonizedResult import hAMRonizedResult

from hAMRonization import AbricateIO
Expand Down Expand Up @@ -35,6 +37,24 @@
"resfams": ResFamsIO.ResFamsIterator
}

_ReportFileToUse = {
"abricate": "OUTPUT.tsv",
"amrfinderplus": "OUTPUT.tsv",
"ariba": "OUTDIR/OUTPUT.tsv",
"rgi": "OUTPUT.txt or OUTPUT_bwtoutput.gene_mapping_data.txt",
"resfinder": "data_resfinder.json",
"srax": "sraX_detected_ARGs.tsv",
"deeparg": "OUTDIR/OUTPUT.mapping.ARG",
"kmerresistance": "OUTPUT.KmerRes",
"srst2": "OUTPUT_srst2_report.tsv",
"groot": "OUTPUT.tsv (from `groot report`)",
"staramr": "resfinder.tsv",
"csstar": "OUTPUT.tsv",
"amrplusplus": "gene.tsv",
"resfams": "resfams.tblout"
}


_RequiredToolMetadata = {
"abricate": AbricateIO.required_metadata,
"amrfinderplus": AmrFinderPlusIO.required_metadata,
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions hamronize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python

import argparse
import hAMRonization

if __name__ == '__main__':

hAMRonization.Interfaces.generic_cli_interface()
7 changes: 0 additions & 7 deletions parsers/abricate_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/amrfinderplus_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/amrplusplus_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/ariba_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/csstar_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/deeparg_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/groot_report_parser.py

This file was deleted.

16 changes: 0 additions & 16 deletions parsers/hAMRonized_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/kmerresistance_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/resfams_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/resfinder_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/rgi_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/srax_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/srst2_report_parser.py

This file was deleted.

7 changes: 0 additions & 7 deletions parsers/staramr_report_parser.py

This file was deleted.

56 changes: 0 additions & 56 deletions parsers/test.sh

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0bf0538

Please sign in to comment.