diff sortmerna_wrapper.py @ 0:2e7f0da431e3 draft default tip

Uploaded version 1.0
author bonsai
date Tue, 30 Apr 2013 13:12:35 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sortmerna_wrapper.py	Tue Apr 30 13:12:35 2013 -0400
@@ -0,0 +1,72 @@
+#!/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()