def _run(runcmd, args=None, defaults=None, quiet=False, dryrun=False, **params): """ Returns the name of the outfile. * cmd - name of the executable * args - unmodified arguments appended to end of command line * dryrun - build the command line and quit * defaults - an optional dict of key=val parameters * params - pass arguments as either key=val pairs, or as key='' if key has no argument """ cmd = sequtil.find_exec(runcmd) if cmd is None: raise OSError('%s could not be found' % runcmd) os_type = sequtil.get_os_type() if os_type != 'POSIX': raise OSError('Operating system must be POSIX-like') if defaults is None: defaults = {} defaults.update(params) arguments = [cmd] for k,v in defaults.items(): if len(k) == 1: dashes = '-' else: dashes = '--' if v is None or not v.strip(): add = ['%(dashes)s%(k)s'] else: add = ['%(dashes)s%(k)s', '%(v)s'] arguments.extend([fstr % locals() for fstr in add]) if args: arguments.extend(args) log.info(arguments) log.info(' '.join(arguments)) if dryrun: return None, None else: stdoutdata, stderrdata = subprocess.Popen(arguments, stdout=subprocess.PIPE).communicate() if stderrdata: log.error(stderrdata) log.info(stdoutdata[:500]) if not quiet: print stdoutdata return stdoutdata, stderrdata
def run(target_file, clustal_cmd='clustalw', run=True, silent=False, **params): """Run clustalw -options for command line parameters""" if sequtil.find_exec(clustal_cmd) is None: raise OSError('%s could not be found' % clustal_cmd) os_type = sequtil.get_os_type() log.debug('os type is set as %s' % os_type) # defaults = getparams( # align=None, # batch=None, # infile=target_file, # output='phylip', # outputtree='nj') target_file = os.path.abspath(target_file) defaults = getparams(infile=target_file) defaults.update(params) args = [] args.append('"%s"'%clustal_cmd) # surround with quotes to accomodate spaces if os_type == 'POSIX': argchar='-' elif os_type == 'WINDOWS': argchar='/' for k,v in defaults.items(): if v: args.append('%s%s="%s"' % (argchar,k,v)) else: args.append('%s%s' % (argchar,k)) if silent: args.append('> %s' % os.devnull) cmd = ' '.join(args) log.info(cmd) if run and os_type == 'POSIX': log.info('running clustalw: %s' % cmd) os.system( cmd ) elif os_type == 'WINDOWS': cwd,_ = os.path.split(target_file) cmds = ['cd "%s"' % cwd, cmd] sequtil.run_bat(cmds, path=cwd, run=run) return cmd
def run(hmmcmd, infile, outfile=False, dryrun=False, defaults=None, quiet=False, **params): """ Returns the name of the outfile. * cmd - choose one of hmmalign hmmbuild hmmcalibrate hmmconvert hmmemit hmmfetch hmmindex hmmpfam hmmsearch * infile - input file name * outfile - optional output file name. If False, no output file is specified; if None, output is written to a file given a random name. * dryrun - build the command line and quit * defaults - an optional dict of key=val parameters * params - pass arguments as either key=val pairs, or as key=None if key is a command line switch """ cmd = sequtil.find_exec(hmmcmd) if cmd is None: raise OSError("%s could not be found" % hmmcmd) os_type = sequtil.get_os_type() log.debug("os type is set as %s" % os_type) infile = os.path.abspath(infile) if outfile: outfile = os.path.abspath(outfile) elif outfile is None: outfile = os.path.join(os.path.split(infile)[0], sequtil.randomname(length=10)) outin = [infile] if not outfile is False: outin.insert(0, outfile) if defaults is None: defaults = {} defaults.update(params) args = [cmd] for k, v in defaults.items(): if len(k) == 1: dashes = "-" else: dashes = "--" if v is None: fstr = "%(dashes)s%(k)s" elif len(str(v).split()) > 1: fstr = '%(dashes)s%(k)s "%(v)s"' else: fstr = "%(dashes)s%(k)s %(v)s" args.append(fstr % locals()) args.extend(outin) log.info(" ".join(args)) if os_type == "POSIX" and not dryrun: # subprocess.check_call(args) if quiet: stdout = open(os.devnull) else: stdout = None subprocess.Popen(args, stdout=stdout).communicate() elif os_type == "WINDOWS": # completely untested cwd, _ = os.path.split(target_file) cmds = ['cd "%s"' % cwd, cmd] sequtil.run_bat(cmds, path=cwd, run=not dryrun) return outfile