1 |
package tools; |
2 |
|
3 |
import java.sql.*; |
4 |
import java.io.*; |
5 |
import java.util.*; |
6 |
import java.text.*; |
7 |
|
8 |
/** |
9 |
* Package: tools |
10 |
* Class: PyMol |
11 |
* Author: Ioannis Filippis, filippis@molgen.mpg.de |
12 |
* Date: 20/03/2006 |
13 |
* |
14 |
* This class serves as a simple PyMol API. Most methods send PyMol commands |
15 |
* to the PrintWriter taken by the constructor, while others return pymol |
16 |
* selection strings. There is also the possibility to send the atomic |
17 |
* coordinates of a specific model of a macromolecule directly from msdsd |
18 |
* and load them on-the-fly. |
19 |
* |
20 |
* Notes: |
21 |
* - variable: this boolean parameter appears in many methods. The idea is that if |
22 |
* the objectName provided is really a PyMol object or selection, then it must be |
23 |
* quoted. However, it may be just a string variable, so it must be evaluated.In |
24 |
* that case parameter variable should be true. |
25 |
* - This is not a full PyMol API. Existing methods wrap basic pymol commands |
26 |
* in a simple, rather stupid way to facilitate contact graph visualization. |
27 |
* - If you want to use cgo edges, create a .pymolrc file in your home directory |
28 |
* with the command "run /project/StruPPi/PyMolAll/pymol/scripts/ioannis/graph.py" |
29 |
* |
30 |
* Changelog: |
31 |
* 27/03/06 modified by IF (refresh method plus refresh added in saveImage) |
32 |
* 23/03/06 modified by IF (python string methods added plus removeList,concatList methods) |
33 |
* 22/03/06 modified by IF (objectNameQuotes class variable replaced by |
34 |
* method parameter variable) |
35 |
* 20/03/06 first created by IF |
36 |
*/ |
37 |
public class PyMol { |
38 |
|
39 |
// Out: the PrintWriter where PyMol commands are sent to |
40 |
// attrs: a HashMap with keys the PyMol state variables allowed to be set |
41 |
// by the set class. Values are integers (could be used in a "switch") |
42 |
// DF: used to format decimal numbers so commands issued are readable |
43 |
private PrintWriter Out = null; |
44 |
private HashMap<String, Integer> attrs = new HashMap<String, Integer>(); |
45 |
private DecimalFormat DF = new DecimalFormat("#0.000"); |
46 |
|
47 |
// constructor |
48 |
public PyMol(PrintWriter out) { |
49 |
|
50 |
this.Out = out; |
51 |
|
52 |
attrs.put("sphere_transparency", new Integer(1)); |
53 |
attrs.put("sphere_scale", new Integer(2)); |
54 |
attrs.put("dash_gap", new Integer(3)); |
55 |
attrs.put("dash_width", new Integer(4)); |
56 |
attrs.put("transparency", new Integer(5)); |
57 |
|
58 |
} |
59 |
|
60 |
/** |
61 |
* sends the atom lines of a model (modelId) of a biological unit (assemblyId) of a protein |
62 |
* (accessionCode) directly from msdsd. In this way, the structure is loaded wihout temporary |
63 |
* pdb files. The structure object is named using the pattern accessionCode_assemblyId_modelId. |
64 |
* |
65 |
* Notes: |
66 |
* - A connection file is needed to connect to msdsd |
67 |
* - The chain_pdb_code is used in the chainID field in the atom line, while the chain_code is used |
68 |
* in the segID field (due to its length). Therefore, "segi" and not "chain" must be used in pymol |
69 |
* selections (all methods take care of that based on the boolean parameter msdsd) |
70 |
* - There are two versions of sendAtomLines. One that takes the atomic coordinates from the |
71 |
* partial atom_data tables (needs the table number e.g. 1 for atom_data_1, but is faster), |
72 |
* while the other uses the merged table (really slow - should be avoided) |
73 |
* - In general, the method is slow. The use of temporary files should be preferred. Have a look |
74 |
* at Msdsd2Pdb class. |
75 |
*/ |
76 |
public void sendAtomLines(String accessionCode, int assemblyId, int modelId, int atomDataTblNum, String connFile) { |
77 |
|
78 |
String query = "SELECT CONCAT("+ |
79 |
"RPAD(\"ATOM\", 6, \" \"), "+ |
80 |
"LPAD(serial, 5, \" \"), "+ |
81 |
"\" \", "+ |
82 |
"LPAD(chem_atom_name, 4, \" \"), "+ |
83 |
"IF(alt_code IS NULL, \" \", alt_code), "+ |
84 |
"code_3_letter, "+ |
85 |
"\" \", "+ |
86 |
"IF(chain_pdb_code IS NULL, \" \", chain_pdb_code), "+ |
87 |
"LPAD(residue_serial, 4, \" \"), "+ |
88 |
"IF(residue_pdb_insert_code IS NULL, \" \", residue_pdb_insert_code), "+ |
89 |
"REPEAT(\" \", 3), "+ |
90 |
"LPAD(x, 8, \" \"), "+ |
91 |
"LPAD(y, 8, \" \"), "+ |
92 |
"LPAD(z, 8, \" \"), "+ |
93 |
"LPAD(occupancy, 6, \" \"), "+ |
94 |
"REPEAT(\" \", 6), "+ |
95 |
"REPEAT(\" \", 6), "+ |
96 |
"RPAD(chain_code, 4, \" \") "+ |
97 |
") AS atom_lines FROM msdsd.atom_data_"+atomDataTblNum+" WHERE "+ |
98 |
"(assembly_id = "+assemblyId+") AND "+ |
99 |
"(model_id = "+modelId+") AND "+ |
100 |
"((alt_code = \"A\") OR (alt_code IS NULL)) AND "+ |
101 |
"(pdb_group = \"A\") "+ |
102 |
"ORDER BY chain_code, residue_serial, serial;"; |
103 |
|
104 |
mySQLConnect SQLC = new mySQLConnect(); |
105 |
SQLC.readConnectionFile(connFile); |
106 |
Connection conn = SQLC.openConnection(); |
107 |
|
108 |
Statement S; |
109 |
ResultSet R; |
110 |
|
111 |
Out.print("cmd.read_pdbstr(\"\"\""); |
112 |
|
113 |
try { |
114 |
S = conn.createStatement(); |
115 |
R = S.executeQuery(query); |
116 |
while (R.next()) { |
117 |
Out.println(R.getString(1)+"\\"); |
118 |
} |
119 |
R.close(); |
120 |
S.close(); |
121 |
} // end try |
122 |
catch (SQLException E) { |
123 |
System.out.println("SQLException: " + E.getMessage()); |
124 |
System.out.println("SQLState: " + E.getSQLState()); |
125 |
System.out.println("VendorError: " + E.getErrorCode()); |
126 |
} // end catch |
127 |
|
128 |
SQLC.closeConnection(conn); |
129 |
|
130 |
Out.println("END\"\"\", \""+accessionCode+"_"+assemblyId+"_"+modelId+"\")"); |
131 |
|
132 |
} |
133 |
|
134 |
/** |
135 |
* sends the atom lines of a model (modelId) of a biological unit (assemblyId) of a protein |
136 |
* (accessionCode) directly from msdsd. In this way, the structure is loaded wihout temporary |
137 |
* pdb files. The structure object is named using the pattern accessionCode_assemblyId_modelId. |
138 |
* |
139 |
* Notes: |
140 |
* - A connection file is needed to connect to msdsd |
141 |
* - The chain_pdb_code is used in the chainID field in the atom line, while the chain_code is used |
142 |
* in the segID field (due to its length). Therefore, "segi" and not "chain" must be used in pymol |
143 |
* selections (all methods take care of that based on the boolean parameter msdsd) |
144 |
* - There are two versions of sendAtomLines. One that takes the atomic coordinates from the |
145 |
* partial atom_data tables (needs the table number e.g. 1 for atom_data_1, but is faster), |
146 |
* while the other uses the merged table (really slow - should be avoided) |
147 |
* - In general, the method is slow. The use of temporary files should be preferred. Have a look |
148 |
* at Msdsd2Pdb class. |
149 |
*/ |
150 |
public void sendAtomLines(String accessionCode, int assemblyId, int modelId, String connFile) { |
151 |
|
152 |
String query = "SELECT CONCAT("+ |
153 |
"RPAD(\"ATOM\", 6, \" \"), "+ |
154 |
"LPAD(serial, 5, \" \"), "+ |
155 |
"\" \", "+ |
156 |
"LPAD(chem_atom_name, 4, \" \"), "+ |
157 |
"IF(alt_code IS NULL, \" \", alt_code), "+ |
158 |
"code_3_letter, "+ |
159 |
"\" \", "+ |
160 |
"IF(chain_pdb_code IS NULL, \" \", chain_pdb_code), "+ |
161 |
"LPAD(residue_serial, 4, \" \"), "+ |
162 |
"IF(residue_pdb_insert_code IS NULL, \" \", residue_pdb_insert_code), "+ |
163 |
"REPEAT(\" \", 3), "+ |
164 |
"LPAD(x, 8, \" \"), "+ |
165 |
"LPAD(y, 8, \" \"), "+ |
166 |
"LPAD(z, 8, \" \"), "+ |
167 |
"LPAD(occupancy, 6, \" \"), "+ |
168 |
"REPEAT(\" \", 6), "+ |
169 |
"REPEAT(\" \", 6), "+ |
170 |
"RPAD(chain_code, 4, \" \") "+ |
171 |
") AS atom_lines FROM msdsd.atom_data WHERE "+ |
172 |
"(assembly_id = "+assemblyId+") AND "+ |
173 |
"(model_id = "+modelId+") AND "+ |
174 |
"((alt_code = \"A\") OR (alt_code IS NULL)) AND "+ |
175 |
"(pdb_group = \"A\") "+ |
176 |
"ORDER BY chain_code, residue_serial, serial;"; |
177 |
|
178 |
mySQLConnect SQLC = new mySQLConnect(); |
179 |
SQLC.readConnectionFile(connFile); |
180 |
Connection conn = SQLC.openConnection(); |
181 |
|
182 |
Statement S; |
183 |
ResultSet R; |
184 |
|
185 |
Out.print("cmd.read_pdbstr(\"\"\""); |
186 |
|
187 |
try { |
188 |
S = conn.createStatement(); |
189 |
R = S.executeQuery(query); |
190 |
while (R.next()) { |
191 |
Out.println(R.getString(1)+"\\"); |
192 |
} |
193 |
R.close(); |
194 |
S.close(); |
195 |
} // end try |
196 |
catch (SQLException E) { |
197 |
System.out.println("SQLException: " + E.getMessage()); |
198 |
System.out.println("SQLState: " + E.getSQLState()); |
199 |
System.out.println("VendorError: " + E.getErrorCode()); |
200 |
} // end catch |
201 |
|
202 |
SQLC.closeConnection(conn); |
203 |
|
204 |
Out.println("END\"\"\", \""+accessionCode+"_"+assemblyId+"_"+modelId+"\")"); |
205 |
|
206 |
} |
207 |
|
208 |
/** |
209 |
* loads a pdb file |
210 |
* |
211 |
* Notes: |
212 |
* - There are two versions. One of them loads the pdb file specified by its name and the directory path, while |
213 |
* the other just uses the filename. |
214 |
* - Pdb file is expected to have the extension ".pdb". |
215 |
* - The structure object is named using the accession code (extension ".pdb" is trimmed) |
216 |
*/ |
217 |
public String loadPDB(String pdbFileName, String pdbDir) { |
218 |
|
219 |
Out.println("cmd.load(\""+pdbDir+"/"+pdbFileName+"\", \""+pdbFileName.substring(0, pdbFileName.length()-4)+"\", 1)"); |
220 |
|
221 |
return (pdbFileName.substring(0, pdbFileName.length()-4)); |
222 |
|
223 |
} |
224 |
|
225 |
/** |
226 |
* loads a pdb file |
227 |
* |
228 |
* Notes: |
229 |
* - There are two versions. One of them loads the pdb file specified by its name and the directory path, while |
230 |
* the other just uses the filename. |
231 |
* - Pdb file is expected to have the extension ".pdb". |
232 |
* - The structure object is named using the accession code (extension ".pdb" is trimmed) |
233 |
*/ |
234 |
public String loadPDB(String pdbFileName) { |
235 |
|
236 |
Out.println("cmd.load(\""+pdbFileName+"\", \""+pdbFileName.substring(0, pdbFileName.length()-4)+"\", 1)"); |
237 |
|
238 |
return (pdbFileName.substring(0, pdbFileName.length()-4)); |
239 |
|
240 |
} |
241 |
|
242 |
/** |
243 |
* adds a node as a sphere centered on the Ca of a residue |
244 |
* |
245 |
* Notes: |
246 |
* - The node object is named n.cid.num (e.g. n.A.15 is the 15th residue-node in chain A) |
247 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
248 |
* segi is used instead of chain |
249 |
*/ |
250 |
public String addNode(String cid, int num, boolean msdsd) { |
251 |
|
252 |
String nodeName = "n."+cid+"."+num; |
253 |
String nodeSel = selectNode(cid, num, msdsd, true); |
254 |
|
255 |
Out.println("cmd.create(\""+nodeName+"\", \""+nodeSel+"\")"); |
256 |
Out.println("cmd.show(\"sphere\", \""+nodeName+"\")"); |
257 |
|
258 |
return nodeName; |
259 |
|
260 |
} |
261 |
|
262 |
/** |
263 |
* adds an edge as a distance object between the Ca's of 2 residues |
264 |
* |
265 |
* Notes: |
266 |
* - The edge object is named e.i_cid.i_num.j_cid.j_num (e.g. e.A.1.B.10 is the edge |
267 |
* between the 1st residue-node in chain A and the 10th residue in chain B) |
268 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
269 |
* segi is used instead of chain |
270 |
*/ |
271 |
public String addEdge(String i_cid, int i_num, String j_cid, int j_num, boolean msdsd) { |
272 |
|
273 |
String edgeName = "e."+i_cid+"."+i_num+"."+j_cid+"."+j_num; |
274 |
String iNodeSel = selectNode(i_cid, i_num, msdsd, true); |
275 |
String jNodeSel = selectNode(j_cid, j_num, msdsd, true); |
276 |
|
277 |
Out.println("cmd.distance(\""+edgeName+"\", \""+iNodeSel+"\", \""+jNodeSel+"\")"); |
278 |
Out.println("cmd.hide(\"labels\")"); |
279 |
|
280 |
return edgeName; |
281 |
|
282 |
} |
283 |
|
284 |
/** |
285 |
* adds an edge as a sausage Compiled Graphic Object (cgo) between the Ca's of 2 residues |
286 |
* |
287 |
* Notes: |
288 |
* - The edge object is named e.i_cid.i_num.j_cid.j_num (e.g. e.A.1.B.10 is the edge |
289 |
* between the 1st residue-node in chain A and the 10th residue in chain B) |
290 |
* - If the graph is directed, then only half of the user-formatted sausage will be |
291 |
* drawn towards the target node and the rest will be drawn as a thin sausage. |
292 |
* - rgb(color) and half_rgb(half_color) define the color of the two parts of the edge (if directed). |
293 |
* There are two versions of addCgoEdge, one with string (color) and one with array of doubles |
294 |
* (rgb) parameters. |
295 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
296 |
* segi is used instead of chain. |
297 |
*/ |
298 |
public String addCgoEdge(String i_cid, int i_num, String j_cid, int j_num, double[] rgb, double dashGap, double dashLength, double dashRadius, boolean directed, double[] half_rgb, boolean msdsd) { |
299 |
|
300 |
String edgeName = "e."+i_cid+"."+i_num+"."+j_cid+"."+j_num; |
301 |
String iNodeSel = selectNode(i_cid, i_num, msdsd, true); |
302 |
String jNodeSel = selectNode(j_cid, j_num, msdsd, true); |
303 |
|
304 |
Out.println("edge name="+edgeName+", i_node="+iNodeSel+", j_node="+jNodeSel+", r="+DF.format(rgb[0])+", g="+DF.format(rgb[1])+", b="+DF.format(rgb[2])+", dg="+DF.format(dashGap)+", dl="+DF.format(dashLength)+", dr="+DF.format(dashRadius)+", dir="+(directed?1:0)+", dir_r="+DF.format(half_rgb[0])+", dir_g="+DF.format(half_rgb[1])+", dir_b="+DF.format(half_rgb[2])+""); |
305 |
|
306 |
return edgeName; |
307 |
|
308 |
} |
309 |
|
310 |
/** |
311 |
* adds an edge as a sausage Compiled Graphic Object (cgo) between the Ca's of 2 residues |
312 |
* |
313 |
* Notes: |
314 |
* - The edge object is named e.i_cid.i_num.j_cid.j_num (e.g. e.A.1.B.10 is the edge |
315 |
* between the 1st residue-node in chain A and the 10th residue in chain B) |
316 |
* - If the graph is directed, then only half of the user-formatted sausage will be |
317 |
* drawn towards the target node and the rest will be drawn as a thin sausage. |
318 |
* - rgb(color) and half_rgb(half_color) define the color of the two parts of the edge (if directed). |
319 |
* There are two versions of addCgoEdge, one with string (color) and one with array of doubles |
320 |
* (rgb) parameters. |
321 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
322 |
* segi is used instead of chain. |
323 |
* - Have a look at the /project/StruPPi/PyMolAll/pymol/scripts/ioannis/graph.py with the |
324 |
* implementation of the edge command |
325 |
*/ |
326 |
public String addCgoEdge(String i_cid, int i_num, String j_cid, int j_num, String color, double dashGap, double dashLength, double dashRadius, boolean directed, String half_color, boolean msdsd) { |
327 |
|
328 |
String edgeName = "e."+i_cid+"."+i_num+"."+j_cid+"."+j_num; |
329 |
String iNodeSel = selectNode(i_cid, i_num, msdsd, true); |
330 |
String jNodeSel = selectNode(j_cid, j_num, msdsd, true); |
331 |
|
332 |
Out.println("edge name="+edgeName+", i_node="+iNodeSel+", j_node="+jNodeSel+", color="+color+", dg="+DF.format(dashGap)+", dl="+DF.format(dashLength)+", dr="+DF.format(dashRadius)+", dir="+(directed?1:0)+", dir_color="+color+""); |
333 |
|
334 |
return edgeName; |
335 |
|
336 |
} |
337 |
|
338 |
/** |
339 |
* deletes an object or selection |
340 |
*/ |
341 |
public void delete(String objectName, boolean variable) { |
342 |
|
343 |
Out.println("cmd.delete("+((!variable)?"\""+objectName+"\"":objectName)+")"); |
344 |
|
345 |
} |
346 |
|
347 |
/** |
348 |
* deletes a node |
349 |
*/ |
350 |
public void delNode(String cid, int num) { |
351 |
|
352 |
String nodeName = "\"n."+cid+"."+num+"\""; |
353 |
Out.println("cmd.delete("+nodeName+")"); |
354 |
|
355 |
} |
356 |
|
357 |
/** |
358 |
* deletes an edge |
359 |
*/ |
360 |
public void delEdge(String i_cid, int i_num, String j_cid, int j_num) { |
361 |
|
362 |
String edgeName = "\"e."+i_cid+"."+i_num+"."+j_cid+"."+j_num+"\""; |
363 |
Out.println("cmd.delete("+edgeName+")"); |
364 |
|
365 |
} |
366 |
|
367 |
/** |
368 |
* turns on atom/bond representation specified by what for an object or selection |
369 |
*/ |
370 |
public void showWhat(String what, String objectName, boolean variable) { |
371 |
|
372 |
Out.println("cmd.show(\""+what+"\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
373 |
|
374 |
} |
375 |
|
376 |
/** |
377 |
* turns on atom/bond representation for all bonds for an object or selection |
378 |
*/ |
379 |
public void show(String objectName, boolean variable) { |
380 |
|
381 |
Out.println("cmd.show(\"everything\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
382 |
|
383 |
} |
384 |
|
385 |
|
386 |
/** |
387 |
* turns on node representation |
388 |
*/ |
389 |
public void showNode(String cid, int num) { |
390 |
|
391 |
String nodeName = "n."+cid+"."+num; |
392 |
Out.println("cmd.show(\"sphere\", \""+nodeName+"\")"); |
393 |
|
394 |
} |
395 |
|
396 |
/** |
397 |
* turns on edge representation |
398 |
*/ |
399 |
public void showEdge(String i_cid, int i_num, String j_cid, int j_num) { |
400 |
|
401 |
String edgeName = "e."+i_cid+"."+i_num+"."+j_cid+"."+j_num; |
402 |
Out.println("cmd.show(\"everything\", \""+edgeName+"\""); |
403 |
|
404 |
} |
405 |
|
406 |
/** |
407 |
* turns off atom/bond representation specified by what for an object or selection |
408 |
*/ |
409 |
public void hideWhat(String what, String objectName, boolean variable) { |
410 |
|
411 |
Out.println("cmd.hide(\""+what+"\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
412 |
|
413 |
} |
414 |
|
415 |
/** |
416 |
* turns off atom/bond representation for all bonds for an object or selection |
417 |
*/ |
418 |
public void hide(String objectName, boolean variable) { |
419 |
|
420 |
Out.println("cmd.hide(\"everything\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
421 |
|
422 |
} |
423 |
|
424 |
/** |
425 |
* turns off node representation |
426 |
*/ |
427 |
public void hideNode(String cid, int num) { |
428 |
|
429 |
String nodeName = "n."+cid+"."+num; |
430 |
Out.println("cmd.hide(\"everything\", \""+nodeName+"\")"); |
431 |
|
432 |
} |
433 |
|
434 |
/** |
435 |
* turns off edge representation |
436 |
*/ |
437 |
public void hideEdge(String i_cid, int i_num, String j_cid, int j_num) { |
438 |
|
439 |
String edgeName = "e."+i_cid+"."+i_num+"."+j_cid+"."+j_num; |
440 |
Out.println("cmd.hide(\"everything\", \""+edgeName+"\")"); |
441 |
|
442 |
} |
443 |
|
444 |
/** |
445 |
* creates a named neighbourhood selection using all atoms within distCutOff Angstrom from |
446 |
* all atoms of the specified residue |
447 |
* |
448 |
* Notes: |
449 |
* - The neighbourhood selection is named n.cid.num.N (e.g. n.A.1.N is the neighbourhood of |
450 |
* the 1st residue-node in chain A) |
451 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
452 |
* segi is used instead of chain. |
453 |
*/ |
454 |
public void selNbrPml(String cid, int num, double distCutOff, boolean msdsd) { |
455 |
|
456 |
String nodeNbrName = "n."+cid+"."+num+".N"; |
457 |
String nodeSel = selectNode(cid, num, msdsd, false); |
458 |
|
459 |
Out.println("cmd.select(\""+nodeNbrName+"\", \""+nodeSel + " around "+DF.format(distCutOff)+"\")"); |
460 |
|
461 |
} |
462 |
|
463 |
/** |
464 |
* creates a selection called name using the atom selection "encoded" in the objectName |
465 |
* |
466 |
* Notes: |
467 |
* - If pink is false, the selection display is disabled (pink stuff go away!!!!) |
468 |
*/ |
469 |
public void selPml(String name, String objectName, boolean pink, boolean variable) { |
470 |
|
471 |
Out.println("cmd.select(\""+name+"\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
472 |
if (!pink) { Out.println("cmd.disable(\""+name+"\")"); }; |
473 |
|
474 |
} |
475 |
|
476 |
/** |
477 |
* returns an atom selection string for a node |
478 |
* |
479 |
* Notes: |
480 |
* - The CA boolean is used to denote whether the Ca atom should be only selected |
481 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
482 |
* segi is used instead of chain. |
483 |
*/ |
484 |
public String selectNode(String cid, int num, boolean msdsd, boolean CA) { |
485 |
|
486 |
return "("+(msdsd?"segi ":"chain ")+(cid.equals("")?"\"\"":cid)+" and resi "+num+(CA?" and name CA":"")+")"; |
487 |
|
488 |
} |
489 |
|
490 |
/** |
491 |
* returns an atom selection string for a chain |
492 |
* |
493 |
* Notes: |
494 |
* - The msdsd boolean is used to denote whether msdsd is the source of the structure. In that case, |
495 |
* segi is used instead of chain. |
496 |
*/ |
497 |
public String selectChain(String cid, boolean msdsd) { |
498 |
|
499 |
return "("+(msdsd?"segi ":"chain ")+(cid.equals("")?"\"\"":cid)+")"; |
500 |
|
501 |
} |
502 |
|
503 |
/** |
504 |
* changes one of the PyMol state variable (attribute) for a specific object or selection |
505 |
* and sets it equal to the provided value. |
506 |
* |
507 |
* Notes: |
508 |
* - If the state variable is not defined in the attrs hashMap, then set returns -1 else 0. |
509 |
*/ |
510 |
public int set(String attribute, double value, String objectName, boolean variable) { |
511 |
|
512 |
Integer key = null; |
513 |
key = attrs.get(attribute); |
514 |
|
515 |
if (key == null) { return -1; } |
516 |
|
517 |
Out.println("cmd.set(\""+attribute+"\", "+DF.format(value)+", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
518 |
|
519 |
return 0; |
520 |
|
521 |
} |
522 |
|
523 |
/** |
524 |
* sets the color for a node |
525 |
*/ |
526 |
public void setNodeColor(String color, String nodeName, boolean variable) { |
527 |
|
528 |
Out.println("cmd.set(\"sphere_color\", \""+color+"\", "+((!variable)?"\""+nodeName+"\"":nodeName)+")"); |
529 |
|
530 |
} |
531 |
|
532 |
/** |
533 |
* sets the color for an object or selection |
534 |
*/ |
535 |
public void setColor(String color, String objectName, boolean variable) { |
536 |
|
537 |
Out.println("cmd.color(\""+color+"\", "+((!variable)?"\""+objectName+"\"":objectName)+")"); |
538 |
|
539 |
} |
540 |
|
541 |
/** |
542 |
* define a new color or rather color name based on an existing color |
543 |
*/ |
544 |
public void createColor(String colorName, String color) { |
545 |
|
546 |
Out.println("cmd.set_color(\""+colorName+"\", cmd.get_color_tuple(cmd.get_color_index(\""+color+"\")))"); |
547 |
|
548 |
} |
549 |
|
550 |
/** |
551 |
* define a new color providing the RGB array of doubles |
552 |
*/ |
553 |
public void createColor(String colorName, double[] rgb) { |
554 |
|
555 |
Out.println("cmd.set_color(\""+colorName+"\", ["+DF.format(rgb[0])+", "+DF.format(rgb[1])+", "+DF.format(rgb[2])+"])"); |
556 |
|
557 |
} |
558 |
|
559 |
/** |
560 |
* create/set a string |
561 |
*/ |
562 |
public void initString(String stringName, String value) { |
563 |
|
564 |
Out.println(stringName+" = \""+value+"\""); |
565 |
|
566 |
} |
567 |
|
568 |
/** |
569 |
* append to String |
570 |
*/ |
571 |
public void appendString(String stringName, String value) { |
572 |
|
573 |
Out.println(stringName+" += \""+value+"\""); |
574 |
|
575 |
} |
576 |
|
577 |
/** |
578 |
* rstring a String |
579 |
*/ |
580 |
public void rstripString(String stringName, String value) { |
581 |
|
582 |
Out.println("string.rstrip("+stringName+", \""+value+"\")"); |
583 |
|
584 |
} |
585 |
|
586 |
/** |
587 |
* replace a specific substring within a string with another one |
588 |
*/ |
589 |
public void replaceString(String stringName, String oldValue, String newValue) { |
590 |
|
591 |
Out.println("string.replace("+stringName+", \""+oldValue+"\", \""+newValue+"\")"); |
592 |
|
593 |
} |
594 |
|
595 |
/** |
596 |
* create/initialize a list |
597 |
*/ |
598 |
public void initList(String listName) { |
599 |
|
600 |
Out.println(listName+" = []"); |
601 |
|
602 |
} |
603 |
|
604 |
/** |
605 |
* append objects to a list |
606 |
*/ |
607 |
public void appendList(String listName, String value) { |
608 |
|
609 |
Out.println(listName+".append(\""+value+"\")"); |
610 |
|
611 |
} |
612 |
|
613 |
/** |
614 |
* remove object from a list |
615 |
*/ |
616 |
public void removeList(String listName, String value) { |
617 |
|
618 |
Out.println(listName+".remove(\""+value+"\")"); |
619 |
|
620 |
} |
621 |
|
622 |
/** |
623 |
* concatenate all members of a list of strings into a string using specific seperator |
624 |
*/ |
625 |
public void concatList(String listName, String sep, String stringName) { |
626 |
|
627 |
Out.println(stringName+" = \""+sep+"\".join("+listName+")"); |
628 |
|
629 |
} |
630 |
|
631 |
/** |
632 |
* iterate through list objects |
633 |
* |
634 |
* Notes: |
635 |
* - item is the name of the variable containing the object |
636 |
* - This is the only method that "print" instead of "println". |
637 |
* In this way, a method can be executed at each iteration e.g. |
638 |
* PyMol pml = new PyMol(out); |
639 |
* ... |
640 |
* pml.iterateList("edges", "edge"); |
641 |
* pml.setColor("red", edge, true); |
642 |
* - In the previous example, the "edge" parameter in the setColor command |
643 |
* is the name of tha variable holding the actual object. Therefore, it must |
644 |
* be evaluated and so not quoted. This is why setColor is called with true |
645 |
* value for the argument variable |
646 |
*/ |
647 |
public void iterateList(String listName, String item) { |
648 |
|
649 |
Out.print("for "+item+" in "+listName+":"); |
650 |
|
651 |
} |
652 |
|
653 |
/** |
654 |
* opens a log file |
655 |
* |
656 |
* Notes: |
657 |
* - Be careful. All log files are saved with extension .pml. Remember to include it in the fileName!!!!! |
658 |
* - There are two versions. One of them opens the log file specified by its name and the directory path, while |
659 |
* the other just uses the filename. |
660 |
*/ |
661 |
public void openLog(String fileName, String dir) { |
662 |
|
663 |
Out.println("log_open "+dir+"/"+fileName+".pml"); |
664 |
|
665 |
} |
666 |
|
667 |
/** |
668 |
* opens a log file |
669 |
* |
670 |
* Notes: |
671 |
* - Be careful. All log files are saved with extension .pml. Remember to include it in the fileName!!!!! |
672 |
* - There are two versions. One of them opens the log file specified by its name and the directory path, while |
673 |
* the other just uses the filename. |
674 |
*/ |
675 |
public void openLog(String fileName) { |
676 |
|
677 |
Out.println("log_open "+fileName+".pml"); |
678 |
|
679 |
} |
680 |
|
681 |
/** |
682 |
* closes the last opened log file |
683 |
*/ |
684 |
public void closeLog() { |
685 |
|
686 |
Out.println("log_close"); |
687 |
|
688 |
} |
689 |
|
690 |
/** |
691 |
* get the current view into a string |
692 |
*/ |
693 |
public void getView(String viewName) { |
694 |
|
695 |
Out.println(viewName+" = cmd.get_view()"); |
696 |
|
697 |
} |
698 |
|
699 |
/** |
700 |
* get the current view into a string from a log file |
701 |
*/ |
702 |
public void getFileView(String viewName, String fileName) { |
703 |
|
704 |
String viewStr = ""; |
705 |
String line = "", viewLine = ""; |
706 |
boolean viewFinished = false; |
707 |
|
708 |
try { |
709 |
//read the first view in the file |
710 |
BufferedReader fileIn = new BufferedReader(new FileReader(new File(fileName))); |
711 |
while ( ((line = fileIn.readLine()) != null) && (!viewFinished) ){ |
712 |
if (line.equals("_ set_view (\\")) { |
713 |
while ( ((viewLine = fileIn.readLine()) != null) && (!viewFinished) ) { |
714 |
if (viewLine.startsWith("_")) { |
715 |
viewStr = viewStr + viewLine.substring(1,viewLine.length()-1); |
716 |
} else { |
717 |
viewFinished = true; |
718 |
} |
719 |
} |
720 |
} |
721 |
} |
722 |
if (fileIn != null) { fileIn.close(); } |
723 |
viewStr = "("+viewStr+")"; |
724 |
} |
725 |
catch (Exception e) { System.out.println(e); } |
726 |
Out.println(viewName+" = "+viewStr); |
727 |
|
728 |
} |
729 |
|
730 |
/** |
731 |
* set the view based on a string's value |
732 |
*/ |
733 |
public void setView(String view) { |
734 |
|
735 |
Out.println("cmd.set_view("+view+")"); |
736 |
|
737 |
} |
738 |
|
739 |
/** |
740 |
* writes a png format image file of the current frame |
741 |
* |
742 |
* Notes: |
743 |
* - rayTraced defines whether the image will be ray-traced or not |
744 |
* - There are two versions. One of them saves the image file as specified by its name and the directory path, |
745 |
* while the other just uses the filename. |
746 |
*/ |
747 |
public void saveImage(String imageName, String dir, boolean rayTraced) { |
748 |
|
749 |
refresh(); |
750 |
if (rayTraced) { Out.println("ray"); } |
751 |
Out.println("png "+dir+"/"+imageName+".png"); |
752 |
|
753 |
} |
754 |
|
755 |
/** |
756 |
* writes a png format image file of the current frame |
757 |
* |
758 |
* Notes: |
759 |
* - rayTraced defines whether the image will be ray-traced or not |
760 |
* - There are two versions. One of them saves the image file as specified by its name and the directory path, |
761 |
* while the other just uses the filename. |
762 |
*/ |
763 |
public void saveImage(String imageName, boolean rayTraced) { |
764 |
|
765 |
refresh(); |
766 |
if (rayTraced) { Out.println("ray"); } |
767 |
Out.println("png "+imageName+".png"); |
768 |
|
769 |
} |
770 |
|
771 |
/** |
772 |
* sources a pymol command script |
773 |
* |
774 |
* Notes: |
775 |
* - There are two versions. One of them runs the file specified by its name and the directory path, |
776 |
* while the other just uses the filename. |
777 |
*/ |
778 |
public void runScript(String fileName, String dir) { |
779 |
|
780 |
Out.println("@"+dir+"/"+fileName+".pml"); |
781 |
|
782 |
} |
783 |
|
784 |
/** |
785 |
* sources a pymol command script |
786 |
* |
787 |
* Notes: |
788 |
* - There are two versions. One of them runs the file specified by its name and the directory path, |
789 |
* while the other just uses the filename. |
790 |
*/ |
791 |
public void runScript(String fileName) { |
792 |
|
793 |
Out.println("@"+fileName+".pml"); |
794 |
|
795 |
} |
796 |
|
797 |
/** |
798 |
* zooms on an object or selection |
799 |
*/ |
800 |
public void zoom(String objectName, boolean variable) { |
801 |
|
802 |
Out.println("cmd.zoom("+((!variable)?"\""+objectName+"\"":objectName)+")"); |
803 |
|
804 |
} |
805 |
|
806 |
/** |
807 |
* sets the background color |
808 |
*/ |
809 |
public void background(String color) { |
810 |
|
811 |
Out.println("cmd.bg_color(\""+color+"\")"); |
812 |
|
813 |
} |
814 |
|
815 |
/** |
816 |
* refresh the scene |
817 |
*/ |
818 |
public void refresh() { |
819 |
|
820 |
Out.println("cmd.refresh()"); |
821 |
|
822 |
} |
823 |
|
824 |
/** |
825 |
* some initialization commands for structure visualization |
826 |
* |
827 |
* Notes: |
828 |
* - These are defined according to Ioannis' preferences |
829 |
*/ |
830 |
public void init() { |
831 |
|
832 |
Out.println("cmd.set(\"depth_cue\", 0)"); |
833 |
Out.println("cmd.set(\"ray_trace_fog\", 0)"); |
834 |
Out.println("cmd.hide()"); |
835 |
Out.println("cmd.show(\"cartoon\")"); |
836 |
Out.println("cmd.cartoon(\"automatic\")"); |
837 |
Out.println("cmd.set(\"cartoon_flat_sheets\", 0)"); |
838 |
Out.println("cmd.set(\"cartoon_fancy_helices\", 1)"); |
839 |
Out.println("cmd.hide(\"spheres\", \"hetatm\")"); |
840 |
|
841 |
} |
842 |
|
843 |
} |