-
Notifications
You must be signed in to change notification settings - Fork 5
/
MasterSearch.py.old
executable file
·154 lines (131 loc) · 4.94 KB
/
MasterSearch.py.old
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
#!/usr/bin/env python
"""
the main master search functionality
author: Tim Tregubov, 12/2014
"""
import os
import tempfile
import subprocess
from werkzeug.utils import secure_filename
from rq import Queue
from redis import Redis
import Tasks
import re
defaults = {
'rmsdCut': '1.5',
'topN': '25',
'outType': 'match',
'bbRMSD': True,
'rmsdMode': '0',
'tune': '0.5',
'dEps': False,
'phiEps': '180.0',
'psiEps': '180.0',
'ddZScore': '3.5'
}
class MasterSearch(object):
"""
main class
uses an rq redis worker to submit a background task
to do the actual search
"""
app = None
db_size = 0
def __init__(self, app):
"""
sets up some initial parameters and a redis queue and connection
"""
self.db_size = sum(1 for line in open(app.config['TARGET_LIST_PATH']) if line.rstrip())
self.app = app
self.redis_conn = Redis()
self.rq = Queue(connection=self.redis_conn)
print('init with db size: ' + str(self.db_size))
def process(self, query_file, database, arguments):
"""
process the query
"""
error = None
search_job = None
tempdir = tempfile.mkdtemp(dir=self.app.config['PROCESSING_PATH'])
query_filepath = os.path.join(tempdir, secure_filename(query_file.filename))
sequence = ''
try:
# save file
query_file.save(query_filepath)
pdsfile = self.pdb2pds(query_filepath)
search_job = self.qsearch(pdsfile, database, arguments)
sequence = self.sequenceFromPDB(query_filepath)
except Exception as e:
print("processing failed: ", e)
error = str(e)
# cleanup all files
# shutil.rmtree(tempdir, ignore_errors=True)
# TODO: need to clean up the above somewhere later
return search_job, tempdir, sequence, error
def sequenceFromPDB(self, pdbfilepath):
"""
get a formatted sequence string from the PDB file
"""
cmd = [self.app.config['SCRIPTS_PATH']+'/getSeq', pdbfilepath]
print("getting query sequence: " + ' '.join(cmd))
seqStr, err = self.runCommand(cmd, "could not extract sequence from query PDB file")
return seqStr
def pdb2pds(self, pdbfilepath):
"""
convert pdb to pds format
"""
# if already a pds do nothing
ext = os.path.splitext(pdbfilepath)[1]
if ext == '.pds' or ext == 'pds':
return pdbfilepath
pdsfilename = pdbfilepath.replace('.pdb', '.pds')
cmd = [self.app.config['CREATEPDS_PATH'], '--type', 'query', '--pdb', pdbfilepath, '--pds', pdsfilename]
print("attempting to convert: " + ' '.join(cmd))
self.runCommand(cmd, "could not create PDS file from the query PDB file")
return pdsfilename
def runCommand(self, cmd, errMessBase):
try:
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
out, err = p.communicate()
# check for error
if re.search('Error:', out):
# might not return -1 so check for text here
err += out
if err:
raise Exception(err)
return out, err
except Exception as e:
raise Exception(errMessBase + "\ncommand: " + cmd + "\n" + e.message)
def qsearch(self, query_filepath, database, arguments):
"""
perform the search
"""
print("OPTIONS" + str(arguments))
# required arguments
tempdir, qfile = os.path.split(query_filepath)
# prefix, ext = os.path.splitext(qfile)
match_out = os.path.join(tempdir, 'matches')
seq_out = os.path.join(tempdir, 'seq')
struct_out = os.path.join(tempdir, 'struct')
rmsd_cut = arguments['rmsdCut'] if 'rmsdCut' in arguments else defaults['rmsdCut']
top_n = arguments['topN'] if 'topN' in arguments else defaults['topN']
out_type = arguments['outType'] if 'outType' in arguments else defaults['outType']
cmd = [self.app.config['MASTER_PATH'],
'--query', query_filepath,
'--targetList', os.path.join(self.app.config['CONFIG_PATH'], database),
'--rmsdCut', rmsd_cut,
'--matchOut', match_out,
'--seqOut', seq_out,
'--topN', top_n,
'--structOut', struct_out,
'--outType', out_type]
if 'bbRMSD' in arguments:
cmd.append('--bbRMSD')
self.db_size = sum(1 for line in open(os.path.join(self.app.config['CONFIG_PATH'], database)) if line.rstrip())
job = self.rq.enqueue_call(Tasks.search, args=(cmd, self.app.config['PROCESSING_PATH'], tempdir, self.db_size), timeout=3600)
return job
if __name__ == "__main__":
pass