-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapofblocks.py
439 lines (359 loc) · 15.3 KB
/
mapofblocks.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict
import sys
def formAdj(chromosome, maximum):
"""make an adjacency list from a list of syntheny blocks"""
adjList = []
start = 0
for elem in chromosome:
adjList.append((start, elem[0]))
start = elem[1]
adjList.append((start, maximum))
return adjList
def formFullMapAdj(chromosome, maximum):
"""make an adjacency list from a list of syntheny blocks"""
adjList = []
start = 0
for i in range(len(chromosome)):
adjList.append((start, chromosome[i][0]))
start = chromosome[i][1]
adjList.append(chromosome[i])
adjList.append((start, maximum))
return adjList
def appending(aDict, aAdjDict, currentLow, currentHigh):
if len(currentLow) == 3 and currentHigh not in aDict[currentLow]:
aDict[currentLow].append(currentHigh)
elif len(currentLow) == 2 and currentHigh not in aAdjDict[currentLow]:
aAdjDict[currentLow].append(currentHigh)
def isInside(block, med):
if len(block) == 3 and block[0] <= med and block[1] >= med:
return True
elif len(block) == 2 and block[0] < med and block[1] > med:
return True
else:
return False
def formMapSmooth(lowResGenome, highResGenome, sixDimVect):
"""returns statistics for M1, M2, M3, N1, N2, N3 for different genomes/chromosomes"""
maximum = 30000000000
lowRes = formFullMapAdj(lowResGenome, maximum)
highRes = formFullMapAdj(highResGenome, maximum)
length1 = len(lowRes)
length2 = len(highRes)
dictOfFakeSynBlocks = defaultdict(bool)
listOfFakes = []
k = 0
l = 0
while k < len(lowResGenome):
if k < len(lowResGenome):
currentLow = lowResGenome[k]
dictOfFakeSynBlocks[currentLow]
for currentHigh in highResGenome:
med = (currentHigh[0] + currentHigh[1]) / 2
if isInside(currentLow, med):
dictOfFakeSynBlocks[currentLow] = True
l += 1
k += 1
for key in sorted(dictOfFakeSynBlocks.iterkeys()):
if dictOfFakeSynBlocks[key] == False:
listOfFakes.append(key)
m = 0
newHighRes = []
filteredHighRes = []
"""if len(listOfFakes) > 0:
newHighRes.extend(listOfFakes)
newHighRes.extend(highResGenome)
newHighRes.sort(key=lambda x: x[0])
prevElem = (0,0)
nextElem = (maximum, maximum)
for i in range(len(newHighRes)):
elem = newHighRes[i]
if i > 0:
prevElem = newHighRes[i-1]
else:
prevElem = (0, 0, "+")
if i < len(newHighRes) - 1:
nextElem = newHighRes[i+1]
else:
nextElem = (maximum, maximum, "+")
if elem[0] < prevElem[1] and elem[1] > nextElem[0]:
continue
elif elem[1] < nextElem[0]:
filteredHighRes.append(elem)
elif elem in listOfFakes:
filteredHighRes.append(elem)
elif elem[1] >= nextElem[0]: #and nextElem in listOfFakes:
filteredHighRes.append((elem[0], nextElem[0], elem[2]))
elif elem[0] <= prevElem[1]: #and prevElem in listOfFakes:
filteredHighRes.append((prevElem[1], elem[1], elem[2]))
elif elem[0] >= prevElem[1] and elem[1] <= nextElem[0]:
filteredHighRes.append(elem)
else:
#print elem
#print nextElem, "NEXT"
elem = list(elem)
elem[0] = nextElem[0]
filteredHighRes.append(tuple(elem))
print elem
print nextElem
print newHighRes
if len(filteredHighRes) != len(highResGenome) + len(listOfFakes):
print "WOW"
print len(filteredHighRes)
print len(highResGenome), len(listOfFakes)
highRes = formFullMapAdj(filteredHighRes, maximum)
length2 = len(highRes)"""
aDict = defaultdict(list)
aAdjDict = defaultdict(list)
i = 0 # low res counter
j = 0 # high res counter
currentLow = lowRes[i]
currentHigh = highRes[j]
#print currentLow, currentHigh
overlapStats = defaultdict(list)
# always add first telomere end of high res in low res
appending(aDict, aAdjDict, currentLow, currentLow)
j += 1
while i < length1 and j < length2:
if len(currentLow) == 3 and currentLow not in aDict:
aDict[currentLow]
if len(currentLow) == 2 and currentLow not in aAdjDict:
aAdjDict[currentLow]
currentLow = lowRes[i]
if len(currentLow) == 2 and currentLow[1] - currentLow[0] == 0\
and currentLow not in aAdjDict:
aAdjDict[currentLow]
i += 1
continue
currentHigh = highRes[j]
med = (currentHigh[0] + currentHigh[1]) / 2
if len(currentHigh) == 2 and currentHigh[1] - currentHigh[0] == 0\
and isInside(currentLow, med):
appending(aDict, aAdjDict, currentLow, currentHigh)
j += 1
continue
#print currentLow, currentHigh
if currentHigh[0] >= currentLow[1]:
i += 1
continue
# if the block of high resolution lays within the block of low resolution
elif currentLow[0] <= currentHigh[0] and currentLow[1] >= currentHigh[1]:
appending(aDict, aAdjDict, currentLow, currentHigh)
j += 1
# if the block of low resolution lays within the block of high resolution
elif currentLow[0] >= currentHigh[0] and currentLow[1] < currentHigh[1]:
i += 1
if isInside(currentLow, med):
appending(aDict, aAdjDict, currentLow, currentHigh)
elif len(currentHigh) == 3 and len(currentLow) == 3:
aDict[currentLow]
elif len(currentHigh) == 2 and len(currentLow) == 3:
aDict[currentLow]
elif len(currentHigh) == 2 and len(currentLow) == 2:
aAdjDict[currentLow]
elif len(currentHigh) == 3 and len(currentLow) == 2:
appending(aDict, aAdjDict, currentLow, currentHigh)
if currentLow[1] == currentHigh[1]:
j += 1
i += 1
# if the block of low resolution overlaps with block of high resolution
elif currentLow[0] > currentHigh[0] and currentLow[0] < currentHigh[1]:
if len(currentLow) == 3 and len(currentHigh) == 3:
overlapStats[currentLow].append(currentHigh)
if isInside(currentLow, med):
appending(aDict, aAdjDict, currentLow, currentHigh)
j += 1
else:
appending(aDict, aAdjDict, lowRes[i-1], currentHigh)
j += 1
elif currentLow[1] > currentHigh[0] and currentLow[1] < currentHigh[1]:
if len(currentLow) == 3 and len(currentHigh) == 3:
overlapStats[currentLow].append(currentHigh)
if isInside(currentLow, med):
appending(aDict, aAdjDict, currentLow, currentHigh)
i += 1
else:
if len(currentLow) == 3 and currentLow not in aDict:
aDict[currentLow]
elif isInside(lowRes[i+1], med):
appending(aDict, aAdjDict, lowRes[i+1], currentHigh)
i += 1
elif currentLow[1] == currentHigh[0]:
i += 1
elif currentLow[0] == currentHigh[1]:
j += 1
else:
print currentLow, currentHigh
sys.exit("Error message")
if lowResGenome[-1] not in aDict:
print lowResGenome[-1]
print aDict
#while currentLow[1] == maximum and currentHigh[1] != maximum:
#print aDict, aAdjDict
#print len(aDict), len(lowResGenome)
return (aDict, aAdjDict, overlapStats, len(listOfFakes))
def formMapSmooth12(lowResGenome, highResGenome, sixDimVect):
"""returns statistics for M1, M2, M3, N1, N2, N3 for different genomes/chromosomes"""
maximum = 30000000000
lowRes = formFullMapAdj(lowResGenome, maximum)
highRes = formFullMapAdj(highResGenome, maximum)
length1 = len(lowRes)
length2 = len(highRes)
aDict = defaultdict(list)
aAdjDict = defaultdict(list)
i = 0
j = 0
currentLow = lowRes[i]
currentHigh = highRes[j]
overlapStats = 0
appending(aDict, aAdjDict, currentLow, currentHigh)
i += 1
if currentHigh[1] <= currentLow[1]:
j += 1
while (currentLow[1] != maximum and currentHigh[1] != maximum):
currentLow = lowRes[i]
currentHigh = highRes[j]
currHighMed = float(currentHigh[1] + currentHigh[0]) / 2
if ((currentLow[0] <= currentHigh[0] and currentLow[1] >= currentHigh[1])):
appending(aDict, aAdjDict, currentLow, currentHigh)
j += 1
elif (currentHigh[0] <= currentLow[0] and currentHigh[1] >= currentLow[1]):
appending(aDict, aAdjDict, currentLow, currentHigh)
j += 1
i += 1
elif currentHigh[0] < currentLow[0] and currentHigh[1] > currentLow[0]:
overlapStats += 1
j += 1
if currHighMed >= currentLow[0] and currHighMed < currentLow[1]:
appending(aDict, aAdjDict, currentLow, currentHigh)
else:
prevLow = lowRes[i-1]
appending(aDict, aAdjDict, prevLow, currentHigh)
elif currentHigh[0] < currentLow[1] and currentHigh[1] > currentLow[1]:
overlapStats += 1
j += 1
if currHighMed < currentLow[1] and currentHigh[1] != maximum:
appending(aDict, aAdjDict, currentLow, currentHigh)
elif currentHigh[1] != maximum:
nextLow = lowRes[i + 1]
appending(aDict, aAdjDict, nextLow, currentHigh)
elif currentHigh[0] >= currentLow[1]:
i += 1
flagOfBlocksWithin = False
if currentLow in aDict:
for elem in aDict[currentLow]:
if len(elem) == 3:
flagOfBlocksWithin = True
if ((len(currentLow) == 3 and currentLow not in aDict)
or (flagOfBlocksWithin == False and len(currentLow) == 3 and currentLow != lowRes[i])):
aDict[currentLow]
while not (currentLow[1] == maximum and currentHigh[1] == maximum):
if (currentLow[1] != maximum):
currentLow = lowRes[i]
elif (currentHigh[1] != maximum):
currentHigh = highRes[j]
if currentHigh[1] == maximum:
currentLow = lowRes[i]
if currentLow[1] > currentHigh[0] and currentLow[1] == maximum:
appending(aDict, aAdjDict, currentLow, currentHigh)
elif currentLow not in aDict and currentLow not in aAdjDict and len(currentLow) == 3:
aDict[currentLow]
i += 1
elif currentLow[1] == maximum:
currentHigh = highRes[j]
if (currentHigh[0] >= currentLow[0]):
appending(aDict, aAdjDict, currentLow, currentHigh)
else:
overlapStats += 1
currHighMed = float(currentHigh[1] + currentHigh[0]) / 2
if currHighMed > currentLow[0]:
appending(aDict, aAdjDict, currentLow, currentHigh)
else:
prevLow = lowRes[i-1]
appending(aDict, aAdjDict, prevLow, currentHigh)
j += 1
return (aDict, aAdjDict, overlapStats)
def formMapStrict(lowResGenome, highResGenome):
"""Returns elements of class that lays between the borders of elements of class b. Class b:
elements of syntheny blocks and adj list"""
flag = False
maximum = "infty" #3 * (10 ** 9) # 3 billions of bp length - no one chromosome can reach this limit! =)
# of course we can compute this value, but...why we need this if we can get it for free? =)
a = iter(lowResGenome)
b = iter(highResGenome)
aAdj = iter(formAdj(lowResGenome, maximum))
bAdj = iter(formAdj(highResGenome, maximum))
aDict = defaultdict(list)
aAdjDict = defaultdict(list)
currLow = next(aAdj)
currHigh = next(bAdj)
done = object()
overlapStats = 0
anotherOverlap = 0
# TODO: rewrite this part of code with while loops
# don't forget to process 1) first part 2) main part 3) end
while (currLow[1] is not done) or (currHigh[1] is not done):
if currLow[1] == currHigh[1] == maximum and currHigh[0] >= currLow[0]:
aAdjDict[currLow].append(currHigh)
break
elif currLow[1] == currHigh[1] == maximum and currHigh[0] < currLow[0]:
break
elif currLow[1] == maximum:
if len(currHigh) == 3:
if currHigh[0] >= currLow[0]:
aAdjDict[currLow].append(currHigh)
currHigh = next(bAdj, done)
elif len(currHigh) == 2:
if currHigh[0] >= currLow[0]:
aAdjDict[currLow].append(currHigh)
currHigh = next(b, done)
elif currHigh[1] == maximum and currLow[1] != maximum:
if len(currLow) == 3:
currLow = next(aAdj, done)
elif len(currLow) == 2:
currLow = next(a, done)
elif (currHigh[0] >= currLow[0] and currHigh[1] <= currLow[1]):
if len(currHigh) == 3 and len(currLow) == 3:
aDict[currLow].append(currHigh)
currHigh = next(bAdj, done)
elif len(currHigh) == 2 and len(currLow) == 2:
aAdjDict[currLow].append(currHigh)
currHigh = next(b, done)
elif len(currHigh) == 3 and len(currLow) == 2:
aAdjDict[currLow].append(currHigh)
currHigh = next(bAdj, done)
elif (len(currHigh) == 2 or len(currHigh) == 1) and len(currLow) == 3:
aDict[currLow].append(currHigh)
currHigh = next(b, done)
elif (currHigh[0] < currLow[1] and currHigh[1] > currLow[1]):
if len(currHigh) == 2:
currHigh = next(b, done)
anotherOverlap += 1
elif len(currHigh) == 3:
if len(currLow) == 3:
flag = True
overlapStats += 1
# print currHigh, currLow
else:
flag = True
overlapStats += 1
# print currHigh, currLow
currHigh = next(bAdj, done)
elif (currHigh[0] < currLow[0] and currHigh[1] > currLow[0]):
if len(currHigh) == 2:
currHigh = next(b, done)
anotherOverlap += 1
elif len(currHigh) == 3:
if len(currLow) == 3:
flag = True
overlapStats += 1
# print currHigh, currLow
else:
anotherOverlap += 1
currHigh = next(bAdj, done)
elif currHigh[0] >= currLow[1]:
if len(currLow) == 2:
currLow = next(a, done)
elif len(currLow) == 3:
currLow = next(aAdj, done)
return (aDict, aAdjDict, overlapStats, anotherOverlap, flag)