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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/usr/bin/env python3
"""Compare the deployment's parents-and-f1s.json against GN2's ParInfo.
`parents_and_f1s.json` is GN2's `ParInfo` as data (see
`gn2/utility/webqtlUtil.py`). Two copies of one fact drift, and this one already
did once: the JSON's `paternal` and `maternal` labels were swapped against the
way GN2 unpacks the tuple (`f1, f12, maternal, paternal`). Nothing mapped wrongly
-- the exclusion rule takes both strains regardless of role -- but the metadata
said the opposite of the truth, and anything that later reads "maternal" would
have been backwards.
So while GN2 exists, check the two agree:
python3 scripts/check-parental-metadata.py \\
--parinfo ../genenetwork2/gn2/utility/webqtlUtil.py \\
--json /path/to/genotype-files/parents_and_f1s.json
Exit 0 when every group GN2 knows is reproduced, 1 otherwise (with the
differences on stdout), 2 when a file cannot be read.
Extra groups in the JSON are reported but are not a failure: the deployment may
know a family GN2 never did (BXD-Longevity), and that is metadata of its own.
"""
import argparse
import ast
import json
import sys
def read_parinfo(path):
"""`ParInfo` as a dict, parsed rather than imported (no gn2 runtime)."""
source = open(path, encoding="utf-8").read()
for node in ast.parse(source).body:
if isinstance(node, ast.Assign):
targets = [t.id for t in node.targets if isinstance(t, ast.Name)]
if "ParInfo" in targets:
return ast.literal_eval(node.value)
raise SystemExit(f"{path}: no ParInfo assignment found")
def roles_from_json(entry):
"""(f1s, maternal, paternal) as the JSON spells them, empty names dropped."""
f1s = [f["strain"] for f in entry.get("f1s") or [] if f.get("strain")]
def strain(key):
role = entry.get(key) or {}
return role.get("strain") or None
return f1s, strain("maternal"), strain("paternal")
def roles_from_parinfo(value):
f1, f12, maternal, paternal = value
return [name for name in (f1, f12) if name], (maternal or None), (paternal or None)
def main(argv):
parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
parser.add_argument("--parinfo", required=True,
help="path to gn2/utility/webqtlUtil.py")
parser.add_argument("--json", required=True,
help="path to parents_and_f1s.json")
args = parser.parse_args(argv)
try:
parinfo = read_parinfo(args.parinfo)
document = json.load(open(args.json, encoding="utf-8"))
except OSError as error:
print(f"cannot read: {error}", file=sys.stderr)
return 2
problems = 0
for group, value in sorted(parinfo.items()):
if group not in document:
print(f"{group}: missing from the JSON (GN2 has it)")
problems += 1
continue
expected = roles_from_parinfo(value)
found = roles_from_json(document[group])
if expected == found:
continue
problems += 1
print(f"{group}: differs")
for label, want, got in (
("F1s", expected[0], found[0]),
("maternal", expected[1], found[1]),
("paternal", expected[2], found[2]),
):
if want != got:
print(f" {label}: GN2 {want!r}, JSON {got!r}")
extra = sorted(set(document) - set(parinfo))
if extra:
print(f"groups in the JSON that GN2 does not list (fine, not a failure): "
f"{', '.join(extra)}")
if problems:
print(f"\n{problems} group(s) disagree; parents_and_f1s.json should be "
f"regenerated from ParInfo")
return 1
print(f"OK: {len(parinfo)} GN2 groups reproduced"
+ (f", {len(extra)} extra in the JSON" if extra else ""))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
|