diff options
| author | pi coding agent <pi@localhost> | 2026-10-15 20:44:47 +0000 |
|---|---|---|
| committer | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-10-15 15:50:18 -0500 |
| commit | 82b9270d344f75302bc06aaaf328b4f432d997c0 (patch) | |
| tree | 2702bc248b47e488e5afcb3173d0e86e4b1c8585 | |
| parent | 5da7f7cf37bf3b2c7ee62f333b536d4fecc7fcea (diff) | |
| download | gn-uploader-82b9270d344f75302bc06aaaf328b4f432d997c0.tar.gz | |
Test: cover per-line accumulation and single-threaded stats
Add pytest unit tests for the extracted `_accumulate` helper and for `file_stats_singlethread`, covering line-type counting, collection of genotype symbols/markers/dot-geno fields, transposed files, and the ordering of header validation errors before per-line errors.
| -rw-r--r-- | tests/genotypes/__init__.py | 0 | ||||
| -rw-r--r-- | tests/genotypes/test_preprocess_csv_collect_info.py | 141 |
2 files changed, 141 insertions, 0 deletions
diff --git a/tests/genotypes/__init__.py b/tests/genotypes/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/genotypes/__init__.py diff --git a/tests/genotypes/test_preprocess_csv_collect_info.py b/tests/genotypes/test_preprocess_csv_collect_info.py new file mode 100644 index 0000000..cb65036 --- /dev/null +++ b/tests/genotypes/test_preprocess_csv_collect_info.py @@ -0,0 +1,141 @@ +"""Tests for the per-line genotype file statistics collection.""" +import pytest + +from scripts.genotypes.preprocess_csv_collect_info import ( + _accumulate, + _ChunkStats, + file_stats_singlethread) + +from scripts.genotypes.types import DotGenoFields + + +UNTRANSPOSED_CONFIG = { + "markers": "Locus", + "chromosome": "Chr", + "cm": "cM", + "mb": "Mb", + "separator": ",", + "comment.char": "#", + "na_strings": ["-", "NA", "N/A"], + "transposed": False, +} + +TRANSPOSED_CONFIG = {**UNTRANSPOSED_CONFIG, "transposed": True} + +UNTRANSPOSED_HEADERS = ("Locus", "Chr", "cM", "Mb", "s1", "s2") + +UNTRANSPOSED_LINES = ( + "Locus,Chr,cM,Mb,s1,s2\n", + "m1,1,0.0,1.0,A,T\n", + "m2,1,1.0,2.0,C,G\n", + "# a comment\n", + "\n", + "@name:test\n", + "@mat:A\n", + "@pat:B\n", + "@het:H\n", +) + +TRANSPOSED_HEADERS = ("Locus", "Chr", "cM", "Mb", "s1", "s2") + +TRANSPOSED_LINES = ( + "Locus,m1,m2,m3\n", + "Chr,1,1,2\n", + "cM,0,1,2\n", + "Mb,0,1,2\n", + "s1,A,T,C\n", + "s2,G,C,A\n", +) + + +@pytest.mark.unit_test +def test_accumulate_counts_each_kind_of_line(): + """ + GIVEN: a mixture of data, comment, blank and dot-geno lines + WHEN: the lines are accumulated + THEN: each type of line is counted separately + """ + stats = _accumulate(UNTRANSPOSED_HEADERS, + UNTRANSPOSED_CONFIG, + list(UNTRANSPOSED_LINES)) + assert isinstance(stats, _ChunkStats) + assert stats.total_lines == 9 + assert stats.data_lines == 2 + assert stats.comment_lines == 1 + assert stats.blank_lines == 1 + assert stats.dot_geno_lines == 4 + + +@pytest.mark.unit_test +def test_accumulate_collects_symbols_markers_and_dot_geno_fields(): + """ + GIVEN: genotype data with sample columns and dot-geno directives + WHEN: the lines are accumulated + THEN: the genotype symbols, marker names and dot-geno fields are collected + """ + stats = _accumulate(UNTRANSPOSED_HEADERS, + UNTRANSPOSED_CONFIG, + list(UNTRANSPOSED_LINES)) + assert stats.geno_symbols == {"A", "T", "C", "G"} + assert stats.markers == ("m1", "m2") + assert stats.markers_seen is False + assert stats.dot_geno_fields == { + "name": "test", "mat": "A", "pat": "B", "het": "H"} + assert stats.errors == () + + +@pytest.mark.unit_test +def test_accumulate_handles_transposed_files(): + """ + GIVEN: a transposed genotype file + WHEN: the lines are accumulated + THEN: the markers are taken from the markers line and no errors reported + """ + stats = _accumulate(TRANSPOSED_HEADERS, + TRANSPOSED_CONFIG, + list(TRANSPOSED_LINES)) + assert stats.total_lines == 6 + assert stats.data_lines == 6 + assert stats.geno_symbols == {"A", "T", "C", "G"} + assert stats.markers == ("m1", "m2", "m3") + assert stats.markers_seen is True + assert stats.errors == () + + +@pytest.mark.unit_test +def test_singlethread_reports_expected_file_stats(): + """ + GIVEN: an untransposed genotype file with dot-geno directives + WHEN: statistics are computed with a single thread + THEN: the resulting file statistics mirror the accumulated statistics + """ + stats = file_stats_singlethread(UNTRANSPOSED_CONFIG, + UNTRANSPOSED_HEADERS, + list(UNTRANSPOSED_LINES)) + assert stats.total_lines == 9 + assert stats.data_lines == 2 + assert stats.comment_lines == 1 + assert stats.blank_lines == 1 + assert stats.dot_geno_lines == 4 + assert set(stats.geno_symbols) == {"A", "T", "C", "G"} + assert stats.samples_list == ("s1", "s2") + assert stats.markers_list == ("m1", "m2") + assert stats.dot_geno_fields == DotGenoFields( + name="test", mat="A", pat="B", het="H") + assert stats.errors == () + + +@pytest.mark.unit_test +def test_singlethread_reports_missing_headers_before_line_errors(): + """ + GIVEN: headers missing the genetic/physical distance columns + WHEN: statistics are computed with a single thread + THEN: the header errors are reported ahead of any line errors + """ + config = {**UNTRANSPOSED_CONFIG} + headers = ("Locus", "Chr", "s1") + lines = ("m1,1,A\n",) + stats = file_stats_singlethread(config, headers, list(lines)) + assert len(stats.errors) == 2 + assert all(error.column_header == "-" for error in stats.errors) + assert stats.markers_list == ("m1",) |
