Mercurial > repos > peterjc > effectivet3
annotate tools/effectiveT3/effectiveT3.py @ 29:35c358aa7801 draft
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 37d5b47ec23e2cbaa453cc660bb1fcbb10dd34ee-dirty
| author | peterjc |
|---|---|
| date | Wed, 17 May 2017 11:16:58 -0400 |
| parents | af14f755c05b |
| children | 007e86eff414 |
| rev | line source |
|---|---|
| 2 | 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 os | |
| 15 import subprocess | |
|
25
69ba0f4e91af
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 5fb6ee31f33e8e24c1ae5d56c93ac3e0157dad72-dirty
peterjc
parents:
24
diff
changeset
|
16 import sys |
| 2 | 17 |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
18 # The Galaxy auto-install via tool_dependencies.xml will set this environment variable |
| 23 | 19 effective_t3_dir = os.environ.get("EFFECTIVET3", "/opt/EffectiveT3/") |
| 20 effective_t3_jar = os.path.join(effective_t3_dir, "TTSS_GUI-1.0.1.jar") | |
| 2 | 21 |
| 22 if "-v" in sys.argv or "--version" in sys.argv: | |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
23 # TODO - Get version of the JAR file dynamically? |
|
29
35c358aa7801
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 37d5b47ec23e2cbaa453cc660bb1fcbb10dd34ee-dirty
peterjc
parents:
27
diff
changeset
|
24 print("Wrapper v0.0.19, TTSS_GUI-1.0.1.jar") |
| 2 | 25 sys.exit(0) |
| 26 | |
| 27 if len(sys.argv) != 5: | |
| 23 | 28 sys.exit("Require four arguments: model, threshold, input protein FASTA file & output tabular file") |
| 2 | 29 |
| 30 model, threshold, fasta_file, tabular_file = sys.argv[1:] | |
| 31 | |
| 32 if not os.path.isfile(fasta_file): | |
| 23 | 33 sys.exit("Input FASTA file not found: %s" % fasta_file) |
| 2 | 34 |
|
24
a3d660e900b1
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 7a746d55f0e8456992ee11c49a507285f9f39383-dirty
peterjc
parents:
23
diff
changeset
|
35 if threshold not in ["selective", "sensitive"] and not threshold.startswith("cutoff="): |
| 23 | 36 sys.exit("Threshold should be selective, sensitive, or cutoff=..., not %r" % threshold) |
| 37 | |
| 2 | 38 |
| 39 def clean_tabular(raw_handle, out_handle): | |
| 40 """Clean up Effective T3 output to make it tabular.""" | |
| 41 count = 0 | |
| 42 positive = 0 | |
| 43 errors = 0 | |
| 44 for line in raw_handle: | |
|
24
a3d660e900b1
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 7a746d55f0e8456992ee11c49a507285f9f39383-dirty
peterjc
parents:
23
diff
changeset
|
45 if not line or line.startswith("#") or line.startswith("Id; Description; Score;"): |
| 2 | 46 continue |
| 47 assert line.count(";") >= 3, repr(line) | |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
48 # Normally there will just be three semi-colons, however the |
|
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
49 # original FASTA file's ID or description might have had |
|
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
50 # semi-colons in it as well, hence the following hackery: |
| 2 | 51 try: |
| 23 | 52 id_descr, score, effective = line.rstrip("\r\n").rsplit(";", 2) |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
53 # Cope when there was no FASTA description |
| 2 | 54 if "; " not in id_descr and id_descr.endswith(";"): |
| 55 id = id_descr[:-1] | |
| 56 descr = "" | |
| 57 else: | |
| 23 | 58 id, descr = id_descr.split("; ", 1) |
| 2 | 59 except ValueError: |
| 23 | 60 sys.exit("Problem parsing line:\n%s\n" % line) |
| 2 | 61 parts = [s.strip() for s in [id, descr, score, effective]] |
| 62 out_handle.write("\t".join(parts) + "\n") | |
| 63 count += 1 | |
| 64 if float(score) < 0: | |
| 23 | 65 errors += 1 |
| 2 | 66 if effective.lower() == "true": |
| 23 | 67 positive += 1 |
| 2 | 68 return count, positive, errors |
| 69 | |
| 23 | 70 |
| 2 | 71 def run(cmd): |
|
27
af14f755c05b
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 7496128ee9ef800fe780e545d2157da8a4f89d35-dirty
peterjc
parents:
25
diff
changeset
|
72 """Run the command line string via subprocess.""" |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
73 # Avoid using shell=True when we call subprocess to ensure if the Python |
|
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
74 # script is killed, so too is the child process. |
| 2 | 75 try: |
| 76 child = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
|
29
35c358aa7801
planemo upload for repository https://github.com/peterjc/pico_galaxy/tree/master/tools/effectiveT3 commit 37d5b47ec23e2cbaa453cc660bb1fcbb10dd34ee-dirty
peterjc
parents:
27
diff
changeset
|
77 except Exception as err: |
| 23 | 78 sys.exit("Error invoking command:\n%s\n\n%s\n" % (" ".join(cmd), err)) |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
79 # Use .communicate as can get deadlocks with .wait(), |
| 2 | 80 stdout, stderr = child.communicate() |
| 81 return_code = child.returncode | |
| 18 | 82 if return_code or stderr.startswith("Exception in thread"): |
| 23 | 83 cmd_str = " ".join(cmd) # doesn't quote spaces etc |
| 2 | 84 if stderr and stdout: |
| 23 | 85 sys.exit("Return code %i from command:\n%s\n\n%s\n\n%s" % (return_code, cmd_str, stdout, stderr)) |
| 2 | 86 else: |
| 23 | 87 sys.exit("Return code %i from command:\n%s\n%s" % (return_code, cmd_str, stderr)) |
| 2 | 88 |
| 18 | 89 |
| 23 | 90 if not os.path.isdir(effective_t3_dir): |
| 91 sys.exit("Effective T3 folder not found: %r" % effective_t3_dir) | |
| 2 | 92 |
| 23 | 93 if not os.path.isfile(effective_t3_jar): |
| 94 sys.exit("Effective T3 JAR file not found: %r" % effective_t3_jar) | |
| 2 | 95 |
| 23 | 96 if not os.path.isdir(os.path.join(effective_t3_dir, "module")): |
| 97 sys.exit("Effective T3 module folder not found: %r" % os.path.join(effective_t3_dir, "module")) | |
|
9
b863c4bec840
Uploaded v0.0.11, take 10, debug output if module folder missing
peterjc
parents:
8
diff
changeset
|
98 |
| 23 | 99 effective_t3_model = os.path.join(effective_t3_dir, "module", model) |
| 100 if not os.path.isfile(effective_t3_model): | |
|
8
4a0aa59062f7
Uploaded v0.0.11, take 9, debug output if can't find model
peterjc
parents:
2
diff
changeset
|
101 sys.stderr.write("Contents of %r is %s\n" |
| 23 | 102 % (os.path.join(effective_t3_dir, "module"), |
| 103 ", ".join(repr(p) for p in os.listdir(os.path.join(effective_t3_dir, "module"))))) | |
| 104 sys.stderr.write("Main JAR was found: %r\n" % effective_t3_jar) | |
| 105 sys.exit("Effective T3 model JAR file not found: %r" % effective_t3_model) | |
| 2 | 106 |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
107 # We will have write access whereever the output should be, |
| 2 | 108 temp_file = os.path.abspath(tabular_file + ".tmp") |
| 109 | |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
110 # Use absolute paths since will change current directory... |
| 2 | 111 tabular_file = os.path.abspath(tabular_file) |
| 112 fasta_file = os.path.abspath(fasta_file) | |
| 113 | |
| 23 | 114 cmd = ["java", "-jar", effective_t3_jar, |
| 2 | 115 "-f", fasta_file, |
| 116 "-m", model, | |
| 117 "-t", threshold, | |
| 118 "-o", temp_file, | |
| 119 "-q"] | |
| 120 | |
| 121 try: | |
|
15
17fbf8b8a554
Uploaded v0.0.13, embed citation, relax test for floating point differences
peterjc
parents:
9
diff
changeset
|
122 # Must run from directory above the module subfolder: |
| 23 | 123 os.chdir(effective_t3_dir) |
| 124 except Exception: | |
| 125 sys.exit("Could not change to Effective T3 folder: %s" % effective_t3_dir) | |
| 2 | 126 |
| 127 run(cmd) | |
| 128 | |
| 129 if not os.path.isfile(temp_file): | |
| 23 | 130 sys.exit("ERROR - No output file from Effective T3") |
| 2 | 131 |
| 132 out_handle = open(tabular_file, "w") | |
| 133 out_handle.write("#ID\tDescription\tScore\tEffective\n") | |
| 134 data_handle = open(temp_file) | |
| 135 count, positive, errors = clean_tabular(data_handle, out_handle) | |
| 136 data_handle.close() | |
| 137 out_handle.close() | |
| 138 | |
| 139 os.remove(temp_file) | |
| 140 | |
| 141 if errors: | |
| 23 | 142 print("%i sequences, %i positive, %i errors" |
| 143 % (count, positive, errors)) | |
| 2 | 144 else: |
| 23 | 145 print("%i/%i sequences positive" % (positive, count)) |
| 2 | 146 |
| 23 | 147 if count and count == errors: |
| 148 # Galaxy will still allow them to see the output file | |
| 149 sys.exit("All your sequences gave an error code") |
