comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:2e7f0da431e3
1 #!/usr/bin/env python
2
3 """
4 Runs SortMeRNA
5 """
6
7 import subprocess
8 import optparse
9 import shlex
10
11
12 def main():
13 """Parse the command line, exectutes SortMeRNA and buildtrie if neeeded."""
14 #TODO: Put all SortMeRNA options in the command-line parser
15 parser = optparse.OptionParser()
16 parser.add_option('--sortmerna', dest='sortmerna_cmd', help='')
17 parser.add_option('--buildtrie', dest='buildtrie',
18 default=False, action='store_true', help='')
19 (options, args) = parser.parse_args()
20 if not args:
21 raise Exception('Please provide at least one database')
22
23 if options.buildtrie:
24 buildtrie = 'buildtrie'
25 for database in args:
26 run_buildtrie([buildtrie, '--db', database])
27
28 if options.sortmerna_cmd:
29 sortmerna = 'sortmerna'
30 run_sortmerna([sortmerna] +
31 shlex.split(options.sortmerna_cmd) +
32 ['-m', '262144', '-n', str(len(args)), '--db'] +
33 args)
34
35
36 def run_buildtrie(cmd):
37 """Run the BuildTrie program."""
38 try:
39 stdout_arg = subprocess.PIPE
40 stderr_arg = subprocess.PIPE
41 child_process = subprocess.Popen(args=" ".join(cmd), shell=True,
42 stdin=None, stdout=stdout_arg,
43 stderr=stderr_arg)
44 stdout_str, stderr_str = child_process.communicate()
45 return_code = child_process.returncode
46 if return_code is not 0:
47 raise Exception(stderr_str)
48
49 except Exception, error:
50 raise Exception('Error while running Buildtrie:\n' +
51 '\n'.join([str(error), stdout_str, stderr_str]))
52
53
54 def run_sortmerna(cmd):
55 """Run the SortMeRNA program."""
56 try:
57 stdout_arg = subprocess.PIPE
58 stderr_arg = subprocess.PIPE
59 child_process = subprocess.Popen(args=" ".join(cmd), shell=True,
60 stdin=None, stdout=stdout_arg,
61 stderr=stderr_arg)
62 stdout_str, stderr_str = child_process.communicate()
63 return_code = child_process.returncode
64 if return_code is not 0:
65 raise Exception(stderr_str)
66 except Exception, error:
67 raise Exception('Error while running SortMeRNA:\n' +
68 '\n'.join([str(error), stdout_str, stderr_str]))
69
70
71 if __name__ == "__main__":
72 main()