-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy-haplogroup-predictor.py
executable file
·272 lines (256 loc) · 8.91 KB
/
y-haplogroup-predictor.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import os
from collections import defaultdict
# Updated dictionary with rsIDs (SNPs) and their associated genotypes (allele pairs) for each haplogroup
haplogroup_snps = {
# Broad Haplogroups and their subclades
"R1a": {
"R1a": {
"rs17222573": ["A;A", "A;G", "G;G"],
"rs17307677": ["C;C", "C;T", "T;T"],
"rs17306692": ["A;A", "A;C", "C;C"],
"rs17250535": ["A;T", "T;T"],
},
"R1a1": {
"rs17307105": ["A;A", "A;G", "G;G"],
"rs9786587": ["G;G", "G;T", "T;T"],
"rs17316227": ["A;A", "A;G", "G;G"],
"rs2534636": ["A;A"],
},
"R1a1a": {
"rs3908": [],
"rs2020857": ["T;T"],
"rs1722146": ["T;T"],
"rs17315926": ["C;C","C;T","T;T"],
"rs17221601": ["A;A","A;T","T;T"],
},
"R1a1a1": {
"rs17316771": ["A;A","A;G","G;G"]
},
"R1a1a1a": {
"rs2032622": ["T;T"]
},
"R1a1a1c": {
"rs2032626": ["G;G"],
"rs2032644": ["C;C"],
"rs2032655": ["G;G"]
},
"R1a1a1h1": {
"rs7892981": ["A;A","A;G","G;G"]
},
"R1a1a1i1": {
"rs7067418": ["A;A","A;G","G;G"]
}
},
"R1b": {
"R1b": {
"rs9786184": ["A;A"],
},
"R1b1": {
"rs17249854": ["C;C","C;T","T;T"],
"rs9786194": ["A;A","A;C","C;C"],
"rs150173": ["A;A"],
},
"R1b1a": {
"rs9785702": ["C;C"],
"rs2917400": ["C;C","C;T","T;T"],
},
"R1b1a1": {
"rs2032634": ["G;T"],
},
"R1b1a2": {
"rs9786882": ["A;A","A;G","G;G"],
"rs9786153": ["T;T"],
"rs877756": ["C;C","C;T","T;T"],
"rs2058276": ["A;A", "A;G", "G;G"],
},
"R1b1a2a": {
"rs9785971": ["A;A","A;G","G;G"],
"rs9786142": ["A;A","A;T","T;T"],
},
"R1b1b2a1": {
"rs9785831": ["C;C","C;T","T;T"],
},
"R1b1a2a1a": {
"rs9786140": ["A;A","A;G","G;G"],
},
"R1b1a2a1a1": {
"rs9786076": ["C;C","C;T","T;T"],
"rs13304168": ["C;C","C;T","T;T"],
"rs2082033": ["C;C","C;T","T;T"],
"rs9786283": ["A;A","A;C","C;C"],
"rs9785659": ["A;A","A;G","G;G"],
},
"R1b1a2a1a1a": {
"rs16981293": ["T;T"],
},
"R1b1a2a1a1a5": {
"rs34001725": ["A;A","A;G","G;G"],
},
"R1b1a2a1a1a5a": {
"rs17222279": ["A;A"],
},
"R1b1a2a1a1a5c1a1": {
"rs13304625": ["A;A"],
},
"R1b1a2a1a1a5c1a1a": {
"rs35760092": ["G;G","G;T","T;T"],
"rs13305517": ["A;A","A;G","G;G"],
"rs13305070": ["A;A","A;C","C;C"],
},
"R1b1a2a1a1b": {
"rs34276300": ["A;A"],
},
"R1b1a2a1a1b2b1": {
"rs1800865": ["T;T"],
},
"R1b1a2a1a1b3": {
"rs1236440": ["T;T"],
},
"R1b1a2a1a1b3a": {
"rs2032615": [],
},
"R1b1a2a1a1b3c": {
"rs2566671": ["C;C"],
},
"R1b1a2a1a1b3c1": {
"rs7067305": ["A;A","A;G","G;G"],
},
"R1b1a2a1a1b3c1a": {
"rs9341273": ["C;C","C;T","T;T"],
},
"R1b1a2a1a1b4": {
"rs11799226": ["G;G"],
},
"R1b1a2a1a1b4b": {
"rs20321": ["A;A"],
},
"R1b1a2a1a1b4e": {
"rs9306842": ["A;A","A;T","T;T"],
},
"R1b1a2a1a1b5": {
"rs35199432": ["A;A","A;G","G;G"],
},
"R1b1a2a1b": {
"rs9786602": ["A;A","A;C","C;C"],
},
"R1b1c1": {
"rs3909": ["A;A"],
},
},
# I1
"I1": {
"I1": {
"rs17307252": ["A;A", "A;G", "G;G"],
"rs871626": ["A;A", "A;G", "G;G"],
"rs17221531": ["C;C","C;T","T;T"],
"rs111697819": ["A;A","A;T","T;T"],
"rs35960273": ["A;A","A;G","G;G"],
"rs3906451": ["A;A", "A;C", "C;C"],
"rs17307007": ["A;A","A;C","C;C"],
"rs17222167": ["A;A", "A;G", "G;G"],
"rs17307315": ["C;C","C;T","T;T"],
"rs17307586": ["C;C","C;T","T;T"],
"rs17249791": ["C;C", "C;T", "T;T"],
"rs17222657": ["A;A","A;T","T;T"],
"rs17250114": ["C;C","C;G","G;G"],
"rs17316639": ["G;G","G;T","T;T"],
"rs9341296": ["T;T"],
"rs13447354": ["A;A"],
"rs17316597": ["A;A"],
"rs112707890": ["A;A","A;G","G;G"],
"rs113686221": ["C;C","C;T","T;T"],
},
"I1a1a": {
"rs9341274": ["G;G"],
},
"I1a1a1": {
"rs2032637": ["G;G"],
},
"I1a1b": {
"rs34626372": ["C;C"],
},
},
# I2
"I2": {
"I2": {
"rs35547782": ["C;C","C;T","T;T"],
"rs17307294": ["G;G"],
},
},
}
def parse_dna_file(file_path):
detected_snps = {}
with open(file_path, 'r') as f:
for line in f:
if not line.startswith("#") and "rsid" not in line:
parts = line.strip().split()
if len(parts) >= 4:
rsid = parts[0]
allele1 = parts[3]
allele2 = parts[4] if len(parts) > 4 else allele1 # For 23andMe format compatibility
genotype = f"{allele1};{allele2}"
detected_snps[rsid] = genotype
return detected_snps
def calculate_haplogroups(detected_snps):
broad_haplogroup_scores = defaultdict(int)
specific_haplogroup_scores = defaultdict(int)
snps_detected_in_haplogroups = defaultdict(list)
# Define SNP weights
high_weight_snps = {
"rs9786184": 15, "rs17307294": 15, "rs9341274": 15,
"rs2032637": 15, "rs34626372": 15, "rs3912": 15,
"rs34276300": 15
}
medium_weight_snps = {
"rs2020857": 10, "rs2032622": 10, "rs2032626": 10, "rs2032644": 10,
"rs2032655": 10, "rs150173": 10, "rs9785702": 10, "rs9786153": 10,
"rs16981293": 10, "rs17222279": 10, "rs1800865": 10, "rs1236440": 10,
"rs2032615": 10, "rs20321": 10, "rs9341296": 10, "rs13447354": 10,
"rs17316597": 10
}
low_weight_snps = defaultdict(lambda: 5)
for broad_haplogroup, subclades in haplogroup_snps.items():
for subclade, snps_list in subclades.items():
subclade_score = 0
for snp, genotypes in snps_list.items():
if snp in detected_snps:
genotype = detected_snps[snp]
if genotype in genotypes:
weight = high_weight_snps.get(snp, medium_weight_snps.get(snp, low_weight_snps[snp]))
subclade_score += weight
broad_haplogroup_scores[broad_haplogroup] += weight
snps_detected_in_haplogroups[subclade].append(snp)
if subclade_score == 0:
subclade_score = 'not calculated'
specific_haplogroup_scores[subclade] = subclade_score
return broad_haplogroup_scores, specific_haplogroup_scores, snps_detected_in_haplogroups
def predict_haplogroup(broad_haplogroup_scores):
if not broad_haplogroup_scores:
return "Unknown"
predicted_haplogroup = max(broad_haplogroup_scores, key=broad_haplogroup_scores.get)
return predicted_haplogroup
def print_results(predicted_haplogroup, broad_haplogroup_scores, specific_haplogroup_scores, snps_detected_in_haplogroups):
print(f"\033[1mPredicted Y Haplogroup:\033[0m {predicted_haplogroup}\n")
print("\033[1mHaplogroup Detection Scores:\033[0m")
for broad_haplogroup, score in broad_haplogroup_scores.items():
print(f"{broad_haplogroup}: {score} points")
print("\n\033[1mSubclade Detection Scores:\033[0m")
for subclade, score in specific_haplogroup_scores.items():
if score == 'not calculated':
print(f"{subclade}: Not calculated")
else:
print(f"{subclade}: {score} points")
print("\n\033[1mDetected SNPs by Haplogroup:\033[0m")
for subclade, snps in snps_detected_in_haplogroups.items():
print(f"{subclade}: {', '.join(snps)}")
def main():
dna_file_path = input("Enter the path to your DNA file: ")
if not os.path.exists(dna_file_path):
print("Error: The specified file does not exist.")
return
detected_snps = parse_dna_file(dna_file_path)
broad_haplogroup_scores, specific_haplogroup_scores, snps_detected_in_haplogroups = calculate_haplogroups(detected_snps)
predicted_haplogroup = predict_haplogroup(broad_haplogroup_scores)
print_results(predicted_haplogroup, broad_haplogroup_scores, specific_haplogroup_scores, snps_detected_in_haplogroups)
if __name__ == "__main__":
main()