diff options
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/example-genotype-upload.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/scripts/example-genotype-upload.py b/scripts/example-genotype-upload.py new file mode 100755 index 0000000..0e36e63 --- /dev/null +++ b/scripts/example-genotype-upload.py @@ -0,0 +1,71 @@ +#!/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 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 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"{min(arguments.markers, len(markers))} marker(s)", file=sys.stderr) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) |
