Skip to content

Commit

Permalink
add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbio committed Aug 13, 2024
1 parent 6030b87 commit 604f059
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
9 changes: 4 additions & 5 deletions BALSAMIC/assets/scripts/postprocess_gens_cnvkit.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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__":
Expand Down
5 changes: 5 additions & 0 deletions BALSAMIC/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
20 changes: 20 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
read_csv,
read_vcf_file,
read_yaml,
write_list_of_strings,
write_finish_file,
write_json,
write_sacct_to_yaml,
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 604f059

Please sign in to comment.