about summary refs log tree commit diff
path: root/scripts/example-genotype-upload.py
blob: 07f683cebd8de3e845665d8c84bbdfde3a1a1b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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())