#!/usr/bin/env python3 """Write an example traits/covariates matrix for a genotype file. The table GN4's POST /matrices accepts: a header row, then one row per individual. The individuals are read from the genotype file's own header, so the matrix passes the panel check by construction -- which is the point: this is the fixture to try an upload with, not a random CSV. scripts/example-matrix.py --genotype \\ /home/wrk/tmp/ai-root/factory/gn4-data/genotype-files/genotype/BXD.4.geno \\ --out /tmp/example-matrix.csv Values are deterministic (a hash of the individual and the column, not `random`), so the same command gives the same table and a test can rely on it. """ import argparse import hashlib import sys def pseudo(individual: str, column: str, low: float, spread: float) -> float: """A reproducible number in [low, low + spread) for this pair.""" digest = hashlib.sha256(f"{individual}:{column}".encode()).digest() unit = int.from_bytes(digest[:8], "big") / 2**64 return round(low + unit * spread, 3) def individuals_of(genotype_path: str) -> list[str]: """The genotype file's individual columns, in file order.""" with open(genotype_path, errors="replace") as handle: for line in handle: if line.startswith(("#", "@")) or not line.strip(): continue columns = line.rstrip("\n").split("\t") start = 4 if len(columns) > 3 and columns[3] == "Mb" else 3 return [name.strip() for name in columns[start:] if name.strip()] raise SystemExit(f"{genotype_path}: no header line") def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--genotype", required=True, help="a .geno file") parser.add_argument("--out", required=True, help="where to write the CSV") parser.add_argument("--traits", type=int, default=2, help="how many trait columns") parser.add_argument("--covariates", type=int, default=1, help="how many covariate columns") parser.add_argument("--separator", default=",", help="field separator (default ',')") arguments = parser.parse_args() individuals = individuals_of(arguments.genotype) if not individuals: raise SystemExit(f"{arguments.genotype}: no individuals in the header") traits = [f"trait_{index + 1}" for index in range(arguments.traits)] covariates = [f"covariate_{index + 1}" for index in range(arguments.covariates)] separator = arguments.separator with open(arguments.out, "w") as handle: handle.write(separator.join(["id", *traits, *covariates]) + "\n") for individual in individuals: row = [individual] row += [f"{pseudo(individual, trait, 1.0, 9.0):.3f}" for trait in traits] # A covariate is a small integer class, like a batch -- and one # cell is left empty, so the upload's missing-value path is # exercised by the example itself. for index, covariate in enumerate(covariates): if index == 0 and individual == individuals[-1]: row.append("") else: row.append(str(1 + int(hashlib.sha256((individual + covariate).encode()).hexdigest(), 16) % 3)) handle.write(separator.join(row) + "\n") print(f"{arguments.out}: {len(individuals)} individuals, {len(traits)} trait(s), " f"{len(covariates)} covariate(s)", file=sys.stderr) return 0 if __name__ == "__main__": raise SystemExit(main())