Skip to content

Commit

Permalink
Merge pull request #100 from Clinical-Genomics-Lund/99-prp-crashed-if…
Browse files Browse the repository at this point in the history
…-mlst-type-was-not-called

Fixed that MLST results without called alleles crashed PRP
  • Loading branch information
mhkc authored Nov 19, 2024
2 parents 5c36e1b + 32ec8d7 commit b21c70f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### Fixed

- Fixed issue where mlst results with no calls crashed PRP.

### Changed

- Added mypy as test dependency

### Changed

## [0.11.1]
Expand Down
5 changes: 4 additions & 1 deletion prp/parse/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ def parse_mlst_results(mlst_fpath: str) -> TypingResultMlst:
LOG.info("Parsing mlst results")
with open(mlst_fpath, "r", encoding="utf-8") as jsonfile:
result = json.load(jsonfile)[0]
# get raw allele info
alleles = {} if result.get("alleles") is None else result["alleles"]
# create typing result object
result_obj = TypingResultMlst(
scheme=result["scheme"],
sequence_type=(
None if result["sequence_type"] == "-" else result["sequence_type"]
),
alleles={
gene: _process_allele_call(allele)
for gene, allele in result["alleles"].items()
for gene, allele in alleles.items()
},
)
return MethodIndex(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dev = [
]
test = [
"pytest-cov ~=4.1.0",
"mypy == 1.13.0"
]

[build-system]
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
from .saureus import *
from .shigella import *
from .streptococcus import *

@pytest.fixture()
def mlst_result_path_no_call(data_path):
"""Get path for mlst file where alleles was not called."""
return str(data_path.joinpath("mlst.nocall.json"))
9 changes: 9 additions & 0 deletions tests/fixtures/mlst.nocall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"sequence_type" : "-",
"scheme" : "-",
"alleles" : null,
"id" : "unique_file_id",
"filename" : "assembly_file_name.fasta"
}
]
28 changes: 26 additions & 2 deletions tests/parse/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
import logging
from prp.parse.typing import replace_cgmlst_errors
from prp.parse.typing import replace_cgmlst_errors, parse_mlst_results
from prp.models.typing import ChewbbacaErrors

# build test cases for handeling chewbacca allele caller errors and annotations
Expand Down Expand Up @@ -84,4 +84,28 @@ def test_replace_cgmlst_errors_warnings(caplog):
# run test that a warning was triggered if input is unknown string
allele = "A_STRANGE_STRING"
replace_cgmlst_errors(allele, include_novel_alleles=True, correct_alleles=True)
assert allele in caplog.text
assert allele in caplog.text


def test_parse_mlst_result(ecoli_mlst_path):
"""Test parsing of MLST result file."""
# FIRST run result parser
res_obj = parse_mlst_results(ecoli_mlst_path)

# THEN verify result type
assert res_obj.type == 'mlst'
# THEN verify software
assert res_obj.software == 'mlst'
# THEN verify sequence type and allele assignment
assert res_obj.result.sequence_type == 58
assert len(res_obj.result.alleles) == 8


def test_parse_mlst_result_w_no_call(mlst_result_path_no_call):
"""Test parsing of MLST results file where the alleles was not called."""
# FIRST run result parser
res_obj = parse_mlst_results(mlst_result_path_no_call)

# THEN verify that sequence type is None
assert res_obj.result.sequence_type is None
assert len(res_obj.result.alleles) == 0

0 comments on commit b21c70f

Please sign in to comment.