comparison tools/effectiveT3/effectiveT3.py @ 6:0f6eb4a75000 draft

v0.0.15 internal changes; v0.0.14 fixed error handling
author peterjc
date Wed, 05 Aug 2015 11:04:42 -0400
parents 1ea715da1879
children 5f85301d50bf
comparison
equal deleted inserted replaced
5:1ea715da1879 6:0f6eb4a75000
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.13, TTSS_GUI-1.0.1.jar") 24 print("Wrapper v0.0.14, 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 sys_exit(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)
30 sys.exit(error_level) 30 sys.exit(error_level)
31 31
32 if len(sys.argv) != 5: 32 if len(sys.argv) != 5:
33 stop_err("Require four arguments: model, threshold, input protein FASTA file & output tabular file") 33 sys_exit("Require four arguments: model, threshold, input protein FASTA file & output tabular file")
34 34
35 model, threshold, fasta_file, tabular_file = sys.argv[1:] 35 model, threshold, fasta_file, tabular_file = sys.argv[1:]
36 36
37 if not os.path.isfile(fasta_file): 37 if not os.path.isfile(fasta_file):
38 stop_err("Input FASTA file not found: %s" % fasta_file) 38 sys_exit("Input FASTA file not found: %s" % fasta_file)
39 39
40 if threshold not in ["selective", "sensitive"] \ 40 if threshold not in ["selective", "sensitive"] \
41 and not threshold.startswith("cutoff="): 41 and not threshold.startswith("cutoff="):
42 stop_err("Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold) 42 sys_exit("Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold)
43 43
44 def clean_tabular(raw_handle, out_handle): 44 def clean_tabular(raw_handle, out_handle):
45 """Clean up Effective T3 output to make it tabular.""" 45 """Clean up Effective T3 output to make it tabular."""
46 count = 0 46 count = 0
47 positive = 0 47 positive = 0
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)
65 except ValueError: 65 except ValueError:
66 stop_err("Problem parsing line:\n%s\n" % line) 66 sys_exit("Problem parsing line:\n%s\n" % line)
67 parts = [s.strip() for s in [id, descr, score, effective]] 67 parts = [s.strip() for s in [id, descr, score, effective]]
68 out_handle.write("\t".join(parts) + "\n") 68 out_handle.write("\t".join(parts) + "\n")
69 count += 1 69 count += 1
70 if float(score) < 0: 70 if float(score) < 0:
71 errors += 1 71 errors += 1
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 sys_exit("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 cmd_str= " ".join(cmd) # doesn't quote spaces etc
88 if stderr and stdout: 88 if stderr and stdout:
89 stop_err("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) 89 sys_exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr))
90 else: 90 else:
91 stop_err("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) 91 sys_exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr))
92 92
93 if not os.path.isdir(effectiveT3_dir): 93 if not os.path.isdir(effectiveT3_dir):
94 stop_err("Effective T3 folder not found: %r" % effectiveT3_dir) 94 sys_exit("Effective T3 folder not found: %r" % effectiveT3_dir)
95 95
96 if not os.path.isfile(effectiveT3_jar): 96 if not os.path.isfile(effectiveT3_jar):
97 stop_err("Effective T3 JAR file not found: %r" % effectiveT3_jar) 97 sys_exit("Effective T3 JAR file not found: %r" % effectiveT3_jar)
98 98
99 if not os.path.isdir(os.path.join(effectiveT3_dir, "module")): 99 if not os.path.isdir(os.path.join(effectiveT3_dir, "module")):
100 stop_err("Effective T3 module folder not found: %r" % os.path.join(effectiveT3_dir, "module")) 100 sys_exit("Effective T3 module folder not found: %r" % os.path.join(effectiveT3_dir, "module"))
101 101
102 effectiveT3_model = os.path.join(effectiveT3_dir, "module", model) 102 effectiveT3_model = os.path.join(effectiveT3_dir, "module", model)
103 if not os.path.isfile(effectiveT3_model): 103 if not os.path.isfile(effectiveT3_model):
104 sys.stderr.write("Contents of %r is %s\n" 104 sys.stderr.write("Contents of %r is %s\n"
105 % (os.path.join(effectiveT3_dir, "module"), 105 % (os.path.join(effectiveT3_dir, "module"),
106 ", ".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")))))
107 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar) 107 sys.stderr.write("Main JAR was found: %r\n" % effectiveT3_jar)
108 stop_err("Effective T3 model JAR file not found: %r" % effectiveT3_model) 108 sys_exit("Effective T3 model JAR file not found: %r" % effectiveT3_model)
109 109
110 # We will have write access whereever the output should be, 110 # We will have write access whereever the output should be,
111 temp_file = os.path.abspath(tabular_file + ".tmp") 111 temp_file = os.path.abspath(tabular_file + ".tmp")
112 112
113 # Use absolute paths since will change current directory... 113 # Use absolute paths since will change current directory...
123 123
124 try: 124 try:
125 # Must run from directory above the module subfolder: 125 # Must run from directory above the module subfolder:
126 os.chdir(effectiveT3_dir) 126 os.chdir(effectiveT3_dir)
127 except: 127 except:
128 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir) 128 sys_exit("Could not change to Effective T3 folder: %s" % effectiveT3_dir)
129 129
130 run(cmd) 130 run(cmd)
131 131
132 if not os.path.isfile(temp_file): 132 if not os.path.isfile(temp_file):
133 stop_err("ERROR - No output file from Effective T3") 133 sys_exit("ERROR - No output file from Effective T3")
134 134
135 out_handle = open(tabular_file, "w") 135 out_handle = open(tabular_file, "w")
136 out_handle.write("#ID\tDescription\tScore\tEffective\n") 136 out_handle.write("#ID\tDescription\tScore\tEffective\n")
137 data_handle = open(temp_file) 137 data_handle = open(temp_file)
138 count, positive, errors = clean_tabular(data_handle, out_handle) 138 count, positive, errors = clean_tabular(data_handle, out_handle)
147 else: 147 else:
148 print("%i/%i sequences positive" % (positive, count)) 148 print("%i/%i sequences positive" % (positive, count))
149 149
150 if count and count==errors: 150 if count and count==errors:
151 # Galaxy will still allow them to see the output file 151 # Galaxy will still allow them to see the output file
152 stop_err("All your sequences gave an error code") 152 sys_exit("All your sequences gave an error code")