Skip to content

Commit

Permalink
add io test
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbio committed Aug 13, 2024
1 parent e3f54f7 commit 6030b87
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
3 changes: 0 additions & 3 deletions BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import click
from BALSAMIC.utils.io import read_csv
import numpy as np
import warnings


def calculate_log2_ratio(purity, log2_ratio, ploidy):
# Ensure that the inputs are within valid ranges
Expand Down
17 changes: 9 additions & 8 deletions BALSAMIC/commands/config/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ def case_config(
gnomad_min_af5=gnomad_min_af5,
panel_bed=panel_bed,
)
# Update references dictionary with GENS reference-files
references.update(
{
gens_file: path
for gens_file, path in gens_references.items()
if path is not None
}
)
if gens_references:
# Update references dictionary with GENS reference-files
references.update(
{
gens_file: path
for gens_file, path in gens_references.items()
if path is not None
}
)

variants_observations = {
"clinical_snv_observations": clinical_snv_observations,
Expand Down
21 changes: 9 additions & 12 deletions BALSAMIC/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,18 @@ def get_gens_references(
gens_coverage_pon: Optional[str],
gnomad_min_af5: Optional[str],
panel_bed: Optional[str],
) -> Dict[str, str]:
) -> Dict[str, str] | None:
"""
Assigns reference-files required for GENS if they have been supplied, else exists with error message.
Args:
genome_interval: Optional[str]. Coverage-regions. (required for WGS GENS)
gens_coverage_pon: Optional[str]. PON for GATK CollectReadCounts. (required for WGS GENS)
genome_interval: Optional[str] Coverage-regions. (required for WGS GENS)
gens_coverage_pon: Optional[str] PON for GATK CollectReadCounts. (required for WGS GENS)
gnomad_min_af5: Optional[str] gnomad VCF filtered to keep variants above 5% VAF (required for WGS and TGA GENS).
panel_bed: Optional[str] Bedfile supplied for TGA analyses.
Returns:
Dict[str, str] with paths to GENS reference files
Dict[str, str] with paths to GENS reference files or None
"""

if panel_bed and gnomad_min_af5:
Expand All @@ -243,14 +243,11 @@ def get_gens_references(
"gens_coverage_pon": gens_coverage_pon,
"gnomad_min_af5": gnomad_min_af5,
}

error_message = (
"GENS reference file is always required to run BALSAMIC.\n"
"WGS requires arguments: genome_interval, gens_coverage_pon, gnomad_min_af5\n"
"TGA requires argument: gnomad_min_af5"
)
LOG.error(error_message)
raise BalsamicError(error_message)
if any([gnomad_min_af5, genome_interval, gens_coverage_pon]):
error_message = "GENS for WGS requires all arguments: genome_interval, gens_coverage_pon, gnomad_min_af5"
LOG.error(error_message)
raise BalsamicError(error_message)
return None


def get_bioinfo_tools_version(
Expand Down
9 changes: 4 additions & 5 deletions tests/commands/config/test_config_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,12 @@ def test_missing_required_gens_arguments_wgs(
# THEN the CLI should exit code 2 and display an informative error message
assert result.exit_code == 1
assert (
"WGS requires arguments: genome_interval, gens_coverage_pon, gnomad_min_af5"
"GENS for WGS requires all arguments: genome_interval, gens_coverage_pon, gnomad_min_af5"
in result.output
)


def test_missing_required_gens_arguments_tga(
def test_missing_gens_arguments_tga(
invoke_cli,
tumor_sample_name: str,
analysis_dir: str,
Expand Down Expand Up @@ -461,9 +461,8 @@ def test_missing_required_gens_arguments_tga(
sentieon_license,
],
)
# THEN the CLI should exit code 2 and display an informative error message
assert result.exit_code == 1
assert "TGA requires argument: gnomad_min_af5" in result.output
# THEN the CLI should exit code 0
assert result.exit_code == 0


def test_config_with_gens_arguments(
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from BALSAMIC.utils.exc import BalsamicError, WorkflowRunError
from BALSAMIC.utils.io import (
read_json,
read_csv,
read_vcf_file,
read_yaml,
write_finish_file,
Expand Down Expand Up @@ -479,6 +480,20 @@ def test_read_json(config_path: str):
assert type(config_dict) is dict


def test_read_csv(purity_csv_path: str):
"""Test data extraction from a CSV file."""

# GIVEN a CSV path

# WHEN calling the function
csv_list: list[Dict] = read_csv(purity_csv_path)

# THEN the config.json file should be correctly parsed
assert len(csv_list) == 1
assert csv_list[0]["Purity"] == "0.64"
assert csv_list[0]["Sampleid"] == "tumor.initial"


def test_read_json_error():
"""Test data extraction from a BALSAMIC config JSON file for an invalid path."""

Expand Down

0 comments on commit 6030b87

Please sign in to comment.