comparison ffp_phylogeny.py @ 1:d1c88b118a3f draft

Uploaded
author damion
date Fri, 13 Mar 2015 20:59:28 -0400
parents eb6e5e78a066
children 671667722d3d
comparison
equal deleted inserted replaced
0:eb6e5e78a066 1:d1c88b118a3f
1 #!/usr/bin/python 1 #!/usr/bin/python
2 import optparse 2 import optparse
3 import re
3 import time 4 import time
4 import os 5 import os
5 import tempfile 6 import tempfile
6 import sys 7 import sys
7 import shlex, subprocess 8 import shlex, subprocess
158 159
159 """ 160 """
160 commands = command.split("|") 161 commands = command.split("|")
161 processes = [] 162 processes = []
162 ptr = 0 163 ptr = 0
164 substantive = re.compile('[a-zA-Z0-9]+')
165
163 for command_line in commands: 166 for command_line in commands:
164 print command_line.strip() 167 print command_line.strip()
165 args = shlex.split(command_line.strip()) 168 args = shlex.split(command_line.strip())
166 if ptr == 0: 169 if ptr == 0:
167 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 170 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
168 processes.append(proc) 171 processes.append(proc)
169 else: 172 else:
173
174 #this has to come before error processing?
175 newProcess = subprocess.Popen(args, stdin=processes[ptr-1].stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
176
170 # It seems the act of reading standard error output is enough to trigger 177 # It seems the act of reading standard error output is enough to trigger
171 # error code signal for that process, i.e. so that retcode returns a code. 178 # error code signal for that process, i.e. so that retcode returns a code.
179 retcode = processes[ptr-1].poll()
172 stderrdata = processes[ptr-1].stderr.read() 180 stderrdata = processes[ptr-1].stderr.read()
173 retcode = processes[ptr-1].poll() 181 #Issue with ffptree is it outputs ----....---- on stderr
174 if retcode or len(stderrdata) > 0: 182 if retcode or (len(stderrdata) > 0 and substantive.search(stderrdata)):
175 stop_err(stderrdata) 183 stop_err(stderrdata)
176 184
177 newProcess = subprocess.Popen(args, stdin=processes[ptr-1].stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 185 processes.append(newProcess)
178 processes.append(newProcess)
179 processes[ptr-1].stdout.close() # Allow prev. process to receive a SIGPIPE if current process exits. 186 processes[ptr-1].stdout.close() # Allow prev. process to receive a SIGPIPE if current process exits.
180 187
181 ptr += 1 188 ptr += 1
182 189
190 retcode = processes[ptr-1].poll()
183 (stdoutdata, stderrdata) = processes[ptr-1].communicate() 191 (stdoutdata, stderrdata) = processes[ptr-1].communicate()
184 retcode = processes[ptr-1].poll() 192 if retcode or (len(stderrdata) > 0 and substantive.search(stderrdata)):
185 if retcode or len(stderrdata) > 0:
186 stop_err(stderrdata) 193 stop_err(stderrdata)
187 194
188 return stdoutdata 195 return stdoutdata
189 196
190 197
320 command += ' | ffprwn' 327 command += ' | ffprwn'
321 328
322 #Now create a taxonomy label file, ensuring a name exists for each profile. 329 #Now create a taxonomy label file, ensuring a name exists for each profile.
323 taxonomyNames = getTaxonomyNames(options.type, options.multiple, options.abbreviate, in_files, options.taxonomy) 330 taxonomyNames = getTaxonomyNames(options.type, options.multiple, options.abbreviate, in_files, options.taxonomy)
324 taxonomyTempFile = getTaxonomyFile(taxonomyNames) 331 taxonomyTempFile = getTaxonomyFile(taxonomyNames)
332
325 # -p = Include phylip format 'infile' of the taxon names to use. Very simple, just a list of fasta identifier names. 333 # -p = Include phylip format 'infile' of the taxon names to use. Very simple, just a list of fasta identifier names.
326 command += ' | ffpjsd -p ' + taxonomyTempFile 334 command += ' | ffpjsd -p ' + taxonomyTempFile
327 335
328 if options.metric and len(options.metric) >0 : 336 if options.metric and len(options.metric) >0 :
329 command += ' --' + options.metric 337 command += ' --' + options.metric
335 if len(taxonomyNames) > 2: 343 if len(taxonomyNames) > 2:
336 command += ' | ffptree -q' 344 command += ' | ffptree -q'
337 else: 345 else:
338 stop_err("For a phylogenetic tree display, one must have at least 3 ffp profiles.") 346 stop_err("For a phylogenetic tree display, one must have at least 3 ffp profiles.")
339 347
348 #print command
349
340 result = check_output(command) 350 result = check_output(command)
341 with open(options.output,'w') as fw: 351 with open(options.output,'w') as fw:
342 fw.writelines(result) 352 fw.writelines(result)
343 os.remove(taxonomyTempFile) 353 os.remove(taxonomyTempFile)
344 354