Debian Codegraph @

atac-driver/chainer/python/PerfectRuns.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 # Looking in /usr/local/ir/bin on the Compaqs for the correct Python interpreter.
3 # export PYTHONPATH=${PYTHONPATH}:$WORK/cds/IR/COMPASS/src/AtacPipeline
4
5 """
6 Extensive documentation for the Python language is available at
7 http://www.python.org.
8 """
9
10 import sys
11 import MyFile
12 import MatchRecord
13
14 def cvm(f,x,y):
15 # A cvm variant (flag ? x : y) = (y,x)[f]
16 if f :
17 return x
18 else:
19 return y
20 # end if
21 # end def
22
23 def createSignedEnumeration(inpfile):
24 outfile = MyFile.myfile()
25 p = 1
26 inpfile.seek(0)
27 for line in inpfile:
28 if(line[0] == 'M'):
29 FM = MatchRecord.MatchRecord(line)
30 forwardX = FM.x_orientation
31 forwardY = FM.y_orientation
32 srank = cvm(forwardX == forwardY, p, -p)
33 p += 1
34 FM.extend['srank'] = srank
35 print >>outfile, FM
36 # end if
37 # end while
38 return outfile
39 # end def
40
41 def findPerfectRuns ( inpfile, maxJump, runIdPrefix ):
42 outfile = MyFile.myfile()
43 left = None
44 runid = 1
45 inpfile.seek(0)
46 for line in inpfile:
47 if(line[0] == 'M'):
48 right = MatchRecord.MatchRecord(line)
49 pr = int(right.extend['srank'])
50 del(right.extend['srank'])
51 if(left != None):
52 maxGapInXandY = 0
53 if(left.x_scaf_uid == right.x_scaf_uid
54 and
55 left.y_scaf_uid == right.y_scaf_uid ):
56 # Find the maximum of the gap in x and y axis.
57
58 x_rs = right.x_start
59 x_re = x_rs + right.x_length
60 x_ls = left.x_start
61 x_le = x_ls + left.x_length
62 assert(x_rs < x_re)
63 assert(x_ls < x_le)
64 # All matches are positive length.
65 x_gapLeftBeforeRight = x_rs - x_le
66 x_gapRightBeforeLeft = x_ls - x_re
67 assert(not(x_gapLeftBeforeRight>0 and x_gapRightBeforeLeft>0))
68 x_gap = max(x_gapLeftBeforeRight,x_gapRightBeforeLeft)
69 # x_gap == 0 is abutting
70 # x_gap < 0 is overlapping
71
72 y_rs = right.y_start
73 y_re = y_rs + right.y_length
74 y_ls = left.y_start
75 y_le = y_ls + left.y_length
76 assert(y_rs < y_re)
77 assert(y_ls < y_le)
78 y_gapLeftBeforeRight = y_rs - y_le
79 y_gapRightBeforeLeft = y_ls - y_re
80 assert(not(y_gapLeftBeforeRight>0 and y_gapRightBeforeLeft>0))
81 y_gap = max(y_gapLeftBeforeRight,y_gapRightBeforeLeft)
82 # y_gap == 0 is abutting
83 # y_gap < 0 is overlapping
84
85 maxGapInXandY = max(x_gap,y_gap)
86
87 if 1:
88 # Check the sorting of the matches.
89
90 sorted_by_x = (x_ls <= x_rs)
91 sorted_by_y = (y_ls <= y_rs)
92
93 if(not(sorted_by_x or sorted_by_y)):
94 print >>sys.stderr, "bad sorting in findPerfectRuns"
95 print >>sys.stderr, left
96 print >>sys.stderr, right
97 assert(sorted_by_x or sorted_by_y)
98 dovetail_in_x = (x_ls <= x_rs) and (x_le <= x_re)
99 dovetail_in_y = (y_ls <= y_rs) and (y_ls <= y_re)
100 if(sorted_by_x and not(dovetail_in_x)):
101 print >>sys.stderr, "contained in x in findPerfectRuns"
102 print >>sys.stderr, left
103 print >>sys.stderr, right
104 if(sorted_by_y and not(dovetail_in_y)):
105 print >>sys.stderr, "contained in y in findPerfectRuns"
106 print >>sys.stderr, left
107 print >>sys.stderr, right
108 # endif
109 if(
110 (left.x_scaf_uid != right.x_scaf_uid) or # check first axis id
111 (left.y_scaf_uid != right.y_scaf_uid) or # check second axis id
112 (maxGapInXandY > maxJump) or
113 (pr != lastpr + 1) # Using the signed rank NOT the run id !!!!
114 ):
115 runid += 1
116 # end if
117 # end if
118 lastpr = pr
119 right.runid = "%s%d" % (runIdPrefix,runid,) # Assign the run id in the same slot as the signed rank.
120 print >>outfile, right
121 left = right
122 # end if
123 # end for
124 return outfile
125 # end def
126
127 def formPerfectRuns ( inpfile, firstSort, secondSort, maxJump, runIdPrefix ):
128 inpfile.seek(0)
129 step = 0
130 print >>sys.stderr, 'formPerfectRuns step=' + str(step)
131 step += 1
132
133 tmpfile = MyFile.myfile()
134 firstSort( inpfile, tmpfile)
135
136 print >>sys.stderr, 'formPerfectRuns step=' + str(step)
137 step += 1
138 outfile = createSignedEnumeration(tmpfile)
139
140 print >>sys.stderr, 'formPerfectRuns step=' + str(step)
141 step += 1
142 tmpfile = MyFile.myfile()
143 secondSort( outfile, tmpfile)
144
145 print >>sys.stderr, 'formPerfectRuns step=' + str(step)
146 step += 1
147 outfile = findPerfectRuns( tmpfile, maxJump, runIdPrefix)
148
149 return outfile
150 # end def
151
152 def runsAsMatches(inpfile):
153
154 outfile = MyFile.myfile()
155 lastF = None
156 firstF = None
157 runFill = 0
158 inpfile.seek(0)
159 for line in inpfile:
160 if(line[0] == 'M'):
161 curF = MatchRecord.MatchRecord(line)
162 if ((lastF == None) or (curF.runid != lastF.runid)):
163 if ((lastF != None) and (firstF.x_scaf_uid != lastF.x_scaf_uid)):
164 print >>sys.stderr, firstF
165 print >>sys.stderr, lastF
166 # end if
167 assert((lastF==None) or (firstF.x_scaf_uid == lastF.x_scaf_uid))
168 assert((lastF==None) or (firstF.y_scaf_uid == lastF.y_scaf_uid))
169 if (None != lastF):
170 x1 = firstF.x_start
171 x2 = lastF.x_start
172 startX = cvm(x1 < x2, x1, x2)
173 x1 += firstF.x_length
174 x2 += lastF.x_length
175 endX = cvm(x1 > x2, x1, x2)
176 y1 = firstF.y_start
177 y2 = lastF.y_start
178 startY = cvm( y1 < y2, y1, y2)
179 y1 += firstF.y_length
180 y2 += lastF.y_length
181 endY = cvm(y1 > y2, y1, y2)
182 lastF.subtype = 'r'
183 lastF.matchid = lastF.runid
184 lastF.runid = "." # the agreed NULL value
185 lastF.x_start = startX
186 lastF.y_start = startY
187 lastF.x_length = endX - startX
188 lastF.y_length = endY - startY
189 lastF.runFill = runFill
190 print >>outfile, lastF
191 # end if
192 firstF = curF
193 runFill = 0
194 # end if
195 runFill += curF.x_length
196 lastF = curF
197 # end if
198 # end for
199
200 if (None != lastF):
201 x1 = firstF.x_start
202 x2 = lastF.x_start
203 startX = cvm( x1 < x2, x1, x2)
204 x1 += firstF.x_length
205 x2 += lastF.x_length
206 endX = cvm( x1 > x2, x1, x2)
207 y1 = firstF.y_start
208 y2 = lastF.y_start
209 startY = cvm( y1 < y2, y1, y2)
210 y1 += firstF.y_length
211 y2 += lastF.y_length
212 endY = cvm( y1 > y2, y1, y2)
213 lastF.subtype = 'r'
214 lastF.matchid = lastF.runid
215 lastF.runid = "." # the agreed NULL value
216 lastF.x_start = startX
217 lastF.y_start = startY
218 lastF.x_length = endX - startX
219 lastF.y_length = endY - startY
220 lastF.runFill = runFill
221 print >>outfile, lastF
222 # end if
223 return outfile
224 # end def
225
226 def main(inpname, outname, maxJump, runIdPrefix):
227
228 print >>sys.stderr, "Beware /tmp!\n"
229
230 inpfile = open(inpname)
231 tempdata1 = formPerfectRuns(inpfile,
232 MatchRecord.sortInXorderAP,
233 MatchRecord.sortInYorderAP,
234 int(maxJump),
235 runIdPrefix
236 )
237 tempdata2 = runsAsMatches( tempdata1)
238
239 # Argh! All our work is done in temporary files in /tmp,
240 # but this wants to create hard links to save the last
241 # result -- the output.
242
243 tempdata1.link("/tmp/"+outname+".matches")
244 tempdata2.link("/tmp/"+outname+".runs")
245
246 if __name__ == '__main__':
247 inpname = sys.argv[1]
248 outname = sys.argv[2]
249 maxJump = int(sys.argv[3])
250 runIdPrefix = sys.argv[4]
251
252 # defaults, 100000, r
253
254 main(inpname, outname, maxJump, runIdPrefix)