Fix AttributeError when AMRFinderPlus subclass is missing from CARD conversion table - #34
Closed
sanjaynagi-eit wants to merge 1 commit into
Closed
Conversation
…ion table Fixes AMRverse#28 _assign_drug_from_amrfp() called card_amrfp_conversion.get(self.amrfp_subclass) and then immediately chained .get('drug', '-') / .get('class', '-') onto the result. When the AMRFinderPlus subclass has no entry in the hand-maintained amrfp_to_card_drugs_classes.txt table, the outer .get() returns None and the chained .get() raises AttributeError: 'NoneType' object has no attribute 'get', crashing the whole run instead of falling through to the existing "unassigned markers" fallback path a few lines below. This now defaults the lookup to an empty dict when the subclass is missing (matching the .get('drug class', '-') fallback pattern already used in _assign_drug_from_rule), and emits a warnings.warn() so a gap in the conversion table is visible instead of silent.
Author
|
Re-opened as #35 from a different account. No code changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #28
The bug
Genotype._assign_drug_from_amrfp()insrc/amrrules/genotype_parser.pylooked up the AMRFinderPlus subclass incard_amrfp_conversionand immediately chained.get('drug', '-')/.get('class', '-')onto the result:card_amrfp_conversionis built from the bundled, hand-maintainedamrfp_to_card_drugs_classes.txtresource, which will always lag behind whatever AMRFinderPlus/NCBI database version is actually in use. Whenself.amrfp_subclassisn't a key in that table, the outer.get()returnsNone, and the chained.get('drug', '-')call raises:This crashes the entire run instead of falling through to the "unassigned markers" fallback path that already exists a few lines below (
if self.drug_class == '-': self.drug_class = 'unassigned markers'), which is clearly the designed-for behaviour for "we don't know the drug class".As reported in #28, this is hit by
FUSIDIC_ACID(underscore) vsFUSIDIC ACID(space) in the AMRFinderPlus subclass forfusApoint mutations. We independently hit the same crash in a production pipeline: 19 failed runs, all on S. aureus samples where AMRFinderPlus reported anfusAsubclass not present in the bundled conversion table.The fix
Default the lookup to an empty dict when the subclass isn't found, so
drug/drug_classfall back to'-'(and then to'unassigned markers'via the existing logic) instead of crashing:This mirrors the
.get(..., '-')fallback pattern already used in the neighbouring_assign_drug_from_rule()method, and useswarnings.warn()(the logging mechanism already used elsewhere in the codebase, e.g.src/amrrules/utils.py) so a gap in the conversion table is surfaced instead of silently swallowed.Testing
I didn't find an existing
pytest-style unit test suite forgenotype_parser.py— the repo'stests/directory is data-driven, comparing full CLI output against example files (docs/source/tests.rst), which felt too heavyweight to extend correctly for this narrow change without risking an unrelated/incorrect fixture update. Manually verified with a minimal reproduction of the reported traceback (an unmapped subclass, e.g.FUSIDIC_ACID) that the code now falls through todrug_class = 'unassigned markers'instead of raising.Happy to add a regression test if you can point me to the preferred harness for this module.