#!/usr/bin/env python3 """Write an example genotype *upload* from an existing .geno file. The upload format GN4 accepts is tab-delimited: the header names the individuals from column 4 on, column 1 is the marker, 2 the chromosome, 3 the position in Mb. This script converts a GN `.geno` into that shape, keeping a subset of the markers, dropping a few individuals and (optionally) adding new ones -- which is exactly what an "update an existing population" upload is. scripts/example-genotype-upload.py \\ --geno /home/wrk/tmp/ai-root/factory/gn4-data/genotype-files/genotype/BXD.4.geno \\ --markers 20 --drop 2 --add 1 --out /tmp/example-upload.tsv """ import argparse import sys def read_geno(path: str): """(individuals, markers) from a GN .geno file.""" labels, individuals, markers = [], [], [] with open(path, errors="replace") as handle: for line in handle: line = line.rstrip("\n") if not line.strip(): continue if line.startswith("#"): continue if line.startswith("@"): labels.append(line) continue fields = line.split("\t") if not individuals: start = 4 if len(fields) > 3 and fields[3] == "Mb" else 3 individuals = [name.strip() for name in fields[start:]] continue start = 4 if len(fields) > 4 else 3 markers.append((fields[1].strip(), fields[0].strip(), fields[start - 1].strip(), [cell.strip() for cell in fields[start:]])) return individuals, markers def spread(markers, wanted): """Markers spread across chromosomes. One chromosome of markers makes a poor example: an LMM's leave-one- chromosome-out kinship has nothing to leave out, so a scan over it is flat or empty. Taking a marker per chromosome first gives a file a scan can actually use. """ seen, first = set(), [] for marker in markers: if marker[1] not in seen: seen.add(marker[1]) first.append(marker) if len(first) >= wanted: return first[:wanted] chosen = {id(marker) for marker in first} rest = [marker for marker in markers if id(marker) not in chosen] return (first + rest)[:wanted] def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--geno", required=True, help="a GN .geno file to convert") parser.add_argument("--out", required=True, help="where to write the upload") parser.add_argument("--markers", type=int, default=20, help="how many marker rows to keep") parser.add_argument("--drop", type=int, default=0, help="individuals to leave out") parser.add_argument("--add", type=int, default=0, help="new individuals to add (max 50)") arguments = parser.parse_args() individuals, markers = read_geno(arguments.geno) if not individuals or not markers: raise SystemExit(f"{arguments.geno}: no individuals or markers") kept = individuals[: len(individuals) - arguments.drop] if arguments.drop else individuals added = [f"NEW{i + 1}" for i in range(arguments.add)] header = ["marker", "chr", "pos", *kept, *added] with open(arguments.out, "w") as handle: handle.write("\t".join(header) + "\n") for name, chr, pos, cells in spread(markers, arguments.markers): # A new individual inherits the first kept individual's calls: a # valid value of the same kind, which is all the upload checks. row = [name, chr, pos, *cells[: len(kept)], *([cells[0]] * len(added))] handle.write("\t".join(row) + "\n") print(f"{arguments.out}: {len(kept)} kept + {len(added)} new individual(s), " f"{len(spread(markers, arguments.markers))} marker(s) across chromosomes", file=sys.stderr) return 0 if __name__ == "__main__": raise SystemExit(main())