-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibd_compiler.py
633 lines (556 loc) · 22.4 KB
/
ibd_compiler.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
'''This file simulates IBD for our experiments. We first use HapGen to simulate
the genotype data and get Hap files and genetic distance files (a legend file and a genetic
distance file).
'''
import numpy as np
dt = np.dtype('uint8')
def load_meta(legendAddr, distsAddr):
legend = np.loadtxt(legendAddr, skiprows=1, dtype=
{'names': ['rsid', 'position', 'a1', 'a2'], 'formats': ['U15', 'i4', 'S1', 'S1']}) # legendFormat
dists = np.loadtxt(distsAddr, skiprows=1, dtype=
{'names': ['position', 'crate', 'gmap'], 'formats': ['i4', 'f4', 'f4']})
dists = [[item[0], item[1], item[2]] for item in dists]
dists = np.array(dists)
nlegend = [(item[1],item[0]) for item in legend]
legend = np.array([item[0] for item in nlegend])
the_map = []
last_base_pos = dists[0]
for item in nlegend:
first_inds = np.where(dists[:, 0] >= item[0])[0]
if len(first_inds) > 0:
if dists[first_inds[0]][0] == item[0]:
last_base_pos = dists[first_inds[0]]
the_map.append([item[1], dists[first_inds[0]][2], item[0] ] )
else:
first_ind = first_inds[0]
guestimate = last_base_pos[2]+( (dists[first_ind][2]-last_base_pos[2])/(dists[first_ind][0]-last_base_pos[0]) )*(item[0]-last_base_pos[0])
the_map.append([item[1], guestimate, item[0] ])
else:
sec_las = the_map[-2]
last_item = the_map[-1]
guestimate = last_item[1] + ( ((last_item[1]-sec_las[1])/(last_item[2]-sec_las[2]))*(item[0]-last_item[2]) )
the_map.append([item[1],guestimate, item[0]])
return np.array(the_map)
def load_haps(hapAddr,size,dim):
print("making the matrix")
haps = np.zeros((size*2,dim),dtype=dt)
counter= 0
print("opening the file")
with open(hapAddr,'r') as file:
for line in file:
haps[:,counter] = np.fromstring(line,dtype=int,sep=' ')
counter += 1
print("File is loaded")
return haps
def find_intersections(results, new):
# results = results[results[:,0].argsort()]
# for i in range(len(results)-1):
# if results[i,0] == results[i,]
for item in results:
if new[0]>item[0] and new[0]<item[1]:
return True
elif new[1]>item[0] and new[1]<item[1]:
return True
elif item[0]>new[0] and item[0]<new[1]:
return True
elif item[1]>new[0] and item[1]<new[1]:
return True
#if len(np.intersect1d(item, new)):
# print "overlap"
# return True
return False
def add_to_dict(res_dic,min_ind,max_ind,coordinates,freeze_dic):
if min_ind in freeze_dic:
if find_intersections(freeze_dic[min_ind],coordinates):
return False
if max_ind in freeze_dic:
if find_intersections(freeze_dic[max_ind], coordinates):
return False
if min_ind in freeze_dic:
freeze_dic[min_ind].append(coordinates)
else:
freeze_dic[min_ind] = [coordinates]
if max_ind in freeze_dic:
freeze_dic[max_ind].append(coordinates)
else:
freeze_dic[max_ind] = [coordinates]
if min_ind in res_dic:
if max_ind in res_dic[min_ind]:
res_dic[min_ind][max_ind].append(coordinates)
else:
res_dic[min_ind][max_ind] = [coordinates]
else:
res_dic[min_ind] = {}
res_dic[min_ind][max_ind] = [coordinates]
return True
def tract_generator_from_dist_poisson_reverse(the_map,tract_dist, hap_count):
results = {}
freez_dict = {}
counter = 0
found = False
temp_map = np.array([[float(item[1]),int(item[2])] for item in the_map])
remaining = 0
for size,count in enumerate(reversed(tract_dist)):
print(remaining)
print("SIZE IS : "+str(50-size)+'------- #'+str(count))
remaining = int(count)
while remaining>0:
print("Remaining:"+str(remaining),end='\r')
found = False
while not found: #finding one of tracts
if 50-size < 10:
shared_by = np.random.poisson(lam=8,size=None)+2
elif 50-size < 30:
shared_by = np.random.poisson(lam=6,size=None)+2
else:
shared_by = np.random.poisson(lam=5,size=None)+2
added_tracts = shared_by*(shared_by-1)/2
if remaining-added_tracts < 0:
shared_by = 2
added_tracts = 1
indices = sorted(np.random.choice(hap_count, shared_by,replace=False))
counter = 0
while counter<3:
counter += 1
start_loc = np.random.choice(len(temp_map),1)[0]
dest = np.where(temp_map[:,0]-temp_map[start_loc,0] >= 50.0-size)[0]
if len(dest) == 0 :
continue
elif add_to_dict_poisson(results,indices,[start_loc,dest[0]],freez_dict):
found = True
remaining -= added_tracts
break
else:
continue
return results
def add_to_dict_poisson(res_dic,indices,coordinates,freeze_dic):
for item in indices:
if item in freeze_dic:
if find_intersections(freeze_dic[item],coordinates):
return False
for item in indices:
if item in freeze_dic:
freeze_dic[item].append(coordinates)
else:
freeze_dic[item] = [coordinates]
for i in range(len(indices)-1):
if indices[i] not in res_dic:
res_dic[indices[i]] = {}
for j in range(i+1,len(indices)):
if indices[j] in res_dic[indices[i]]:
res_dic[indices[i]][indices[j]].append(coordinates)
else:
res_dic[indices[i]][indices[j]] = [coordinates]
return True
def tract_generator_from_dist_reverse(the_map,tract_dist, hap_count):
results = {}
freez_dict = {}
counter = 0
found = False
temp_map = np.array([[float(item[1]),int(item[2])] for item in the_map])
for size,count in enumerate(reversed(tract_dist)):
print("SIZE IS : "+str(50-size)+'------- #'+str(count))
for i in range(int(count)):
found = False
while not found: #finding one of tracts
indexes = np.random.choice(hap_count, 2)
counter = 0
min_ind = min(indexes)
max_ind = max(indexes)
while counter<3:
counter += 1
start_loc = np.random.choice(len(temp_map),1)[0]
dest = np.where(temp_map[:,0]-temp_map[start_loc,0] >= 50.0-size)[0]
if len(dest) == 0 :
continue
elif add_to_dict(results,min_ind,max_ind,[start_loc,dest[0]],freez_dict):
found = True
break
else:
continue
return results
def HD_ibd_generator(the_map,tract_dist, hap_count):
results = {}
freez_dict = {}
counter = 0
found = False
temp_map = np.array([[float(item[1]),int(item[2])] for item in the_map])
permutation = np.random.permutation(hap_count)
head = np.zeros(hap_count/2)
for size,count in enumerate(reversed(tract_dist)):
print("SIZE IS : "+str(50-size)+'------- #'+str(count))
for i in range(int(count)):
found = False
for j in range(head.shape[0]):
if temp_map[-1,0]-temp_map[head[j],0] >= 50-size:
results
def tract_generator_from_dist(the_map,tract_dist, hap_count):
results = {}
freez_dict = {}
counter = 0
found = False
temp_map = np.array([[item[1],item[2]] for item in the_map])
for size,count in enumerate(tract_dist):
for i in range(count):
found = False
while not found: #finding one of tracts
indexes = np.random.choice(hap_count, 2)
counter = 0
min_ind = min(indexes)
max_ind = max(indexes)
while counter<3:
start_loc = np.random.choice(len(temp_map),1)[0]
dest = np.where(temp_map[:,0]-temp_map[start_loc,0] >= size+3)[0]
if len(dest) == 0 :
continue
elif add_to_dict(results,min_ind,max_ind,[start_loc,dest[0]],freez_dict):
found = True
break
else:
continue
return results
def family_generator(the_map, tract_sizes,hap_count):
results = {}
freez_dict = {}
counter = 0
found = False
temp_map = np.array([[item[1],item[2]] for item in the_map])
for ind,key in enumerate(tract_sizes):
for i in range(tract_sizes[key]):
found = False
while not found: #finding one of tracts
indexes = np.random.choice(hap_count, 2)
counter = 0
min_ind = min(indexes)
max_ind = max(indexes)
while counter<3:
start_loc = np.random.choice(len(temp_map),1)[0]
dest = np.where(temp_map[:,0]-temp_map[start_loc,0] >= key)[0]
if len(dest) == 0 :
continue
elif add_to_dict(results,min_ind,max_ind,[start_loc,dest[0]],freez_dict):
found = True
break
else:
continue
return results
def make_related(relations, haps):
for ind,p1 in enumerate(relations):
for ind2,p2 in enumerate(relations[p1]):
for item in relations[p1][p2]:
haps[p2,item[0]:item[1]] = haps[p1,item[0]:item[1]]
def save_map(chr, the_map, file_name):
with open(file_name,'w') as output:
for item in the_map:
output.write(str(chr)+'\t'+item[0]+'\t'+str(item[1])+'\t'+str(item[2])+'\n')
def save_hap(data,file_name):
with open(file_name,'w') as output:
for i in range(data.shape[0]//2):
prepended = '%05d' % i
output.write('0 ' + prepended + ' 0 0 0 -9 ')
for j in range((data.shape[1])-1):
output.write(str(int(data[2*i,j]))+' ')
output.write(str(int(data[2*i +1,j]))+' ')
output.write(str(int(data[data.shape[0]-2,data.shape[1]-1]))+' ')
output.write(str(int(data[data.shape[0]-1,data.shape[1]-1]))+'\n')
def load_ilash_for_power(file_name,pos_dic,min_length=1.0,min_acc=0.0,mode='normal'):
count = 0
match_list = {}
total_length = 0
flag = False
with open(file_name) as iLash:
for line in iLash:
data = line.split('\t')
flag = False
temp_item = [pos_dic[int(data[5])], pos_dic[int(data[6])],float(data[-2]),float(data[-1])]
if temp_item[2] < min_length or temp_item[3] < min_acc:
continue
count += 1
total_length += temp_item[2]
if mode == 'individual':
data[1] = int(data[1][:-2])
data[3] = int(data[3][:-2])
else:
data[1] = int(data[1][:-2])*2 + int(data[1][-1])
data[3] = int(data[3][:-2])*2 + int(data[3][-1])
if data[1] in match_list:
if data[3] in match_list[data[1]]:
flag = True
match_list[data[1]][data[3]].append(temp_item)
continue
if (not flag) and data[3] in match_list:
if data[1] in match_list[data[3]]:
flag = True
match_list[data[3]][data[1]].append(temp_item)
continue
if not flag:
if data[1] in match_list:
match_list[data[1]][data[3]] = [temp_item]
continue
elif data[3] in match_list:
match_list[data[3]][data[1]] = [temp_item]
continue
else:
match_list[data[1]] = {}
match_list[data[1]][data[3]] = [temp_item]
return match_list,count,total_length
def load_beagle_for_power(addr,pos_dic,map_data, min_length=2.75,diploid=True):
count = 0
flag = False
total_length = 0
match_list = {}
with open(addr) as beagleIBD:
for line in beagleIBD:
flag = False
data = line.strip().split()
start_dist = map_data[pos_dic[int(data[5])]][2]
end_dist = map_data[pos_dic[int(data[6])]][2]
genetic_dist = end_dist-start_dist
if genetic_dist < min_length:
continue
id1 = int(data[0][2:])
id2 = int(data[2][2:])
if not diploid:
sign1 = 0 if data[1] == 1 else 1
sign2 = 0 if data[3] == 1 else 1
id1 = id1*2 + sign1
id2 = id2*2 + sign2
temp_item = [pos_dic[int(data[5])],pos_dic[int(data[6])],genetic_dist]
count += 1
total_length += genetic_dist
if id1 in match_list:
if id2 in match_list[id1]:
flag = True
match_list[id1][id2].append(temp_item)
if (not flag) and id2 in match_list:
if id1 in match_list[id2]:
match_list[id2][id1].append(temp_item)
if not flag:
if id1 in match_list:
match_list[id1][id2] = [temp_item]
elif id2 in match_list:
match_list[id2][id1] = [temp_item]
else:
match_list[id1] = {}
match_list[id1][id2] = [temp_item]
return match_list,count,total_length
def load_ribd_for_power(file_name,pos_dic,min_length=1.0,mode='normal'):
count = 0
match_list = {}
total_length = 0
flag = False
with open(file_name) as ribdFile:
for line in ribdFile:
data = line.split('\t')
flag = False
temp_item = [pos_dic[int(data[5])], pos_dic[int(data[6])],float(data[-1])]
if temp_item[2] < min_length:
continue
count += 1
total_length += temp_item[2]
if mode == 'individual':
data[1] = int(data[0])
data[3] = int(data[2])
else:
data[1] = int(data[0])*2 + int(data[1])-1
data[3] = int(data[2])*2 + int(data[3])-1
if data[1] in match_list:
if data[3] in match_list[data[1]]:
flag = True
match_list[data[1]][data[3]].append(temp_item)
continue
if (not flag) and data[3] in match_list:
if data[1] in match_list[data[3]]:
flag = True
match_list[data[3]][data[1]].append(temp_item)
continue
if not flag:
if data[1] in match_list:
match_list[data[1]][data[3]] = [temp_item]
continue
elif data[3] in match_list:
match_list[data[3]][data[1]] = [temp_item]
continue
else:
match_list[data[1]] = {}
match_list[data[1]][data[3]] = [temp_item]
return match_list,count,total_length
def load_germline_for_power(addr,pos_dic, min_length=1.0,mode='normal'):
count = 0
flag = False
total_length = 0
match_list = {}
with open(addr) as germFile:
for line in germFile:
flag = False
data = line.split()
id1 = id2 = None
if mode == 'individual':
id1 = int(data[1][:-2])
id2 = int(data[3][:-2])
else:
if data[1][-1] == '1':
sign = '1'
else:
sign = '0'
if data[3][-1] == '1':
sign2 = '1'
else:
sign2 = '0'
id1 = 2*int(data[1][:-2]) + int(sign)
id2 = 2*int(data[3][:-2]) + int(sign2)
temp_item = [pos_dic[int(data[5])], pos_dic[int(data[6])],float(data[10])]
if temp_item[2] < min_length:
continue
count += 1
total_length += temp_item[2]
if id1 in match_list:
if id2 in match_list[id1]:
flag = True
match_list[id1][id2].append(temp_item)
if (not flag) and id2 in match_list:
if id1 in match_list[id2]:
flag = True
match_list[id2][id1].append(temp_item)
if not flag:
if id1 in match_list:
match_list[id1][id2] = [temp_item]
elif id2 in match_list:
match_list[id2][id1] = [temp_item]
else:
match_list[id1] = {}
match_list[id1][id2] = [temp_item]
return match_list,count,total_length
def load_rapid_for_power(addr,map_data,pos_dic, min_length):
count = 0
total_length = 0
match_dic = {}
with open(addr) as results:
for line in results:
flag = False
data = line.strip().split()
id1 = int(data[1])
id2 = int(data[2])
length = map_data[pos_dic[int(data[4])]][2]-map_data[pos_dic[int(data[3])]][2]
temp_item = [pos_dic[int(data[3])],pos_dic[int(data[4])], length]
if length < min_length:
continue
count += 1
total_length += length
if id1 in match_dic:
if id2 in match_dic[id1]:
match_dic[id1][id2].append(temp_item)
continue
if id2 in match_dic:
if id1 in match_dic[id2]:
match_dic[id2][id1].append(temp_item)
continue
if id1 in match_dic:
match_dic[id1][id2] = [temp_item]
elif id2 in match_dic:
match_dic[id2][id1] = [temp_item]
else:
match_dic[id1] = {}
match_dic[id1][id2] = [temp_item]
return match_dic,count,total_length
def load_ilash_length(address, pos_dic):
count = 0
total_length = 0
with open(address) as iLash:
for line in iLash:
data = line.split('\t')
flag = False
temp_item = float(data[-2])
count += 1
total_length += temp_item
return count,total_length
def load_ribd_length(address, pos_dic):
count = 0
total_length = 0
with open(address) as ribdFile:
for line in ribdFile:
data = line.split('\t')
flag = False
temp_item = float(data[-1])
count += 1
total_length += temp_item
return count,total_length
def load_germline_length(address,pos_dic):
count = 0
total_length = 0
with open(address) as germFile:
for line in germFile:
data = line.split()
temp_item =float(data[10])
count += 1
total_length += temp_item
return count,total_length
def load_rapid_length(address,pos_dic,map_data):
count = 0
total_length = 0
with open(address) as inputfile:
for line in inputfile:
data = line.split()
length = map_data[pos_dic[int(data[4])]][2]-map_data[pos_dic[int(data[3])]][2]
count += 1
total_length += length
return count,total_length
def load_beagle_length(addr,pos_dic,map_data, min_length=2.75):
count = 0
total_length = 0
with open(addr) as beagleIBD:
for line in beagleIBD:
data = line.strip().split()
start_dist = map_data[pos_dic[int(data[5])]][2]
end_dist = map_data[pos_dic[int(data[6])]][2]
genetic_dist = end_dist-start_dist
if genetic_dist < min_length:
continue
count += 1
total_length += genetic_dist
return count,total_length
#dictSize should be in haps since the input is haplotype based.
def change_hap_dict(refDictList,dictSize,indCount):
hapCount = 2*indCount
dictToTraverse = hapCount//dictSize
if hapCount%dictSize == 0 :
dictToTraverse -= 1
resultDictList = []
for i in range(dictToTraverse+1):
tempDict = {}
for key1 in refDictList[i]:
if key1+(i*dictSize) > hapCount:
continue
for key2 in refDictList[i][key1]:
if key2+(i*dictSize) > hapCount:
continue
tempTractList = refDictList[i][key1][key2]
tkey1 = key1//2
tkey2 = key2//2
if tkey1 not in tempDict and tkey2 not in tempDict:
tempDict[tkey1] = {tkey2:tempTractList}
elif tkey2 not in tempDict:
if tkey2 in tempDict[tkey1]:
tempDict[tkey1][tkey2] += tempTractList
else:
tempDict[tkey1][tkey2] = tempTractList
elif tkey1 not in tempDict:
if tkey1 in tempDict[tkey2]:
tempDict[tkey2][tkey1] += tempTractList
else:
tempDict[tkey2][tkey1] = tempTractList
else:
if tkey2 in tempDict[tkey1]:
tempDict[tkey1][tkey2] += tempTractList
elif tkey1 in tempDict[tkey2]:
tempDict[tkey2][tkey1] += tempTractList
else:
tempDict[tkey1][tkey2] = tempTractList
resultDictList.append(tempDict)
return resultDictList
def check_sim_ibd(fam_dic,match_dic):
for ind1,p1 in enumerate(fam_dic):
for ind2,p2 in enumerate(fam_dic[p1]):
if p1 in match_dic:
if p2 in match_dic[p1]:
pass