Mercurial > repos > peterjc > tmhmm_and_signalp
annotate tools/protein_analysis/psortb.py @ 30:6d9d7cdf00fc draft
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
author | peterjc |
---|---|
date | Thu, 21 Sep 2017 11:15:55 -0400 |
parents | 3cb02adf4326 |
children | 20da7f48b56f |
rev | line source |
---|---|
8 | 1 #!/usr/bin/env python |
2 """Wrapper for psortb for use in Galaxy. | |
3 | |
4 This script takes exactly six command line arguments - which includes the | |
5 number of threads, and the input protein FASTA filename and output | |
6 tabular filename. It then splits up the FASTA input and calls multiple | |
7 copies of the standalone psortb v3 program, then collates the output. | |
8 e.g. Rather than this, | |
9 | |
10 psort $type -c $cutoff -d $divergent -o long $sequence > $outfile | |
11 | |
12 Call this: | |
13 | |
14 psort $threads $type $cutoff $divergent $sequence $outfile | |
15 | |
16 If ommitting -c or -d options, set $cutoff and $divergent to zero or blank. | |
17 | |
18 Note that this is somewhat redundant with job-splitting available in Galaxy | |
19 itself (see the SignalP XML file for settings), but both can be applied. | |
20 | |
21 Additionally it ensures the header line (with the column names) starts | |
22 with a # character as used elsewhere in Galaxy. | |
23 """ | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
24 |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
25 from __future__ import print_function |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
26 |
8 | 27 import os |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
28 import sys |
8 | 29 import tempfile |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
30 |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
31 from seq_analysis_utils import run_jobs, split_fasta, thread_count |
8 | 32 |
33 FASTA_CHUNK = 500 | |
34 | |
35 if "-v" in sys.argv or "--version" in sys.argv: | |
36 """Return underlying PSORTb's version""" | |
37 sys.exit(os.system("psort --version")) | |
38 | |
39 if len(sys.argv) != 8: | |
29 | 40 sys.exit("Require 7 arguments, number of threads (int), type (e.g. archaea), " |
8 | 41 "output (e.g. terse/normal/long), cutoff, divergent, input protein " |
42 "FASTA file & output tabular file") | |
43 | |
44 num_threads = thread_count(sys.argv[1], default=4) | |
45 org_type = sys.argv[2] | |
46 out_type = sys.argv[3] | |
47 cutoff = sys.argv[4] | |
48 if cutoff.strip() and float(cutoff.strip()) != 0.0: | |
49 cutoff = "-c %s" % cutoff | |
50 else: | |
51 cutoff = "" | |
52 divergent = sys.argv[5] | |
53 if divergent.strip() and float(divergent.strip()) != 0.0: | |
54 divergent = "-d %s" % divergent | |
55 else: | |
56 divergent = "" | |
57 fasta_file = sys.argv[6] | |
58 tabular_file = sys.argv[7] | |
59 | |
60 if out_type == "terse": | |
61 header = ['SeqID', 'Localization', 'Score'] | |
62 elif out_type == "normal": | |
29 | 63 sys.exit("Normal output not implemented yet, sorry.") |
8 | 64 elif out_type == "long": |
65 if org_type == "-n": | |
29 | 66 # Gram negative bacteria |
8 | 67 header = ['SeqID', 'CMSVM-_Localization', 'CMSVM-_Details', 'CytoSVM-_Localization', 'CytoSVM-_Details', |
68 'ECSVM-_Localization', 'ECSVM-_Details', 'ModHMM-_Localization', 'ModHMM-_Details', | |
69 'Motif-_Localization', 'Motif-_Details', 'OMPMotif-_Localization', 'OMPMotif-_Details', | |
70 'OMSVM-_Localization', 'OMSVM-_Details', 'PPSVM-_Localization', 'PPSVM-_Details', | |
71 'Profile-_Localization', 'Profile-_Details', | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
72 'SCL-BLAST-_Localization', 'SCL-BLAST-_Details', |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
73 'SCL-BLASTe-_Localization', 'SCL-BLASTe-_Details', |
8 | 74 'Signal-_Localization', 'Signal-_Details', |
75 'Cytoplasmic_Score', 'CytoplasmicMembrane_Score', 'Periplasmic_Score', 'OuterMembrane_Score', | |
76 'Extracellular_Score', 'Final_Localization', 'Final_Localization_Details', 'Final_Score', | |
77 'Secondary_Localization', 'PSortb_Version'] | |
78 elif org_type == "-p": | |
29 | 79 # Gram positive bacteria |
8 | 80 header = ['SeqID', 'CMSVM+_Localization', 'CMSVM+_Details', 'CWSVM+_Localization', 'CWSVM+_Details', |
81 'CytoSVM+_Localization', 'CytoSVM+_Details', 'ECSVM+_Localization', 'ECSVM+_Details', | |
82 'ModHMM+_Localization', 'ModHMM+_Details', 'Motif+_Localization', 'Motif+_Details', | |
83 'Profile+_Localization', 'Profile+_Details', | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
84 'SCL-BLAST+_Localization', 'SCL-BLAST+_Details', |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
85 'SCL-BLASTe+_Localization', 'SCL-BLASTe+_Details', |
8 | 86 'Signal+_Localization', 'Signal+_Details', |
87 'Cytoplasmic_Score', 'CytoplasmicMembrane_Score', 'Cellwall_Score', | |
88 'Extracellular_Score', 'Final_Localization', 'Final_Localization_Details', 'Final_Score', | |
89 'Secondary_Localization', 'PSortb_Version'] | |
90 elif org_type == "-a": | |
29 | 91 # Archaea |
8 | 92 header = ['SeqID', 'CMSVM_a_Localization', 'CMSVM_a_Details', 'CWSVM_a_Localization', 'CWSVM_a_Details', |
93 'CytoSVM_a_Localization', 'CytoSVM_a_Details', 'ECSVM_a_Localization', 'ECSVM_a_Details', | |
94 'ModHMM_a_Localization', 'ModHMM_a_Details', 'Motif_a_Localization', 'Motif_a_Details', | |
95 'Profile_a_Localization', 'Profile_a_Details', | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
96 'SCL-BLAST_a_Localization', 'SCL-BLAST_a_Details', |
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
97 'SCL-BLASTe_a_Localization', 'SCL-BLASTe_a_Details', |
8 | 98 'Signal_a_Localization', 'Signal_a_Details', |
99 'Cytoplasmic_Score', 'CytoplasmicMembrane_Score', 'Cellwall_Score', | |
100 'Extracellular_Score', 'Final_Localization', 'Final_Localization_Details', 'Final_Score', | |
101 'Secondary_Localization', 'PSortb_Version'] | |
102 else: | |
29 | 103 sys.exit("Expected -n, -p or -a for the organism type, not %r" % org_type) |
8 | 104 else: |
29 | 105 sys.exit("Expected terse, normal or long for the output type, not %r" % out_type) |
8 | 106 |
107 tmp_dir = tempfile.mkdtemp() | |
108 | |
29 | 109 |
8 | 110 def clean_tabular(raw_handle, out_handle): |
111 """Clean up tabular TMHMM output, returns output line count.""" | |
112 global header | |
113 count = 0 | |
114 for line in raw_handle: | |
115 if not line.strip() or line.startswith("#"): | |
29 | 116 # Ignore any blank lines or comment lines |
8 | 117 continue |
118 parts = [x.strip() for x in line.rstrip("\r\n").split("\t")] | |
119 if parts == header: | |
29 | 120 # Ignore the header line |
8 | 121 continue |
122 if not parts[-1] and len(parts) == len(header) + 1: | |
29 | 123 # Ignore dummy blank extra column, e.g. |
124 # "...2.0\t\tPSORTb version 3.0\t\n" | |
8 | 125 parts = parts[:-1] |
126 assert len(parts) == len(header), \ | |
127 "%i fields, not %i, in line:\n%r" % (len(line), len(header), line) | |
128 out_handle.write(line) | |
129 count += 1 | |
130 return count | |
131 | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
132 |
29 | 133 # Note that if the input FASTA file contains no sequences, |
134 # split_fasta returns an empty list (i.e. zero temp files). | |
8 | 135 fasta_files = split_fasta(fasta_file, os.path.join(tmp_dir, "tmhmm"), FASTA_CHUNK) |
29 | 136 temp_files = [f + ".out" for f in fasta_files] |
8 | 137 jobs = ["psort %s %s %s -o %s %s > %s" % (org_type, cutoff, divergent, out_type, fasta, temp) |
138 for fasta, temp in zip(fasta_files, temp_files)] | |
139 | |
29 | 140 |
8 | 141 def clean_up(file_list): |
142 for f in file_list: | |
143 if os.path.isfile(f): | |
144 os.remove(f) | |
145 try: | |
146 os.rmdir(tmp_dir) | |
29 | 147 except Exception: |
8 | 148 pass |
149 | |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
150 |
8 | 151 if len(jobs) > 1 and num_threads > 1: |
29 | 152 # A small "info" message for Galaxy to show the user. |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
153 print("Using %i threads for %i tasks" % (min(num_threads, len(jobs)), len(jobs))) |
8 | 154 results = run_jobs(jobs, num_threads) |
155 for fasta, temp, cmd in zip(fasta_files, temp_files, jobs): | |
156 error_level = results[cmd] | |
157 if error_level: | |
158 try: | |
159 output = open(temp).readline() | |
160 except IOError: | |
161 output = "" | |
162 clean_up(fasta_files + temp_files) | |
29 | 163 sys.exit("One or more tasks failed, e.g. %i from %r gave:\n%s" % (error_level, cmd, output), |
8 | 164 error_level) |
165 del results | |
166 del jobs | |
167 | |
168 out_handle = open(tabular_file, "w") | |
169 out_handle.write("#%s\n" % "\t".join(header)) | |
11 | 170 count = 0 |
8 | 171 for temp in temp_files: |
172 data_handle = open(temp) | |
11 | 173 count += clean_tabular(data_handle, out_handle) |
8 | 174 data_handle.close() |
175 if not count: | |
176 clean_up(fasta_files + temp_files) | |
29 | 177 sys.exit("No output from psortb") |
8 | 178 out_handle.close() |
30
6d9d7cdf00fc
v0.2.11 Job splitting fast-fail; RXLR tools supports HMMER2 from BioConda; Capture more version information; misc internal changes
peterjc
parents:
29
diff
changeset
|
179 print("%i records" % count) |
8 | 180 |
181 clean_up(fasta_files + temp_files) |