-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_predictors.py
221 lines (179 loc) · 8.03 KB
/
run_predictors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
"""
# NOD cli downloaded from: http://www.compbio.dundee.ac.uk/www-nod/downloads.jsp
# Running instructions: http://www.compbio.dundee.ac.uk/www-nod/cli_help.jsp
# java -jar clinod-1.3.jar -in=test2.fasta -out=test2.nod -f=COMPLETE
# 14-3-3-Pred downloaded from: https://github.com/bartongroup/FM_14-3-3/www1433/app
# python2.7 prediction.py -i ./test.fasta -o ./
# Jpred downloaded from: http://www.compbio.dundee.ac.uk/jpred/api.shtml#download
# perl jpredapi submit mode=single format=fasta [email protected] file=test.fasta name=test
"""
import os
import sys
import time
import click
import logging
import requests
import textwrap
import click_log
from Bio import SeqIO
from subprocess import Popen
nod_jar = os.path.join('lib', 'NOD', 'clinod-1.3.jar')
pred1433_python = os.path.join('lib', '1433pred', 'prediction.py')
jpred_perl = os.path.join('lib', 'Jpred', 'jpredapi')
# main application
@click_log.simple_verbosity_option(default="INFO")
@click.group(chain=True,
context_settings={'help_option_names': ['-h', '--help']})
def main():
"""Main interface to the available pySTAMP sub-commands and options."""
@main.command('nod')
@click.option('-l', '--log', default=sys.stderr,
help="Path to the logfile.",
type=click.File('wb'))
@click.argument('input',
type=click.File('r'), required=True)
# @click.argument('output',
# type=click.File('w'), required=False)
@click_log.simple_verbosity_option(default="INFO")
def nod(input, log):
logging.basicConfig(stream=log,
format='%(asctime)s - %(levelname)s - %(message)s ')
# process input
seqs = SeqIO.parse(input, "fasta")
for i, record in enumerate(seqs):
seq = record.seq
pid = record.id
print(i + 1, pid)
# write a sequence per file
input_seq = os.path.join(os.path.dirname(__file__),
'data', 'input', '{}.fasta'.format(pid))
output_nod = os.path.join(os.path.dirname(__file__),
'data', 'output', 'NOD', '{}.nod'.format(pid))
if not os.path.exists(input_seq):
output = open(input_seq, 'w')
seq = '\n'.join(textwrap.wrap(str(seq), width=60))
output.write(">{}\n{}\n".format(pid, seq))
output.close()
cmd = ['java', '-jar', nod_jar,
'-in={}'.format(input_seq),
'-out={}'.format(output_nod),
'-f=COMPLETE']
Popen(cmd)
@main.command('1433pred')
@click.option('-l', '--log', default=sys.stderr,
help="Path to the logfile.",
type=click.File('wb'))
@click.argument('input',
type=click.File('r'), required=True)
@click.argument('pred1433_python_bin', type=click.Path(exists=True), default='/usr/bin/python')
# @click.argument('output',
# type=click.File('w'), required=False)
@click_log.simple_verbosity_option(default="INFO")
def pred1433(input, log, pred1433_python_bin):
logging.basicConfig(stream=log,
format='%(asctime)s - %(levelname)s - %(message)s ')
# process input
seqs = SeqIO.parse(input, "fasta")
for i, record in enumerate(seqs):
seq = record.seq
pid = record.id
print(i + 1, pid)
# write a sequence per file
input_seq = os.path.join(os.path.dirname(__file__),
'data', 'input', '{}.fasta'.format(pid))
output_1433pred = os.path.join(os.path.dirname(__file__),
'data', 'output', '1433pred')
if not os.path.exists(input_seq):
output = open(input_seq, 'w')
seq = '\n'.join(textwrap.wrap(str(seq), width=60))
output.write(">{}\n{}\n".format(pid, seq))
output.close()
cmd = [pred1433_python_bin, pred1433_python,
'-i', input_seq, '-o', output_1433pred]
# Popen(cmd)
os.system(' '.join(cmd))
@main.command('jpred')
@click.option('-l', '--log', default=sys.stderr,
help="Path to the logfile.",
type=click.File('wb'))
@click.argument('input',
type=click.File('r'), required=True)
# @click.argument('output',
# type=click.File('w'), required=False)
@click_log.simple_verbosity_option(default="INFO")
def jpred(input, log):
logging.basicConfig(stream=log,
format='%(asctime)s - %(levelname)s - %(message)s ')
# process input
seqs = SeqIO.parse(input, "fasta")
for i, record in enumerate(seqs):
seq = record.seq
pid = record.id
print(i + 1, pid)
# write a sequence per file
input_seq = os.path.join(os.path.dirname(__file__),
'data', 'input', '{}.fasta'.format(pid))
output_jpred = os.path.join(os.path.dirname(__file__),
'data', 'output', 'Jpred')
output_jpred_jnet = os.path.join(os.path.dirname(__file__),
'data', 'output', 'Jpred',
'{}.jnet'.format(pid))
output_jpred_tar = os.path.join(os.path.dirname(__file__),
'data', 'output', 'Jpred',
'{}.tar.gz'.format(pid))
if not os.path.exists(input_seq):
output = open(input_seq, 'w')
seq = '\n'.join(textwrap.wrap(str(seq), width=60))
output.write(">{}\n{}\n".format(pid, seq))
output.close()
cmd = ['perl', jpred_perl, 'submit', 'mode=single', 'format=fasta',
'file={}'.format(input_seq), 'name={}'.format(pid),
'>', os.path.join(output_jpred, '{}.log'.format(pid))]
# Popen(cmd)
os.system(' '.join(cmd))
# parse Jpred job id
jobid = ""
with open(os.path.join(output_jpred, '{}.log'.format(pid)), 'r') as lines:
for line in lines:
if line.startswith("Created JPred job with jobid: "):
jobid = line.strip().split()[-1]
finished = False
while not finished:
# check job status
cmd = ['perl', jpred_perl, 'status', 'jobid={}'.format(jobid), 'getResults=no',
'checkEvery=once', 'silent', '[email protected]',
'>', os.path.join(output_jpred, '{}.status.log'.format(pid))]
# Popen(cmd)
os.system(' '.join(cmd))
# parse job status
jobstatus = ""
with open(os.path.join(output_jpred, '{}.status.log'.format(pid)), 'r') as lines:
for line in lines:
if "Results available at the following URL:" in line:
jobstatus = "Job finished"
elif "ERROR" in line:
jobstatus = "Job failed"
if jobstatus == "Job finished":
finished = True
# get results: jnet
jpred_url = "http://www.compbio.dundee.ac.uk/jpred4/results"
jpred_url_full = "{}/{}/{}.jnet".format(jpred_url, jobid, jobid)
with open(output_jpred_jnet, "wb") as outfile:
r = requests.get(url=jpred_url_full)
outfile.write(r.content)
# get results: tar.gz
jpred_url = "http://www.compbio.dundee.ac.uk/jpred4/results"
jpred_url_full = "{}/{}/{}.tar.gz".format(jpred_url, jobid, jobid)
with open(output_jpred_tar, "wb") as outfile:
r = requests.get(url=jpred_url_full)
outfile.write(r.content)
elif jobstatus == "Job failed":
finished = True
print("Jpred job {} has errored. See {} for more details"
"".format(jobid, os.path.join(output_jpred, '{}.status.log'.format(pid))))
# sleep 30 seconds
time.sleep(30)
if __name__ == "__main__":
main()