Mercurial > repos > peterjc > effectivet3
comparison tools/effectiveT3/effectiveT3.py @ 5:1ea715da1879 draft
Uploaded v0.0.13, embed citation, relax test for floating point differences
author | peterjc |
---|---|
date | Tue, 25 Nov 2014 08:28:24 -0500 |
parents | b0b927299aee |
children | 0f6eb4a75000 |
comparison
equal
deleted
inserted
replaced
4:f7ce32e13bc6 | 5:1ea715da1879 |
---|---|
13 """ | 13 """ |
14 import sys | 14 import sys |
15 import os | 15 import os |
16 import subprocess | 16 import subprocess |
17 | 17 |
18 #The Galaxy auto-install via tool_dependencies.xml will set this environment variable | 18 # The Galaxy auto-install via tool_dependencies.xml will set this environment variable |
19 effectiveT3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/") | 19 effectiveT3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/") |
20 effectiveT3_jar = os.path.join(effectiveT3_dir, "TTSS_GUI-1.0.1.jar") | 20 effectiveT3_jar = os.path.join(effectiveT3_dir, "TTSS_GUI-1.0.1.jar") |
21 | 21 |
22 if "-v" in sys.argv or "--version" in sys.argv: | 22 if "-v" in sys.argv or "--version" in sys.argv: |
23 #TODO - Get version of the JAR file dynamically? | 23 # TODO - Get version of the JAR file dynamically? |
24 print "Wrapper v0.0.11, TTSS_GUI-1.0.1.jar" | 24 print("Wrapper v0.0.13, TTSS_GUI-1.0.1.jar") |
25 sys.exit(0) | 25 sys.exit(0) |
26 | 26 |
27 def stop_err(msg, error_level=1): | 27 def stop_err(msg, error_level=1): |
28 """Print error message to stdout and quit with given error level.""" | 28 """Print error message to stdout and quit with given error level.""" |
29 sys.stderr.write("%s\n" % msg) | 29 sys.stderr.write("%s\n" % msg) |
49 for line in raw_handle: | 49 for line in raw_handle: |
50 if not line or line.startswith("#") \ | 50 if not line or line.startswith("#") \ |
51 or line.startswith("Id; Description; Score;"): | 51 or line.startswith("Id; Description; Score;"): |
52 continue | 52 continue |
53 assert line.count(";") >= 3, repr(line) | 53 assert line.count(";") >= 3, repr(line) |
54 #Normally there will just be three semi-colons, however the | 54 # Normally there will just be three semi-colons, however the |
55 #original FASTA file's ID or description might have had | 55 # original FASTA file's ID or description might have had |
56 #semi-colons in it as well, hence the following hackery: | 56 # semi-colons in it as well, hence the following hackery: |
57 try: | 57 try: |
58 id_descr, score, effective = line.rstrip("\r\n").rsplit(";",2) | 58 id_descr, score, effective = line.rstrip("\r\n").rsplit(";",2) |
59 #Cope when there was no FASTA description | 59 # Cope when there was no FASTA description |
60 if "; " not in id_descr and id_descr.endswith(";"): | 60 if "; " not in id_descr and id_descr.endswith(";"): |
61 id = id_descr[:-1] | 61 id = id_descr[:-1] |
62 descr = "" | 62 descr = "" |
63 else: | 63 else: |
64 id, descr = id_descr.split("; ",1) | 64 id, descr = id_descr.split("; ",1) |
72 if effective.lower() == "true": | 72 if effective.lower() == "true": |
73 positive += 1 | 73 positive += 1 |
74 return count, positive, errors | 74 return count, positive, errors |
75 | 75 |
76 def run(cmd): | 76 def run(cmd): |
77 #Avoid using shell=True when we call subprocess to ensure if the Python | 77 # Avoid using shell=True when we call subprocess to ensure if the Python |
78 #script is killed, so too is the child process. | 78 # script is killed, so too is the child process. |
79 try: | 79 try: |
80 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 80 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
81 except Exception, err: | 81 except Exception, err: |
82 stop_err("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) | 82 stop_err("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
83 #Use .communicate as can get deadlocks with .wait(), | 83 # Use .communicate as can get deadlocks with .wait(), |
84 stdout, stderr = child.communicate() | 84 stdout, stderr = child.communicate() |
85 return_code = child.returncode | 85 return_code = child.returncode |
86 if return_code: | 86 if return_code: |
87 cmd_str= " ".join(cmd) # doesn't quote spaces etc | |
87 if stderr and stdout: | 88 if stderr and stdout: |
88 stop_err("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, err, stdout, stderr)) | 89 stop_err("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) |
89 else: | 90 else: |
90 stop_err("Return code %i from command:\n%s\n%s" % (return_code, err, stderr)) | 91 stop_err("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) |
91 | 92 |
92 if not os.path.isdir(effectiveT3_dir): | 93 if not os.path.isdir(effectiveT3_dir): |
93 stop_err("Effective T3 folder not found: %r" % effectiveT3_dir) | 94 stop_err("Effective T3 folder not found: %r" % effectiveT3_dir) |
94 | 95 |
95 if not os.path.isfile(effectiveT3_jar): | 96 if not os.path.isfile(effectiveT3_jar): |
104 % (os.path.join(effectiveT3_dir, "module"), | 105 % (os.path.join(effectiveT3_dir, "module"), |
105 ", ".join(repr(p) for p in os.listdir(os.path.join(effectiveT3_dir, "module"))))) | 106 ", ".join(repr(p) for p in os.listdir(os.path.join(effectiveT3_dir, "module"))))) |
106 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar) | 107 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar) |
107 stop_err("Effective T3 model JAR file not found: %r" % effectiveT3_model) | 108 stop_err("Effective T3 model JAR file not found: %r" % effectiveT3_model) |
108 | 109 |
109 #We will have write access whereever the output should be, | 110 # We will have write access whereever the output should be, |
110 temp_file = os.path.abspath(tabular_file + ".tmp") | 111 temp_file = os.path.abspath(tabular_file + ".tmp") |
111 | 112 |
112 #Use absolute paths since will change current directory... | 113 # Use absolute paths since will change current directory... |
113 tabular_file = os.path.abspath(tabular_file) | 114 tabular_file = os.path.abspath(tabular_file) |
114 fasta_file = os.path.abspath(fasta_file) | 115 fasta_file = os.path.abspath(fasta_file) |
115 | 116 |
116 cmd = ["java", "-jar", effectiveT3_jar, | 117 cmd = ["java", "-jar", effectiveT3_jar, |
117 "-f", fasta_file, | 118 "-f", fasta_file, |
119 "-t", threshold, | 120 "-t", threshold, |
120 "-o", temp_file, | 121 "-o", temp_file, |
121 "-q"] | 122 "-q"] |
122 | 123 |
123 try: | 124 try: |
124 #Must run from directory above the module subfolder: | 125 # Must run from directory above the module subfolder: |
125 os.chdir(effectiveT3_dir) | 126 os.chdir(effectiveT3_dir) |
126 except: | 127 except: |
127 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir) | 128 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir) |
128 | 129 |
129 run(cmd) | 130 run(cmd) |
139 out_handle.close() | 140 out_handle.close() |
140 | 141 |
141 os.remove(temp_file) | 142 os.remove(temp_file) |
142 | 143 |
143 if errors: | 144 if errors: |
144 print "%i sequences, %i positive, %i errors" \ | 145 print("%i sequences, %i positive, %i errors" |
145 % (count, positive, errors) | 146 % (count, positive, errors)) |
146 else: | 147 else: |
147 print "%i/%i sequences positive" % (positive, count) | 148 print("%i/%i sequences positive" % (positive, count)) |
148 | 149 |
149 if count and count==errors: | 150 if count and count==errors: |
150 #Galaxy will still allow them to see the output file | 151 # Galaxy will still allow them to see the output file |
151 stop_err("All your sequences gave an error code") | 152 stop_err("All your sequences gave an error code") |