Mercurial > repos > peterjc > blast2go
comparison tools/blast2go/blast2go.py @ 9:887adf823bc0 draft
v0.0.10 - Python 3 compatiblity etc (overdue upload)
author | peterjc |
---|---|
date | Tue, 06 Dec 2022 16:26:16 +0000 |
parents | e23b621eb7bb |
children | 8664c4c94764 |
comparison
equal
deleted
inserted
replaced
8:e23b621eb7bb | 9:887adf823bc0 |
---|---|
28 """ | 28 """ |
29 import sys | 29 import sys |
30 import os | 30 import os |
31 import subprocess | 31 import subprocess |
32 | 32 |
33 #You may need to edit this to match your local setup, | 33 # You may need to edit this to match your local setup, |
34 blast2go_dir = os.environ.get("B2G4PIPE", "/opt/b2g4pipe_v2.5/") | 34 blast2go_dir = os.environ.get("B2G4PIPE", "/opt/b2g4pipe_v2.5/") |
35 blast2go_jar = os.path.join(blast2go_dir, "blast2go.jar") | 35 blast2go_jar = os.path.join(blast2go_dir, "blast2go.jar") |
36 | 36 |
37 def stop_err(msg, error_level=1): | |
38 """Print error message to stdout and quit with given error level.""" | |
39 sys.stderr.write("%s\n" % msg) | |
40 sys.exit(error_level) | |
41 | 37 |
42 try: | 38 try: |
43 from massage_xml_for_blast2go import prepare_xml | 39 from massage_xml_for_blast2go import prepare_xml |
44 except ImportError: | 40 except ImportError: |
45 stop_err("Missing sister file massage_xml_for_blast2go.py") | 41 sys.exit("Missing sister file massage_xml_for_blast2go.py") |
46 | 42 |
47 if len(sys.argv) != 4: | 43 if len(sys.argv) != 4: |
48 stop_err("Require three arguments: XML filename, properties filename, output tabular filename") | 44 sys.exit("Require three arguments: XML filename, properties filename, output tabular filename") |
49 | 45 |
50 xml_file, prop_file, tabular_file = sys.argv[1:] | 46 xml_file, prop_file, tabular_file = sys.argv[1:] |
51 | 47 |
52 #We should have write access here: | 48 # We should have write access here: |
53 tmp_xml_file = tabular_file + ".tmp.xml" | 49 tmp_xml_file = tabular_file + ".tmp.xml" |
54 | 50 |
55 if not os.path.isfile(blast2go_jar): | 51 if not os.path.isfile(blast2go_jar): |
56 stop_err("Blast2GO JAR file not found: %s" % blast2go_jar) | 52 sys.exit("Blast2GO JAR file not found: %s" % blast2go_jar) |
57 | 53 |
58 if not os.path.isfile(xml_file): | 54 if not os.path.isfile(xml_file): |
59 stop_err("Input BLAST XML file not found: %s" % xml_file) | 55 sys.exit("Input BLAST XML file not found: %s" % xml_file) |
60 | 56 |
61 if not os.path.isfile(prop_file): | 57 if not os.path.isfile(prop_file): |
62 tmp = os.path.join(os.path.split(blast2go_jar)[0], prop_file) | 58 tmp = os.path.join(os.path.split(blast2go_jar)[0], prop_file) |
63 if os.path.isfile(tmp): | 59 if os.path.isfile(tmp): |
64 #The properties file seems to have been given relative to the JAR | 60 # The properties file seems to have been given relative to the JAR |
65 prop_file = tmp | 61 prop_file = tmp |
66 else: | 62 else: |
67 stop_err("Blast2GO configuration file not found: %s" % prop_file) | 63 sys.exit("Blast2GO configuration file not found: %s" % prop_file) |
68 del tmp | 64 del tmp |
69 | 65 |
70 | 66 |
71 def run(cmd): | 67 def run(cmd): |
72 #Avoid using shell=True when we call subprocess to ensure if the Python | 68 # Avoid using shell=True when we call subprocess to ensure if the Python |
73 #script is killed, so too is the child process. | 69 # script is killed, so too is the child process. |
74 try: | 70 try: |
75 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 71 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
76 except Exception, err: | 72 except Exception, err: |
77 stop_err("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 73 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
78 #Use .communicate as can get deadlocks with .wait(), | |
79 stdout, stderr = child.communicate() | 74 stdout, stderr = child.communicate() |
80 return_code = child.returncode | 75 return_code = child.returncode |
81 | 76 |
82 #keep stdout minimal as shown prominently in Galaxy | 77 # keep stdout minimal as shown prominently in Galaxy |
83 #Record it in case a silent error needs diagnosis | 78 # Record it in case a silent error needs diagnosis |
84 if stdout: | 79 if stdout: |
85 sys.stderr.write("Standard out:\n%s\n\n" % stdout) | 80 sys.stderr.write("Standard out:\n%s\n\n" % stdout) |
86 if stderr: | 81 if stderr: |
87 sys.stderr.write("Standard error:\n%s\n\n" % stderr) | 82 sys.stderr.write("Standard error:\n%s\n\n" % stderr) |
88 | 83 |
89 error_msg = None | 84 error_msg = None |
90 if return_code: | 85 if return_code: |
91 cmd_str = " ".join(cmd) | 86 cmd_str = " ".join(cmd) |
92 error_msg = "Return code %i from command:\n%s" % (return_code, cmd_str) | 87 error_msg = "Return code %i from command:\n%s" % (return_code, cmd_str) |
93 elif "Database or network connection (timeout) error" in stdout+stderr: | 88 elif "Database or network connection (timeout) error" in stdout + stderr: |
94 error_msg = "Database or network connection (timeout) error" | 89 error_msg = "Database or network connection (timeout) error" |
95 elif "Annotation of 0 seqs with 0 annots finished." in stdout+stderr: | 90 elif "Annotation of 0 seqs with 0 annots finished." in stdout + stderr: |
96 error_msg = "No sequences processed!" | 91 error_msg = "No sequences processed!" |
97 | 92 |
98 if error_msg: | 93 if error_msg: |
99 print error_msg | 94 print error_msg |
100 stop_err(error_msg) | 95 sys.exit(error_msg) |
101 | 96 |
102 | 97 |
103 blast2go_classpath = os.path.split(blast2go_jar)[0] | 98 blast2go_classpath = os.path.split(blast2go_jar)[0] |
104 assert os.path.isdir(blast2go_classpath) | 99 assert os.path.isdir(blast2go_classpath) |
105 blast2go_classpath = "%s/*:%s/ext/*:" % (blast2go_classpath, blast2go_classpath) | 100 blast2go_classpath = "%s/*:%s/ext/*:" % (blast2go_classpath, blast2go_classpath) |
106 | 101 |
107 prepare_xml(xml_file, tmp_xml_file) | 102 prepare_xml(xml_file, tmp_xml_file) |
108 #print "XML file prepared for Blast2GO" | 103 # print "XML file prepared for Blast2GO" |
109 | 104 |
110 #We will have write access wherever the output should be, | 105 # We will have write access wherever the output should be, |
111 #so we'll ask Blast2GO to use that as the stem for its output | 106 # so we'll ask Blast2GO to use that as the stem for its output |
112 #(it will append .annot to the filename) | 107 # (it will append .annot to the filename) |
113 cmd = ["java", "-cp", blast2go_classpath, "es.blast2go.prog.B2GAnnotPipe", | 108 cmd = ["java", "-cp", blast2go_classpath, "es.blast2go.prog.B2GAnnotPipe", |
114 "-in", tmp_xml_file, | 109 "-in", tmp_xml_file, |
115 "-prop", prop_file, | 110 "-prop", prop_file, |
116 "-out", tabular_file, #Used as base name for output files | 111 "-out", tabular_file, # Used as base name for output files |
117 "-annot", # Generate *.annot tabular file | 112 "-annot", # Generate *.annot tabular file |
118 #NOTE: For v2.3.5 must use -a, for v2.5 must use -annot instead | 113 # NOTE: For v2.3.5 must use -a, for v2.5 must use -annot instead |
119 #"-img", # Generate images, feature not in v2.3.5 | 114 # "-img", # Generate images, feature not in v2.3.5 |
120 ] | 115 ] |
121 #print " ".join(cmd) | 116 # print " ".join(cmd) |
122 run(cmd) | 117 run(cmd) |
123 | 118 |
124 #Remove the temp XML file | 119 # Remove the temp XML file |
125 os.remove(tmp_xml_file) | 120 os.remove(tmp_xml_file) |
126 | 121 |
127 out_file = tabular_file + ".annot" | 122 out_file = tabular_file + ".annot" |
128 if not os.path.isfile(out_file): | 123 if not os.path.isfile(out_file): |
129 stop_err("ERROR - No output annotation file from Blast2GO") | 124 sys.exit("ERROR - No output annotation file from Blast2GO") |
130 | 125 |
131 #Move the output file where Galaxy expects it to be: | 126 # Move the output file where Galaxy expects it to be: |
132 os.rename(out_file, tabular_file) | 127 os.rename(out_file, tabular_file) |
133 | 128 |
134 print "Done" | 129 print "Done" |