Debian Codegraph @

atac-driver/chainer/python/MatchRecord.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
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 # end if
16 # end if
17 # end class
18
19 class MatchRecord:
20 """Class representing an exact match."""
21
22 #def __init__ (self, *args):
23 #print args
24 # if(args):
25 # (line,) = args
26 # # print " parse line= " + line
27 # self.fromString(line)
28 # # end if
29 # end def
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 # end def
43
44 def isInsideBox(self, one, two):
45 # We need to modify this because the matches are not points.
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 # (self.x_orientation == one.x_orientation) and
52 # (self.x_orientation == two.x_orientation) and
53 # (self.y_orientation == one.y_orientation) and
54 # (self.y_orientation == two.y_orientation) and
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 # end def
64
65 def inSameRunAs(self,x):
66 return self.runid == x.runid # same parent
67 # end def
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 #self.mismatches = int(fields[12])
89 elif(line[0] == '-'):
90 orientation = (fields[0][1:]=='f')
91 self.rowtype = 'M'
92 self.subtype = 'x'
93 self.matchid = '.' # "BMX"+str(lineCount)
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] # A cvm variant (flag ? x : y).
103 # end def
104
105 def __init__ (self, line, *args):
106 #print args
107 #if(args):
108 # (line,) = args
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 # end def
146
147 # end class
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] # A cvm variant (flag ? x : y).
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 #FM.mismatches = 0
•172 print(FM, file=outfile)
173 # end for
•174 print("convertRecordFormat done: lineCount=%d" % lineCount, file=sys.stderr)
175 outfile.seek(0)
176 return
177 # end def
178
179
180 def sortInXorderAP( inpfile, outfile):
181 # (x_scaf_uid, x_start, x_length, y_scaf_uid, y_start, y_length)
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 # Use -u to remove the palindromes.
184 # Use -k 7nr -k 11nr to remove abutting contained matches.
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 # end def
202
203 def sortInYorderAP( inpfile, outfile):
204 # (x_scaf_uid, x_start, x_length, y_scaf_uid, y_start, y_length)
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 # Use -u to remove the palindromes.
207 # Use -k 7nr -k 11nr to remove abutting contained matches.
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 # end def
225
226 def sortInXorderPP( inpname, outfile):
227 # (x_win, ywin, x_scaf_uid, y_scaf_uid, x_start, y_start, x_length, y_length)
228 assert(1)
229 # end def
230
231 def sortInYorderPP( inpname, outfile):
232 # (y_win, x_win, y_scaf_uid, x_scaf_uid, y_start, x_start, y_length, x_length)
233 assert(1)
234 # end def