diff options
| author | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-11-11 17:48:53 +0000 |
|---|---|---|
| committer | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-11-11 13:43:09 -0600 |
| commit | f2196e817c7a665c4fb899680ddb2e028d5edfb2 (patch) | |
| tree | 9a4e6972aa7f0c0756af45a7239d2e51c900025e /uploader/genotypes/normalise.py | |
| parent | 75d30d520c3332e08f1200cce56b7da7e4796efc (diff) | |
| download | gn-uploader-f2196e817c7a665c4fb899680ddb2e028d5edfb2.tar.gz | |
Normalise genotype files and move Stage 02 state into the QC job
Add `uploader.genotypes.normalise`, which writes a canonical untransposed matrix (`<stem>__work<suffix>`) next to the original, dropping comments and `@` directives and leaving the original untouched. Ragged or empty matrices raise `NormalisationError`, and `qc.check_file` now handles only the normal orientation. Stage 02 now puts `stage02`, `genotype_collector_job_id`, `file` and `config` in the QC job's metadata at launch, and uses `jobs_by_metadata` to reuse an existing QC job. The completed collector job's metadata is no longer modified. The Stage 03 worker normalises and QCs the working file from its own metadata. Change the placeholder marker ordering to chromosome, then Mb, then cM. Co-authored-by: pi coding agent <pi@pi.dev>
Diffstat (limited to 'uploader/genotypes/normalise.py')
| -rw-r--r-- | uploader/genotypes/normalise.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/uploader/genotypes/normalise.py b/uploader/genotypes/normalise.py new file mode 100644 index 0000000..795eb63 --- /dev/null +++ b/uploader/genotypes/normalise.py @@ -0,0 +1,48 @@ +"""Normalise genotype files to a single (untransposed) form.""" +from pathlib import Path +from typing import Any, Mapping + +from uploader.csvprocessing import (is_blank_line, make_comment_line_checker, + read_text_file) + + +class NormalisationError(Exception): + """Raised when a genotype file cannot be normalised.""" + + +def work_filepath(original: Path) -> Path: + """Return the canonical working-file path for ``original``.""" + return original.with_name(f"{original.stem}__work{original.suffix}") + + +def normalise_file(original: Path, config: Mapping[str, Any]) -> Path: + """Write a canonical untransposed data matrix for ``original``. + + Comments and ``@`` directives are dropped (Stage 01 already captured them). + The original file is left untouched; the canonical file is written next to + it as ``<stem>__work<suffix>``. + """ + _is_comment = make_comment_line_checker(config["comment.char"]) + + def __is_skippable__(line: str) -> bool: + return (is_blank_line(line) or _is_comment(line) + or line.startswith("@")) + + rows = tuple( + tuple(field.strip() for field in line.split(config["separator"])) + for line in read_text_file(original) + if not __is_skippable__(line.strip())) + if not rows: + raise NormalisationError( + "The genotype file has no data matrix rows to normalise.") + if len({len(row) for row in rows}) != 1: + raise NormalisationError( + "The genotype data matrix has ragged rows: all rows must have the " + "same number of fields.") + + output_rows = tuple(zip(*rows)) if config["transposed"] else rows + outpath = work_filepath(original) + with outpath.open("w", encoding="utf8") as outfile: + for row in output_rows: + outfile.write(config["separator"].join(row) + "\n") + return outpath |
