-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparents2plot.py
83 lines (54 loc) · 1.91 KB
/
parents2plot.py
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
import sys
def catch_age(id_str):
if id_str == 'NA':
return 'NA', 'NA'
catch_year = id_str.split('_')[1]
if 'A' in catch_year:
age = 'A'
catch_year = catch_year.replace('A', '')
else:
age = id_str.split('_')[2]
return catch_year, age
def main():
dams = {}
sires = {}
for line in sys.stdin:
if line.startswith('"id"'):
continue
else:
line = line.replace('"', '').rstrip()
offspring, dam, sire, LLRdam, LLRsire, LLRpair, OHdam, OHsire, MEpair = line.split(',')
year, age = catch_age(offspring)
# update dam dict
if dam not in dams.keys():
dams[dam] = {}
if year not in dams[dam].keys():
dams[dam][year] = {}
if age not in dams[dam][year].keys():
dams[dam][year][age] = 0
dams[dam][year][age] += 1
# update sire dict - lazy coding
if sire not in sires.keys():
sires[sire] = {}
if year not in sires[sire].keys():
sires[sire][year] = {}
if age not in sires[sire][year].keys():
sires[sire][year][age] = 0
sires[sire][year][age] += 1
# output plot csv
print('id', 'offspring_year', 'offspring_age_class', 'n_offspring',
'parent_type', 'parent_year', 'parent_age_class', sep=',')
# dams
for d in dams.keys():
d_year, d_age = catch_age(d)
for y in dams[d].keys():
for a in dams[d][y].keys():
print(d, y, a, dams[d][y][a], 'dam', d_year, d_age, sep=',')
# sires
for s in sires.keys():
s_year, s_age = catch_age(s)
for y in sires[s].keys():
for a in sires[s][y].keys():
print(s, y, a, sires[s][y][a], 'sire', s_year, s_age, sep=',')
if __name__ == '__main__':
main()