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