-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitTetra_counts_groups.py
executable file
·148 lines (133 loc) · 5.8 KB
/
fitTetra_counts_groups.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""use output score file from fitTetra, lists header with 14 columns
first column marker number, second markerID, last column assigned genotype group (0, 1, 2, 3,4)
input s to be file with fitTetra score results
input t tab file with list of sampleID and type: either parent1, parent2, progeny, control(DM), diploid, sample
output file -o consist of counts for parents and progeny according to genotype group
"""
import sys
import argparse
parser = argparse.ArgumentParser(description='GS output to fitTetra')
parser.add_argument('-s', type=argparse.FileType('r'), help="score output file from fitTetra, required", dest='in_scores', required=True)
parser.add_argument('-t', type=argparse.FileType('r'), help="tab file with sampleID and type(parent1, parent2, progeny, control, diploid)", dest='sample_type', required=True)
parser.add_argument('-o', type=argparse.FileType('w'), help="output file with group counts for population and controls", dest='out_file', required=True)
parser.add_argument('-c', type=argparse.FileType('w'), help="output file with group counts for mixed samples and controls", dest='out_samples', required=True)
args = parser.parse_args()
T = args.sample_type
F = args.in_scores
o = args.out_file
t = args.out_control
def add_progeny(name):
progeny_groups[name] += 1
def add_sample(name):
if sample_groups.has_key(name):
sample_groups[name] += 1
else:
sample_groups[name] = 1
def get_geno(sample, genotype):
if sample in control_type:
Test[sample] = genotype
elif sample in parent_type:
Parent[sample] = genotype
elif sample in progeny_type:
add_progeny(genotype)
elif sample in sample_type:
add_sample(genotype)
progeny_groups = {"NS": 0, "0": 0, "1": 0, "2": 0, "3":0, "4": 0}
sample_groups = {}
Test = {}
Parent = {}
marker_list = []
parent_type = []
progeny_type = []
sample_type = []
control_type = []
for line in T:
stype = line.split()
if stype[1] == "parent1":
parent_type.append(stype[0])
elif stype[1] == "parent2":
parent_type.append(stype[0])
elif stype[1] == "progeny":
progeny_type.append(stype[0])
elif stype[1] == "control":
control_type.append(stype[0])
elif stype[1] == "sample":
sample_type.append(stype[0])
else:
print("some of these entries are labelled wrongly!")
cur_marker = ""
for line in F:
info = line.split("\t")
if info[0]!= "marker":
sampleID = info[2].strip()
markerID = info[1].strip()
geno = info[13].strip()
if geno == "":
geno = "NS"
#get the various scores from diploids and parents
#only count scores for the mapping population, exclude diploid and other samples
#check if marker in the list, if it is add to the count for the specific genotype
if markerID in marker_list:
if markerID == cur_marker:
if sampleID in control_type:
Test[sampleID] = geno
elif sampleID in parent_type:
Parent[sampleID] = geno
elif sampleID in progeny_type:
add_progeny(geno)
elif sampleID in sample_type:
add_sample(geno)
else:
if cur_marker != "":
o.write("\n%s\t" % (cur_marker))
t.write("\n%s\t" % (cur_marker))
for key, value in Test.items():
o.write("%s:%s\t" % (key, value))
t.write("%s:%s\t" % (key, value))
for key, value in Parent.items():
o.write("%s:%s\t" % (key, value))
for key, value in progeny_groups.items():
o.write("%s:%s\t" % (key, value))
for key, value in sample_groups.items():
t.write("%s:%s\t" % (key, value))
progeny_groups = {"NS":0, "0": 0, "1": 0, "2": 0, "3": 0, "4": 0}
sample_groups = {}
Parents = {}
Test = {}
cur_marker = markerID
marker_list.append(cur_marker)
if sampleID in control_type:
Test[sampleID] = geno
elif sampleID in parent_type:
Parent[sampleID] = geno
elif sampleID in progeny_type:
add_progeny(geno)
elif sampleID in sample_type:
add_sample(geno)
#add new markerID to the list and start counting
else:
cur_marker = markerID
marker_list.append(cur_marker)
if sampleID in control_type:
Test[sampleID] = geno
elif sampleID in parent_type:
Parent[sampleID] = geno
elif sampleID in progeny_type:
add_progeny(geno)
elif sampleID in sample_type:
add_sample(geno)
if sample_groups or progeny_groups:
o.write("\n%s\t" % (cur_marker.strip()))
for key, value in Test.items():
o.write("%s:%s\t" % (key, value.strip()))
for key, value in Parent.items():
o.write("%s:%s\t" % (key, value.strip()))
for key, value in progeny_groups.items():
o.write("%s:%s\t" % (key, value))
t.write("\n%s\t" % (cur_marker.strip()))
for key, value in Test.items():
t.write("%s:%s\t" % (key, value))
for key, value in sample_groups.items():
t.write("%s:%s\t" % (key, value))
o.close()
t.close()