Skip to content

Commit

Permalink
Re-organise metrics_parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrizsavinhas committed Jul 26, 2024
1 parent 0e74b7b commit 1bfb650
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
52 changes: 26 additions & 26 deletions cg/meta/workflow/mutant/metrics_parser/metrics_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,26 @@ class MetricsParser:
}

@classmethod
def get_raw_results(cls, results_file_path: Path) -> list[dict[str, Any]]:
def parse_samples_results(cls, case: Case, results_file_path: Path) -> dict[str, SampleResults]:
try:
raw_results: list[dict[str, Any]] = cls._get_raw_results(
results_file_path=results_file_path
)
except Exception as exception_object:
raise CgError from exception_object

validated_results_list: list[SampleResults] = cls._get_validated_results_list(
raw_results=raw_results
)

samples_results: dict[str, SampleResults] = cls._get_samples_results(
case=case, results_list=validated_results_list
)

return samples_results

@classmethod
def _get_raw_results(cls, results_file_path: Path) -> list[dict[str, Any]]:
"""Parses raw_results from the results file."""
try:
raw_results: list[dict[Any, Any]] = [
Expand All @@ -66,7 +85,7 @@ def get_raw_results(cls, results_file_path: Path) -> list[dict[str, Any]]:
return raw_results

@classmethod
def get_altered_sample_result(cls, sample_result: dict[str, Any]) -> dict[str, Any]:
def _get_altered_sample_result(cls, sample_result: dict[str, Any]) -> dict[str, Any]:
"""Takes a raw_sample_result with headers from the results file (MutantResultsHeaderRawData)
and returns an altered_sample_result with the corrected headers from MutantResultsHeaderData.
"""
Expand All @@ -77,54 +96,35 @@ def get_altered_sample_result(cls, sample_result: dict[str, Any]) -> dict[str, A
return altered_sample_result

@classmethod
def get_validated_results_list(cls, raw_results: list[dict[str, Any]]) -> list[SampleResults]:
def _get_validated_results_list(cls, raw_results: list[dict[str, Any]]) -> list[SampleResults]:
"""Takes raw_results and returns a list of validated SampleResults with the corrected headers."""
validated_results_list = []
for sample_result in raw_results:
altered_sample_result: dict[str, Any] = cls.get_altered_sample_result(
altered_sample_result: dict[str, Any] = cls._get_altered_sample_result(
sample_result=sample_result
)
validated_result: SampleResults = SampleResults.model_validate(altered_sample_result)
validated_results_list.append(validated_result)
return validated_results_list

@classmethod
def get_sample_name_to_id_mapping(cls, case: Case) -> dict[str, str]:
def _get_sample_name_to_id_mapping(cls, case: Case) -> dict[str, str]:
sample_name_to_id_mapping: dict[str, str] = {}
for sample in case.samples:
sample_name_to_id_mapping[sample.name] = sample.internal_id
return sample_name_to_id_mapping

@classmethod
def get_samples_results(
def _get_samples_results(
cls, case: Case, results_list: list[SampleResults]
) -> dict[str, SampleResults]:
"""Takes a Case object and a list of SampleResults and builds a dict[str, SampleResults] with
sample_internal_ids as keys."""

sample_name_to_id_mapping: dict[str, str] = cls.get_sample_name_to_id_mapping(case=case)
sample_name_to_id_mapping: dict[str, str] = cls._get_sample_name_to_id_mapping(case=case)

samples_results: dict[str, SampleResults] = {}
for result in results_list:
sample_internal_id = sample_name_to_id_mapping[result.sample_name]
samples_results[sample_internal_id] = result
return samples_results

@classmethod
def parse_samples_results(cls, case: Case, results_file_path: Path) -> dict[str, SampleResults]:
try:
raw_results: list[dict[str, Any]] = cls.get_raw_results(
results_file_path=results_file_path
)
except Exception as exception_object:
raise CgError from exception_object

validated_results_list: list[SampleResults] = cls.get_validated_results_list(
raw_results=raw_results
)

samples_results: dict[str, SampleResults] = cls.get_samples_results(
case=case, results_list=validated_results_list
)

return samples_results
6 changes: 2 additions & 4 deletions tests/meta/workflow/mutant/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
)

from cg.meta.workflow.mutant.quality_controller.quality_controller import QualityController
from cg.meta.workflow.mutant.quality_controller.report_generator_utils import ReportGenerator
from cg.meta.workflow.mutant.quality_controller.result_logger_utils import ResultLogger
# from cg.meta.workflow.mutant.quality_controller.utils import get_quality_metrics, get_report_path


Expand Down Expand Up @@ -210,12 +208,12 @@ def mutant_results_file_path_qc_pass(mutant_analysis_dir_case_qc_pass: Path) ->

@pytest.fixture
def mutant_raw_results_qc_pass(mutant_results_file_path_qc_pass: Path) -> list[dict[str, Any]]:
return MetricsParser.get_raw_results(results_file_path=mutant_results_file_path_qc_pass)
return MetricsParser._get_raw_results(results_file_path=mutant_results_file_path_qc_pass)


@pytest.fixture
def mutant_results_list_qc_pass(mutant_raw_results_qc_pass: list[dict[str, Any]]):
return MetricsParser.get_validated_results_list(raw_results=mutant_raw_results_qc_pass)
return MetricsParser._get_validated_results_list(raw_results=mutant_raw_results_qc_pass)


# @pytest.fixture
Expand Down
16 changes: 8 additions & 8 deletions tests/meta/workflow/mutant/test_mutant_metrics_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
from cg.store.models import Case


def test_get_raw_results(mutant_results_file_path_qc_pass: Path):
def test__get_raw_results(mutant_results_file_path_qc_pass: Path):
# GIVEN a path to a valid results file

# WHEN parsing the file
MetricsParser.get_raw_results(results_file_path=mutant_results_file_path_qc_pass)
MetricsParser._get_raw_results(results_file_path=mutant_results_file_path_qc_pass)

# THEN no error is thrown


def test_get_validated_results_list(mutant_raw_results_qc_pass):
def test__get_validated_results_list(mutant_raw_results_qc_pass):
# GIVEN a valid raw_results: list[dict[str, Any]] objects

# WHEN parsing the file
MetricsParser.get_validated_results_list(raw_results=mutant_raw_results_qc_pass)
MetricsParser._get_validated_results_list(raw_results=mutant_raw_results_qc_pass)

# THEN no error is thrown


def test_get_sample_name_to_id_mapping(mutant_case_qc_pass: Case):
def test__get_sample_name_to_id_mapping(mutant_case_qc_pass: Case):
# GIVEN a case

# WHEN creating a sample_name_to_id_mapping dict
sample_name_to_id_mapping: dict[str, str] = MetricsParser.get_sample_name_to_id_mapping(
sample_name_to_id_mapping: dict[str, str] = MetricsParser._get_sample_name_to_id_mapping(
case=mutant_case_qc_pass
)

Expand All @@ -36,13 +36,13 @@ def test_get_sample_name_to_id_mapping(mutant_case_qc_pass: Case):
assert sample_name_to_id_mapping["0PROVSEK"] == "external_negative_control_qc_pass"


def test_get_samples_results(
def test__get_samples_results(
mutant_case_qc_pass: Case, mutant_results_list_qc_pass: list[SampleResults]
):
# GIVEN a case and corresponding results_list

# WHEN creating a sample_name_to_id_mapping dict
MetricsParser.get_samples_results(
MetricsParser._get_samples_results(
case=mutant_case_qc_pass, results_list=mutant_results_list_qc_pass
)

Expand Down

0 comments on commit 1bfb650

Please sign in to comment.