| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import sys |
| 4 |
import MyFile |
| 5 |
import MatchRecord |
| 6 |
|
| 7 |
def cvm(f,x,y): |
| 8 |
# A cvm variant (flag ? x : y) = (x,y)[f] |
| 9 |
if f : |
| 10 |
return x |
| 11 |
else: |
| 12 |
return y |
| 13 |
# end if |
| 14 |
# end def |
| 15 |
|
| 16 |
def coalesceMatches ( inpfile, outfile, needs_to_share_diagonal ): |
| 17 |
"Coalesce overlapping and abutting matches within the same run." |
| 18 |
|
| 19 |
firstF = None |
| 20 |
lastF = None |
| 21 |
|
| 22 |
lastLX = -3 |
| 23 |
lastLY = -4 |
| 24 |
lastForward = 0 |
| 25 |
|
| 26 |
lowHitPX = None |
| 27 |
lowHitPY = None |
| 28 |
hghHitPX = None |
| 29 |
hghHitPY = None |
| 30 |
inpfile.seek(0) |
| 31 |
outfile.seek(0) |
| 32 |
for line in inpfile: |
| 33 |
if(line[0] == 'M'): |
| 34 |
curF = MatchRecord.MatchRecord(line) |
| 35 |
px = curF.x_start |
| 36 |
nx = curF.x_length |
| 37 |
py = curF.y_start |
| 38 |
ny = curF.y_length |
| 39 |
assert(px >= 0) |
| 40 |
assert(nx >= 0) |
| 41 |
assert(py >= 0) |
| 42 |
assert(ny >= 0) |
| 43 |
if (not (not needs_to_share_diagonal or nx == ny)): |
| 44 |
print >>sys.stderr, 'Bombed on:' |
| 45 |
print >>sys.stderr, str(curF) |
| 46 |
print >>sys.stderr, 'needs_to_share_diagonal=' + str(needs_to_share_diagonal) |
| 47 |
print >>sys.stderr, 'nx=' + str(nx) + ' ny=' + str(ny) |
| 48 |
# end if |
| 49 |
assert((hghHitPX == None or (not needs_to_share_diagonal) or nx == ny)) |
| 50 |
forward = (curF.x_orientation == curF.y_orientation) |
| 51 |
lx = px |
| 52 |
ly = cvm( forward, py, py + ny) |
| 53 |
rx = px + nx |
| 54 |
ry = cvm( forward, py + ny, py) |
| 55 |
|
| 56 |
overlapping = ((lastF != None) and |
| 57 |
(curF.x_scaf_uid == lastF.x_scaf_uid) and |
| 58 |
(curF.y_scaf_uid == lastF.y_scaf_uid) and |
| 59 |
(((lx >= lowHitPX and lx <= hghHitPX) and |
| 60 |
(ly >= lowHitPY and ly <= hghHitPY)) or |
| 61 |
((rx >= lowHitPX and rx <= hghHitPX) and |
| 62 |
(ry >= lowHitPY and ry <= hghHitPY)))) |
| 63 |
on_diagonal = ((forward == lastForward) and |
| 64 |
((lx - lastLX) == ((ly - lastLY) * cvm(forward, 1, -1)))) |
| 65 |
# print >>sys.stdout, lastF, curF |
| 66 |
# print >>sys.stdout, lx,rx,ly,ry |
| 67 |
# print >>sys.stdout, lowHitPX,hghHitPX,lowHitPY,hghHitPY |
| 68 |
# print >>sys.stdout, "overlapping=",overlapping |
| 69 |
# print >>sys.stdout, "on_diagonal=",on_diagonal |
| 70 |
|
| 71 |
lowMerPX = px |
| 72 |
lowMerPY = py |
| 73 |
hghMerPX = px + nx |
| 74 |
hghMerPY = py + ny |
| 75 |
if (not (overlapping and (not needs_to_share_diagonal or on_diagonal))): |
| 76 |
if (firstF != None): |
| 77 |
# if (lastF == None or firstF.runid != lastF.runid): |
| 78 |
# end if |
| 79 |
firstF.subtype = ('g','u')[needs_to_share_diagonal] |
| 80 |
firstF.x_start = lowHitPX |
| 81 |
firstF.y_start = lowHitPY |
| 82 |
firstF.x_length = hghHitPX - lowHitPX |
| 83 |
firstF.y_length = hghHitPY - lowHitPY |
| 84 |
print >>outfile, firstF |
| 85 |
# end if |
| 86 |
firstF = curF |
| 87 |
lowHitPX = lowMerPX |
| 88 |
lowHitPY = lowMerPY |
| 89 |
hghHitPX = hghMerPX |
| 90 |
hghHitPY = hghMerPY |
| 91 |
# end if |
| 92 |
lowHitPX = cvm(lowHitPX < lowMerPX, lowHitPX, lowMerPX) |
| 93 |
lowHitPY = cvm(lowHitPY < lowMerPY, lowHitPY, lowMerPY) |
| 94 |
hghHitPX = cvm(hghHitPX > hghMerPX, hghHitPX, hghMerPX) |
| 95 |
hghHitPY = cvm(hghHitPY > hghMerPY, hghHitPY, hghMerPY) |
| 96 |
|
| 97 |
lastLX = lx |
| 98 |
lastLY = ly |
| 99 |
lastForward = forward |
| 100 |
lastF = curF |
| 101 |
# end if |
| 102 |
# end for |
| 103 |
|
| 104 |
|
| 105 |
if (firstF != None): |
| 106 |
firstF.subtype = ('g','u')[needs_to_share_diagonal] |
| 107 |
firstF.x_start = lowHitPX |
| 108 |
firstF.y_start = lowHitPY |
| 109 |
firstF.x_length = hghHitPX - lowHitPX |
| 110 |
firstF.y_length = hghHitPY - lowHitPY |
| 111 |
print >>outfile, firstF |
| 112 |
|
| 113 |
return |
| 114 |
# end def |
| 115 |
|
| 116 |
def trimMatchOverlapsInX(inpfile,outfile, trim_subtype): |
| 117 |
"Trim the match overlaps with respect to the X assembly." |
| 118 |
overlaps=0 |
| 119 |
abuts=0 |
| 120 |
posgaps=0 |
| 121 |
contained = 0 |
| 122 |
trimmed = 0 |
| 123 |
left = None |
| 124 |
|
| 125 |
picket = 0 |
| 126 |
# For each genomic axis we scan left to right using this picket |
| 127 |
# position to annihilating any part of the current match to the |
| 128 |
# left of this picket. |
| 129 |
|
| 130 |
inpfile.seek(0) |
| 131 |
for line in iter(inpfile): |
| 132 |
if(line[0] == 'M'): |
| 133 |
right = MatchRecord.MatchRecord(line) |
| 134 |
if( right.subtype != trim_subtype): |
| 135 |
print >>outfile, line, |
| 136 |
continue |
| 137 |
if( left == None or |
| 138 |
#left.x_scaf_uid < right.x_scaf_uid): |
| 139 |
left.x_scaf_uid != right.x_scaf_uid): |
| 140 |
picket = 0 |
| 141 |
else: |
| 142 |
assert(left != None) |
| 143 |
assert(right != None) |
| 144 |
if(left.x_scaf_uid > right.x_scaf_uid): |
| 145 |
print >>sys.stderr, "sequence ids out of x sorted order" |
| 146 |
print >>sys.stderr, left |
| 147 |
print >>sys.stderr, right |
| 148 |
assert(left.subtype == right.subtype) |
| 149 |
assert(left.x_scaf_uid == right.x_scaf_uid) |
| 150 |
if(not(left.x_start <= right.x_start)): |
| 151 |
print >>sys.stderr, "trimMatchOverlapsInX: Woops not sorted anymore!" |
| 152 |
print >>sys.stderr, left |
| 153 |
print >>sys.stderr, right |
| 154 |
#assert(0) |
| 155 |
|
| 156 |
thisbgn = right.x_start |
| 157 |
thisend = right.x_start + right.x_length |
| 158 |
if(picket < thisend): |
| 159 |
gaplen = thisbgn - picket |
| 160 |
if(gaplen > 0): |
| 161 |
posgaps += 1 |
| 162 |
if(gaplen == 0): |
| 163 |
abuts += 1 |
| 164 |
if(gaplen < 0): |
| 165 |
overlaps += 1 |
| 166 |
trimmed -= gaplen |
| 167 |
right.x_start -= gaplen # modify the match |
| 168 |
right.x_length += gaplen |
| 169 |
right.y_length += gaplen |
| 170 |
if(right.x_orientation == right.y_orientation): |
| 171 |
right.y_start -= gaplen # modify the match |
| 172 |
else: |
| 173 |
# picketed region contains right. |
| 174 |
#print >>sys.stderr, "trimMatchOverlapsInX: Contained" |
| 175 |
#print >>sys.stderr, left |
| 176 |
#print >>sys.stderr, right |
| 177 |
contained += 1 |
| 178 |
right = None # remove this match |
| 179 |
if(right != None): |
| 180 |
print >>outfile, right |
| 181 |
newpicket = right.x_start + right.x_length |
| 182 |
assert(picket < newpicket) |
| 183 |
picket = newpicket |
| 184 |
left = right |
| 185 |
else: |
| 186 |
print >>outfile, line, |
| 187 |
print >>sys.stderr, "trimMatchOverlapsInX:\n", |
| 188 |
print >>sys.stderr, "#posgaps, #abuts, #overlaps, #contained, bp_trimmed= %d %d %d %d %d\n" \ |
| 189 |
% (posgaps, abuts, overlaps, contained, trimmed, ) |
| 190 |
return |
| 191 |
|
| 192 |
def trimMatchOverlapsInY(inpfile,outfile, trim_subtype): |
| 193 |
"Trim the match overlaps with respect to the Y assembly." |
| 194 |
overlaps=0 |
| 195 |
abuts=0 |
| 196 |
posgaps=0 |
| 197 |
contained = 0 |
| 198 |
trimmed = 0 |
| 199 |
left = None |
| 200 |
|
| 201 |
picket = 0 |
| 202 |
# For each genomic axis we scan left to right using this picket |
| 203 |
# position to annihilating any part of the current match to the |
| 204 |
# left of this picket. |
| 205 |
|
| 206 |
inpfile.seek(0) |
| 207 |
for line in iter(inpfile): |
| 208 |
if(line[0] == 'M'): |
| 209 |
right = MatchRecord.MatchRecord(line) |
| 210 |
if( right.subtype != trim_subtype): |
| 211 |
print >>outfile, line, |
| 212 |
continue |
| 213 |
if( left == None or |
| 214 |
#left.y_scaf_uid < right.y_scaf_uid): |
| 215 |
left.y_scaf_uid != right.y_scaf_uid): |
| 216 |
picket = 0 |
| 217 |
else: |
| 218 |
assert(left != None) |
| 219 |
assert(right != None) |
| 220 |
if(left.y_scaf_uid > right.y_scaf_uid): |
| 221 |
print >>sys.stderr, "sequence ids out of y sorted order" |
| 222 |
print >>sys.stderr, left |
| 223 |
print >>sys.stderr, right |
| 224 |
assert(left.subtype == right.subtype) |
| 225 |
assert(left.y_scaf_uid == right.y_scaf_uid) |
| 226 |
if(not(left.y_start <= right.y_start)): |
| 227 |
print >>sys.stderr, "trimMatchOverlapsInY: Woops not sorted anymore!" |
| 228 |
print >>sys.stderr, left |
| 229 |
print >>sys.stderr, right |
| 230 |
#assert(0) |
| 231 |
|
| 232 |
thisbgn = right.y_start |
| 233 |
thisend = right.y_start + right.y_length |
| 234 |
if(picket < thisend): |
| 235 |
gaplen = thisbgn - picket |
| 236 |
if(gaplen > 0): |
| 237 |
posgaps += 1 |
| 238 |
if(gaplen == 0): |
| 239 |
abuts += 1 |
| 240 |
if(gaplen < 0): |
| 241 |
overlaps += 1 |
| 242 |
trimmed -= gaplen |
| 243 |
right.y_start -= gaplen # modify the match |
| 244 |
right.y_length += gaplen |
| 245 |
right.x_length += gaplen |
| 246 |
if(right.x_orientation == right.y_orientation): |
| 247 |
right.x_start -= gaplen # modify the match |
| 248 |
else: |
| 249 |
# picketed region contains right. |
| 250 |
#print >>sys.stderr, "trimMatchOverlapsInY: Contained" |
| 251 |
#print >>sys.stderr, left |
| 252 |
#print >>sys.stderr, right |
| 253 |
contained += 1 |
| 254 |
right = None # remove this match |
| 255 |
if(right != None): |
| 256 |
print >>outfile, right |
| 257 |
newpicket = right.y_start + right.y_length |
| 258 |
assert(picket < newpicket) |
| 259 |
picket = newpicket |
| 260 |
left = right |
| 261 |
else: |
| 262 |
print >>outfile, line, |
| 263 |
print >>sys.stderr, "trimMatchOverlapsInY:\n", |
| 264 |
print >>sys.stderr, "#posgaps, #abuts, #overlaps, #contained, bp_trimmed= %d %d %d %d %d\n" \ |
| 265 |
% (posgaps, abuts, overlaps, contained, trimmed, ) |
| 266 |
return |
| 267 |
|
| 268 |
|
| 269 |
def trimMatchOverlapsInBoth(inpfile,outfile,trim_subtype): |
| 270 |
gp = MyFile.myfile() |
| 271 |
MatchRecord.sortInXorderAP(inpfile,gp) |
| 272 |
# The following coalescing assumes perfect runs. |
| 273 |
hp = MyFile.myfile() |
| 274 |
coalesceMatches( gp, hp, ((trim_subtype == 'x') or (trim_subtype == 'u')) ) |
| 275 |
gp = MyFile.myfile() |
| 276 |
trimMatchOverlapsInX(hp,gp,trim_subtype) |
| 277 |
hp = MyFile.myfile() |
| 278 |
MatchRecord.sortInYorderAP(gp,hp) |
| 279 |
trimMatchOverlapsInY(hp,outfile,trim_subtype) |
| 280 |
return |
| 281 |
|
| 282 |
def main(inpname, outname, trim_subtype): |
| 283 |
inpfile = open(inpname) |
| 284 |
outfile = open(outname,"w") |
| 285 |
trimMatchOverlapsInBoth(inpfile,outfile,trim_subtype) |
| 286 |
|
| 287 |
if __name__ == '__main__': |
| 288 |
inpname = sys.argv[1] |
| 289 |
outname = sys.argv[2] |
| 290 |
trim_subtype = sys.argv[3] |
| 291 |
main(inpname, outname, trim_subtype) |