Debian Codegraph @

atac-driver/chainer/python/squeezeIntraRunGaps.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env python
2
3 import sys
4 import string
5 import MatchRecord
6 #import MyFile
7 import IdxStore
8 #import localAlignerInterface
9 #import shelve
10
11 # True=1
12 False=0
13
14
15 theIsolatedSNPcount = 0
16 completefillednotXY = 0
17 completefilledXnotY = 0
18 completefilledYnotX = 0
19 completefilledXandY = 0
20
21 def analyzeGap(x,y,left,right,outfile,maxgap,margin):
22 global theIsolatedSNPcount
23 global completefillednotXY
24 global completefilledXnotY
25 global completefilledYnotX
26 global completefilledXandY
27 solidThreshold=20
28 inter_run_gap_count = 0
29 x_chCount = {}
30 y_chCount = {}
31 x_notACGT = 0
32 y_notACGT = 0
33
34 lp = 0 # We should modify the match instead!
35 rp = 0
36 x_pos = 0
37 x_len = 0
38 y_pos = 0
39 y_len = 0
40 if( (left.x_scaf_uid == right.x_scaf_uid) and
41 (left.y_scaf_uid == right.y_scaf_uid) and
42 (left.runid == right.runid) ):
43 # (left.sindex + 1 == right.sindex) ): # This is obsolete in Russell's file format.
44 # sys.stderr.write("Intra-run gap\n")
45 left_forward = (left.x_orientation == left.y_orientation)
46 right_forward = (right.x_orientation == right.y_orientation)
47 if( left_forward != right_forward): sys.stderr.write("Bad orientations in run\n")
48 assert(left_forward == right_forward)
49
50 sorted_by_x = (left.x_start <= right.x_start)
51 dovetail_in_x = sorted_by_x and (left.x_start+left.x_length <= right.x_start+right.x_length)
52 sorted_by_y = (left.y_start <= right.y_start)
53 dovetail_in_y = sorted_by_y and (left.y_start+left.y_length <= right.y_start+right.y_length)
54 if(not(sorted_by_x or sorted_by_y)):
55 print >>sys.stderr, "bad sorting in runs"
56 print >>sys.stderr, left
57 print >>sys.stderr, right
58 assert(sorted_by_x or sorted_by_y)
59 # This concept of sorted allows neggaps but not containmant in both axes.
60
61 if(sorted_by_x and not dovetail_in_x):
62 print >>sys.stderr, "sorted_by_x and not dovetail_in_x"
63 print >>sys.stderr, left
64 print >>sys.stderr, right
65
66 if(sorted_by_y and not dovetail_in_y):
67 print >>sys.stderr, "sorted_by_y and not dovetail_in_y"
68 print >>sys.stderr, left
69 print >>sys.stderr, right
70
71 if(not((not left_forward) or (sorted_by_x and sorted_by_y))):
72 print >>sys.stderr, "bad sorting in runs"
73 print >>sys.stderr, left
74 print >>sys.stderr, right
75 if(not((left_forward) or (not(sorted_by_x and sorted_by_y)))):
76 print >>sys.stderr, "bad sorting in runs"
77 print >>sys.stderr, left
78 print >>sys.stderr, right
79 assert((not left_forward) or (sorted_by_x and sorted_by_y))
80 assert((left_forward) or (not(sorted_by_x and sorted_by_y)))
81
82 if(sorted_by_x): # Sorted by x positions.
83 #print "Sorted by X"
84 x_pos = left.x_start + left.x_length # Start of the intra-run gap.
85 x_len = right.x_start - x_pos # Length of the intra-run gap.
86 if(left_forward):
87 y_pos = left.y_start + left.y_length
88 y_len = right.y_start - y_pos
89 else:
90 y_pos = right.y_start + right.y_length
91 y_len = left.y_start - y_pos
92 # end if
93 else: # Assume sorted by y positions
94 #print "Sorted by Y"
95 y_pos = left.y_start + left.y_length
96 y_len = right.y_start - y_pos
97 if(left_forward):
98 x_pos = left.x_start + left.x_length
99 x_len = right.x_start - x_pos
100 else:
101 x_pos = right.x_start + right.x_length
102 x_len = left.x_start - x_pos
103 # end if
104 # end if
105 # print "Left %d,%d Right %d,%d Width %d,%d" % (x_pos,y_pos,x_pos+x_len,y_pos+y_len,x_len,y_len)
106
107 assert(left.x_start >= 0)
108 assert(left.x_length > 0)
109 assert(left.y_start >= 0)
110 assert(left.y_length > 0)
111 assert(right.x_start >= 0)
112 assert(right.x_length > 0)
113 assert(right.y_start >= 0)
114 assert(right.y_length > 0)
115
116 # Trim the intra-run neggaps to become proper gaps.
117 if(0 and (x_len < 0 or y_len < 0)):
118 sys.stderr.write("neggap x_uid= %s x_pos= %d x_len= %d y_uid= %s y_pos= %d y_len= %d\n" %
119 (left.x_scaf_uid,x_pos,x_len,left.y_scaf_uid,y_pos,y_len))
120 trim_len = max( -x_len, -y_len)
121 # Increase the intra-run gap length:
122 x_len += trim_len; y_len += trim_len
123 # Decrease the right-hand match length:
124 right.x_length -= trim_len
125 right.y_length -= trim_len
126 # Adjust the right-hand gap ending position:
127 if(left_forward):
128 right.x_start += trim_len
129 right.y_start += trim_len
130 else:
131 if(sorted_by_x):
132 right.x_start += trim_len
133 y_pos -= trim_len
134 else: # assume sorted_by_y
135 right.y_start += trim_len
136 x_pos -= trim_len
137 # end if
138 #end if
139 sys.stderr.write("newgap x_uid= %s x_pos= %d x_len= %d y_uid= %s y_pos= %d y_len= %d\n" %
140 (left.x_scaf_uid,x_pos,x_len,left.y_scaf_uid,y_pos,y_len))
141 # end if
142 assert(right.x_length > 0)
143 assert(right.y_length > 0)
144 # We now have a proper intra-run gap segment between two match segments.
145
146 x_substring = ""
147 if(x_len > 0):
148 x_substring = string.upper(
149 x.getStringFromFasta( sorted_by_x, left.x_scaf_uid, x_pos, x_len));
150 # end if
151 y_substring = ""
152 if(y_len > 0):
153 y_substring = string.upper(
154 y.getStringFromFasta( sorted_by_y, left.y_scaf_uid, y_pos, y_len));
155 # end if
156 if(x_len > 0 and not(x_len == len(x_substring))):
157 sys.stderr.write("x string lengths mismatch asked=%d got=%d\n" % (x_len,len(x_substring)))
158 sys.stderr.write("x_uid= %s x_pos= %d x_len= %d y_uid= %s y_pos= %d y_len= %d\n" %
159 (left.x_scaf_uid,x_pos,x_len,left.y_scaf_uid,y_pos,y_len))
160 print >>sys.stderr, "left match"
161 print >>sys.stderr, left
162 print >>sys.stderr, "right match"
163 print >>sys.stderr, right
164 # end if
165 if(y_len > 0 and not(y_len == len(y_substring))):
166 sys.stderr.write("y string lengths mismatch asked=%d got=%d\n" % (y_len,len(y_substring)))
167 sys.stderr.write("x_uid= %s x_pos= %d x_len= %d y_uid= %s y_pos= %d y_len= %d\n" %
168 (left.x_scaf_uid,x_pos,x_len,left.y_scaf_uid,y_pos,y_len))
169 print >>sys.stderr, "left match"
170 print >>sys.stderr, left
171 print >>sys.stderr, "right match"
172 print >>sys.stderr, right
173 # end if
174
175 assert(x_len < 0 or x_len == len(x_substring))
176 assert(y_len < 0 or y_len == len(y_substring))
177 assert(lp == 0)
178 assert(rp == 0)
179
180 # Next we extend the raw matches to squeeze the intra-run gaps
181 # with exactly matching sequence.
182 if( lp+rp < x_len and lp+rp < y_len ):
183 while(lp+rp < x_len and lp+rp < y_len):
184 # modify lp
185 x_ch = x_substring[lp]; y_ch = y_substring[lp];
186 is_a_match = (x_ch==y_ch) and \
187 (x_ch=="A" or x_ch=="C" or x_ch=="G" or x_ch=="T")
188 if(is_a_match):
189 lp += 1
190 else:
191 break
192 # end if
193 # end while
194 while(lp+rp < x_len and lp+rp < y_len):
195 # modify rp
196 x_ch = x_substring[-1-rp]; y_ch = y_substring[-1-rp];
197 is_a_match = (x_ch==y_ch) and \
198 (x_ch=="A" or x_ch=="C" or x_ch=="G" or x_ch=="T")
199 if(is_a_match):
200 rp += 1
201 else:
202 break
203 # end if
204 # end while
205 # end if
206
207 # Next we extend the raw matches to squeeze the intra-run gaps.
208 # Each mismatch character must be padded on both sides by
209 # "solidThreshold" characters from {A,C,G,T}.
210 if( x_len > lp+rp and y_len > lp+rp ):
211 lq = lp; solid = solidThreshold; tentativeSNPCount = 0
212 while(lq+rp < x_len and lq+rp < y_len):
213 x_ch = x_substring[lq]; y_ch = y_substring[lq];
214 is_a_match = (x_ch==y_ch) and \
215 (x_ch=="A" or x_ch=="C" or x_ch=="G" or x_ch=="T")
216 if(solid >= solidThreshold):
217 lp = lq
218 theIsolatedSNPcount += tentativeSNPCount
219 tentativeSNPCount = 0
220 if(is_a_match):
221 solid += 1
222 lp += 1
223 else:
224 solid = 0
225 tentativeSNPCount = 1
226 tentativeSNPposition = lq
227 # end if
228 else:
229 if(is_a_match):
230 solid += 1
231 else: # a second mismatch within 20 bp
232 break
233 # end if
234 # end if
235 lq += 1
236 # end while
237 if(lq+rp == x_len and lq+rp == y_len):
238 lp = lq
239 theIsolatedSNPcount += tentativeSNPCount
240 tentativeSNPCount = 0
241 # end if
242 rq = rp; solid = solidThreshold; tentativeSNPCount = 0
243 while( lp+rq < x_len and lp+rq < y_len ):
244 x_ch = x_substring[-1-rq]; y_ch = y_substring[-1-rq];
245 is_a_match = (x_ch==y_ch) and \
246 (x_ch=="A" or x_ch=="C" or x_ch=="G" or x_ch=="T")
247 if(solid >= solidThreshold):
248 rp = rq
249 theIsolatedSNPcount += tentativeSNPCount
250 tentativeSNPCount = 0
251 if(is_a_match):
252 solid += 1
253 rp += 1
254 else:
255 solid = 0
256 tentativeSNPCount = 1
257 # end if
258 else:
259 if(is_a_match):
260 solid += 1
261 else: # a second mismatch within 20 bp
262 break
263 # end if
264 # end if
265 rq += 1
266 # end while
267 if( lp+rq == x_len and lp+rq == y_len ):
268 rp = rq
269 theIsolatedSNPcount += tentativeSNPCount
270 tentativeSNPCount = 0
271 # end if
272 # end if
273
274 # Next we close any remaining intra-run gaps that can form
275 # ungapped alignments of a specified high quality.
276 # Currently we have hard coded that there must be
277 # 5 or less mismatches or
278 # better than 95% identity
279 # in the intrarun gap remaining after the previous gap closing.
280 assert(lp >= 0)
281 assert(rp >= 0)
282 if( x_len == y_len and x_len > lp+rp):
283 lq = lp; mismatchCount = 0
284 while(lq+rp < x_len and lq+rp < y_len):
285 x_ch = x_substring[lq]; y_ch = y_substring[lq];
286 is_a_match = (x_ch==y_ch) and \
287 (x_ch=="A" or x_ch=="C" or x_ch=="G" or x_ch=="T")
288 if(not is_a_match): mismatchCount += 1
289 lq += 1
290 # end while
291 if(mismatchCount <= 5 or
292 mismatchCount <= 0.05*(x_len-lp-rp) ):
293 lp=lq
294 # sys.stderr.write("# Closed gap by jumping\n");
295 # end if
296 # end if
297
298 if(0):
299 sys.stderr.write( "rawX: %s\n" % x_substring)
300 sys.stderr.write( "rawY: %s\n" % y_substring)
301 sys.stderr.write( "squX: %s\n" % x_substring[:lp]+ \
302 string.lower(x_substring[lp:x_len-rp]) + \
303 x_substring[x_len-rp:x_len])
304 sys.stderr.write( "squY: %s\n" % y_substring[:lp]+ \
305 string.lower(y_substring[lp:y_len-rp]) + \
306 y_substring[y_len-rp:y_len])
307 sys.stderr.write( "sqeX: %s\n" % x_substring[lp:x_len-rp])
308 sys.stderr.write( "sqeY: %s\n" % y_substring[lp:y_len-rp])
309 sys.stderr.write( "x_seg=(%s %s %d %d)" % \
310 (left.x_orientation, left.x_scaf_uid, x_pos+lp, x_len-rp-lp))
311 sys.stderr.write( "y_seg=(%s %s %d %d)\n" % \
312 (left.y_orientation, left.y_scaf_uid, y_pos+lp, y_len-rp-lp))
313
314 if(lp+rp > x_len and x_len >= 0):
315 sys.stderr.write("overfilledX ")
316 sys.stderr.write( "x_seg=(%s %s %d %d) " % \
317 (left.x_orientation, left.x_scaf_uid, x_pos+lp, x_len-rp-lp))
318 sys.stderr.write( "y_seg=(%s %s %d %d)\n" % \
319 (left.y_orientation, left.y_scaf_uid, y_pos+lp, y_len-rp-lp))
320 if(lp+rp > y_len and y_len >= 0):
321 sys.stderr.write("overfilledY ")
322 sys.stderr.write( "x_seg=(%s %s %d %d) " % \
323 (left.x_orientation, left.x_scaf_uid, x_pos+lp, x_len-rp-lp))
324 sys.stderr.write( "y_seg=(%s %s %d %d)\n" % \
325 (left.y_orientation, left.y_scaf_uid, y_pos+lp, y_len-rp-lp))
326 if(lp+rp < x_len and lp+rp < y_len): completefillednotXY += 1
327 if(lp+rp == x_len and x_len < y_len): completefilledXnotY += 1
328 if(lp+rp == y_len and y_len < x_len): completefilledYnotX += 1
329 if(lp+rp == x_len and x_len == y_len): completefilledXandY += 1
330
331 # Print out abutting intervals to fill gaps.
332 if(lp>0):
333 left_fill = left.copy()
334 if(left_forward):
335 left_fill.subtype = "L"
336 left_fill.x_start = x_pos
337 left_fill.x_length = lp
338 left_fill.y_start = y_pos
339 left_fill.y_length = lp
340 left_fill.matchid = left_fill.matchid + "L"
341
342 else:
343 if(sorted_by_x):
344 left_fill.subtype = "L"
345 left_fill.x_start = x_pos
346 left_fill.x_length = lp
347 left_fill.y_start = y_pos+y_len-lp
348 left_fill.y_length = lp
349 left_fill.matchid = left_fill.matchid + "L"
350 else: # assume sorted_by_y
351 left_fill.subtype = "L"
352 left_fill.x_start = x_pos+x_len-lp
353 left_fill.x_length = lp
354 left_fill.y_start = y_pos
355 left_fill.y_length = lp
356 left_fill.matchid = left_fill.matchid + "L"
357 # end if
358 # end if
359 # outfile.write(str(left_fill))
360 print >>outfile, left_fill
361 # end if
362 if(rp>0):
363 right_fill = right.copy()
364 if(left_forward):
365 right_fill.subtype = "R"
366 right_fill.x_start = x_pos+x_len-rp
367 right_fill.x_length = rp
368 right_fill.y_start = y_pos+y_len-rp
369 right_fill.y_length = rp
370 right_fill.matchid = right_fill.matchid + "R"
371 else:
372 if(sorted_by_x):
373 right_fill.subtype = "R"
374 right_fill.x_start = x_pos+x_len-rp
375 right_fill.x_length = rp
376 right_fill.y_start = y_pos
377 right_fill.y_length = rp
378 right_fill.matchid = right_fill.matchid + "R"
379 else: # assume sorted_by_y
380 right_fill.subtype = "R"
381 right_fill.x_start = x_pos
382 right_fill.x_length = rp
383 right_fill.y_start = y_pos+y_len-rp
384 right_fill.y_length = rp
385 right_fill.matchid = right_fill.matchid + "R"
386 # end if
387 # end if
388 #outfile.write(str(right_fill))
389 print >>outfile, right_fill
390 # end if
391
392 if(0): # Start gap composition diagnostics.
393 if( (x_len > lp+rp) or (y_len > lp+rp) ):
394 for ch in x_substring[lp:x_len-rp]:
395 if(not(ch=='A' or ch=='C' or ch=='G' or ch=='T')):
396 x_notACGT += 1
397 try:
398 x_chCount[ch] += 1
399 except KeyError:
400 x_chCount[ch] = 1
401 for ch in y_substring[lp:y_len-rp]:
402 if(not(ch=='A' or ch=='C' or ch=='G' or ch=='T')):
403 y_notACGT += 1
404 try:
405 y_chCount[ch] += 1
406 except KeyError:
407 y_chCount[ch] = 1
408 if(1 or x_notACGT > 0 or y_notACGT > 0):
409 sys.stderr.write("Ncounts %d %d\n" % (x_notACGT,y_notACGT))
410 sys.stderr.write("x_gap_len= %d y_gap_len= %d\n" % (x_len-lp-rp,y_len-lp-rp))
411 sys.stderr.write("x_seg=(%s %s %d %d)\n" % \
412 (left.x_orientation, left.x_scaf_uid, x_pos+lp, x_len-lp-rp))
413 sys.stderr.write("y_seg=(%s %s %d %d)\n" % \
414 (left.y_orientation, left.y_scaf_uid, y_pos+lp, y_len-lp-rp))
415 # sys.stderr.write("x_chCount= ")
416 # sys.stderr.write(x_chCount)
417 # sys.stderr.write("y_chCount= ")
418 # sys.stderr.write( y_chCount);
419 else:
420 # sys.stderr.write("Inter-run gap\n")
421 inter_run_gap_count += 1
422 # sys.stderr.write("done\n")
423 squeezed = lp+rp
424 return (inter_run_gap_count,
425 squeezed,x_len-squeezed,y_len-squeezed,x_notACGT,y_notACGT)
426 # end def
427
428 def mainLoop( inpfile, outfile, xIdx, yIdx):
429
430 maxgap = 100000 # This should be set by an ATAC global.
431 margin = 20 # This should be set by an ATAC global.
432
433 countLines = 0
434 inter_run_gap_count_total = 0
435 closed_gap_count_total = 0
436 squeezed_total = 0
437 x_len_total = 0
438 y_len_total = 0
439 x_nonACGT_total = 0
440 y_nonACGT_total = 0
441
442 inpfile.seek(0)
443 inpfileIter = iter(inpfile)
444
445 left = None
446 for line in inpfileIter:
447 if(line[0] == 'M'):
448 left = MatchRecord.MatchRecord(line)
449 # outfile.write(str(left))
450 print >>outfile, left
451 countLines += 1
452 break;
453
454 for line in inpfileIter:
455 if(line[0] == 'M'):
456 right = MatchRecord.MatchRecord(line)
457
458 #if( countLines % 10000 == 0):
459 # sys.stderr.write("countLines=%d\n" % countLines)
460
461 (inter_run_gap_count,squeezed,x_len,y_len,x_notACGT,y_notACGT) \
462 = analyzeGap(xIdx,yIdx,left,right, outfile, maxgap, margin)
463 inter_run_gap_count_total += inter_run_gap_count
464 squeezed_total += squeezed
465 x_len_total += x_len
466 y_len_total += y_len
467 x_nonACGT_total += x_notACGT
468 y_nonACGT_total += y_notACGT
469 if(x_len == 0 and y_len == 0): closed_gap_count_total += 1
470
471 # Output the record which was possibly trimmed.
472 #outfile.write(str(right))
473 print >>outfile, right
474 countLines += 1
475 left = right
476 # end if
477 # end for
478
479 sys.stderr.write(
480 "countLines %d inter_run_gap_count %d closed_gap_count %d squeezed %d x_len %d y_len %d x_nonACGT %d y_nonACGT %d\n" %
481 (countLines,inter_run_gap_count_total,closed_gap_count_total,
482 squeezed_total,x_len_total,y_len_total,x_nonACGT_total,y_nonACGT_total))
483
484 sys.stderr.write("theIsolatedSNPcount = %d\n" % theIsolatedSNPcount)
485 sys.stderr.write("completefillednotXY = %d\n" % completefillednotXY)
486 sys.stderr.write("completefilledXnotY = %d\n" % completefilledXnotY)
487 sys.stderr.write("completefilledYnotX = %d\n" % completefilledYnotX)
488 sys.stderr.write("completefilledXandY = %d\n" % completefilledXandY)
489
490 # end def
491
492 # Allow each module to have its own main for testing.
493 if __name__ == '__main__':
494
495 inpname = sys.argv[1]
496 outname = sys.argv[2]
497 xname = sys.argv[3]
498 yname = sys.argv[4]
499 assemblyId1 = sys.argv[5]
500 assemblyId2 = sys.argv[6]
501
502 # mismatches = checkExactMatches( x, y, inpfile)
503 # sys.stderr.write("mismatches = %d\n" % mismatches)
504
505 xIdx = IdxStore.IdxStore(xname,assemblyId1)
506 yIdx = IdxStore.IdxStore(yname,assemblyId2)
507
508 inpfile = open(inpname)
509 outfile = open(outname,"w")
510 mainLoop( inpfile, outfile, xIdx, yIdx)
511 outfile.close()
512 # end if