-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainCodeTemplate
More file actions
398 lines (327 loc) · 11.1 KB
/
Copy pathMainCodeTemplate
File metadata and controls
398 lines (327 loc) · 11.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import gzip
import numpy as np
import pandas as pd
CHR = 22 # CHROMOSOME: Change only one parameter, the rest of the code is the same
## Create a list of all vcf filenames
onek = "1kGenomeSNPSchr" + str(CHR) + ".vcf.gz" #1000 Genome Project Data
den = "DenFilteredGQ40DPchr" + str(CHR) + ".vcf.gz" #Denisovan
vin = "VinFilteredGQ40DPchr" + str(CHR) + ".vcf.gz" #Vindija Neanderthal
alt_file = "AltFilteredGQ40DPchr" + str(CHR) + ".vcf.gz" #Altai Neanderthal
## 2D numpy arrays -- array[r,c]
o_list = np.array(['POS','REF','ALT1','ALT2','INFO']) # 2D list of data from 1kGP [pos,ref,alt,info]
## lists for each archaic genomes
d_list = np.array(['POS','H1','H2']) # 2D list of data from Den [pos,ref,alt]
v_list = np.array(['POS','H1','H2']) # 2D list of data from Vin [pos,ref,alt]
a_list = np.array(['POS','H1','H2'])# 2D list of data from Alt [pos,ref,alt]
## array for the final result
final = np.array(['POS','REF','ALT1','ALT2','DEN1','DEN2','VIN1','VIN2','ALT1','ALT2','EAS_AF1','EAS_AF2',
'AMR_AF1','AMR_AF2','AFR_AF1','AFR_AF2','EUR_AF1','EUR_AF2','SAS_AF1','SAS_AF2'])
indices = []
ind_set = set()
with gzip.open(onek, "rt") as data:
for line in data:
# Skip all the info line
if line.startswith("##") or line.startswith("#"):
continue
# Split the line by column
split_line = line.split()
# Break down the archaic genotypes into strings
pos = int(split_line[1]) # Position
ref = split_line[3] # Reference
alt = list(split_line[4]) # Alternative with string split into each letter i.e. ['A',',','T']
info = split_line[7] # Information including allele frequency for each population
#######
#######
##info_split = info.split(";") # Separate the string of info to obtain allele frequencies
## For the split INFO line, the indices for each super population are
# 5 = EAS_AF, 6 = AMR_AF, 7 = AFR_AF, 8 = EUR_AF, 9 = SAS_AF
#'EAS1','EAS2','AMR1','AMR2','AFR1','AFR2','EUR1','EUR2','SAS1','SAS2'
#######
#######
if len(alt) == 3 : # look for triallelic sites ['N1' , ',' , 'N2']
combined = [pos,ref,alt[0],alt[2],info]
o_list = np.vstack([o_list,combined])
#print("1k combined = " + str(combined))
#print('list = ' + str(o_list))
####################
####################
## DENISOVAN
####################
####################
with gzip.open(den, "rt") as data:
index = 1
counter = 0
for line in data:
# Skip all the info line
if line.startswith("##") or line.startswith("#"):
continue
# Split the line by column
split_line = line.split()
pos = int(split_line[1])
ref = split_line[3]
alt = split_line[4]
allele = list(split_line[-1])[0:3]
h1 = None
h2 = None
## figure out homozygous or heterozygous
## In case it is triallelic
if allele[0] == '2' or allele[2] == '2':
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = list(alt)[0]
elif allele[0] == '2':
h1 = list(alt)[2]
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = list(alt)[0]
elif allele[2] == '2':
h2 = list(alt)[2]
else: # Ordinary biallelic site
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = alt
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = alt
for i in range(index,len(o_list)-1):
one = o_list[i,0].astype(int)
#print("index = " + str(index))
#print("1k = " + str(o_list[i,0]))
#print("den = " + str(pos))
if one < pos:
index += 1
continue
elif pos == one:
indices.append(index)
ind_set.add(index)
index += 1
counter += 1
#print("BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM!")
combined = [pos,h1,h2]
d_list = np.vstack([d_list,combined])
#print("Den combined = " + str(combined))
break
elif one > pos:
break
print("DENISOVAN BAAM! COUNTER = " + str(counter))
####################
####################
## VINDIJA NEANDERTHAL
####################
####################
with gzip.open(vin, "rt") as data:
index = 1
counter = 0
for line in data:
# Skip all the info line
if line.startswith("##") or line.startswith("#"):
continue
# Split the line by column
split_line = line.split()
pos = int(split_line[1])
ref = split_line[3]
alt = split_line[4]
allele = list(split_line[-1])[0:3]
h1 = None
h2 = None
## figure out homozygous or heterozygous
## In case it is triallelic
if allele[0] == '2' or allele[2] == '2':
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = list(alt)[0]
elif allele[0] == '2':
h1 = list(alt)[2]
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = list(alt)[0]
elif allele[2] == '2':
h2 = list(alt)[2]
else: # Ordinary biallelic site
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = alt
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = alt
for i in range(index,len(o_list)-1):
one = o_list[i,0].astype(int)
#print("index = " + str(index))
#print("1k = " + str(o_list[i,0]))
#print("vin = " + str(pos))
if one < pos:
index += 1
continue
elif pos == one:
if index in ind_set:
pass
else:
indices.append(index)
ind_set.add(index)
index += 1
counter += 1
#print("BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM!")
combined = [pos,h1,h2]
v_list = np.vstack([v_list,combined])
#print("Vin combined = " + str(combined))
break
elif one > pos:
break
print("VINDIJA BAAM! COUNTER = " + str(counter))
####################
####################
## ALTAI NEANDERTHAL
####################
####################
with gzip.open(alt_file, "rt") as data:
index = 1
counter = 0
for line in data:
# Skip all the info line
if line.startswith("##") or line.startswith("#"):
continue
# Split the line by column
split_line = line.split()
pos = int(split_line[1])
ref = split_line[3]
alt = split_line[4]
allele = list(split_line[-1])[0:3]
h1 = None
h2 = None
## figure out homozygous or heterozygous
## In case it is triallelic
if allele[0] == '2' or allele[2] == '2':
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = list(alt)[0]
elif allele[0] == '2':
h1 = list(alt)[2]
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = list(alt)[0]
elif allele[2] == '2':
h2 = list(alt)[2]
else: # Ordinary biallelic site
if allele[0] == '0':
h1 = ref
elif allele[0] == '1':
h1 = alt
if allele[2] == '0':
h2 = ref
elif allele[2] == '1':
h2 = alt
for i in range(index,len(o_list)-1):
one = o_list[i,0].astype(int)
#print("index = " + str(index))
#print("1k = " + str(o_list[i,0]))
#print("alt = " + str(pos))
if one < pos:
index += 1
continue
elif pos == one:
if index in ind_set:
pass
else:
indices.append(index)
ind_set.add(index)
index += 1
counter += 1
combined = [pos,h1,h2]
a_list = np.vstack([a_list,combined])
break
elif one > pos:
break
print("ALTAI BAAM! COUNTER = " + str(counter))
indices.sort()
#print("indices = " + str(indices))
d_i = 1
v_i = 1
a_i = 1
for i in indices:
pos = o_list[i,0].astype(int)
d1 = None
d2 = None
v1 = None
v2 = None
a1 = None
a2 = None
# DEN
for j in range(d_i,len(d_list)-1):
d_j = d_list[j,0].astype(int)
if d_j < pos:
d_i +=1
continue
elif d_j == pos:
d1 = d_list[j,1]
d2 = d_list[j,2]
d_i += 1
break
elif d_j > pos:
break
# VIN
for j in range(v_i,len(v_list)-1):
v_j = v_list[j,0].astype(int)
if v_j < pos:
v_i +=1
continue
elif v_j == pos:
v1 = v_list[j,1]
v2 = v_list[j,2]
v_i += 1
break
elif v_j > pos:
break
# ALT
for j in range(a_i,len(a_list)-1):
a_j = a_list[j,0].astype(int)
if a_j < pos:
a_i +=1
continue
elif a_j == pos:
a1 = a_list[j,1]
a2 = a_list[j,2]
a_i += 1
break
elif a_j > pos:
break
# Get Allele Frequencies for each superpop
info = o_list[i,4].split(";")
eas = info[5].split("=")[1]
amr = info[6].split("=")[1]
afr = info[7].split("=")[1]
eur = info[8].split("=")[1]
sas = info[9].split("=")[1]
eas1 = eas.split(",")[0]
eas2 = eas.split(",")[1]
amr1 = amr.split(",")[0]
amr2 = amr.split(",")[1]
afr1 = afr.split(",")[0]
afr2 = afr.split(",")[1]
eur1 = eur.split(",")[0]
eur2 = eur.split(",")[1]
sas1 = sas.split(",")[0]
sas2 = sas.split(",")[1]
combined = [pos,o_list[i,1],o_list[i,2],o_list[i,3],d1,d2,v1,v2,a1,a2,eas1,eas2,amr1,amr2,afr1,afr2,eur1,eur2,sas1,sas2]
final = np.vstack([final,combined])
print(final)
#output = pd.DataFrame(final)
#filepath = 'CHR' + str(CHR) + 'TriallelicSNPs.xlsx'
#output.to_excel(filepath, index=False)
#######
#######
##info_split = info.split(";") # Separate the string of info to obtain allele frequencies
## For the split INFO line, the indices for each super population are
# 5 = EAS_AF, 6 = AMR_AF, 7 = AFR_AF, 8 = EUR_AF, 9 = SAS_AF
#'EAS1','EAS2','AMR1','AMR2','AFR1','AFR2','EUR1','EUR2','SAS1','SAS2'
#######
#######