Mercurial > repos > peterjc > tmhmm_and_signalp
annotate tools/protein_analysis/wolf_psort.py @ 34:7a2e20baacee draft default tip
"v0.2.13 - Python 3 fix for raising StopIteration"
author | peterjc |
---|---|
date | Thu, 17 Jun 2021 17:58:23 +0000 |
parents | 20da7f48b56f |
children |
rev | line source |
---|---|
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
2 """Wrapper for WoLF PSORT v0.2 for use in Galaxy. |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
3 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
4 This script takes exactly four command line arguments: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
5 * the organism type (animal, plant or fungi) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
6 * number of threads to use (integer) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
7 * an input protein FASTA filename |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
8 * output tabular filename. |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
9 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
10 It then calls the standalone WoLF PSORT v0.2 program runWolfPsortSummary |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
11 (not the webservice), and coverts the output from something like this: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
12 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
13 # k used for kNN is: 27 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
14 gi|301087619|ref|XP_002894699.1| extr 12, mito 4, E.R. 3, golg 3, mito_nucl 3 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
15 gi|301087623|ref|XP_002894700.1| extr 21, mito 2, cyto 2, cyto_mito 2 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
16 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
17 In order to make it easier to use in Galaxy, this wrapper script reformats |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
18 this to use tab separators, with one line per compartment prediction: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
19 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
20 #ID Compartment Score Rank |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
21 gi|301087619|ref|XP_002894699.1| extr 12 1 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
22 gi|301087619|ref|XP_002894699.1| mito 4 2 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
23 gi|301087619|ref|XP_002894699.1| E.R. 3 3 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
24 gi|301087619|ref|XP_002894699.1| golg 3 4 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
25 gi|301087619|ref|XP_002894699.1| mito_nucl 3 5 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
26 gi|301087623|ref|XP_002894700.1| extr 21 1 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
27 gi|301087623|ref|XP_002894700.1| mito 2 2 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
28 gi|301087623|ref|XP_002894700.1| cyto 2 3 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
29 gi|301087623|ref|XP_002894700.1| cyto_mito 2 4 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
30 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
31 Additionally in order to take full advantage of multiple cores, by subdividing |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
32 the input FASTA file multiple copies of WoLF PSORT are run in parallel. I would |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
33 normally use Python's multiprocessing library in this situation but it requires |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
34 at least Python 2.6 and at the time of writing Galaxy still supports Python 2.4. |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
35 """ |
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
|
36 |
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
|
37 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
|
38 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
39 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
|
40 import sys |
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
|
41 |
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
|
42 from seq_analysis_utils import run_jobs, split_fasta, thread_count |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
43 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
44 FASTA_CHUNK = 500 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
45 exe = "runWolfPsortSummary" |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
46 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
47 """ |
7 | 48 Note: I had trouble getting runWolfPsortSummary on the path (via a link), other |
6
39a6e46cdda3
Migrated tool version 0.0.9 from old tool shed archive to new tool shed repository
peterjc
parents:
5
diff
changeset
|
49 than by including all of /opt/WoLFPSORT_package_v0.2/bin , so used a wrapper |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
50 python script called runWolfPsortSummary as follows: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
51 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
52 #!/usr/bin/env python |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
53 #Wrapper script to call WoLF PSORT from its own directory. |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
54 import os |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
55 import sys |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
56 import subprocess |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
57 saved_dir = os.path.abspath(os.curdir) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
58 os.chdir("/opt/WoLFPSORT_package_v0.2/bin") |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
59 args = ["./runWolfPsortSummary"] + sys.argv[1:] |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
60 return_code = subprocess.call(args) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
61 os.chdir(saved_dir) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
62 sys.exit(return_code) |
29 | 63 |
64 For more details on this workaround, see: | |
65 https://lists.galaxyproject.org/pipermail/galaxy-dev/2015-December/023386.html | |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
66 """ |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
67 |
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
|
68 if "-v" in sys.argv or "--version" in sys.argv: |
34 | 69 sys.exit("WoLF-PSORT wrapper version 0.0.12") |
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
|
70 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
71 if len(sys.argv) != 5: |
32 | 72 sys.exit( |
73 "Require four arguments, organism, threads, input protein FASTA file, " | |
74 "and output tabular file" | |
75 ) | |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
76 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
77 organism = sys.argv[1] |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
78 if organism not in ["animal", "plant", "fungi"]: |
29 | 79 sys.exit("Organism argument %s is not one of animal, plant, fungi" % organism) |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
80 |
7 | 81 num_threads = thread_count(sys.argv[2], default=4) |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
82 fasta_file = sys.argv[3] |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
83 tabular_file = sys.argv[4] |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
84 |
29 | 85 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
86 def clean_tabular(raw_handle, out_handle): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
87 """Clean up WoLF PSORT output to make it tabular.""" |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
88 for line in raw_handle: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
89 if not line or line.startswith("#"): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
90 continue |
29 | 91 name, data = line.rstrip("\r\n").split(None, 1) |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
92 for rank, comp_data in enumerate(data.split(",")): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
93 comp, score = comp_data.split() |
32 | 94 out_handle.write("%s\t%s\t%s\t%i\n" % (name, comp, score, rank + 1)) |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
95 |
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 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
97 fasta_files = split_fasta(fasta_file, tabular_file, n=FASTA_CHUNK) |
29 | 98 temp_files = [f + ".out" for f in fasta_files] |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
99 assert len(fasta_files) == len(temp_files) |
32 | 100 jobs = [ |
101 "%s %s < %s > %s" % (exe, organism, fasta, temp) | |
102 for (fasta, temp) in zip(fasta_files, temp_files) | |
103 ] | |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
104 assert len(fasta_files) == len(temp_files) == len(jobs) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
105 |
29 | 106 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
107 def clean_up(file_list): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
108 for f in file_list: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
109 if os.path.isfile(f): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
110 os.remove(f) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
111 |
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
|
112 |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
113 if len(jobs) > 1 and num_threads > 1: |
29 | 114 # 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
|
115 print("Using %i threads for %i tasks" % (min(num_threads, len(jobs)), len(jobs))) |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
116 results = run_jobs(jobs, num_threads) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
117 assert len(fasta_files) == len(temp_files) == len(jobs) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
118 for fasta, temp, cmd in zip(fasta_files, temp_files, jobs): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
119 error_level = results[cmd] |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
120 try: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
121 output = open(temp).readline() |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
122 except IOError: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
123 output = "" |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
124 if error_level or output.lower().startswith("error running"): |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
125 clean_up(fasta_files) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
126 clean_up(temp_files) |
32 | 127 sys.exit( |
128 "One or more tasks failed, e.g. %i from %r gave:\n%s" | |
129 % (error_level, cmd, output), | |
130 error_level, | |
131 ) | |
5
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
132 del results |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
133 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
134 out_handle = open(tabular_file, "w") |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
135 out_handle.write("#ID\tCompartment\tScore\tRank\n") |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
136 for temp in temp_files: |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
137 data_handle = open(temp) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
138 clean_tabular(data_handle, out_handle) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
139 data_handle.close() |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
140 out_handle.close() |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
141 |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
142 clean_up(fasta_files) |
ef7ceca37e3f
Migrated tool version 0.0.8 from old tool shed archive to new tool shed repository
peterjc
parents:
diff
changeset
|
143 clean_up(temp_files) |