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 |
|
package ext.edu.ucsf.rbvi.strucviz2; |
34 |
|
|
35 |
|
import java.awt.Color; |
36 |
|
import java.io.BufferedReader; |
37 |
|
import java.io.File; |
38 |
|
import java.io.IOException; |
39 |
|
import java.io.InputStream; |
40 |
|
import java.io.InputStreamReader; |
41 |
|
import java.io.UnsupportedEncodingException; |
42 |
|
import java.net.URLEncoder; |
43 |
|
import java.nio.charset.StandardCharsets; |
44 |
|
import java.nio.file.Paths; |
45 |
|
import java.util.ArrayList; |
46 |
|
import java.util.Collection; |
47 |
|
import java.util.HashMap; |
48 |
|
import java.util.List; |
49 |
|
import java.util.Map; |
50 |
|
|
51 |
|
import org.apache.http.NameValuePair; |
52 |
|
import org.apache.http.message.BasicNameValuePair; |
53 |
|
import org.slf4j.Logger; |
54 |
|
import org.slf4j.LoggerFactory; |
55 |
|
|
56 |
|
import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType; |
57 |
|
import ext.edu.ucsf.rbvi.strucviz2.port.ListenerThreads; |
58 |
|
import jalview.bin.Console; |
59 |
|
import jalview.ws.HttpClientUtils; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
|
|
| 0% |
Uncovered Elements: 452 (452) |
Complexity: 122 |
Complexity Density: 0.41 |
|
64 |
|
public class ChimeraManager |
65 |
|
{ |
66 |
|
private static final int REST_REPLY_TIMEOUT_MS = 15000; |
67 |
|
|
68 |
|
private static final int CONNECTION_TIMEOUT_MS = 100; |
69 |
|
|
70 |
|
private static final boolean debug = false; |
71 |
|
|
72 |
|
private int chimeraRestPort; |
73 |
|
|
74 |
|
private Process chimera; |
75 |
|
|
76 |
|
private ListenerThreads chimeraListenerThread; |
77 |
|
|
78 |
|
private Map<Integer, ChimeraModel> currentModelsMap; |
79 |
|
|
80 |
|
private Logger logger = LoggerFactory |
81 |
|
.getLogger(ext.edu.ucsf.rbvi.strucviz2.ChimeraManager.class); |
82 |
|
|
83 |
|
private StructureManager structureManager; |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
85 |
0 |
public ChimeraManager(StructureManager structureManager)... |
86 |
|
{ |
87 |
0 |
this.structureManager = structureManager; |
88 |
0 |
chimera = null; |
89 |
0 |
chimeraListenerThread = null; |
90 |
0 |
currentModelsMap = new HashMap<>(); |
91 |
|
|
92 |
|
} |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
94 |
0 |
public List<ChimeraModel> getChimeraModels(String modelName)... |
95 |
|
{ |
96 |
0 |
List<ChimeraModel> models = getChimeraModels(modelName, |
97 |
|
ModelType.PDB_MODEL); |
98 |
0 |
models.addAll(getChimeraModels(modelName, ModelType.SMILES)); |
99 |
0 |
return models; |
100 |
|
} |
101 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
102 |
0 |
public List<ChimeraModel> getChimeraModels(String modelName,... |
103 |
|
ModelType modelType) |
104 |
|
{ |
105 |
0 |
List<ChimeraModel> models = new ArrayList<>(); |
106 |
0 |
for (ChimeraModel model : currentModelsMap.values()) |
107 |
|
{ |
108 |
0 |
if (modelName.equals(model.getModelName()) |
109 |
|
&& modelType.equals(model.getModelType())) |
110 |
|
{ |
111 |
0 |
models.add(model); |
112 |
|
} |
113 |
|
} |
114 |
0 |
return models; |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
117 |
0 |
public Map<String, List<ChimeraModel>> getChimeraModelsMap()... |
118 |
|
{ |
119 |
0 |
Map<String, List<ChimeraModel>> models = new HashMap<>(); |
120 |
0 |
for (ChimeraModel model : currentModelsMap.values()) |
121 |
|
{ |
122 |
0 |
String modelName = model.getModelName(); |
123 |
0 |
if (!models.containsKey(modelName)) |
124 |
|
{ |
125 |
0 |
models.put(modelName, new ArrayList<ChimeraModel>()); |
126 |
|
} |
127 |
0 |
if (!models.get(modelName).contains(model)) |
128 |
|
{ |
129 |
0 |
models.get(modelName).add(model); |
130 |
|
} |
131 |
|
} |
132 |
0 |
return models; |
133 |
|
} |
134 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
135 |
0 |
public ChimeraModel getChimeraModel(Integer modelNumber,... |
136 |
|
Integer subModelNumber) |
137 |
|
{ |
138 |
0 |
Integer key = ChimUtils.makeModelKey(modelNumber, subModelNumber); |
139 |
0 |
if (currentModelsMap.containsKey(key)) |
140 |
|
{ |
141 |
0 |
return currentModelsMap.get(key); |
142 |
|
} |
143 |
0 |
return null; |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0 |
public ChimeraModel getChimeraModel()... |
147 |
|
{ |
148 |
0 |
return currentModelsMap.values().iterator().next(); |
149 |
|
} |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
151 |
0 |
public Collection<ChimeraModel> getChimeraModels()... |
152 |
|
{ |
153 |
|
|
154 |
0 |
return currentModelsMap.values(); |
155 |
|
} |
156 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
157 |
0 |
public int getChimeraModelsCount(boolean smiles)... |
158 |
|
{ |
159 |
|
|
160 |
0 |
int counter = currentModelsMap.size(); |
161 |
0 |
if (smiles) |
162 |
|
{ |
163 |
0 |
return counter; |
164 |
|
} |
165 |
|
|
166 |
0 |
for (ChimeraModel model : currentModelsMap.values()) |
167 |
|
{ |
168 |
0 |
if (model.getModelType() == ModelType.SMILES) |
169 |
|
{ |
170 |
0 |
counter--; |
171 |
|
} |
172 |
|
} |
173 |
0 |
return counter; |
174 |
|
} |
175 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
0 |
public boolean hasChimeraModel(Integer modelNubmer)... |
177 |
|
{ |
178 |
0 |
return hasChimeraModel(modelNubmer, 0); |
179 |
|
} |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
0 |
public boolean hasChimeraModel(Integer modelNubmer,... |
182 |
|
Integer subModelNumber) |
183 |
|
{ |
184 |
0 |
return currentModelsMap.containsKey( |
185 |
|
ChimUtils.makeModelKey(modelNubmer, subModelNumber)); |
186 |
|
} |
187 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
188 |
0 |
public void addChimeraModel(Integer modelNumber, Integer subModelNumber,... |
189 |
|
ChimeraModel model) |
190 |
|
{ |
191 |
0 |
currentModelsMap.put( |
192 |
|
ChimUtils.makeModelKey(modelNumber, subModelNumber), model); |
193 |
|
} |
194 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
195 |
0 |
public void removeChimeraModel(Integer modelNumber,... |
196 |
|
Integer subModelNumber) |
197 |
|
{ |
198 |
0 |
int modelKey = ChimUtils.makeModelKey(modelNumber, subModelNumber); |
199 |
0 |
if (currentModelsMap.containsKey(modelKey)) |
200 |
|
{ |
201 |
0 |
currentModelsMap.remove(modelKey); |
202 |
|
} |
203 |
|
} |
204 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
205 |
0 |
public List<ChimeraModel> openModel(String modelPath, ModelType type)... |
206 |
|
{ |
207 |
0 |
return openModel(modelPath, getFileNameFromPath(modelPath), type); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
@param |
215 |
|
@param |
216 |
|
@return |
217 |
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 8 |
Complexity Density: 0.36 |
|
218 |
0 |
public List<ChimeraModel> openModel(String modelPath, String modelName,... |
219 |
|
ModelType type) |
220 |
|
{ |
221 |
0 |
logger.info("chimera open " + modelPath); |
222 |
|
|
223 |
0 |
List<ChimeraModel> modelList = getModelList(); |
224 |
0 |
List<String> response = null; |
225 |
|
|
226 |
0 |
if (type == ModelType.MODBASE_MODEL) |
227 |
|
{ |
228 |
0 |
response = sendChimeraCommand("open modbase:" + modelPath, true); |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
} |
233 |
|
else |
234 |
|
{ |
235 |
0 |
response = sendChimeraCommand("open " + modelPath, true); |
236 |
|
} |
237 |
0 |
if (response == null) |
238 |
|
{ |
239 |
|
|
240 |
0 |
logger.warn("Could not open " + modelPath); |
241 |
0 |
return null; |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
0 |
for (ChimeraModel newModel : getModelList()) |
247 |
|
{ |
248 |
0 |
if (!modelList.contains(newModel)) |
249 |
|
{ |
250 |
0 |
newModel.setModelName(modelName); |
251 |
0 |
sendChimeraCommand("setattr M name " + modelName + " #" |
252 |
|
+ newModel.getModelNumber(), false); |
253 |
0 |
modelList.add(newModel); |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
0 |
for (ChimeraModel chimeraModel : modelList) |
259 |
|
{ |
260 |
|
|
261 |
0 |
Color modelColor = isChimeraX() ? null : getModelColor(chimeraModel); |
262 |
0 |
if (modelColor != null) |
263 |
|
{ |
264 |
0 |
chimeraModel.setModelColor(modelColor); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
0 |
if (type != ModelType.SMILES && !isChimeraX()) |
273 |
|
{ |
274 |
0 |
addResidues(chimeraModel); |
275 |
|
} |
276 |
|
} |
277 |
|
|
278 |
0 |
sendChimeraCommand("focus", false); |
279 |
|
|
280 |
0 |
return modelList; |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
@param |
288 |
|
@return |
289 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
290 |
0 |
private String getFileNameFromPath(String modelPath)... |
291 |
|
{ |
292 |
0 |
String modelName = modelPath; |
293 |
0 |
if (modelPath == null) |
294 |
|
{ |
295 |
0 |
return null; |
296 |
|
} |
297 |
|
|
298 |
0 |
if (modelPath.lastIndexOf(File.separator) > 0) |
299 |
|
{ |
300 |
0 |
modelName = modelPath |
301 |
|
.substring(modelPath.lastIndexOf(File.separator) + 1); |
302 |
|
} |
303 |
0 |
else if (modelPath.lastIndexOf("/") > 0) |
304 |
|
{ |
305 |
0 |
modelName = modelPath.substring(modelPath.lastIndexOf("/") + 1); |
306 |
|
} |
307 |
0 |
return modelName; |
308 |
|
} |
309 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
310 |
0 |
public void closeModel(ChimeraModel model)... |
311 |
|
{ |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
0 |
stopListening(); |
316 |
0 |
logger.info("chimera close model " + model.getModelName()); |
317 |
0 |
if (currentModelsMap.containsKey(ChimUtils.makeModelKey( |
318 |
|
model.getModelNumber(), model.getSubModelNumber()))) |
319 |
|
{ |
320 |
0 |
sendChimeraCommand("close " + model.toSpec(), false); |
321 |
|
|
322 |
0 |
currentModelsMap.remove(ChimUtils.makeModelKey(model.getModelNumber(), |
323 |
|
model.getSubModelNumber())); |
324 |
|
|
325 |
|
} |
326 |
|
else |
327 |
|
{ |
328 |
0 |
logger.warn("Could not find model " + model.getModelName() |
329 |
|
+ " to close."); |
330 |
|
} |
331 |
0 |
startListening(); |
332 |
|
} |
333 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
334 |
0 |
public void startListening()... |
335 |
|
{ |
336 |
0 |
sendChimeraCommand("listen start models; listen start selection", |
337 |
|
false); |
338 |
|
} |
339 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
340 |
0 |
public void stopListening()... |
341 |
|
{ |
342 |
0 |
String command = "listen stop models ; listen stop selection "; |
343 |
0 |
sendChimeraCommand(command, false); |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
@param |
350 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
351 |
0 |
public void startListening(String uri)... |
352 |
|
{ |
353 |
|
|
354 |
|
|
355 |
|
|
356 |
0 |
String command = "listen start models url " + uri; |
357 |
0 |
sendChimeraCommand(command, false); |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
0 |
command = "listen start select prefix SelectionChanged url " + uri; |
363 |
0 |
sendChimeraCommand(command, false); |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
@param |
370 |
|
|
371 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
372 |
0 |
public void select(String command)... |
373 |
|
{ |
374 |
0 |
sendChimeraCommand("listen stop selection; " + command |
375 |
|
+ "; listen start selection", false); |
376 |
|
} |
377 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
378 |
0 |
public void focus()... |
379 |
|
{ |
380 |
0 |
sendChimeraCommand("focus", false); |
381 |
|
} |
382 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
383 |
0 |
public void clearOnChimeraExit()... |
384 |
|
{ |
385 |
0 |
chimera = null; |
386 |
0 |
currentModelsMap.clear(); |
387 |
0 |
this.chimeraRestPort = 0; |
388 |
0 |
structureManager.clearOnChimeraExit(); |
389 |
|
} |
390 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 4 |
Complexity Density: 0.8 |
|
391 |
0 |
public void exitChimera()... |
392 |
|
{ |
393 |
0 |
if (isChimeraLaunched() && chimera != null) |
394 |
|
{ |
395 |
0 |
sendChimeraCommand("stop really", false); |
396 |
0 |
try |
397 |
|
{ |
398 |
|
|
399 |
|
|
400 |
0 |
chimera.destroy(); |
401 |
|
} catch (Exception ex) |
402 |
|
{ |
403 |
|
|
404 |
|
} |
405 |
|
} |
406 |
0 |
clearOnChimeraExit(); |
407 |
|
} |
408 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
409 |
0 |
public Map<Integer, ChimeraModel> getSelectedModels()... |
410 |
|
{ |
411 |
0 |
Map<Integer, ChimeraModel> selectedModelsMap = new HashMap<>(); |
412 |
0 |
List<String> chimeraReply = sendChimeraCommand( |
413 |
|
"list selection level molecule", true); |
414 |
0 |
if (chimeraReply != null) |
415 |
|
{ |
416 |
0 |
for (String modelLine : chimeraReply) |
417 |
|
{ |
418 |
0 |
ChimeraModel chimeraModel = new ChimeraModel(modelLine); |
419 |
0 |
Integer modelKey = ChimUtils.makeModelKey( |
420 |
|
chimeraModel.getModelNumber(), |
421 |
|
chimeraModel.getSubModelNumber()); |
422 |
0 |
selectedModelsMap.put(modelKey, chimeraModel); |
423 |
|
} |
424 |
|
} |
425 |
0 |
return selectedModelsMap; |
426 |
|
} |
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
@return |
433 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
434 |
0 |
public List<String> getSelectedResidueSpecs()... |
435 |
|
{ |
436 |
0 |
List<String> selectedResidues = new ArrayList<>(); |
437 |
|
|
438 |
0 |
String command = "list selection level residue"; |
439 |
0 |
List<String> chimeraReply = sendChimeraCommand(command, true); |
440 |
0 |
if (chimeraReply != null) |
441 |
|
{ |
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
0 |
for (String inputLine : chimeraReply) |
451 |
|
{ |
452 |
0 |
String[] inputLineParts = inputLine.split("\\s+"); |
453 |
0 |
if (inputLineParts.length >= 5) |
454 |
|
{ |
455 |
0 |
selectedResidues.add(inputLineParts[2]); |
456 |
|
} |
457 |
|
} |
458 |
|
} |
459 |
0 |
return selectedResidues; |
460 |
|
} |
461 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
462 |
0 |
public void getSelectedResidues(... |
463 |
|
Map<Integer, ChimeraModel> selectedModelsMap) |
464 |
|
{ |
465 |
0 |
List<String> chimeraReply = sendChimeraCommand( |
466 |
|
"list selection level residue", true); |
467 |
0 |
if (chimeraReply != null) |
468 |
|
{ |
469 |
0 |
for (String inputLine : chimeraReply) |
470 |
|
{ |
471 |
0 |
ChimeraResidue r = new ChimeraResidue(inputLine); |
472 |
0 |
Integer modelKey = ChimUtils.makeModelKey(r.getModelNumber(), |
473 |
|
r.getSubModelNumber()); |
474 |
0 |
if (selectedModelsMap.containsKey(modelKey)) |
475 |
|
{ |
476 |
0 |
ChimeraModel model = selectedModelsMap.get(modelKey); |
477 |
0 |
model.addResidue(r); |
478 |
|
} |
479 |
|
} |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
|
|
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
@return |
489 |
|
|
490 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
491 |
0 |
public List<ChimeraModel> getModelList()... |
492 |
|
{ |
493 |
0 |
List<ChimeraModel> modelList = new ArrayList<>(); |
494 |
0 |
String command = "list models type " |
495 |
0 |
+ (isChimeraX() ? "AtomicStructure" : "molecule"); |
496 |
0 |
List<String> list = sendChimeraCommand(command, true); |
497 |
0 |
if (list != null) |
498 |
|
{ |
499 |
0 |
for (String modelLine : list) |
500 |
|
{ |
501 |
0 |
try |
502 |
|
{ |
503 |
0 |
ChimeraModel chimeraModel = new ChimeraModel(modelLine); |
504 |
0 |
modelList.add(chimeraModel); |
505 |
|
} catch (NullPointerException e) |
506 |
|
{ |
507 |
|
|
508 |
|
} |
509 |
|
} |
510 |
|
} |
511 |
0 |
return modelList; |
512 |
|
} |
513 |
|
|
514 |
|
|
515 |
|
|
516 |
|
|
517 |
|
|
518 |
|
|
519 |
|
@return |
520 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
521 |
0 |
public List<String> getPresets()... |
522 |
|
{ |
523 |
0 |
ArrayList<String> presetList = new ArrayList<>(); |
524 |
0 |
List<String> output = sendChimeraCommand("preset list", true); |
525 |
0 |
if (output != null) |
526 |
|
{ |
527 |
0 |
for (String preset : output) |
528 |
|
{ |
529 |
0 |
preset = preset.substring(7); |
530 |
0 |
preset = preset.replaceFirst("\"", "("); |
531 |
0 |
preset = preset.replaceFirst("\"", ")"); |
532 |
|
|
533 |
0 |
presetList.add(preset); |
534 |
|
} |
535 |
|
} |
536 |
0 |
return presetList; |
537 |
|
} |
538 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
539 |
0 |
public boolean isChimeraLaunched()... |
540 |
|
{ |
541 |
0 |
boolean launched = false; |
542 |
0 |
if (chimera != null) |
543 |
|
{ |
544 |
0 |
try |
545 |
|
{ |
546 |
0 |
chimera.exitValue(); |
547 |
|
|
548 |
|
} catch (IllegalThreadStateException e) |
549 |
|
{ |
550 |
|
|
551 |
0 |
launched = true; |
552 |
|
} |
553 |
|
} |
554 |
0 |
return launched; |
555 |
|
} |
556 |
|
|
557 |
|
|
558 |
|
|
559 |
|
|
560 |
|
|
561 |
|
|
562 |
|
@param |
563 |
|
@return |
564 |
|
|
|
|
| 0% |
Uncovered Elements: 36 (36) |
Complexity: 5 |
Complexity Density: 0.17 |
|
565 |
0 |
public boolean launchChimera(List<String> chimeraPaths)... |
566 |
|
{ |
567 |
|
|
568 |
0 |
if (isChimeraLaunched()) |
569 |
|
{ |
570 |
0 |
Console.debug("Chimera is already launched"); |
571 |
0 |
return true; |
572 |
|
} |
573 |
|
|
574 |
|
|
575 |
0 |
String error = "Error message: "; |
576 |
0 |
String workingPath = ""; |
577 |
|
|
578 |
0 |
for (String chimeraPath : chimeraPaths) |
579 |
|
{ |
580 |
0 |
Console.debug("Using '" + chimeraPath + "' to look for Chimera"); |
581 |
0 |
try |
582 |
|
{ |
583 |
|
|
584 |
0 |
chimeraPath = Paths.get(chimeraPath).toRealPath().toString(); |
585 |
0 |
File path = new File(chimeraPath); |
586 |
|
|
587 |
|
|
588 |
0 |
if (!path.canExecute()) |
589 |
|
{ |
590 |
0 |
error += "File '" + path + "' does not exist.\n"; |
591 |
0 |
continue; |
592 |
|
} |
593 |
0 |
List<String> args = new ArrayList<>(); |
594 |
0 |
args.add(chimeraPath); |
595 |
|
|
596 |
|
|
597 |
0 |
addLaunchArguments(args); |
598 |
0 |
ProcessBuilder pb = new ProcessBuilder(args); |
599 |
0 |
Console.debug("Running Chimera with '" |
600 |
|
+ String.join(" ", pb.command()) + "'"); |
601 |
0 |
chimera = pb.start(); |
602 |
0 |
error = ""; |
603 |
0 |
workingPath = chimeraPath; |
604 |
0 |
break; |
605 |
|
} catch (Exception e) |
606 |
|
{ |
607 |
|
|
608 |
0 |
error += e.getMessage(); |
609 |
|
} |
610 |
|
} |
611 |
|
|
612 |
0 |
if (error.length() == 0) |
613 |
|
{ |
614 |
0 |
this.chimeraRestPort = getPortNumber(); |
615 |
0 |
System.out.println( |
616 |
|
"Chimera REST API started on port " + chimeraRestPort); |
617 |
|
|
618 |
0 |
structureManager.setChimeraPathProperty(workingPath); |
619 |
|
|
620 |
|
|
621 |
|
|
622 |
0 |
return (chimeraRestPort > 0); |
623 |
|
} |
624 |
|
|
625 |
|
|
626 |
0 |
logger.warn(error); |
627 |
0 |
return false; |
628 |
|
} |
629 |
|
|
630 |
|
|
631 |
|
|
632 |
|
|
633 |
|
|
634 |
|
|
635 |
|
@param |
636 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
637 |
0 |
protected void addLaunchArguments(List<String> args)... |
638 |
|
{ |
639 |
0 |
args.add("--start"); |
640 |
0 |
args.add("RESTServer"); |
641 |
|
} |
642 |
|
|
643 |
|
|
644 |
|
|
645 |
|
|
|
|
| 0% |
Uncovered Elements: 36 (36) |
Complexity: 9 |
Complexity Density: 0.38 |
|
646 |
0 |
private int getPortNumber()... |
647 |
|
{ |
648 |
0 |
int port = 0; |
649 |
0 |
InputStream readChan = chimera.getInputStream(); |
650 |
0 |
BufferedReader lineReader = new BufferedReader( |
651 |
|
new InputStreamReader(readChan)); |
652 |
0 |
StringBuilder responses = new StringBuilder(); |
653 |
0 |
try |
654 |
|
{ |
655 |
0 |
String response = lineReader.readLine(); |
656 |
0 |
while (response != null) |
657 |
|
{ |
658 |
0 |
responses.append("\n" + response); |
659 |
|
|
660 |
|
|
661 |
0 |
if (response.startsWith("REST server")) |
662 |
|
{ |
663 |
0 |
String[] tokens = response.split(" "); |
664 |
0 |
for (int i = 0; i < tokens.length - 1; i++) |
665 |
|
{ |
666 |
0 |
if ("port".equals(tokens[i])) |
667 |
|
{ |
668 |
0 |
port = Integer.parseInt(tokens[i + 1]); |
669 |
0 |
break; |
670 |
|
} |
671 |
|
} |
672 |
|
} |
673 |
0 |
if (port > 0) |
674 |
|
{ |
675 |
0 |
break; |
676 |
|
} |
677 |
0 |
response = lineReader.readLine(); |
678 |
|
} |
679 |
|
} catch (Exception e) |
680 |
|
{ |
681 |
0 |
logger.error("Failed to get REST port number from " + responses + ": " |
682 |
|
+ e.getMessage()); |
683 |
|
} finally |
684 |
|
{ |
685 |
0 |
try |
686 |
|
{ |
687 |
0 |
lineReader.close(); |
688 |
|
} catch (IOException e2) |
689 |
|
{ |
690 |
|
} |
691 |
|
} |
692 |
0 |
if (port == 0) |
693 |
|
{ |
694 |
0 |
System.err.println( |
695 |
|
"Failed to start Chimera with REST service, response was: " |
696 |
|
+ responses); |
697 |
|
} |
698 |
0 |
logger.info( |
699 |
|
"Chimera REST service listening on port " + chimeraRestPort); |
700 |
0 |
return port; |
701 |
|
} |
702 |
|
|
703 |
|
|
704 |
|
|
705 |
|
|
706 |
|
@param |
707 |
|
|
708 |
|
@return |
709 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
710 |
0 |
public Color getModelColor(ChimeraModel model)... |
711 |
|
{ |
712 |
0 |
List<String> colorLines = sendChimeraCommand( |
713 |
|
"list model spec " + model.toSpec() + " attribute color", true); |
714 |
0 |
if (colorLines == null || colorLines.size() == 0) |
715 |
|
{ |
716 |
0 |
return null; |
717 |
|
} |
718 |
0 |
return ChimUtils.parseModelColor(colorLines.get(0)); |
719 |
|
} |
720 |
|
|
721 |
|
|
722 |
|
|
723 |
|
|
724 |
|
|
725 |
|
|
726 |
|
|
727 |
|
@param |
728 |
|
|
729 |
|
|
730 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
731 |
0 |
public void addResidues(ChimeraModel model)... |
732 |
|
{ |
733 |
0 |
int modelNumber = model.getModelNumber(); |
734 |
0 |
int subModelNumber = model.getSubModelNumber(); |
735 |
|
|
736 |
0 |
List<String> reply = sendChimeraCommand( |
737 |
|
"list residues spec " + model.toSpec(), true); |
738 |
0 |
if (reply == null) |
739 |
|
{ |
740 |
0 |
return; |
741 |
|
} |
742 |
0 |
for (String inputLine : reply) |
743 |
|
{ |
744 |
0 |
ChimeraResidue r = new ChimeraResidue(inputLine); |
745 |
0 |
if (r.getModelNumber() == modelNumber |
746 |
|
|| r.getSubModelNumber() == subModelNumber) |
747 |
|
{ |
748 |
0 |
model.addResidue(r); |
749 |
|
} |
750 |
|
} |
751 |
|
} |
752 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
753 |
0 |
public List<String> getAttrList()... |
754 |
|
{ |
755 |
0 |
List<String> attributes = new ArrayList<>(); |
756 |
0 |
String command = (isChimeraX() ? "info " : "list ") + "resattr"; |
757 |
0 |
final List<String> reply = sendChimeraCommand(command, true); |
758 |
0 |
if (reply != null) |
759 |
|
{ |
760 |
0 |
for (String inputLine : reply) |
761 |
|
{ |
762 |
0 |
String[] lineParts = inputLine.split("\\s"); |
763 |
0 |
if (lineParts.length == 2 && lineParts[0].equals("resattr")) |
764 |
|
{ |
765 |
0 |
attributes.add(lineParts[1]); |
766 |
|
} |
767 |
|
} |
768 |
|
} |
769 |
0 |
return attributes; |
770 |
|
} |
771 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0.42 |
|
772 |
0 |
public Map<ChimeraResidue, Object> getAttrValues(String aCommand,... |
773 |
|
ChimeraModel model) |
774 |
|
{ |
775 |
0 |
Map<ChimeraResidue, Object> values = new HashMap<>(); |
776 |
0 |
final List<String> reply = sendChimeraCommand("list residue spec " |
777 |
|
+ model.toSpec() + " attribute " + aCommand, true); |
778 |
0 |
if (reply != null) |
779 |
|
{ |
780 |
0 |
for (String inputLine : reply) |
781 |
|
{ |
782 |
0 |
String[] lineParts = inputLine.split("\\s"); |
783 |
0 |
if (lineParts.length == 5) |
784 |
|
{ |
785 |
0 |
ChimeraResidue residue = ChimUtils.getResidue(lineParts[2], |
786 |
|
model); |
787 |
0 |
String value = lineParts[4]; |
788 |
0 |
if (residue != null) |
789 |
|
{ |
790 |
0 |
if (value.equals("None")) |
791 |
|
{ |
792 |
0 |
continue; |
793 |
|
} |
794 |
0 |
if (value.equals("True") || value.equals("False")) |
795 |
|
{ |
796 |
0 |
values.put(residue, Boolean.valueOf(value)); |
797 |
0 |
continue; |
798 |
|
} |
799 |
0 |
try |
800 |
|
{ |
801 |
0 |
Double doubleValue = Double.valueOf(value); |
802 |
0 |
values.put(residue, doubleValue); |
803 |
|
} catch (NumberFormatException ex) |
804 |
|
{ |
805 |
0 |
values.put(residue, value); |
806 |
|
} |
807 |
|
} |
808 |
|
} |
809 |
|
} |
810 |
|
} |
811 |
0 |
return values; |
812 |
|
} |
813 |
|
|
814 |
|
private volatile boolean busy = false; |
815 |
|
|
816 |
|
|
817 |
|
|
818 |
|
|
819 |
|
@param |
820 |
|
|
821 |
|
@param |
822 |
|
|
823 |
|
|
824 |
|
@return |
825 |
|
|
826 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 9 |
Complexity Density: 0.53 |
|
827 |
0 |
public List<String> sendChimeraCommand(String command, boolean reply)... |
828 |
|
{ |
829 |
0 |
if (debug) |
830 |
|
{ |
831 |
0 |
System.out.println("chimeradebug>> " + command); |
832 |
|
} |
833 |
0 |
if (!isChimeraLaunched() || command == null |
834 |
|
|| "".equals(command.trim())) |
835 |
|
{ |
836 |
0 |
return null; |
837 |
|
} |
838 |
|
|
839 |
|
|
840 |
|
|
841 |
|
|
842 |
0 |
int waited = 0; |
843 |
0 |
int pause = 25; |
844 |
0 |
while (busy && waited < 1001) |
845 |
|
{ |
846 |
0 |
try |
847 |
|
{ |
848 |
0 |
Thread.sleep(pause); |
849 |
0 |
waited += pause; |
850 |
|
} catch (InterruptedException q) |
851 |
|
{ |
852 |
|
} |
853 |
|
} |
854 |
0 |
busy = true; |
855 |
0 |
long startTime = System.currentTimeMillis(); |
856 |
0 |
try |
857 |
|
{ |
858 |
0 |
return sendRestCommand(command); |
859 |
|
} finally |
860 |
|
{ |
861 |
|
|
862 |
|
|
863 |
|
|
864 |
0 |
busy = false; |
865 |
0 |
if (debug) |
866 |
|
{ |
867 |
0 |
System.out.println("Chimera command took " |
868 |
|
+ (System.currentTimeMillis() - startTime) + "ms: " |
869 |
|
+ command); |
870 |
|
} |
871 |
|
} |
872 |
|
} |
873 |
|
|
874 |
|
|
875 |
|
|
876 |
|
|
877 |
|
@param |
878 |
|
@return |
879 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 8 |
Complexity Density: 0.4 |
|
880 |
0 |
protected List<String> sendRestCommand(String command)... |
881 |
|
{ |
882 |
0 |
String restUrl = "http://127.0.0.1:" + this.chimeraRestPort + "/run"; |
883 |
0 |
List<NameValuePair> commands = new ArrayList<>(1); |
884 |
0 |
String method = getHttpRequestMethod(); |
885 |
0 |
if ("GET".equals(method)) |
886 |
|
{ |
887 |
0 |
try |
888 |
|
{ |
889 |
0 |
command = URLEncoder.encode(command, StandardCharsets.UTF_8.name()); |
890 |
|
} catch (UnsupportedEncodingException e) |
891 |
|
{ |
892 |
0 |
command = command.replace(" ", "+").replace("#", "%23") |
893 |
|
.replace("|", "%7C").replace(";", "%3B") |
894 |
|
.replace(":", "%3A"); |
895 |
|
} |
896 |
|
} |
897 |
0 |
commands.add(new BasicNameValuePair("command", command)); |
898 |
|
|
899 |
0 |
List<String> reply = new ArrayList<>(); |
900 |
0 |
BufferedReader response = null; |
901 |
0 |
try |
902 |
|
{ |
903 |
0 |
response = "GET".equals(method) |
904 |
|
? HttpClientUtils.doHttpGet(restUrl, commands, |
905 |
|
CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS) |
906 |
|
: HttpClientUtils.doHttpUrlPost(restUrl, commands, |
907 |
|
CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS); |
908 |
0 |
String line = ""; |
909 |
0 |
while ((line = response.readLine()) != null) |
910 |
|
{ |
911 |
0 |
reply.add(line); |
912 |
|
} |
913 |
|
} catch (Exception e) |
914 |
|
{ |
915 |
0 |
logger.error("REST call '" + command + "' failed: " + e.getMessage()); |
916 |
|
} finally |
917 |
|
{ |
918 |
0 |
if (response != null) |
919 |
|
{ |
920 |
0 |
try |
921 |
|
{ |
922 |
0 |
response.close(); |
923 |
|
} catch (IOException e) |
924 |
|
{ |
925 |
|
} |
926 |
|
} |
927 |
|
} |
928 |
0 |
return reply; |
929 |
|
} |
930 |
|
|
931 |
|
|
932 |
|
|
933 |
|
|
934 |
|
|
935 |
|
@return |
936 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
937 |
0 |
protected String getHttpRequestMethod()... |
938 |
|
{ |
939 |
0 |
return "POST"; |
940 |
|
} |
941 |
|
|
942 |
|
|
943 |
|
|
944 |
|
|
945 |
|
|
946 |
|
@param |
947 |
|
@param |
948 |
|
@return |
949 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.23 |
|
950 |
0 |
protected List<String> sendStdinCommand(String command, boolean readReply)... |
951 |
|
{ |
952 |
0 |
chimeraListenerThread.clearResponse(command); |
953 |
0 |
String text = command.concat("\n"); |
954 |
0 |
try |
955 |
|
{ |
956 |
|
|
957 |
0 |
chimera.getOutputStream().write(text.getBytes()); |
958 |
0 |
chimera.getOutputStream().flush(); |
959 |
|
} catch (IOException e) |
960 |
|
{ |
961 |
|
|
962 |
|
|
963 |
0 |
logger.warn("Unable to execute command: " + text); |
964 |
0 |
logger.warn("Exiting..."); |
965 |
0 |
clearOnChimeraExit(); |
966 |
0 |
return null; |
967 |
|
} |
968 |
0 |
if (!readReply) |
969 |
|
{ |
970 |
0 |
return null; |
971 |
|
} |
972 |
0 |
List<String> rsp = chimeraListenerThread.getResponse(command); |
973 |
0 |
return rsp; |
974 |
|
} |
975 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
976 |
0 |
public StructureManager getStructureManager()... |
977 |
|
{ |
978 |
0 |
return structureManager; |
979 |
|
} |
980 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
981 |
0 |
public boolean isBusy()... |
982 |
|
{ |
983 |
0 |
return busy; |
984 |
|
} |
985 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
986 |
0 |
public Process getChimeraProcess()... |
987 |
|
{ |
988 |
0 |
return chimera; |
989 |
|
} |
990 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
991 |
0 |
public boolean isChimeraX()... |
992 |
|
{ |
993 |
0 |
return false; |
994 |
|
} |
995 |
|
} |