| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import sys |
| 4 |
import string |
| 5 |
import MatchRecord |
| 6 |
import IdxStore |
| 7 |
import localAlignerInterface |
| 8 |
import halign |
| 9 |
#import shelve |
| 10 |
|
| 11 |
# True=1 |
| 12 |
False=0 |
| 13 |
|
| 14 |
def analyzeGap(x,y,left,right,outfile,maxgap,erate,margin): |
| 15 |
inter_run_gap_count = 0 |
| 16 |
|
| 17 |
x_pos = 0 |
| 18 |
x_len = 0 |
| 19 |
y_pos = 0 |
| 20 |
y_len = 0 |
| 21 |
if( (left.x_scaf_uid == right.x_scaf_uid) and |
| 22 |
(left.y_scaf_uid == right.y_scaf_uid) and |
| 23 |
(left.runid == right.runid) ): |
| 24 |
# (left.sindex + 1 == right.sindex) ): # This is obsolete in Russell's file format. |
| 25 |
# sys.stderr.write("Intra-run gap\n") |
| 26 |
left_forward = (left.x_orientation == left.y_orientation) |
| 27 |
right_forward = (right.x_orientation == right.y_orientation) |
| 28 |
if( left_forward != right_forward): sys.stderr.write("Bad orientations\n") |
| 29 |
assert(left_forward == right_forward) |
| 30 |
|
| 31 |
sorted_by_x = (left.x_start <= right.x_start) and \ |
| 32 |
(left.x_start+left.x_length <= right.x_start+right.x_length) |
| 33 |
sorted_by_y = (left.y_start <= right.y_start) and \ |
| 34 |
(left.y_start+left.y_length <= right.y_start+right.y_length) |
| 35 |
if(not(sorted_by_x or sorted_by_y)): |
| 36 |
print >>sys.stderr, "bad sorting in runs" |
| 37 |
print >>sys.stderr, left |
| 38 |
print >>sys.stderr, right |
| 39 |
assert(sorted_by_x or sorted_by_y) |
| 40 |
# This concept of sorted allows neggaps but not containmant in both axes. |
| 41 |
|
| 42 |
if(not((not left_forward) or (sorted_by_x and sorted_by_y))): |
| 43 |
print >>sys.stderr, "bad sorting in runs" |
| 44 |
print >>sys.stderr, left |
| 45 |
print >>sys.stderr, right |
| 46 |
if(not((left_forward) or (not(sorted_by_x and sorted_by_y)))): |
| 47 |
print >>sys.stderr, "bad sorting in runs" |
| 48 |
print >>sys.stderr, left |
| 49 |
print >>sys.stderr, right |
| 50 |
assert((not left_forward) or (sorted_by_x and sorted_by_y)) |
| 51 |
assert((left_forward) or (not(sorted_by_x and sorted_by_y))) |
| 52 |
|
| 53 |
if(sorted_by_x): # Sorted by x positions. |
| 54 |
x_pos = left.x_start + left.x_length # Start of the intra-run gap. |
| 55 |
x_len = right.x_start - x_pos # Length of the intra-run gap. |
| 56 |
if(left_forward): |
| 57 |
y_pos = left.y_start + left.y_length |
| 58 |
y_len = right.y_start - y_pos |
| 59 |
else: |
| 60 |
y_pos = right.y_start + right.y_length |
| 61 |
y_len = left.y_start - y_pos |
| 62 |
# end if |
| 63 |
else: # Assume sorted by y positions |
| 64 |
y_pos = left.y_start + left.y_length |
| 65 |
y_len = right.y_start - y_pos |
| 66 |
if(left_forward): |
| 67 |
x_pos = left.x_start + left.x_length |
| 68 |
x_len = right.x_start - x_pos |
| 69 |
else: |
| 70 |
x_pos = right.x_start + right.x_length |
| 71 |
x_len = left.x_start - x_pos |
| 72 |
# end if |
| 73 |
# end if |
| 74 |
# 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) |
| 75 |
|
| 76 |
assert(left.x_start >= 0) |
| 77 |
assert(left.x_length > 0) |
| 78 |
assert(left.y_start >= 0) |
| 79 |
assert(left.y_length > 0) |
| 80 |
assert(right.x_start >= 0) |
| 81 |
assert(right.x_length > 0) |
| 82 |
assert(right.y_start >= 0) |
| 83 |
assert(right.y_length > 0) |
| 84 |
|
| 85 |
|
| 86 |
if( 1 |
| 87 |
and 0 < x_len and 0 < y_len |
| 88 |
and x_len < maxgap and y_len < maxgap |
| 89 |
): |
| 90 |
|
| 91 |
if 0: |
| 92 |
sys.stderr.write("About to call local aligner with %d margins\n" % margin); |
| 93 |
sys.stderr.write("# left = %s\n" % str(left)) |
| 94 |
sys.stderr.write("# right= %s\n" % str(right)) |
| 95 |
sys.stderr.write("x_len=%d y_len=%d\n" % (x_len, y_len) ); |
| 96 |
|
| 97 |
# Why two orientation flags? We want the output matches |
| 98 |
# to be in the same sorted order as the left and right |
| 99 |
# matches. |
| 100 |
|
| 101 |
parent_x_start = x_pos - margin |
| 102 |
parent_y_start = y_pos - margin |
| 103 |
parent_x_length = x_len + 2*margin |
| 104 |
parent_y_length = y_len + 2*margin |
| 105 |
|
| 106 |
if 0: |
| 107 |
print >>sys.stderr, "parent_x_start=%d" % parent_x_start |
| 108 |
print >>sys.stderr, "parent_y_start=%d" % parent_y_start |
| 109 |
print >>sys.stderr, "parent_x_length=%d" % parent_x_length |
| 110 |
print >>sys.stderr, "parent_y_length=%d" % parent_y_length |
| 111 |
|
| 112 |
x_seq = "" |
| 113 |
if(x_len > 0): |
| 114 |
x_seq = string.upper( |
| 115 |
x.getStringFromFasta( sorted_by_x, left.x_scaf_uid, |
| 116 |
parent_x_start, parent_x_length)); |
| 117 |
# end if |
| 118 |
y_seq = "" |
| 119 |
if(y_len > 0): |
| 120 |
y_seq = string.upper( |
| 121 |
y.getStringFromFasta( sorted_by_y, left.y_scaf_uid, |
| 122 |
parent_y_start, parent_y_length)); |
| 123 |
# end if |
| 124 |
|
| 125 |
if 0: |
| 126 |
print >>outfile, "# STARTED localAlignerInterface.syntenicSegments" |
| 127 |
print >>outfile, "# left = %s" % str(left) |
| 128 |
print >>outfile, "# right= %s" % str(right) |
| 129 |
print >>sys.stderr, "x_seq="+x_seq |
| 130 |
print >>sys.stderr, "len(x_seq)=",len(x_seq) |
| 131 |
print >>sys.stderr, "y_seq="+y_seq |
| 132 |
print >>sys.stderr, "len(y_seq)=",len(y_seq) |
| 133 |
outfile.flush() |
| 134 |
|
| 135 |
try: |
| 136 |
localAlignerInterface.syntenicSegments(outfile, |
| 137 |
x_seq, 0, parent_x_length, |
| 138 |
y_seq, 0, parent_y_length, |
| 139 |
erate) |
| 140 |
|
| 141 |
FM = left |
| 142 |
parent_id = FM.matchid |
| 143 |
#FM.x_orientation = sorted_by_x |
| 144 |
#FM.y_orientation = sorted_by_y |
| 145 |
|
| 146 |
# Why two orientations and not just a flipped flag? |
| 147 |
# Because we want the resulting matches to come out in |
| 148 |
# the same sorted order as the input matches. |
| 149 |
|
| 150 |
ii = 0 |
| 151 |
for segment in iter(localAlignerInterface.iterateSegments,None): |
| 152 |
#print >>outfile, segment |
| 153 |
(bgn1,bgn2,len1,len2,fid) = segment |
| 154 |
assert(len1 >= 0) |
| 155 |
assert(len2 >= 0) |
| 156 |
assert(bgn1 >= 0) |
| 157 |
assert(bgn2 >= 0) |
| 158 |
if(not(bgn1 + len1 <= parent_x_length)): |
| 159 |
print >>sys.stdout,"# warn(not(bgn1 + len1 <= parent_x_length))" |
| 160 |
print >>sys.stdout,"# bgn1=%d len1=%d parent_x_length=%d" % (bgn1,len1,parent_x_length) |
| 161 |
print >>sys.stdout,"# left = %s" % str(left) |
| 162 |
print >>sys.stdout,"# right= %s" % str(right) |
| 163 |
print >>sys.stdout,"# bgn1,bgn2,len1,len2=", bgn1,bgn2,len1,len2 |
| 164 |
#print >>sys.stdout,"# xseq=%s" % x_seq[bgn1:bgn1+len1] |
| 165 |
#print >>sys.stdout,"# yseq=%s" % y_seq[bgn2:bgn2+len2] |
| 166 |
len1 = parent_x_length - bgn1 |
| 167 |
print >>sys.stdout, "# Change len1 = %d" % len1 |
| 168 |
if(not(bgn2 + len2 <= parent_y_length)): |
| 169 |
print >>sys.stdout,"# warn(not(bgn2 + len2 <= parent_y_length))" |
| 170 |
print >>sys.stdout,"# bgn2=%d len2=%d parent_y_length=%d" % (bgn2,len2,parent_y_length) |
| 171 |
print >>sys.stdout,"# left = %s" % str(left) |
| 172 |
print >>sys.stdout,"# right= %s" % str(right) |
| 173 |
print >>sys.stdout,"# bgn1,bgn2,len1,len2=", bgn1,bgn2,len1,len2 |
| 174 |
#print >>sys.stdout,"# xseq=%s" % x_seq[bgn1:bgn1+len1] |
| 175 |
#print >>sys.stdout,"# yseq=%s" % y_seq[bgn2:bgn2+len2] |
| 176 |
len2 = parent_y_length - bgn2 |
| 177 |
print >>sys.stdout,"# Change len2 = %d" % len2 |
| 178 |
if (len1 == 0): |
| 179 |
print >>sys.stdout,"# warn(len1 == 0)" |
| 180 |
print >>sys.stdout,"# bgn1,bgn2,len1,len2=", bgn1,bgn2,len1,len2 |
| 181 |
print >>sys.stdout,"# bgn1=%d len1=%d parent_x_length=%d" % (bgn1,len1,parent_x_length) |
| 182 |
continue |
| 183 |
if (len2 == 0): |
| 184 |
print >>sys.stdout,"# warn(len2 == 0)" |
| 185 |
print >>sys.stdout,"# bgn1,bgn2,len1,len2=", bgn1,bgn2,len1,len2 |
| 186 |
print >>sys.stdout,"# bgn2=%d len2=%d parent_y_length=%d" % (bgn2,len2,parent_y_length) |
| 187 |
continue |
| 188 |
assert(bgn1 >= 0) |
| 189 |
assert(bgn2 >= 0) |
| 190 |
assert(len1 > 0) |
| 191 |
assert(len2 > 0) |
| 192 |
assert(bgn1 + len1 <= parent_x_length); |
| 193 |
assert(bgn2 + len2 <= parent_y_length); |
| 194 |
# Filter by a minimum length? say four bp. |
| 195 |
ii += 1 |
| 196 |
FM.subtype = 'l' |
| 197 |
FM.matchid = parent_id + 'l' + str(ii) |
| 198 |
# FM.runid = parent_id |
| 199 |
child_x_start = parent_x_start + (parent_x_length-bgn1-len1,bgn1)[sorted_by_x] |
| 200 |
child_y_start = parent_y_start + (parent_y_length-bgn2-len2,bgn2)[sorted_by_y] |
| 201 |
child_x_length = len1 |
| 202 |
child_y_length = len2 |
| 203 |
|
| 204 |
#FM.identifier = " %f" % fid # CMM BEWARE |
| 205 |
FM.x_start = child_x_start |
| 206 |
FM.y_start = child_y_start |
| 207 |
FM.x_length = child_x_length |
| 208 |
FM.y_length = child_y_length |
| 209 |
#FM.extend['fid'] = str(fid) |
| 210 |
#print >>outfile, FM |
| 211 |
|
| 212 |
# Here we call the dedasher. |
| 213 |
#assert(len1 > 0) |
| 214 |
#assert(len2 > 0) |
| 215 |
#assert(bgn1 >= 0) |
| 216 |
#assert(bgn2 >= 0) |
| 217 |
#assert(bgn1+len1 <= parent_x_length) |
| 218 |
#assert(bgn2+len2 <= parent_y_length) |
| 219 |
|
| 220 |
if 0: |
| 221 |
print >>sys.stderr, "# x_seq=%s" % x_seq |
| 222 |
print >>sys.stderr, "# y_seq=%s" % y_seq |
| 223 |
print >>sys.stderr, "# bgn1,bgn2,len1,len2=", bgn1,bgn2,len1,len2 |
| 224 |
print >>sys.stderr, "# xseq=%s" % x_seq[bgn1:bgn1+len1] |
| 225 |
print >>sys.stderr, "# yseq=%s" % y_seq[bgn2:bgn2+len2] |
| 226 |
|
| 227 |
halign.halignStart(x_seq[bgn1:bgn1+len1], y_seq[bgn2:bgn2+len2]) |
| 228 |
outfile.flush() |
| 229 |
for hsegment in iter(halign.halignDedash,None): |
| 230 |
#print >>outfile, segment |
| 231 |
(bgn1h,bgn2h,len1h,len2h,nmat) = hsegment |
| 232 |
# Filter by a minimum length? say four bp. |
| 233 |
ii += 1 |
| 234 |
FM.subtype = 'u' |
| 235 |
FM.matchid = parent_id + 'a' + str(ii) |
| 236 |
# FM.runid = parent_id |
| 237 |
FM.x_start = child_x_start + (child_x_length-bgn1h-len1h,bgn1h)[sorted_by_x] |
| 238 |
FM.y_start = child_y_start + (child_y_length-bgn2h-len2h,bgn2h)[sorted_by_y] |
| 239 |
FM.x_length = len1h |
| 240 |
FM.y_length = len2h |
| 241 |
|
| 242 |
assert(len1h == len2h) |
| 243 |
mismatches = 0 |
| 244 |
for ic in range(len1h): |
| 245 |
if(x_seq[bgn1+bgn1h+ic] != y_seq[bgn2+bgn2h+ic]): |
| 246 |
mismatches += 1 |
| 247 |
FM.extend['mm'] = str(mismatches) |
| 248 |
#FM.identifier = "" # BEWARE |
| 249 |
print >>outfile, FM |
| 250 |
|
| 251 |
# localAlignerInterface.free() |
| 252 |
# print >>outfile,"# FINISHED localAlignerInterface.syntenicSegments" |
| 253 |
except RuntimeError: |
| 254 |
print >>outfile, "# NOTE syntenicSegments failed between these records" |
| 255 |
print >>outfile, "# STARTED localAlignerInterface.syntenicSegments" |
| 256 |
print >>outfile, "# left = %s" % str(left) |
| 257 |
print >>outfile, "# right= %s" % str(right) |
| 258 |
print >>sys.stderr, "NOTE syntenicSegments failed in fillIntraRunGaps for:" |
| 259 |
print >>sys.stderr, "x_seq="+x_seq |
| 260 |
print >>sys.stderr, "len(x_seq)=",len(x_seq) |
| 261 |
print >>sys.stderr, "y_seq="+y_seq |
| 262 |
print >>sys.stderr, "len(y_seq)=",len(y_seq) |
| 263 |
# end if |
| 264 |
|
| 265 |
else: |
| 266 |
# sys.stderr.write("Inter-run gap\n") |
| 267 |
inter_run_gap_count += 1 |
| 268 |
# sys.stderr.write("done\n") |
| 269 |
return (inter_run_gap_count,) |
| 270 |
# end def |
| 271 |
|
| 272 |
|
| 273 |
def mainLoop( inpfile, outfile, xIdx, yIdx, maxgap, erate): |
| 274 |
|
| 275 |
margin = 20 # This should be set by an ATAC global. |
| 276 |
countLines = 0 |
| 277 |
inter_run_gap_count_total = 0 |
| 278 |
closed_gap_count_total = 0 |
| 279 |
squeezed_total = 0 |
| 280 |
x_len_total = 0 |
| 281 |
y_len_total = 0 |
| 282 |
x_nonACGT_total = 0 |
| 283 |
y_nonACGT_total = 0 |
| 284 |
|
| 285 |
inpfile.seek(0) |
| 286 |
inpfileIter = iter(inpfile) |
| 287 |
|
| 288 |
sys.stderr.write("begin\n") |
| 289 |
|
| 290 |
left = None |
| 291 |
for line in inpfileIter: |
| 292 |
if(line[0] == 'M'): |
| 293 |
left = MatchRecord.MatchRecord(line) |
| 294 |
print >>outfile, left |
| 295 |
countLines += 1 |
| 296 |
break; |
| 297 |
|
| 298 |
sys.stderr.write("countLines=%d\n" % countLines) |
| 299 |
|
| 300 |
for line in inpfileIter: |
| 301 |
if(line[0] == 'M'): |
| 302 |
newRight = MatchRecord.MatchRecord(line) |
| 303 |
if( newRight.subtype == 'u' ): |
| 304 |
right = newRight |
| 305 |
|
| 306 |
#if( countLines % 10000 == 0): |
| 307 |
# sys.stderr.write("countLines=%d\n" % countLines) |
| 308 |
|
| 309 |
(inter_run_gap_count,) = analyzeGap( xIdx, yIdx, left,right, outfile, maxgap, erate, margin) |
| 310 |
|
| 311 |
inter_run_gap_count_total += inter_run_gap_count |
| 312 |
|
| 313 |
# Output the record which was possibly trimmed. |
| 314 |
print >>outfile, right |
| 315 |
countLines += 1 |
| 316 |
left = right |
| 317 |
# end if |
| 318 |
# end for |
| 319 |
|
| 320 |
sys.stderr.write("countLines %d inter_run_gap_count %d \n" % (countLines,inter_run_gap_count_total)) |
| 321 |
# end def |
| 322 |
|
| 323 |
import AtacFile |
| 324 |
import MyFile |
| 325 |
|
| 326 |
def main( inpname, outname): |
| 327 |
obj = AtacFile.AtacFile(inpname) |
| 328 |
assemblyId1 = obj.globals['assemblyId1'] |
| 329 |
assemblyId2 = obj.globals['assemblyId2'] |
| 330 |
assemblyFilePrefix1 = obj.globals['assemblyFilePrefix1'] |
| 331 |
assemblyFilePrefix2 = obj.globals['assemblyFilePrefix2'] |
| 332 |
|
| 333 |
if(not obj.globals.has_key('fillIntraRunGapsErate')): |
| 334 |
obj.globals['fillIntraRunGapsErate'] = 0.10 |
| 335 |
if(not obj.globals.has_key('fillIntraRunGapsMaxGap')): |
| 336 |
obj.globals['fillIntraRunGapsMaxGap'] = 100000 |
| 337 |
fillIntraRunGapsErate = float(obj.globals['fillIntraRunGapsErate']) |
| 338 |
fillIntraRunGapsMaxGap = int(obj.globals['fillIntraRunGapsMaxGap']) |
| 339 |
|
| 340 |
# mismatches = checkExactMatches( x, y, inpfile) |
| 341 |
# sys.stderr.write("mismatches = %d\n" % mismatches) |
| 342 |
|
| 343 |
xIdx = IdxStore.IdxStore(assemblyFilePrefix1,assemblyId1) |
| 344 |
yIdx = IdxStore.IdxStore(assemblyFilePrefix2,assemblyId2) |
| 345 |
|
| 346 |
tempfile = MyFile.myfile() |
| 347 |
mainLoop( obj.matches, tempfile, xIdx, yIdx, |
| 348 |
fillIntraRunGapsMaxGap, fillIntraRunGapsErate) |
| 349 |
obj.matches = tempfile |
| 350 |
obj.checkpoint(outname) |
| 351 |
|
| 352 |
|
| 353 |
# Allow each module to have its own main for testing. |
| 354 |
if __name__ == '__main__': |
| 355 |
inpname = sys.argv[1] |
| 356 |
outname = sys.argv[2] |
| 357 |
main( inpname, outname) |
| 358 |
# end if |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|