| •1 |
|
| 2 |
|
| 3 |
import sys, os, copy, string, tempfile |
| 4 |
|
| 5 |
class AtacRow: |
| 6 |
"A general ATAC row object" |
| 7 |
def __init__(self,line): |
| 8 |
self.kind = line[0] |
| 9 |
splitline = line[1:].split(">") |
| 10 |
self.fixed = splitline[0].split() |
| 11 |
if(len(splitline)>1): |
| 12 |
self.defline = splitline[1] |
| 13 |
else: |
| 14 |
self.defline = "" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class MatchRecord: |
| 20 |
"""Class representing an exact match.""" |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def sameAs(self,other): |
| 32 |
return ( |
| 33 |
(self.x_orientation == other.x_orientation) and |
| 34 |
(self.x_scaf_uid == other.x_scaf_uid) and |
| 35 |
(self.x_start == other.x_start) and |
| 36 |
(self.y_orientation == other.y_orientation) and |
| 37 |
(self.y_scaf_uid == other.y_scaf_uid) and |
| 38 |
(self.y_start == other.y_start) and |
| 39 |
(self.x_length == other.x_length) and |
| 40 |
(self.y_length == other.y_length) |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def isInsideBox(self, one, two): |
| 45 |
|
| 46 |
dxone = self.x_start - one.x_start |
| 47 |
dxtwo = self.x_start - two.x_start |
| 48 |
dyone = self.y_start - one.y_start |
| 49 |
dytwo = self.y_start - two.y_start |
| 50 |
flag = ( |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
(self.x_scaf_uid == one.x_scaf_uid) and |
| 56 |
(self.x_scaf_uid == two.x_scaf_uid) and |
| 57 |
(self.y_scaf_uid == one.y_scaf_uid) and |
| 58 |
(self.y_scaf_uid == two.y_scaf_uid) and |
| 59 |
(((dxone > 0) and ( dxtwo < 0)) or ((dxone < 0) and ( dxtwo > 0))) and |
| 60 |
(((dyone > 0) and ( dytwo < 0)) or ((dyone < 0) and ( dytwo > 0))) |
| 61 |
) |
| 62 |
return flag |
| 63 |
|
| 64 |
|
| 65 |
def inSameRunAs(self,x): |
| 66 |
return self.runid == x.runid |
| 67 |
|
| 68 |
|
| 69 |
def copy(self): |
| 70 |
other = copy.copy(self) |
| 71 |
return other |
| 72 |
|
| 73 |
def convertFromAtacMatchFormat(self,line): |
| 74 |
fields = line.split() |
| 75 |
if(line[0] == 'M'): |
| 76 |
self.rowtype = fields[0] |
| 77 |
self.subtype = fields[1] |
| 78 |
self.matchid = fields[2] |
| 79 |
self.runid = fields[3] |
| 80 |
self.x_scaf_uid = fields[4] |
| 81 |
self.x_start = int(fields[5]) |
| 82 |
self.x_length = int(fields[6]) |
| 83 |
self.x_orientation = int(fields[7]) |
| 84 |
self.y_scaf_uid = fields[8] |
| 85 |
self.y_start = int(fields[9]) |
| 86 |
self.y_length = int(fields[10]) |
| 87 |
self.y_orientation = int(fields[11]) |
| 88 |
|
| 89 |
elif(line[0] == '-'): |
| 90 |
orientation = (fields[0][1:]=='f') |
| 91 |
self.rowtype = 'M' |
| 92 |
self.subtype = 'x' |
| 93 |
self.matchid = '.' |
| 94 |
self.runid = '.' |
| 95 |
self.x_scaf_uid = assemblyId1 + ":" + fields[2] |
| 96 |
self.x_start = int(fields[3]) |
| 97 |
self.x_length = int(fields[4]) |
| 98 |
self.x_orientation = 1 |
| 99 |
self.y_scaf_uid = assemblyId2 + ":" + fields[6] |
| 100 |
self.y_start = int(fields[7]) |
| 101 |
self.y_length = int(fields[8]) |
| 102 |
self.y_orientation = (-1,1)[orientation] |
| 103 |
|
| 104 |
|
| 105 |
def __init__ (self, line, *args): |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if '>' in line: |
| 110 |
(line1, line2) = line.split('>') |
| 111 |
else: |
| 112 |
line1 = line |
| 113 |
line2 = "" |
| 114 |
try: |
| 115 |
self.convertFromAtacMatchFormat(line1) |
| 116 |
except IndexError: |
| 117 |
sys.stderr.write("MatchRecord-- IndexError: line did not split correctly: %s\n" |
| 118 |
% line1) |
| 119 |
raise |
| 120 |
except ValueError: |
| 121 |
sys.stderr.write("MatchRecord-- ValueError: line did not unpack correctly: %s\n" |
| 122 |
% line1) |
| 123 |
raise |
| 124 |
self.extend = {} |
| 125 |
extensions = line2.split('/') |
| 126 |
self.identifier = extensions[0].strip() |
| 127 |
for argpair in extensions[1:]: |
| 128 |
if '=' in argpair: |
| 129 |
(key,value) = argpair.split('=') |
| 130 |
self.extend[key] = value.strip() |
| 131 |
return |
| 132 |
|
| 133 |
def __str__ (self): |
| 134 |
extension = " >" + self.identifier |
| 135 |
for key in self.extend: |
| 136 |
extension += ' /' + key + '=' + str(self.extend[key]) |
| 137 |
if(len(extension)<3): |
| 138 |
extension = "" |
| 139 |
return "%s %s %s %s %s %d %d %d %s %d %d %d %s" % ( |
| 140 |
self.rowtype, self.subtype, self.matchid, self.runid, |
| 141 |
self.x_scaf_uid, self.x_start, self.x_length, self.x_orientation, |
| 142 |
self.y_scaf_uid, self.y_start, self.y_length, self.y_orientation, |
| 143 |
extension |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
def convertBrianRecordFormat( inpfile, outfile, assemblyId1, assemblyId2): |
| 151 |
"Convert the match record format from Brian's to atac format." |
| 152 |
lineCount = 0 |
| 153 |
for line in inpfile: |
| 154 |
lineCount += 1 |
| 155 |
if(lineCount % 100000 == 0): |
| •156 |
print("lineCount=%d" % lineCount, file=sys.stderr) |
| 157 |
FB = line.split() |
| 158 |
orientation = (FB[0][1:]=='f') |
| 159 |
FM = MatchRecord("M x . . . 0 0 0 . 0 0 0 0\n") |
| 160 |
FM.x_orientation = 1 |
| 161 |
FM.matchid = "BMX"+str(lineCount) |
| 162 |
FM.x_scaf_uid = assemblyId1 + ":" + FB[2] |
| 163 |
FM.x_start = int(FB[3]) |
| 164 |
FM.x_length = int(FB[4]) |
| 165 |
FM.y_orientation = (-1,1)[orientation] |
| 166 |
FM.y_scaf_uid = assemblyId2 + ":" + FB[6] |
| 167 |
FM.y_start = int(FB[7]) |
| 168 |
FM.y_length = int(FB[8]) |
| 169 |
FM.identifier = "" |
| 170 |
FM.extend = {} |
| 171 |
|
| •172 |
print(FM, file=outfile) |
| 173 |
|
| •174 |
print("convertRecordFormat done: lineCount=%d" % lineCount, file=sys.stderr) |
| 175 |
outfile.seek(0) |
| 176 |
return |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
def sortInXorderAP( inpfile, outfile): |
| 181 |
|
| 182 |
InXOrderAP = '-k 1,1 -k 2,2 -k 5,5 -k 6n -k 7nr -k 8nr -k 9,9 -k 10n -k 11nr -k 12nr' |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
inpfile.seek(0) |
| 187 |
outfile.seek(0) |
| 188 |
inpfile.flush() |
| 189 |
outfile.flush() |
| 190 |
|
| 191 |
ierr = os.system("sync;sync;sync") |
| 192 |
assert(ierr == 0) |
| 193 |
ierr = os.system("sort -T . %s %s > %s" % (InXOrderAP, inpfile.name, outfile.name)); |
| 194 |
assert(ierr == 0) |
| 195 |
ierr = os.system("sync;sync;sync") |
| 196 |
assert(ierr == 0) |
| 197 |
inpfile.seek(0) |
| 198 |
outfile.seek(0) |
| 199 |
|
| 200 |
return |
| 201 |
|
| 202 |
|
| 203 |
def sortInYorderAP( inpfile, outfile): |
| 204 |
|
| 205 |
InYOrderAP = '-k 1,1 -k 2,2 -k 9,9 -k 10n -k 11nr -k 12nr -k 5,5 -k 6n -k 7nr -k 8nr' |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
inpfile.seek(0) |
| 210 |
outfile.seek(0) |
| 211 |
inpfile.flush() |
| 212 |
outfile.flush() |
| 213 |
|
| 214 |
ierr = os.system("sync;sync;sync") |
| 215 |
assert(ierr == 0) |
| 216 |
ierr = os.system("sort -T . %s %s > %s" % (InYOrderAP, inpfile.name, outfile.name)); |
| 217 |
assert(ierr == 0) |
| 218 |
ierr = os.system("sync;sync;sync") |
| 219 |
assert(ierr == 0) |
| 220 |
inpfile.seek(0) |
| 221 |
outfile.seek(0) |
| 222 |
|
| 223 |
return |
| 224 |
|
| 225 |
|
| 226 |
def sortInXorderPP( inpname, outfile): |
| 227 |
|
| 228 |
assert(1) |
| 229 |
|
| 230 |
|
| 231 |
def sortInYorderPP( inpname, outfile): |
| 232 |
|
| 233 |
assert(1) |
| 234 |
|