comparison tools/ncbi_blast_plus/blast2go.py @ 0:cd52c931b325

Migrated tool version 0.0.1 from old tool shed archive to new tool shed repository
author peterjc
date Tue, 07 Jun 2011 16:28:31 -0400
parents
children 0f159cf346c8
comparison
equal deleted inserted replaced
-1:000000000000 0:cd52c931b325
1 #!/usr/bin/env python
2 """Galaxy wrapper for Blast2GO for pipelines, b2g4pipe v2.3.5.
3
4 This script takes exactly three command line arguments:
5 * Input BLAST XML filename
6 * Blast2GO properties filename (settings file)
7 * Output tabular filename
8
9 It then calls the Java command line tool, and moves the output file to
10 the location Galaxy is expecting.
11 """
12 import sys
13 import os
14 import subprocess
15
16 #You may need to edit this to match your local setup,
17 blast2go_jar = "/opt/b2g4pipe/blast2go.jar"
18
19
20 def stop_err(msg, error_level=1):
21 """Print error message to stdout and quit with given error level."""
22 sys.stderr.write("%s\n" % msg)
23 sys.exit(error_level)
24
25 if len(sys.argv) != 4:
26 stop_err("Require three arguments: XML filename, properties filename, output tabular filename")
27
28 xml_file, prop_file, tabular_file = sys.argv[1:]
29
30 if not os.path.isfile(xml_file):
31 stop_err("Input BLAST XML file not found: %s" % xml_file)
32
33 if not os.path.isfile(prop_file):
34 stop_err("Blast2GO configuration file not found: %s" % prop_file)
35
36 def run(cmd):
37 #Avoid using shell=True when we call subprocess to ensure if the Python
38 #script is killed, so too is the child process.
39 try:
40 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41 except Exception, err:
42 stop_err("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err))
43 #Use .communicate as can get deadlocks with .wait(),
44 stdout, stderr = child.communicate()
45 return_code = child.returncode
46 if return_code:
47 if stderr and stdout:
48 stop_err("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, err, stdout, stderr))
49 else:
50 stop_err("Return code %i from command:\n%s\n%s" % (return_code, err, stderr))
51 #For early diagnostics,
52 else:
53 print stdout
54 print stderr
55
56 if not os.path.isfile(blast2go_jar):
57 stop_err("Blast2GO JAR file not found: %s" % blast2go_jar)
58
59 #We will have write access whereever the output should be,
60 #so we'll ask Blast2GO to use that as the stem for its output
61 #(it will append .annot to the filename)
62 cmd = ["java", "-jar", blast2go_jar,
63 "-in", xml_file,
64 "-prop", prop_file,
65 "-out", tabular_file,
66 "-a"]
67 run(cmd)
68
69 out_file = tabular_file + ".annot"
70 if not os.path.isfile(out_file):
71 stop_err("ERROR - No output annotation file from Blast2GO")
72
73 #Move the output file where Galaxy expects it to be:
74 os.rename(out_file, tabular_file)
75
76 print "Done"