annotate tools/ilmn_pacbio/smrtpipe_galaxy.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import subprocess
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import optparse as op
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import xml.etree.cElementTree as et
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 TRACE=False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 # Turn on tracing to dump out __input__.xml and __settings__.xml somewhere
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 #TRACE=True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 #TRACE_PATH='/home/UNIXHOME/jsorenson'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 class SmrtpipeGalaxy:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 """Wrapper for running smrtpipe under galaxy"""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def __init__( self, argv ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 self.__parseOptions( argv )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 def __parseOptions( self, argv ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 usage = 'Usage: %prog [--help] [options] smrtpipe.ini'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 parser = op.OptionParser( usage=usage, description=SmrtpipeGalaxy.__doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 parser.add_option( "--output",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 help="Designate a file generated by smrtpipe as the expected output for galaxy" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parser.add_option( "--nproc", type="int",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 help="Number of processes to use (-D NPROC)" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 parser.add_option( "--galaxy_output",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 help="File name provided by galaxy where output should be placed" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 parser.add_option( "--dry_run", action="store_true",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 help="Create auxiliary XML files and exit" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 parser.add_option( "--dat_extension",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 help="Soft link .dat files to have this extension (some pipelines require certain extensions)" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 parser.set_defaults( output=None, dry_run=False, galaxy_output=None,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 dat_extension=None, nproc=0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 self.options, self.args = parser.parse_args( argv )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if len(self.args)!=2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 parser.error( 'Expected 1 argument' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 self.configFile = self.args[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 def __parseConfig( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 infile = open( self.configFile, 'r' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 section = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 sections = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 for line in infile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 l = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if len(l)==0 or line.startswith('#'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 if l.startswith('[') and l.endswith(']'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 section = section_factory( l[1:-1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 sections.append(section)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if section is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if '=' in l:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 section.addParameterLine(l)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 section.addLine(l)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 infile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 return sections
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 def transferOutput( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 if not self.options.output or not self.options.galaxy_output:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 return True, ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if not os.path.exists(self.options.output):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 return False, "Can't find file %s (job error?)" % self.options.output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 os.system( 'cp %s %s' % (self.options.output, self.options.galaxy_output ))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 return True, ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 def run( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if not os.path.exists( self.configFile ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 print >>sys.stderr, "Can't find config file %s" % self.configFile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 sections = self.__parseConfig()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if len(sections)==0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 print >>sys.stderr, "No sections found in %s" % self.configFile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 if sections[0].name != 'input':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 print >>sys.stderr, "No [input] section found in %s" % self.configFile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 INPUT_FILE = '__input__.xml'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 SETTINGS_FILE = '__settings__.xml'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 sections[0].softLinkDats( self.options.dat_extension )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 inputXml = sections[0].makeXmlElement()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 write_xml_to_file( INPUT_FILE, inputXml )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 if TRACE:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 write_xml_to_file( os.path.join(TRACE_PATH,INPUT_FILE), inputXml )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 settings = et.Element( 'smrtpipeSettings' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 for s in sections[1:]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 s.makeXmlElement( settings )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 write_xml_to_file( SETTINGS_FILE, settings )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 if TRACE:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 write_xml_to_file( os.path.join(TRACE_PATH,SETTINGS_FILE), settings )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 nproc = '-D NPROC=%d' % self.options.nproc if self.options.nproc>0 else ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 cmd = 'smrtpipe.py %s --params=%s xml:%s > smrtpipe.err 2>1' % \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 ( nproc, SETTINGS_FILE, INPUT_FILE )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 if self.options.dry_run:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 print 'Command to run:'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 print cmd
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 return 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 out, errCode, errMsg = backticks( cmd )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 if errCode!=0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 print >>sys.stderr, "error while running: %s" % cmd
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 print >>sys.stderr, errMsg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 if os.path.exists('log/smrtpipe.log'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 print >>sys.stderr, 'Log:'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 infile = open('log/smrtpipe.log','r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 for line in infile: sys.stderr.write(line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 infile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 return errCode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 success, errMsg = self.transferOutput()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 if not success:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 print >>sys.stderr, errMsg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 return 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 def write_xml_to_file( fileName, root ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 outfile = open( fileName, 'w' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 outfile.write( '<?xml version="1.0"?>\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 outfile.write( et.tostring(root) + '\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 outfile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 def section_factory( name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 if name=='input':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 return InputSection(name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 return Section(name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 class Section:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 def __init__( self, name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 self._name = name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 self._lines = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 self._vars = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 @property
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 def name(self):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 return self._name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 def addLine( self, line ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 self._lines.append(line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 def addParameterLine( self, line ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 self.addLine(line)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 i = line.find( '=' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 key = line[:i].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 value = line[i+1:].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 self._vars[key] = value
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 def makeXmlElement( self, settings ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 if self._name=='global':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 root = et.SubElement( settings, "protocol", {'name':'generic'} )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 root = et.SubElement( settings, "module", {'name':self._name} )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 for k,v in self._vars.iteritems():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 param = et.SubElement( root, 'param', {'name':k} )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 val = et.SubElement( param, 'value' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 val.text = v
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 return None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 def __str__( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 "for debugging"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 buffer = [ 'S { name=' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 buffer.append(self._name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 buffer.append('; lines=%s' % ','.join(self._lines) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 for k,v in self._vars.iteritems():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 buffer.append('; %s=%s' % (k,v) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 buffer.append(' }')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 return ''.join(buffer)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 class InputSection( Section ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 def __init__( self, name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 Section.__init__(self,name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 def softLinkDats( self, newExtension ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 if not newExtension:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 return
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 newLines = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 for l in self._lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 if ':' in l:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 protocol = l[:l.find(':')+1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 file = l[l.find(':')+1:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 protocol = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 file = l
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 if os.path.exists(file) and file.endswith('.dat'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 newFile = '%s.%s' % ( file, newExtension )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 if not os.path.exists(newFile):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 os.system( 'ln -s %s %s' % ( file, newFile ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 newLines.append(protocol+newFile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 newLines.append(l)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 self._lines = newLines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 def makeXmlElement( self, parent=None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 root = et.Element( "pacbioAnalysisInputs" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 data = et.SubElement( root, 'dataReferences' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 iRef = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 for l in self._lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 def add(x,iRef):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 if len(x)==0: return iRef
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 node = et.SubElement( data, 'url' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 if ':' in x:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 node.attrib[ 'ref' ] = x
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 node.attrib[ 'ref' ] = 'run:0000000-%04d' % iRef
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 node2 = et.SubElement( node, 'location' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 node2.text = x
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 return iRef+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 if l.endswith('fofn') and os.path.exists(l):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 infile = open(l,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 for j,line in enumerate(infile): iRef=add(line.strip(),iRef)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 infile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 iRef=add(l,iRef)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 return root
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 def backticks( cmd, merge_stderr=True ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 Simulates the perl backticks (``) command with error-handling support
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 Returns ( command output as sequence of strings, error code, error message )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 if merge_stderr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 _stderr = subprocess.STDOUT
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 _stderr = subprocess.PIPE
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 p = subprocess.Popen( cmd, shell=True, stdin=subprocess.PIPE,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 stdout=subprocess.PIPE, stderr=_stderr,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 close_fds=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 out = [ l[:-1] for l in p.stdout.readlines() ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 p.stdout.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 if not merge_stderr:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 p.stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 # need to allow process to terminate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 p.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 errCode = p.returncode and p.returncode or 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 if p.returncode>0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 errorMessage = os.linesep.join(out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 output = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 errorMessage = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 output = out
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 return output, errCode, errorMessage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 if __name__=='__main__':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 app = SmrtpipeGalaxy( sys.argv )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 sys.exit( app.run() )