annotate tools/ilmn_pacbio/quake_wrapper.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # Copyright (c) 2011, Pacific Biosciences of California, Inc.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # All rights reserved.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 # * Neither the name of Pacific Biosciences nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 #THIS SOFTWARE IS PROVIDED BY PACIFIC BIOSCIENCES AND ITS CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PACIFIC BIOSCIENCES OR ITS CONTRIBUTORS BE LIABLE FOR ANY
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 #DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 #LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 #(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 import os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 import subprocess
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 QUAKE_EXE = os.path.join( os.path.dirname(os.path.abspath(sys.argv[0])), 'quake.py' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 cmdLine = sys.argv
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 cmdLine.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 # horribly not robust, but it was a pain to rewrite everything with
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 # optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 j = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 cut = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 for i,arg in enumerate(cmdLine):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if '--default_cutoff' in arg:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 j = i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 cut = int(arg.split('=')[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if j>=0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 cmdLine = cmdLine[:j] + cmdLine[j+1:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 j = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 output=''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 for i,arg in enumerate(cmdLine):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if '--output' in arg:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 j = i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 output = arg.split('=')[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if j>=0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 cmdLine = cmdLine[:j] + cmdLine[j+1:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 def backticks( cmd, merge_stderr=True ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 Simulates the perl backticks (``) command with error-handling support
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 Returns ( command output as sequence of strings, error code, error message )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 if merge_stderr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 _stderr = subprocess.STDOUT
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 _stderr = subprocess.PIPE
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 p = subprocess.Popen( cmd, shell=True, stdin=subprocess.PIPE,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 stdout=subprocess.PIPE, stderr=_stderr,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 close_fds=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 out = [ l[:-1] for l in p.stdout.readlines() ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 p.stdout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 if not merge_stderr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 p.stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 # need to allow process to terminate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 p.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 errCode = p.returncode and p.returncode or 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if p.returncode>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 errorMessage = os.linesep.join(out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 output = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 errorMessage = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 output = out
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 return output, errCode, errorMessage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 def to_stdout():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 def toCorFastq(f):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 stem, ext = os.path.splitext( os.path.basename(f) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 dir = os.path.dirname(f)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 corFastq = os.path.join(dir,'%s.cor%s' % (stem,ext) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 if not os.path.exists(corFastq):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 print >>sys.stderr, "Can't find path %s" % corFastq
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 return corFastq
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 if '-r' in cmdLine:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 fastqFile = cmdLine[ cmdLine.index('-r')+1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 corFastq = toCorFastq(fastqFile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 infile = open( corFastq, 'r' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 for line in infile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 sys.stdout.write( line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 infile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 fofnFile = cmdLine[ cmdLine.index('-f')+1 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 infile = open(fofnFile,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 for line in infile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 if len(line)>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 fastqFiles = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 infile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 outs = output.split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 for o,f in zip(outs,fastqFiles):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 cf = toCorFastq(f)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 os.system( 'cp %s %s' % ( cf, o ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 def run():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 cmd = '%s %s' % ( QUAKE_EXE, " ".join(cmdLine) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 output, errCode, errMsg = backticks( cmd )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 if errCode==0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 to_stdout()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 # if Quake exits with an error in cutoff determination we
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 # can force correction if requested
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 if 'cutoff.txt' in errMsg and cut>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 outfile = open( 'cutoff.txt', 'w' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 print >>outfile, str(cut)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 outfile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 cmd = '%s --no_count --no_cut %s' % ( QUAKE_EXE, " ".join(cmdLine) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 output, errCode, errMsg = backticks( cmd )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if errCode==0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 to_stdout()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 print >>sys.stderr, errMsg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if __name__=='__main__': run()