comparison tools/effectiveT3/effectiveT3.py @ 2:66e9d4c44ca2 draft

Uploaded v0.0.11, auto installation
author peterjc
date Tue, 30 Apr 2013 09:32:56 -0400
parents
children 4a0aa59062f7
comparison
equal deleted inserted replaced
1:7479dbb285b5 2:66e9d4c44ca2
1 #!/usr/bin/env python
2 """Wrapper for EffectiveT3 v1.0.1 for use in Galaxy.
3
4 This script takes exactly five command line arguments:
5 * model name (e.g. TTSS_STD-1.0.1.jar)
6 * threshold (selective or sensitive)
7 * an input protein FASTA filename
8 * output tabular filename
9
10 It then calls the standalone Effective T3 v1.0.1 program (not the
11 webservice), and reformats the semi-colon separated output into
12 tab separated output for use in Galaxy.
13 """
14 import sys
15 import os
16 import subprocess
17
18 #The Galaxy auto-install via tool_dependencies.xml will set this environment variable
19 effectiveT3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/")
20 effectiveT3_jar = os.path.join(effectiveT3_dir, "TTSS_GUI-1.0.1.jar")
21
22 if "-v" in sys.argv or "--version" in sys.argv:
23 #TODO - Get version of the JAR file dynamically?
24 print "Wrapper v0.0.11, TTSS_GUI-1.0.1.jar"
25 sys.exit(0)
26
27 def stop_err(msg, error_level=1):
28 """Print error message to stdout and quit with given error level."""
29 sys.stderr.write("%s\n" % msg)
30 sys.exit(error_level)
31
32 if len(sys.argv) != 5:
33 stop_err("Require four arguments: model, threshold, input protein FASTA file & output tabular file")
34
35 model, threshold, fasta_file, tabular_file = sys.argv[1:]
36
37 if not os.path.isfile(fasta_file):
38 stop_err("Input FASTA file not found: %s" % fasta_file)
39
40 if threshold not in ["selective", "sensitive"] \
41 and not threshold.startswith("cutoff="):
42 stop_err("Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold)
43
44 def clean_tabular(raw_handle, out_handle):
45 """Clean up Effective T3 output to make it tabular."""
46 count = 0
47 positive = 0
48 errors = 0
49 for line in raw_handle:
50 if not line or line.startswith("#") \
51 or line.startswith("Id; Description; Score;"):
52 continue
53 assert line.count(";") >= 3, repr(line)
54 #Normally there will just be three semi-colons, however the
55 #original FASTA file's ID or description might have had
56 #semi-colons in it as well, hence the following hackery:
57 try:
58 id_descr, score, effective = line.rstrip("\r\n").rsplit(";",2)
59 #Cope when there was no FASTA description
60 if "; " not in id_descr and id_descr.endswith(";"):
61 id = id_descr[:-1]
62 descr = ""
63 else:
64 id, descr = id_descr.split("; ",1)
65 except ValueError:
66 stop_err("Problem parsing line:\n%s\n" % line)
67 parts = [s.strip() for s in [id, descr, score, effective]]
68 out_handle.write("\t".join(parts) + "\n")
69 count += 1
70 if float(score) < 0:
71 errors += 1
72 if effective.lower() == "true":
73 positive += 1
74 return count, positive, errors
75
76 def run(cmd):
77 #Avoid using shell=True when we call subprocess to ensure if the Python
78 #script is killed, so too is the child process.
79 try:
80 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
81 except Exception, 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(),
84 stdout, stderr = child.communicate()
85 return_code = child.returncode
86 if return_code:
87 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 else:
90 stop_err("Return code %i from command:\n%s\n%s" % (return_code, err, stderr))
91
92 if not os.path.isdir(effectiveT3_dir):
93 stop_err("Effective T3 folder not found: %s" % effectiveT3_dir)
94
95 if not os.path.isfile(effectiveT3_jar):
96 stop_err("Effective T3 JAR file not found: %s" % effectiveT3_jar)
97
98 effectiveT3_model = os.path.join(effectiveT3_dir, "module", model)
99 if not os.path.isfile(effectiveT3_model):
100 stop_err("Effective T3 model JAR file not found: %s" % effectiveT3_model)
101
102 #We will have write access whereever the output should be,
103 temp_file = os.path.abspath(tabular_file + ".tmp")
104
105 #Use absolute paths since will change current directory...
106 tabular_file = os.path.abspath(tabular_file)
107 fasta_file = os.path.abspath(fasta_file)
108
109 cmd = ["java", "-jar", effectiveT3_jar,
110 "-f", fasta_file,
111 "-m", model,
112 "-t", threshold,
113 "-o", temp_file,
114 "-q"]
115
116 try:
117 #Must run from directory above the module subfolder:
118 os.chdir(effectiveT3_dir)
119 except:
120 stop_err("Could not change to Effective T3 folder: %s" % effectiveT3_dir)
121
122 run(cmd)
123
124 if not os.path.isfile(temp_file):
125 stop_err("ERROR - No output file from Effective T3")
126
127 out_handle = open(tabular_file, "w")
128 out_handle.write("#ID\tDescription\tScore\tEffective\n")
129 data_handle = open(temp_file)
130 count, positive, errors = clean_tabular(data_handle, out_handle)
131 data_handle.close()
132 out_handle.close()
133
134 os.remove(temp_file)
135
136 if errors:
137 print "%i sequences, %i positive, %i errors" \
138 % (count, positive, errors)
139 else:
140 print "%i/%i sequences positive" % (positive, count)
141
142 if count and count==errors:
143 #Galaxy will still allow them to see the output file
144 stop_err("All your sequences gave an error code")