hAMRonization 1.1.8__tar.gz → 1.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {hamronization-1.1.8 → hamronization-1.2.1}/PKG-INFO +34 -22
  2. {hamronization-1.1.8 → hamronization-1.2.1}/README.md +12 -15
  3. hamronization-1.2.1/hAMRonization/AmrFinderPlusIO.py +136 -0
  4. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/Interfaces.py +13 -5
  5. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/MykrobeIO.py +2 -0
  6. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/ResFinderIO.py +67 -48
  7. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/RgiIO.py +10 -12
  8. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/__init__.py +1 -5
  9. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/hAMRonizedResult.py +1 -1
  10. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/hamronize.py +1 -1
  11. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/summarize.py +11 -2
  12. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/PKG-INFO +34 -22
  13. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/SOURCES.txt +0 -1
  14. {hamronization-1.1.8 → hamronization-1.2.1}/setup.py +7 -6
  15. {hamronization-1.1.8 → hamronization-1.2.1}/test/test_interfaces.py +0 -5
  16. {hamronization-1.1.8 → hamronization-1.2.1}/test/test_parsing_validity.py +61 -74
  17. hamronization-1.1.8/hAMRonization/AmrFinderPlusIO.py +0 -137
  18. hamronization-1.1.8/hAMRonization/PointFinderIO.py +0 -68
  19. {hamronization-1.1.8 → hamronization-1.2.1}/LICENSE.txt +0 -0
  20. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/AbricateIO.py +0 -0
  21. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/AmrPlusPlusIO.py +0 -0
  22. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/AribaIO.py +0 -0
  23. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/CSStarIO.py +0 -0
  24. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/DeepArgIO.py +0 -0
  25. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/FARGeneIO.py +0 -0
  26. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/GrootIO.py +0 -0
  27. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/KmerResistanceIO.py +0 -0
  28. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/ResFamsIO.py +0 -0
  29. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/SraxIO.py +0 -0
  30. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/Srst2IO.py +0 -0
  31. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/StarAmrIO.py +0 -0
  32. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/TBProfilerIO.py +0 -0
  33. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization/constants.py +0 -0
  34. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/dependency_links.txt +0 -0
  35. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/entry_points.txt +0 -0
  36. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/requires.txt +0 -0
  37. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/top_level.txt +0 -0
  38. {hamronization-1.1.8 → hamronization-1.2.1}/hAMRonization.egg-info/zip-safe +0 -0
  39. {hamronization-1.1.8 → hamronization-1.2.1}/setup.cfg +0 -0
@@ -1,9 +1,9 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: hAMRonization
3
- Version: 1.1.8
3
+ Version: 1.2.1
4
4
  Summary: Tool to convert and summarize AMR gene detection outputs using the hAMRonization specification
5
5
  Home-page: https://github.com/pha4ge/hAMRonization
6
- Download-URL: https://github.com/pha4ge/archive/v1.1.8.tar.gz
6
+ Download-URL: https://github.com/pha4ge/archive/v1.2.1.tar.gz
7
7
  Author: Finlay Maguire
8
8
  Author-email: finlaymaguire@gmail.com
9
9
  License: LGPLv3
@@ -16,16 +16,31 @@ Classifier: Operating System :: MacOS
16
16
  Classifier: Operating System :: POSIX
17
17
  Classifier: Operating System :: POSIX :: Linux
18
18
  Classifier: Programming Language :: Python :: 3
19
- Classifier: Programming Language :: Python :: 3.7
20
- Classifier: Programming Language :: Python :: 3.8
21
- Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
22
24
  Classifier: Intended Audience :: Science/Research
23
25
  Classifier: Intended Audience :: Healthcare Industry
24
26
  Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
25
- Requires-Python: >=3.7
27
+ Requires-Python: >=3.10
26
28
  Description-Content-Type: text/markdown
27
29
  License-File: LICENSE.txt
28
30
  Requires-Dist: pandas
31
+ Dynamic: author
32
+ Dynamic: author-email
33
+ Dynamic: classifier
34
+ Dynamic: description
35
+ Dynamic: description-content-type
36
+ Dynamic: download-url
37
+ Dynamic: home-page
38
+ Dynamic: keywords
39
+ Dynamic: license
40
+ Dynamic: license-file
41
+ Dynamic: requires-dist
42
+ Dynamic: requires-python
43
+ Dynamic: summary
29
44
 
