view sortmerna.py @ 0:a8ac09e937f3 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/rna_tools/sortmerna commit 04cfb5475292e4fd1f7c0ca86d8d0d5e5f886c3d-dirty
author rnateam
date Mon, 03 Aug 2015 08:18:26 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python

"""
Runs SortMeRNA
"""

import subprocess
import optparse
import shlex


def main():
    """Parse the command line, exectutes SortMeRNA and buildtrie if neeeded."""
    #TODO: Put all SortMeRNA options in the command-line parser
    parser = optparse.OptionParser()
    parser.add_option('--sortmerna', dest='sortmerna_cmd', help='')
    parser.add_option('--buildtrie', dest='buildtrie',
                      default=False, action='store_true', help='')
    (options, args) = parser.parse_args()
    if not args:
        raise Exception('Please provide at least one database')

    if options.buildtrie:
        buildtrie = 'buildtrie'
        for database in args:
            run_buildtrie([buildtrie, '--db', database])

    if options.sortmerna_cmd:
        sortmerna = 'sortmerna'
        run_sortmerna([sortmerna] +
                      shlex.split(options.sortmerna_cmd) +
                      ['-m', '262144', '-n', str(len(args)), '--db'] +
                      args)


def run_buildtrie(cmd):
    """Run the BuildTrie program."""
    try:
        stdout_arg = subprocess.PIPE
        stderr_arg = subprocess.PIPE
        child_process = subprocess.Popen(args=" ".join(cmd), shell=True,
                                         stdin=None, stdout=stdout_arg,
                                         stderr=stderr_arg)
        stdout_str, stderr_str = child_process.communicate()
        return_code = child_process.returncode
        if return_code is not 0:
            raise Exception(stderr_str)

    except Exception, error:
        raise Exception('Error while running Buildtrie:\n' +
                        '\n'.join([str(error), stdout_str, stderr_str]))


def run_sortmerna(cmd):
    """Run the SortMeRNA program."""
    try:
        stdout_arg = subprocess.PIPE
        stderr_arg = subprocess.PIPE
        child_process = subprocess.Popen(args=" ".join(cmd), shell=True,
                                         stdin=None, stdout=stdout_arg,
                                         stderr=stderr_arg)
        stdout_str, stderr_str = child_process.communicate()
        return_code = child_process.returncode
        if return_code is not 0:
            raise Exception(stderr_str)
    except Exception, error:
        raise Exception('Error while running SortMeRNA:\n' +
                        '\n'.join([str(error), stdout_str, stderr_str]))


if __name__ == "__main__":
    main()