forked from ibivu/whole-genome-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
190 lines (143 loc) · 6.64 KB
/
manager.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
from praline.core import Manager, BeginMessage, CompleteMessage, ProgressMessage
from praline.component import TreeMultipleSequenceAligner
from praline import write_alignment_clustal
from praline.core import *
from praline.container import Sequence, ScoreMatrix, TRACK_ID_INPUT
from praline.container import ProfileTrack, PlainTrack
from praline.container import TRACK_ID_PREPROFILE, TRACK_ID_PROFILE
from praline.container import SequenceTree, Alignment
from praline.component import PairwiseAligner
from praline.util import auto_align_mode
import numpy as np
import os
import requests
import time
with open(os.getenv("PRALINE_CASHMERE_DIR") + '/bowbeforeme', 'r') as myfile:
host = myfile.read()
SERVER = "http://" + host + ":4567"
use_our_stuff = True
_INTERCEPT_TIDS = {TreeMultipleSequenceAligner.tid}
class ConstellationManager(Manager):
def execute_many(self, requestss, parent_tag):
not_msa = not use_our_stuff
for tid, inputs, tag, env in requestss:
if not tid in _INTERCEPT_TIDS:
not_msa = True
if not_msa:
gen = super(ConstellationManager, self).execute_many(requestss,parent_tag)
for message in gen:
yield message
return
start = time.time()
trees = []
# We're not handling these tasks in this manager, so execute them
# normally using the functionality of the superclass.
for tid, inputs, tag, env in requestss:
# We want to intercept execution of these tasks and send them off
# to constellation. First pass a message saying we've begun
# executing the tasks.
for tid, inputs, tag, env in requestss:
begin_message = BeginMessage(parent_tag)
begin_message.tag = tag
yield begin_message
# TODO: convert task inputs to constellation format, send it to
# Constellation and wait for completion. Yield ProgressMessage
# instances to report progress to the UI if applicable.
# We're not handling these tasks in this manager, so execute them
# normally using the functionality of the superclass.
score_matrices = inputs.get("score_matrices", None)
seqs = inputs.get("sequences", None)
tree = inputs.get("guide_tree", None)
track_id_sets = inputs.get("track_id_sets",None)
gap_series = env['gap_series']
merge_mode = env['merge_mode']
s = [sm.matrix.astype(np.float32) for sm in score_matrices]
uniquestr = str(time.time())
costName = "cost" + uniquestr
treeName = "tree" + uniquestr
sendCosts(costName, s)
uniquestr = str(time.time())
alignmentsi = {i:[seq] for i, seq in enumerate(seqs)}
for i, j in tree.merge_orders:
alignmentsi[i]+=alignmentsi[j]
data = ' '.join([str(i) + "," + str(j) for i, j in tree.merge_orders])
start_gap, extend_gap = 0, 0
if len(gap_series) == 2:
start_gap, extend_gap = gap_series[0], gap_series[1]
elif len(gap_series) == 1:
start_gap = extend_gap = gap_series[0]
else :
raise ComponentError("NO GAP COST!!!")
reqstring = "/register/tree/" + treeName + "/" + str(len(seqs)) + "/" + costName + "/" + merge_mode + "/" + str(start_gap) + "/" + str(extend_gap)
#print data
req = requests.post(SERVER + reqstring , data=data)
for i, seq in enumerate(seqs):
sendSequence(treeName,i,[seq.get_track(t[0]) for t in track_id_sets],s)
trees+=[treeName]
end = time.time()
print "Sending jobs took " + (str(end - start)) + " seconds"
start = time.time()
# for tree in trees:
# print(tree)
requests.get(SERVER + "/processtrees")
first = True
for (tid, inputs, tag, env),tree in zip(requestss,trees):
while True:
req = requests.get(SERVER + "/retrieve/steps/" + tree)
# print("not yet" + tree + " " + str(req.status_code))
if req.status_code == 200:
if first:
end = time.time()
print "Waiting for result " + (str(end - start)) + " seconds"
start = end
first = False
res = np.array([[int(d) for d in c.split(';')] for c in req.text.split(' ')])
outputs={}
outputs['alignment'] =Alignment(alignmentsi[0], res)
complete_message = CompleteMessage(outputs=outputs)
complete_message.tag = tag
yield complete_message
break
end = time.time()
print "Obtaining results took " + (str(end - start)) + " seconds"
def sendSequence(tree_name, leaf, tracks,s):
# nrTracks = len(tracks[0].values)
seqs = [profToSequence(track) for track in tracks ]
nrTracks = len(tracks)
nrPos = len(seqs[0])
data = ' '.join([ str(seqs[t][p]) for t in range(nrTracks) for p in range(nrPos) ])
#/send/sequence/:length/toqueue/:queue_name
reqstring = "/send/sequence/" + str(leaf) + "/" + str(nrPos) + "/totree/" + tree_name
# print reqstring
req = requests.post(SERVER + reqstring, data=data)
#print req
#print data
if req.status_code != 201:
raise ComponentError("HTTP ERROR" + req.text)
def profToSequence(track):
if track.tid == PlainTrack.tid :
return track.values
else :
prof = track.profile.astype(np.float32)
i,t = prof.shape
return [getIndex(prof,z,t) for z in range(i)]
def getIndex(prof, i, t):
for j in range(t):
if prof[i][j] > 0:
return j
return 0
def sendCosts(name, matrixes):
req = requests.get(SERVER + "/register/cost_matrix/" + name + "/" + str(len(matrixes)))
if req.status_code != 201:
raise ComponentError("HTTP ERROR" + req.text)
for i,t in enumerate(matrixes) :
(x,y) = t.shape
if x != y:
raise ComponentError("Matrix not square!")
data = ' '.join([ str(t[z,j]) for z in range(x) for j in range(y) ])
reqstring = "/send/cost_matrix/" + name + "/" + str(i) + "/" + str(x)
#print reqstring
#print data
req = requests.post(SERVER + reqstring , data=data)
if req.status_code != 201:
raise ComponentError("HTTP ERROR: " + req.text)