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