From 604f0596ad111811595505349b3d8090454f7087 Mon Sep 17 00:00:00 2001 From: Mathias Johansson Date: Tue, 13 Aug 2024 14:50:24 +0200 Subject: [PATCH] add new test --- .../assets/scripts/postprocess_gens_cnvkit.py | 9 ++++----- BALSAMIC/utils/io.py | 5 +++++ tests/utils/test_utils.py | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py b/BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py index a50e354d0..2899091ff 100644 --- a/BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py +++ b/BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py @@ -1,5 +1,5 @@ import click -from BALSAMIC.utils.io import read_csv +from BALSAMIC.utils.io import read_csv, write_list_of_strings def calculate_log2_ratio(purity, log2_ratio, ploidy): # Ensure that the inputs are within valid ranges @@ -82,10 +82,9 @@ def create_gens_cov_file( log2_data.append(f"{row['chromosome']}\t{midpoint - 1}\t{midpoint}\t{log2}") # Write log2 data to output file - with open(output_file, "w") as log2_out: - for resolution in ["o", "a", "b", "c", "d"]: - for line in log2_data: - log2_out.write(f"{resolution}_{line}\n") + resolutions = ["o", "a", "b", "c", "d"] + resolution_log2_lines = [f"{resolution}_{line}" for resolution in resolutions for line in log2_data] + write_list_of_strings(resolution_log2_lines, output_file) if __name__ == "__main__": diff --git a/BALSAMIC/utils/io.py b/BALSAMIC/utils/io.py index 0b615265d..315899f93 100644 --- a/BALSAMIC/utils/io.py +++ b/BALSAMIC/utils/io.py @@ -73,6 +73,11 @@ def read_csv(csv_path: str, delimeter: str = ",") -> List[dict]: csv_reader = csv.DictReader(csv_file, delimiter=delimeter) return [row for row in csv_reader] +def write_list_of_strings(list_of_strings: list[str], output_file: str): + """Writes a list of strings to a file, each on a new line.""" + with open(output_file, 'w') as file: + for string in list_of_strings: + file.write(f"{string}\n") def read_yaml(yaml_path: str) -> dict: """Read data from a yaml file.""" diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 0cd721f78..503134c2e 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -49,6 +49,7 @@ read_csv, read_vcf_file, read_yaml, + write_list_of_strings, write_finish_file, write_json, write_sacct_to_yaml, @@ -455,6 +456,25 @@ def test_write_json(tmp_path, reference): assert len(list(tmp.iterdir())) == 1 +def test_write_list_of_strings(tmp_path): + """Test writing list of strings to file""" + + # GIVEN a list of strings and an output file path + list_of_strings = [f"header1\theader2\theader3", f"row1_col1\trow1_col2\trow1_col3"] + tmp = tmp_path / "tmp" + tmp.mkdir() + output_file: Path = Path(tmp / "output.csv") + + # WHEN writing list of strings + write_list_of_strings(list_of_strings, output_file.as_posix()) + + # THEN file should have been written + assert output_file.exists() + + # AND contain the same information + read_written_file = read_csv(csv_path=output_file, delimeter="\t") + assert read_written_file == [{"header1": "row1_col1", "header2": "row1_col2", "header3": "row1_col3"}] + assert len(read_written_file) == 1 def test_write_json_error(tmp_path: Path): """Test JSON write error."""