Skip to content

Commit

Permalink
formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkc committed Dec 5, 2023
1 parent ad7657b commit 0e63196
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
16 changes: 12 additions & 4 deletions prp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import click
from pydantic import ValidationError

from .models.metadata import SoupVersion, SoupType
from .models.phenotype import ElementType, ElementStressSubtype
from .models.metadata import SoupType, SoupVersion
from .models.phenotype import ElementStressSubtype, ElementType
from .models.qc import QcMethodIndex
from .models.sample import MethodIndex, PipelineResult
from .models.typing import TypingMethod
from .parse.metadata import get_database_info, parse_run_info
from .parse import (
parse_amrfinder_amr_pred,
parse_amrfinder_vir_pred,
Expand All @@ -27,6 +26,7 @@
parse_tbprofiler_lineage_results,
parse_virulencefinder_vir_pred,
)
from .parse.metadata import get_database_info, parse_run_info

logging.basicConfig(
level=logging.INFO, format="[%(asctime)s] %(levelname)s in %(module)s: %(message)s"
Expand Down Expand Up @@ -55,7 +55,6 @@ def cli():
"-d",
"--process-metadata",
type=click.File(),
required=True,
multiple=True,
help="Nextflow processes metadata from the pipeline in json format",
)
Expand Down Expand Up @@ -181,6 +180,15 @@ def create_output(
if mykrobe:
LOG.info("Parse mykrobe results")
pred_res = json.load(mykrobe)

# verify that sample id is in prediction result
if not sample_id in pred_res:
LOG.warning(
"Sample id %s is not in Mykrobe result, possible sample mixup",
sample_id,
)
raise click.Abort()

results["run_metadata"]["databases"].append(
SoupVersion(
name="mykrobe-predictor",
Expand Down
2 changes: 1 addition & 1 deletion prp/models/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Generic database objects of which several other models are based on."""
from pydantic import ConfigDict, BaseModel
from pydantic import BaseModel, ConfigDict


class RWModel(BaseModel): # pylint: disable=too-few-public-methods
Expand Down
1 change: 1 addition & 0 deletions prp/models/phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ElementVirulenceSubtype(Enum):

VIR = "VIRULENCE"


class DatabaseReference(RWModel):
"""Refernece to a database."""

Expand Down
5 changes: 3 additions & 2 deletions prp/parse/metadata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Parse metadata passed to pipeline."""
from ..models.metadata import SoupVersion, RunInformation, RunMetadata
from typing import List, TextIO
import json
import logging
from typing import List, TextIO

from ..models.metadata import RunInformation, RunMetadata, SoupVersion

LOG = logging.getLogger(__name__)

Expand Down
8 changes: 4 additions & 4 deletions prp/parse/phenotype/amrfinder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Parse AMRfinder plus result."""
import logging
from typing import Tuple

import logging
import pandas as pd

from ...models.phenotype import ElementType, ElementTypeResult
Expand Down Expand Up @@ -65,9 +65,9 @@ def parse_amrfinder_amr_pred(file: str, element_type: ElementType) -> ElementTyp
hits = hits.drop(columns=["Protein identifier", "HMM id", "HMM description"])
hits = hits.where(pd.notnull(hits), None)
# group predictions based on their element type
predictions = (hits
.loc[lambda row: row.element_type == element_type]
.to_dict(orient="records"))
predictions = hits.loc[lambda row: row.element_type == element_type].to_dict(
orient="records"
)
results: ElementTypeResult = _parse_amrfinder_amr_results(predictions)
return MethodIndex(type=element_type, result=results, software=Software.AMRFINDER)

Expand Down
2 changes: 1 addition & 1 deletion prp/parse/phenotype/mykrobe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Parse Mykrobe results."""
import logging
import re
from typing import Tuple, Dict, Any
from typing import Any, Dict, Tuple

from ...models.metadata import SoupVersions
from ...models.phenotype import ElementTypeResult
Expand Down
22 changes: 15 additions & 7 deletions prp/parse/phenotype/resfinder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""Parse resfinder results."""
import logging
from typing import Any, Dict, Tuple, List

from itertools import chain
from typing import Any, Dict, List, Tuple

from ...models.metadata import SoupVersions
from ...models.phenotype import ElementType, ElementAmrSubtype, ElementStressSubtype, ElementTypeResult
from ...models.phenotype import (
ElementAmrSubtype,
ElementStressSubtype,
ElementType,
ElementTypeResult,
)
from ...models.phenotype import PredictionSoftware as Software
from ...models.phenotype import ResistanceGene, ResistanceVariant, VariantType
from ...models.sample import MethodIndex
Expand All @@ -19,12 +24,15 @@
"ethidium bromide",
"chlorhexidine",
"cetylpyridinium chloride",
"hydrogen peroxide"],
ElementStressSubtype.HEAT: ["temperature"]
"hydrogen peroxide",
],
ElementStressSubtype.HEAT: ["temperature"],
}


def _assign_res_subtype(prediction: Dict[str, Any], element_type: ElementType) -> ElementStressSubtype | None:
def _assign_res_subtype(
prediction: Dict[str, Any], element_type: ElementType
) -> ElementStressSubtype | None:
"""Assign element subtype from resfindere prediction."""
assigned_subtype = None
if element_type == ElementType.STRESS:
Expand Down Expand Up @@ -161,7 +169,7 @@ def parse_resfinder_amr_pred(
stress_factors = list(chain(*STRESS_FACTORS.values()))
categories = {
ElementType.STRESS: stress_factors,
ElementType.AMR: list(set(prediction["phenotypes"]) - set(stress_factors))
ElementType.AMR: list(set(prediction["phenotypes"]) - set(stress_factors)),
}
# parse resistance
sr_profile = _get_resfinder_amr_sr_profie(
Expand Down

0 comments on commit 0e63196

Please sign in to comment.