30
45
  ![Python package](https://github.com/pha4ge/hAMRonization/workflows/test_package/badge.svg)
31
46
  [![Preprint](https://img.shields.io/badge/Preprint-10.1101/2024.03.07.583950-blue)](https://doi.org/10.1101/2024.03.07.583950)
@@ -37,7 +52,7 @@ Requires-Dist: pandas
37
52
  # hAMRonization
38
53
 
39
54
  This repo contains the hAMRonization module and CLI parser tools combine the outputs of
40
- 18 disparate antimicrobial resistance gene detection tools into a single unified format.
55
+ 17 disparate antimicrobial resistance gene detection tools into a single unified format.
41
56
 
42
57
  This is an implementation of the [hAMRonization AMR detection specification scheme](docs/hAMRonization_specification_details.csv) which supports gene presence/absence resistance and mutational resistance (if supported by the underlying tool).
43
58
 
@@ -48,7 +63,7 @@ This supports a variety of summary options including an [interactive summary](ht
48
63
 
49
64
  ## Installation
50
65
 
51
- This tool requires python>=3.7 and [pandas](https://pandas.pydata.org/)
66
+ This tool requires python>=3.9 and [pandas](https://pandas.pydata.org/)
52
67
  and the latest release can be installed directly from pip, conda, docker, this repository, or from the galaxy toolshed:
53
68
  ```
54
69
  pip install hAMRonization
@@ -59,16 +74,17 @@ pip install hAMRonization
59
74
  Or
60
75
 
61
76
  ```
62
- conda create --name hamronization --channel conda-forge --channel bioconda --channel defaults hamronization
77
+ conda create --name hamronization --channel conda-forge --channel bioconda hamronization
63
78
  ```
64
79
  ![version-on-conda](https://anaconda.org/bioconda/hamronization/badges/version.svg)
65
80
  ![conda-download](https://anaconda.org/bioconda/hamronization/badges/downloads.svg)
66
81
  ![last-update-on-conda](https://anaconda.org/bioconda/hamronization/badges/latest_release_date.svg)
67
82
 
68
83
 
69
- Or to install using docker:
84
+ Or to install and run using docker, podman, singularity:
70
85
  ```
71
- docker pull finlaymaguire/hamronization:latest
86
+ docker pull docker.io/finlaymaguire/hamronization:latest
87
+ docker run --rm docker.io/finlaymaguire/hamronization:latest hamronize --help
72
88
  ```
73
89
 
74
90
  Or to install the latest development version:
@@ -95,7 +111,7 @@ options:
95
111
  -v, --version show program's version number and exit
96
112
 
97
113
  Tools with hAMRonizable reports:
98
- {abricate,amrfinderplus,amrplusplus,ariba,csstar,deeparg,fargene,groot,kmerresistance,resfams,resfinder,mykrobe,pointfinder,rgi,srax,srst2,staramr,tbprofiler,summarize}
114
+ {abricate,amrfinderplus,amrplusplus,ariba,csstar,deeparg,fargene,groot,kmerresistance,resfams,resfinder,mykrobe,rgi,srax,srst2,staramr,tbprofiler,summarize}
99
115
  abricate hAMRonize abricate's output report i.e., OUTPUT.tsv
100
116
  amrfinderplus hAMRonize amrfinderplus's output report i.e., OUTPUT.tsv
101
117
  amrplusplus hAMRonize amrplusplus's output report i.e., gene.tsv
@@ -109,11 +125,8 @@ Tools with hAMRonizable reports:
109
125
  report`)
110
126
  kmerresistance hAMRonize kmerresistance's output report i.e., OUTPUT.res
111
127
  resfams hAMRonize resfams's output report i.e., resfams.tblout
112
- resfinder hAMRonize resfinder's output report i.e.,
113
- ResFinder_results_tab.txt
128
+ resfinder hAMRonize resfinder's JSON output report (use -j to produce)
114
129
  mykrobe hAMRonize mykrobe's output report i.e., OUTPUT.json
115
- pointfinder hAMRonize pointfinder's output report i.e.,
116
- PointFinder_results.txt
117
130
  rgi hAMRonize rgi's output report i.e., OUTPUT.txt or
118
131
  OUTPUT_bwtoutput.gene_mapping_data.txt
119
132
  srax hAMRonize srax's output report i.e., sraX_detected_ARGs.tsv
@@ -222,7 +235,7 @@ If you want to write multiple reports to one file, this `.write` method can acce
222
235
  Currently implemented parsers and the last tool version for which they have been validated:
223
236
 
224
237
  1. [abricate](hAMRonization/AbricateIO.py): last updated for v1.0.0
225
- 2. [amrfinderplus](hAMRonization/AmrFinderPlusIO.py): last updated for v3.12.18
238
+ 2. [amrfinderplus](hAMRonization/AmrFinderPlusIO.py): last updated for v4.0.3
226
239
  3. [amrplusplus](hAMRonization/AmrPlusPlusIO.py): last updated for c6b097a
227
240
  4. [ariba](hAMRonization/AribaIO.py): last updated for v2.14.6
228
241
  5. [csstar](hAMRonization/CSStarIO.py): last updated for v2.1.0
@@ -231,7 +244,7 @@ Currently implemented parsers and the last tool version for which they have been
231
244
  8. [groot](hAMRonization/GrootIO.py): last updated for v1.1.2
232
245
  9. [kmerresistance](hAMRonization/KmerResistanceIO.py): late updated for v2.2.0
233
246
  10. [mykrobe](test/data/raw_outputs/mykrobe/report.json): last updated for v0.8.1
234
- 11. [pointfinder](hAMRonization/PointFinderIO.py): last updated for v4.1.0
247
+ 11. ~pointfinder~ (removed, PointFinder is now integrated in ResFinder)
235
248
  12. [resfams](hAMRonization/ResFamsIO.py): last updated for hmmer v3.3.2
236
249
  13. [resfinder](hAMRonization/ResFinderIO.py): last updated for v4.6.0
237
250
  14. [rgi](hAMRonization/RgiIO.py) (includes RGI-BWT) last updated for v5.2.0
@@ -292,13 +305,12 @@ First fork this repository and set up a development environment (replacing `YOUR
292
305
 
293
306
  ```
294
307
  git clone https://github.com/YOURUSERNAME/hAMRonization
295
- conda create -n hAMRonization
308
+ conda create -n hAMRonization python pip pytest flake8
296
309
  conda activate hAMRonization
297
310
  cd hAMRonization
298
- pip install pytest flake8
299
311
  pip install -e .
300
-
301
312
  ```
313
+
302
314
  ## Testing and Linting
303
315
 
304
316
  On every commit github actions automatically runs tests and linting to check
@@ -8,7 +8,7 @@
8
8
  # hAMRonization
9
9
 
10
10
  This repo contains the hAMRonization module and CLI parser tools combine the outputs of
11
- 18 disparate antimicrobial resistance gene detection tools into a single unified format.
11
+ 17 disparate antimicrobial resistance gene detection tools into a single unified format.
12
12
 
13
13
  This is an implementation of the [hAMRonization AMR detection specification scheme](docs/hAMRonization_specification_details.csv) which supports gene presence/absence resistance and mutational resistance (if supported by the underlying tool).
14
14
 
@@ -19,7 +19,7 @@ This supports a variety of summary options including an [interactive summary](ht
19
19
 
20
20
  ## Installation
21
21
 
22
- This tool requires python>=3.7 and [pandas](https://pandas.pydata.org/)
22
+ This tool requires python>=3.9 and [pandas](https://pandas.pydata.org/)
23
23
  and the latest release can be installed directly from pip, conda, docker, this repository, or from the galaxy toolshed:
24
24
  ```
25
25
  pip install hAMRonization
@@ -30,16 +30,17 @@ pip install hAMRonization
30
30
  Or
31
31
 
32
32
  ```
33
- conda create --name hamronization --channel conda-forge --channel bioconda --channel defaults hamronization
33
+ conda create --name hamronization --channel conda-forge --channel bioconda hamronization
34
34
  ```
35
35
  ![version-on-conda](https://anaconda.org/bioconda/hamronization/badges/version.svg)
36
36
  ![conda-download](https://anaconda.org/bioconda/hamronization/badges/downloads.svg)
37
37
  ![last-update-on-conda](https://anaconda.org/bioconda/hamronization/badges/latest_release_date.svg)
38
38
 
39
39
 
40
- Or to install using docker:
40
+ Or to install and run using docker, podman, singularity:
41
41
  ```
42
- docker pull finlaymaguire/hamronization:latest
42
+ docker pull docker.io/finlaymaguire/hamronization:latest
43
+ docker run --rm docker.io/finlaymaguire/hamronization:latest hamronize --help
43
44
  ```
44
45
 
45
46
  Or to install the latest development version:
@@ -66,7 +67,7 @@ options:
66
67
  -v, --version show program's version number and exit
67
68
 
68
69
  Tools with hAMRonizable reports:
69
- {abricate,amrfinderplus,amrplusplus,ariba,csstar,deeparg,fargene,groot,kmerresistance,resfams,resfinder,mykrobe,pointfinder,rgi,srax,srst2,staramr,tbprofiler,summarize}
70
+ {abricate,amrfinderplus,amrplusplus,ariba,csstar,deeparg,fargene,groot,kmerresistance,resfams,resfinder,mykrobe,rgi,srax,srst2,staramr,tbprofiler,summarize}
70
71
  abricate hAMRonize abricate's output report i.e., OUTPUT.tsv
71
72
  amrfinderplus hAMRonize amrfinderplus's output report i.e., OUTPUT.tsv
72
73
  amrplusplus hAMRonize amrplusplus's output report i.e., gene.tsv
@@ -80,11 +81,8 @@ Tools with hAMRonizable reports:
80
81
  report`)
81
82
  kmerresistance hAMRonize kmerresistance's output report i.e., OUTPUT.res
82
83
  resfams hAMRonize resfams's output report i.e., resfams.tblout
83
- resfinder hAMRonize resfinder's output report i.e.,
84
- ResFinder_results_tab.txt
84
+ resfinder hAMRonize resfinder's JSON output report (use -j to produce)
85
85
  mykrobe hAMRonize mykrobe's output report i.e., OUTPUT.json
86
- pointfinder hAMRonize pointfinder's output report i.e.,
87
- PointFinder_results.txt
88
86
  rgi hAMRonize rgi's output report i.e., OUTPUT.txt or
89
87
  OUTPUT_bwtoutput.gene_mapping_data.txt
90
88
  srax hAMRonize srax's output report i.e., sraX_detected_ARGs.tsv
@@ -193,7 +191,7 @@ If you want to write multiple reports to one file, this `.write` method can acce
193
191
  Currently implemented parsers and the last tool version for which they have been validated:
194
192
 
195
193
  1. [abricate](hAMRonization/AbricateIO.py): last updated for v1.0.0
196
- 2. [amrfinderplus](hAMRonization/AmrFinderPlusIO.py): last updated for v3.12.18
194
+ 2. [amrfinderplus](hAMRonization/AmrFinderPlusIO.py): last updated for v4.0.3
197
195
  3. [amrplusplus](hAMRonization/AmrPlusPlusIO.py): last updated for c6b097a
198
196
  4. [ariba](hAMRonization/AribaIO.py): last updated for v2.14.6
199
197
  5. [csstar](hAMRonization/CSStarIO.py): last updated for v2.1.0
@@ -202,7 +200,7 @@ Currently implemented parsers and the last tool version for which they have been
202
200
  8. [groot](hAMRonization/GrootIO.py): last updated for v1.1.2
203
201
  9. [kmerresistance](hAMRonization/KmerResistanceIO.py): late updated for v2.2.0
204
202
  10. [mykrobe](test/data/raw_outputs/mykrobe/report.json): last updated for v0.8.1
205
- 11. [pointfinder](hAMRonization/PointFinderIO.py): last updated for v4.1.0
203
+ 11. ~pointfinder~ (removed, PointFinder is now integrated in ResFinder)
206
204
  12. [resfams](hAMRonization/ResFamsIO.py): last updated for hmmer v3.3.2
207
205
  13. [resfinder](hAMRonization/ResFinderIO.py): last updated for v4.6.0
208
206
  14. [rgi](hAMRonization/RgiIO.py) (includes RGI-BWT) last updated for v5.2.0
@@ -263,13 +261,12 @@ First fork this repository and set up a development environment (replacing `YOUR
263
261
 
264
262
  ```
265
263
  git clone https://github.com/YOURUSERNAME/hAMRonization
266
- conda create -n hAMRonization
264
+ conda create -n hAMRonization python pip pytest flake8
267
265
  conda activate hAMRonization
268
266
  cd hAMRonization
269
- pip install pytest flake8
270
267
  pip install -e .
271
-
272
268
  ```
269
+
273
270
  ## Testing and Linting
274
271
 
275
272
  On every commit github actions automatically runs tests and linting to check
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env python
2
+
3
+ import csv
4
+ import warnings
5
+ import re
6
+ from .Interfaces import hAMRonizedResultIterator
7
+ from hAMRonization.constants import (
8
+ NUCLEOTIDE_VARIANT,
9
+ AMINO_ACID_VARIANT,
10
+ GENE_PRESENCE,
11
+ )
12
+
13
+ required_metadata = [
14
+ "analysis_software_version",
15
+ "reference_database_version",
16
+ "input_file_name",
17
+ ]
18
+
19
+
20
+ class AmrFinderPlusIterator(hAMRonizedResultIterator):
21
+
22
+ nuc_field_map = {
23
+ "Protein id": None,
24
+ "Contig id": "input_sequence_id",
25
+ "Start": "input_gene_start",
26
+ "Stop": "input_gene_stop",
27
+ "Strand": "strand_orientation",
28
+ "Element symbol": "gene_symbol",
29
+ "Element name": "gene_name",
30
+ "Scope": None,
31
+ "Type": None,
32
+ "Subtype": None,
33
+ "Class": "drug_class",
34
+ "Subclass": "antimicrobial_agent",
35
+ "Method": None,
36
+ "Target length": "input_gene_length",
37
+ "Reference sequence length": "reference_gene_length",
38
+ "% Coverage of reference": "coverage_percentage",
39
+ "% Identity to reference": "sequence_identity",
40
+ "Alignment length": None,
41
+ "Closest reference accession": "reference_accession",
42
+ "Closest reference name": None,
43
+ "HMM accession": None,
44
+ "HMM description": None,
45
+ "Hierarchy node": None,
46
+ # Fields we compute below (not in TSV)
47
+ "amino_acid_mutation": "amino_acid_mutation",
48
+ "nucleotide_mutation": "nucleotide_mutation",
49
+ "genetic_variation_type": "genetic_variation_type",
50
+ }
51
+
52
+ # AMP outputs the same column set for nuc and prot detections,
53
+ # with Start and Stop always in nt units; however target and
54
+ # reference length are reported in AA for proteins.
55
+ prot_field_map = nuc_field_map.copy()
56
+ prot_field_map.update({
57
+ "Target length": "input_protein_length",
58
+ "Reference sequence length": "reference_protein_length"
59
+ })
60
+
61
+ def __init__(self, source, metadata):
62
+ metadata["analysis_software_name"] = "amrfinderplus"
63
+ metadata["reference_database_name"] = "NCBI Reference Gene Database"
64
+ self.metadata = metadata
65
+
66
+ # We pass None for the field_map as it differs depending on
67
+ # whether we return a nucleotide or protein variant detection.
68
+ # TODO: refactor field_map out of super's constructor, and make
69
+ # it a parameter on super's hARMonize().
70
+ super().__init__(source, None, self.metadata)
71
+
72
+ def parse(self, handle):
73
+ """
74
+ Read each and return it
75
+ """
76
+ skipped_truncated = 0
77
+ reader = csv.DictReader(handle, delimiter="\t")
78
+ for result in reader:
79
+
80
+ # Replace NA value with None for consistency
81
+ for field, value in result.items():
82
+ if value == "NA":
83
+ result[field] = None
84
+
85
+ # Skip reported virulence genes
86
+ if result['Type'] == "VIRULENCE":
87
+ continue
88
+
89
+ # AFP reports partial hits so to avoid misleadingly listing these
90
+ # as present skip results with INTERNAL_STOP
91
+ # recommended by developers
92
+ if "INTERNAL_STOP" in result['Method']:
93
+ skipped_truncated += 1
94
+ continue
95
+
96
+ # "POINT" indicates mutational resistance
97
+ # amrfinderplus has no special fields but the mutation itself is
98
+ # appended to the symbol name so we want to split this
99
+ result['amino_acid_mutation'] = None
100
+ result['nucleotide_mutation'] = None
101
+ result['genetic_variation_type'] = GENE_PRESENCE
102
+
103
+ if result['Subtype'] == "POINT":
104
+ gene_symbol, mutation = result['Element symbol'].rsplit("_", 1)
105
+ result['Element symbol'] = gene_symbol
106
+ _, ref, pos, alt, _ = re.split(r"(\D+)(\d+)(\D+)", mutation)
107
+ # this means it is a protein mutation
108
+ if result['Method'] in ["POINTX", "POINTP"]:
109
+ result['amino_acid_mutation'] = f"p.{ref}{pos}{alt}"
110
+ result['genetic_variation_type'] = AMINO_ACID_VARIANT
111
+ elif result['Method'] == "POINTN":
112
+ # e.g., 23S_G2032G ampC_C-11C -> c.2032G>G
113
+ result['nucleotide_mutation'] = f"c.{pos}{ref}>{alt}"
114
+ result['genetic_variation_type'] = NUCLEOTIDE_VARIANT
115
+
116
+ # Determine the field_map to use depending on the method used
117
+ # The following seems to cover all bases with a minimum of fuss
118
+ have_prot = result['Protein id'] is not None
119
+ method = result['Method']
120
+ if method.endswith('P') or method.endswith('X'):
121
+ field_map = self.prot_field_map
122
+ elif method.endswith('N'):
123
+ field_map = self.nuc_field_map
124
+ elif method in ['COMPLETE', 'HMM']:
125
+ field_map = self.prot_field_map if have_prot else self.nuc_field_map
126
+ else:
127
+ warnings.warn(f"Assuming unknown method {method} implies a protein detection"
128
+ f" in {self.metadata['input_file_name']}")
129
+ field_map = self.prot_field_map
130
+
131
+ # This uses the "override hack" that should perhaps be cleaned up
132
+ yield self.hAMRonize(result, self.metadata, field_map)
133
+
134
+ if skipped_truncated > 0:
135
+ warnings.warn(f"Skipping {skipped_truncated} records with INTERNAL_STOP "
136
+ f"from {self.metadata['input_file_name']}")
@@ -50,16 +50,24 @@ class hAMRonizedResultIterator(ABC):
50
50
  except Exception:
51
51
  self.stream.close()
52
52
 
53
- def hAMRonize(self, report_data, metadata):
53
+ # TODO: the field_map_override is a half-hack to support the scenario
54
+ # (as for amrfinderplus) where different records need different mappings,
55
+ # so setting a field_map in the constructor makes no sense.
56
+ # It might be cleaner to remove it from the constructor altogether and
57
+ # make it a parameter of this method (which is the only place where it
58
+ # is referenced anyway), and subclasses can trivially pass it in.
59
+ def hAMRonize(self, report_data, metadata, field_map_override=None):
54
60
  """
55
61
  Convert a line of parsed AMR report in original format to the
56
62
  hAMRonization specification
57
- - report_result parsed dict of single results from report
58
- - metadata dict of additional metadata fields that need added
63
+ - report_data parsed dict of single result from report
64
+ - metadata dict of additional metadata fields
65
+ - field_map_override optional override of field_map passed in c'tor
59
66
  """
60
67
  hAMRonized_result_data = {**metadata}
61
68
 
62
- for original_field, hAMRonized_field in self.field_map.items():
69
+ field_map = field_map_override or self.field_map
70
+ for original_field, hAMRonized_field in field_map.items():
63
71
  if hAMRonized_field:
64
72
  hAMRonized_result_data[hAMRonized_field] = report_data[original_field]
65
73
 
@@ -115,7 +123,7 @@ class hAMRonizedResultIterator(ABC):
115
123
  # to get first result to build csvwriter
116
124
  try:
117
125
  first_result = next(self)
118
- fieldnames = first_result.__annotations__.keys()
126
+ fieldnames = [f.name for f in dataclasses.fields(type(first_result))]
119
127
  writer = csv.DictWriter(
120
128
  out_fh,
121
129
  delimiter="\t",
@@ -75,6 +75,8 @@ class MykrobeIterator(hAMRonizedResultIterator):
75
75
  data = json.load(handle)
76
76
 
77
77
  sample_names = list(data.keys())
78
+ if len(sample_names) == 0:
79
+ return # take an empty JSON object as a success but with no results
78
80
  assert len(sample_names) == 1, "can only parse output with a single "
79
81
  "sample currently, found {}".format(len(sample_names))
80
82
  sample_name = sample_names[0]
@@ -26,7 +26,7 @@ class ResFinderIterator(hAMRonizedResultIterator):
26
26
  def __init__(self, source, metadata):
27
27
 
28
28
  # We don't use metadata or field mapping so can just defer to super,
29
- # which will open source and invoke our parse method with the open stream.
29
+ # which opens source and invokes our parse() on the open stream.
30
30
  super().__init__(source, dict(), metadata)
31
31
 
32
32
  def parse(self, handle):
@@ -42,7 +42,7 @@ class ResFinderIterator(hAMRonizedResultIterator):
42
42
  # Input data read in ResFinder 4.2+ JSON format. This has three main elements:
43
43
  # - seq_regions: loci/genes that were found, keying into 0 or more phenotypes
44
44
  # - seq_variations: mutations that key into a seq_region and 0 or more phenotypes
45
- # - phenotypes: antimicriobals keying back into the above objects
45
+ # - phenotypes: antimicrobials keying back into the above objects
46
46
  data = json.load(handle)
47
47
 
48
48
  # Helpers to fetch database names and versions from the JSON data
@@ -63,17 +63,19 @@ class ResFinderIterator(hAMRonizedResultIterator):
63
63
  res.gene_symbol = r.get('name', "unspecified")
64
64
  res.gene_name = r.get('name', "unspecified")
65
65
  res.reference_accession = r.get('ref_acc', r.get('ref_id', r.get('key', "unknown")))
66
+ res.reference_database_name = _get_db_name(r.get('ref_database'))
67
+ res.reference_database_version = _get_db_ver(r.get('ref_database'))
66
68
 
67
69
  # optional
68
70
  res.coverage_percentage = _safe_round(r.get('coverage'), 1)
69
71
  res.coverage_depth = None # we may have this for mutations detected from reads
70
- res.coverage_ratio = r.get('coverage')/100.0
72
+ res.coverage_ratio = None
71
73
  res.input_sequence_id = r.get('query_id')
72
74
  res.input_gene_length = _get_length(r.get('query_start_pos'), r.get('query_end_pos'))
73
75
  res.input_gene_start = _get_start_pos(r.get('query_start_pos'), r.get('query_end_pos'))
74
76
  res.input_gene_stop = _get_end_pos(r.get('query_start_pos'), r.get('query_end_pos'))
75
77
  res.strand_orientation = _get_strand(r.get('query_start_pos'), r.get('query_end_pos'))
76
- res.predicted_phenotype = _empty_to_none(", ".join(r.get('phenotypes', [])))
78
+ res.predicted_phenotype = 'antimicrobial resistance' # we report only resistant phenotypes
77
79
  res.predicted_phenotype_confidence_level = _condense_notes(r.get('notes'), r.get('pmids'))
78
80
  res.reference_gene_length = r.get('ref_seq_length')
79
81
  res.reference_gene_start = r.get('ref_start_pos')
@@ -89,20 +91,19 @@ class ResFinderIterator(hAMRonizedResultIterator):
89
91
 
90
92
  # Setter for the hAMRonizedResult fields related to mutations
91
93
  def set_variation_fields(r, vs):
92
- """Sets the mutation-specific fields in res, aggregating from all variations in vs on region r."""
94
+ """Sets the mutation-specific fields in res, aggregating from all variations vs."""
93
95
 
94
96
  # Bags to collect variations, phenotypes and notes across the variations
95
97
  _aa_vars = list()
96
98
  _nt_vars = list()
97
- _codon = list()
98
- _phenos = set()
99
+ _codons = list()
99
100
  _notes = set()
100
101
  _pmids = set()
101
102
 
102
- # Iterate v over the variations in vs that lie on region r in order of their position
103
- # (variation->regions strangely is a list, so we need to check if r.key is in it)
104
- for v in sorted(filter(lambda v: r['key'] in v.get('seq_regions', []), vs),
105
- key=lambda v: v.get('ref_start_pos', 0)):
103
+ res.genetic_variation_type = NUCLEOTIDE_VARIANT
104
+
105
+ # Iterate v over the variations in vs in order of their position
106
+ for v in sorted(vs, key=lambda v: v.get('ref_start_pos', 0)):
106
107
 
107
108
  # May need refinement to properly accommodate inserts and deletes,
108
109
  # though it seems recent Res/PointFinder output uses HGVS coordinates.
@@ -115,19 +116,17 @@ class ResFinderIterator(hAMRonizedResultIterator):
115
116
 
116
117
  _cod_chg = v.get('codon_change')
117
118
  if _cod_chg:
118
- _codon.append(v.get('codon_change'))
119
+ _codons.append(_cod_chg)
119
120
 
120
121
  # Add the content of the list fields to the bags above
121
- fold(lambda s, e: s.add(e), _phenos, v.get('phenotypes', []))
122
- fold(lambda s, e: s.add(e), _notes, v.get('notes', []))
123
- fold(lambda s, e: s.add(e), _pmids, v.get('pmids', []))
122
+ _notes.update(v.get('notes', []))
123
+ _pmids.update(v.get('pmids', []))
124
124
 
125
125
  # We have collected all variations on region r, now collapse into fields on res
126
- res.predicted_phenotype = _empty_to_none(", ".join(filter(None, _phenos)))
127
126
  res.predicted_phenotype_confidence_level = _condense_notes(_notes, _pmids)
128
127
  res.amino_acid_mutation = _empty_to_none(", ".join(filter(None, _aa_vars)))
129
128
  res.nucleotide_mutation = _empty_to_none(", ".join(filter(None, _nt_vars)))
130
- res.nucleotide_mutation_interpretation = ("Codon changes: " + " ".join(_codon)) if _codon else None
129
+ res.nucleotide_mutation_interpretation = ("Codon changes: " + " ".join(_codons)) if _codons else None
131
130
 
132
131
  # --- Do the actual work --- #
133
132
 
@@ -136,44 +135,64 @@ class ResFinderIterator(hAMRonizedResultIterator):
136
135
  res.analysis_software_name = data['software_name']
137
136
  res.analysis_software_version = data['software_version']
138
137
 
139
- # We flatten the ResFinder data graph as follows
140
- # - iterate over all phenotypes p (generally: antimicrobials) that have amr_resistant=true
141
- # - iterate over the seq_regions r referenced by p (generally: resistance genes)
142
- # - for each r report a GENE_PRESENCE
143
- # - group the seq_variations referenced by p by the seq_region r they lie on
144
- # - iterate over the regions r
145
- # - for each r report one AMINO_ACID_VARIANT record, collapsing the seq_variations
146
- for p in filter(lambda d: d.get('amr_resistant', False), data['phenotypes'].values()):
147
-
148
- # Set the fields available on phenotype object
149
- res.drug_class = ", ".join(p.get('amr_classes', []))
150
- res.antimicrobial_agent = p.get('amr_resistance', "unspecified")
151
- res.reference_database_name = _get_db_name(p.get('ref_database'))
152
- res.reference_database_version = _get_db_ver(p.get('ref_database'))
153
-
154
- # Iterate r over the regions (AMR genes) referenced by p, and yield each in turn
155
- for r in map(lambda k: data['seq_regions'][k], p.get('seq_regions', [])):
156
-
138
+ # To obtain the AMR genes, we flatten the ResFinder data graph as follows
139
+ # - iterate over each region r
140
+ # - iterate over phenotypes p that reference region r and are amr_resistant
141
+ # - collect their amr_classes and antimicrobials
142
+ # - emit a GENE_PRESENCE record if any AMR was found
143
+ for r in data['seq_regions'].values():
144
+ amr_cls = set()
145
+ amr_res = set()
146
+
147
+ # Iterate p over the phenotypes that reference r and have amr_resistant set true
148
+ # and collect their AMR classes and antimicrobials
149
+ for p in filter(lambda p: r['key'] in p.get('seq_regions', [])
150
+ and p.get('amr_resistant', False), data['phenotypes'].values()):
151
+ amr_cls.update(p.get('amr_classes', []))
152
+ amr_res.add(p.get('amr_resistance', "unspecified"))
153
+
154
+ # If we collected any AMR we emit the region as a GENE_PRESENCE record
155
+ if amr_cls or amr_res:
156
+
157
+ # Set the fields collected from the phenotypes and from the region object
157
158
  res.genetic_variation_type = GENE_PRESENCE
159
+ res.drug_class = ", ".join(sorted(amr_cls))
160
+ res.antimicrobial_agent = ", ".join(sorted(amr_res))
158
161
  set_shared_fields(r)
159
162
 
160
- # Yield a new hAMRonizedResult ours using super's method as that may do the needful
163
+ # Yield a new hAMRonizedResult using super's method as that may do the needful
161
164
  yield self.hAMRonize(None, res.__dict__)
162
165
 
163
- # Collect the list of seq_variations (if any) referenced from phenotype p,
164
- # and the set of regions that these mutations lie on, so that we iterate
165
- # these regions and "collapse" all mutations for that region onto one record
166
- vs = list(map(lambda k: data['seq_variations'][k], p.get('seq_variations', [])))
167
- rs = set(fold(lambda a, v: a + v.get('seq_regions', []), [], vs))
168
-
169
- # Iterate r over each region referenced by some set of variations, and yield each
170
- for r in map(lambda k: data['seq_regions'][k], rs):
171
-
166
+ # For the variants things are slightly more involved, as phenotypes don't reference
167
+ # seq_regions directly, but through seq_variations. We have some indirection here.
168
+
169
+ for r in data['seq_regions'].values():
170
+ amr_cls = set()
171
+ amr_res = set()
172
+ vs_dict = dict()
173
+
174
+ # We want to collect all variations vs that reference region r AND are referenced
175
+ # by a phenotype p that is amr_resistant. Along the way we collect from the p
176
+ # the AMR classes and antimicriobials (to save us another iteration)
177
+ for v in filter(lambda v: r['key'] in v.get('seq_regions', []), data['seq_variations'].values()):
178
+ for p in filter(lambda p: v['key'] in p.get('seq_variations', [])
179
+ and (p.get('amr_classes') or p.get('amr_resistance'))
180
+ and p.get('amr_resistant', False), data['phenotypes'].values()):
181
+ amr_cls.update(p.get('amr_classes', []))
182
+ amr_res.add(p.get('amr_resistance', "unspecified"))
183
+ vs_dict[v['key']] = v # need to do this in inner loop but dups will squish
184
+
185
+ # If we collected variants with resistant phenotypes then emit a record
186
+ if vs_dict:
187
+
188
+ # Set fields we collected plus the region and variant ones as above
172
189
  res.genetic_variation_type = NUCLEOTIDE_VARIANT # default may be overridden
190
+ res.drug_class = ", ".join(sorted(amr_cls))
191
+ res.antimicrobial_agent = ", ".join(sorted(amr_res))
173
192
  set_shared_fields(r)
174
- set_variation_fields(r, vs)
193
+ set_variation_fields(r, vs_dict.values())
175
194
 
176
- # Yield a new hAMRonizedResult ours using super's method as that may do the needful
195
+ # Yield a new hAMRonizedResult using super's method as that may do the needful
177
196
  yield self.hAMRonize(None, res.__dict__)
178
197
 
179
198
 
@@ -209,7 +228,7 @@ def _condense_notes(notes, pmids):
209
228
  lines += filter(None, notes)
210
229
  pmids = list(filter(None, pmids))
211
230
  if pmids:
212
- lines.append("PMIDs: " + ", ".join(set(pmids)))
231
+ lines.append("PMIDs: " + ", ".join(sorted(set(pmids))))
213
232
  return ". ".join(lines) if lines else None
214
233
 
215
234