Debian Codegraph @

atac-driver/chainer/python/UniqueFilter.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2
3 import os, sys, time, tempfile
4 import MyFile
5 import MatchRecord
6 import AtacFile
7
8 def xorIntervals( inpname, outname):
9 # not tested yet
10 leftPicket = 0
11 rghtPicket = 0
12 inpfile = open(inpname,"r")
13 outfile = open(outname,"w")
14 for line in inpfile:
15 fields = line.split()
16 newstart = int(fields[0])
17 newend = int(fields[1])
18 assert(leftPicket <= newstart)
19 rghtSide = min(newstart,rghtPicket)
20 if rghtSide > leftPicket:
21 # interval has positive length
•22 print(leftPicket, rghtSide, file=outfile)
23 leftPicket = max(newstart, min(rightPicket,newend))
24 rghtPicket = max(leftPicket,max(rightPicket,newend))
25
26
27 def findUniformCoverageIntervals(inpfile,outfile):
28 # The input records are ("E", id, position, coverage_increment).
29 # The output records are ("C", id, start_position, length, coverage_level).
30 inpfile.seek(0)
31 outfile.seek(0)
32 oldaxis = None; oldposition = 0; cov = 0
33 for line in inpfile:
34 ( recordtype, newaxis, newposition, newchange) = line.split()
35 if(recordtype == "E"):
36 newposition = int(newposition)
37 newchange = int(newchange)
•38 if(newaxis != oldaxis and cov != 0): print("Woops", file=sys.stderr)
39 len = newposition - oldposition
40 if(cov>0 and len>0):
•41 print("C", oldaxis, oldposition, len, cov, file=outfile);
42 cov += newchange;
43 assert(cov >= 0)
44 oldaxis = newaxis; oldposition = newposition;
45 assert(cov == 0)
46 outfile.flush()
47
48
49 def findCoverageIntervals( inpfile, outfile, processFirstAxis):
50 # The input file is an ATAC matches file.
51 # The output file is an ATAC coverage intervals file.
52 inpfile.seek(0)
53 outfile.seek(0)
54 t0 = time.time()
55 tmpfile3 = MyFile.myfile()
56 for line in inpfile:
57 if(line[0]=="M"):
58 fields = line.split()
59 if(fields[1]=="u" or fields[1]=="x"):
60 if(processFirstAxis):
61 axis = fields[4]
62 bgn = int(fields[5])
63 end = bgn+int(fields[6])
64 else:
65 axis = fields[8]
66 bgn = int(fields[9])
67 end = bgn+int(fields[10])
•68 print("E", axis,bgn,1, file=tmpfile3)
69 print("E", axis,end,-1, file=tmpfile3)
70 tmpfile3.close()
71 tmpname = tempfile.mktemp()
72 cmd = "sort -T . -k 1,1 -k 2,2 -k 3n -k 4nr %s > %s" % (tmpfile3.name, tmpname)
•73 print(cmd, file=sys.stderr)
74 iret = os.system(cmd); assert(iret==0)
•75 print("time elapsed is ", (time.time() - t0), file=sys.stderr)
76 tmpfile4 = open(tmpname)
77 t0 = time.time()
78 findUniformCoverageIntervals( tmpfile4, outfile)
•79 print("time elapsed is ", (time.time() - t0), file=sys.stderr)
80 tmpfile4.close()
81 os.system("rm -f " + tmpname)
82 outfile.seek(0)
83
84
85 def applyOneKeepMask( inpfile, outfile, keepMaskFile, processFirstAxis):
86 # Note that the following merge-like control structure is
87 # influenced by the function property of keep intevals to matches.
88
89 debug = 0
90 inpfile.seek(0)
91 outfile.seek(0)
92 keepMaskFile.seek(0)
93
94 # Put the first valid match record into FM. Each input ATAC match
95 # record produces zero, one or more output ATAC matches.
96 FM = None;
97 ma = None; ms = None; me = None
98 qa = None; qs = None; ql = None;
99
100 # the set of masking intervals, using the q variables and iline
101 maskiter = iter(keepMaskFile)
102
103 # the set of masked matches using the m variables and mline
104 inpiter = iter(inpfile)
105
106 iline = None
107 mline = None
108
109 last_matchid = None; subcount = 0
110
111 try: # StopIteration exception from either iterator gets us out
112 while 1:
113 if(iline == None):
•114 iline = next(maskiter)
115 (subtype, qa, qs, ql, cov, ) = iline.split()
116 assert(subtype=='C')
117 cov= int(cov)
118 if(cov != 1):
119 iline = None
120 continue
121 qs = int(qs)
122 ql = int(ql)
123 qe = qs + ql
124
125 if(mline == None):
•126 mline = next(inpiter)
127 if(mline[0] != 'M'):
128 # not a match record, so just pass it through
•129 print(mline, end=' ', file=outfile)
130 mline = None
131 continue
132 FM = MatchRecord.MatchRecord(mline)
133 assert(FM.subtype == "u" or FM.subtype == "x")
134 if(processFirstAxis):
135 ma = FM.x_scaf_uid
136 ms = FM.x_start # match start
137 me = ms + FM.x_length # match end
138 else:
139 ma = FM.y_scaf_uid
140 ms = FM.y_start # match start
141 me = ms + FM.y_length # match end
142
143 # holding valid iline and mline data now
144
145 if not(ma==qa):
146 # not on same axis, need to get a new one
147 if(ma < qa):
148 mline = None
149 else:
150 iline = None
151
152 elif not( (ms < qe) and (qs < me) ):
153 # we are not overlapping, need to get a new one of them
154 if(ms < qs):
155 mline = None
156 else:
157 iline = None
158
159 else:
160 # processing for overlaps
161 FT = FM.copy()
162 mx = max(ms,qs)
163 mn = min(me,qe)
164 trimFromStart = mx - ms
165 trimFromEnd = me - mn
166 trimmedLength = mn - mx
167 if( FT.x_orientation == FT.y_orientation):
168 FT.x_start += trimFromStart
169 FT.y_start += trimFromStart
170 else:
171 if(processFirstAxis):
172 FT.x_start += trimFromStart
173 FT.y_start += trimFromEnd
174 else:
175 FT.y_start += trimFromStart
176 FT.x_start += trimFromEnd
177 FT.x_length = trimmedLength
178 FT.y_length = trimmedLength
179 if debug:
•180 print("# trimmed ", file=sys.stdout)
181 print(FT, file=sys.stdout)
182
183 # We must insure that the match identifier is still unique.
184 if last_matchid == FM.matchid :
185 subcount += 1
186 else:
187 subcount = 0
188 # print >>sys.stderr, last_matchid, FM.matchid, subcount
189 last_matchid = FM.matchid
190
191 if(subcount > 0):
192 if processFirstAxis :
193 FT.matchid = FT.matchid + "x" + str(subcount)
194 else:
195 FT.matchid = FT.matchid + "y" + str(subcount)
196
•197 print(FT, file=outfile)
198 # we need to get a new one
199 if(qe < me):
200 iline = None
201 else:
202 mline = None
203
204 except StopIteration:
205 # If there are any left over non-match lines, then output them!
206 for mline in inpiter:
207 if(mline[0] != "M"):
•208 print(mline, end=' ', file=outfile)
209
210 def applyBothKeepMasks( inpfile, outfile ):
211
212 # Maybe we can think of a masking implementation where each ATAC match
213 # is treated atomicly. Assume that the keep mask intervals are sorted
214 # by start postition. Assume that the ATAC matches are sorted by start
215 # postion. Assert that all keep mask intervals are non-overlapping and
216 # were cut from only one ATAC match. Thus the mapping from keep mask
217 # intervals is a function. Note that this requires that we do not
218 # coalesce abutting keep mask intervals that originate from multiple
219 # matches. Note this still allows an ATAC match to overlap more than
220 # one keep mask interval. Ignore all keep mask intervals with zero
221 # length their creation has tie breaking problems. See notes on 2003
222 # Jul 29.
223
224 debug = 0
225 debugnum = 0
226 inpfile.seek(0)
227 outfile.seek(0)
228
229
230 # Apply the keepMask for the first axis.
231 # Make the sorted the keep mask intervals for the first axis.
232 processFirstAxis = 1
233 keepMaskFile = MyFile.myfile()
234 tmpfile2 = inpfile
235 tmpfile3 = MyFile.myfile()
236 tmpfile4 = MyFile.myfile()
237
238 findCoverageIntervals( inpfile, keepMaskFile, processFirstAxis)
239 if debug:
240 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•241 for line in keepMaskFile: print(line, end=' ', file=debugfile)
242
243 MatchRecord.sortInXorderAP(tmpfile2,tmpfile3)
244 if debug:
245 #tmpfile2.seek(0)
246 #debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
247 #for line in tmpfile2: print >>debugfile, line,
248 tmpfile3.seek(0)
249 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•250 for line in tmpfile3: print(line, end=' ', file=debugfile)
251
252 applyOneKeepMask( tmpfile3, tmpfile4, keepMaskFile, processFirstAxis)
253 if debug:
254 tmpfile4.seek(0)
255 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•256 for line in tmpfile4: print(line, end=' ', file=debugfile)
257
258 # Apply the keepMask for the second axis.
259 # Make the sorted the keep mask intervals for the second axis.
260 processFirstAxis = 0
261 keepMaskFile = MyFile.myfile()
262 tmpfile2 = tmpfile4
263 tmpfile3 = MyFile.myfile()
264 tmpfile4 = outfile
265
266 findCoverageIntervals( inpfile, keepMaskFile, processFirstAxis)
267 if debug:
268 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•269 for line in keepMaskFile: print(line, end=' ', file=debugfile)
270
271
272 MatchRecord.sortInYorderAP(tmpfile2,tmpfile3)
273 if debug:
274 #tmpfile2.seek(0)
275 #debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
276 #for line in tmpfile2: print >>debugfile, line,
277 tmpfile3.seek(0)
278 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•279 for line in tmpfile3: print(line, end=' ', file=debugfile)
280
281 applyOneKeepMask( tmpfile3, tmpfile4, keepMaskFile, processFirstAxis)
282 if debug:
283 tmpfile4.seek(0)
284 debugnum += 1; debugfile = open("debugfile.%d" % debugnum, "w")
•285 for line in tmpfile4: print(line, end=' ', file=debugfile)
286
287
288 def main( inpfile, outfile):
289 applyBothKeepMasks( inpfile, outfile)
290
291 # Should we check if the first and last characters of the masked
292 # matches are matching?
293
294 # Should we compute the percent identity in this module?
295
296
297
298 # Allow each module to have its own main for testing.
299 if __name__ == '__main__':
300 inpname = sys.argv[1]
301 outname = sys.argv[2]
302 inpfile = open(inpname)
303 outfile = open(outname,"w")
304 main(inpfile, outfile)
305 # end if