Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 20:42:03 GMT
  2. Package jalview.ext.jmol

File JmolCommands.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
29% of files have more coverage

Code metrics

52
139
23
1
539
361
56
0.4
6.04
23
2.43

Classes

Class Line # Actions
JmolCommands 52 139 56
0.686915968.7%
 

Contributing tests

This file is covered by 38 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.ext.jmol;
22   
23    import java.awt.Color;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.List;
27    import java.util.Map;
28   
29    import jalview.api.AlignViewportI;
30    import jalview.api.AlignmentViewPanel;
31    import jalview.api.FeatureRenderer;
32    import jalview.api.SequenceRenderer;
33    import jalview.datamodel.AlignmentI;
34    import jalview.datamodel.HiddenColumns;
35    import jalview.datamodel.SequenceI;
36    import jalview.renderer.seqfeatures.FeatureColourFinder;
37    import jalview.structure.AtomSpecModel;
38    import jalview.structure.StructureCommand;
39    import jalview.structure.StructureCommandI;
40    import jalview.structure.StructureCommandsBase;
41    import jalview.structure.StructureMapping;
42    import jalview.structure.StructureSelectionManager;
43    import jalview.util.Comparison;
44    import jalview.util.Platform;
45   
46    /**
47    * Routines for generating Jmol commands for Jalview/Jmol binding
48    *
49    * @author JimP
50    *
51    */
 
52    public class JmolCommands extends StructureCommandsBase
53    {
54    private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
55    "select *; cartoons off; backbone");
56   
57    private static final StructureCommand FOCUS_VIEW = new StructureCommand(
58    "zoom 0");
59   
60    private static final StructureCommand COLOUR_ALL_WHITE = new StructureCommand(
61    "select *;color white;");
62   
63    private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
64    "select *;color white;select ASP,GLU;color red;"
65    + "select LYS,ARG;color blue;select CYS;color yellow");
66   
67    private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
68    "select *;color chain");
69   
70    private static final String PIPE = "|";
71   
72    private static final String HYPHEN = "-";
73   
74    private static final String COLON = ":";
75   
76    private static final String SLASH = "/";
77   
78    /**
79    * {@inheritDoc}
80    *
81    * @return
82    */
 
83  1 toggle @Override
84    public int getModelStartNo()
85    {
86  1 return 1;
87    }
88   
89    /**
90    * Returns a string representation of the given colour suitable for inclusion
91    * in Jmol commands
92    *
93    * @param c
94    * @return
95    */
 
96  1622 toggle protected String getColourString(Color c)
97    {
98  1622 return c == null ? null
99    : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
100    c.getBlue());
101    }
102   
 
103  1 toggle @Override
104    public StructureCommandI colourByChain()
105    {
106  1 return COLOUR_BY_CHAIN;
107    }
108   
 
109  1 toggle @Override
110    public List<StructureCommandI> colourByCharge()
111    {
112  1 return Arrays.asList(COLOUR_BY_CHARGE);
113    }
114   
 
115  0 toggle @Override
116    public List<StructureCommandI> colourByResidues(
117    Map<String, Color> colours)
118    {
119  0 List<StructureCommandI> cmds = super.colourByResidues(colours);
120  0 cmds.add(0, COLOUR_ALL_WHITE);
121  0 return cmds;
122    }
123   
 
124  1 toggle @Override
125    public StructureCommandI setBackgroundColour(Color col)
126    {
127  1 return new StructureCommand("background " + getColourString(col));
128    }
129   
 
130  1 toggle @Override
131    public StructureCommandI focusView()
132    {
133  1 return FOCUS_VIEW;
134    }
135   
 
136  0 toggle @Override
137    public List<StructureCommandI> showChains(List<String> toShow)
138    {
139  0 StringBuilder atomSpec = new StringBuilder(128);
140  0 boolean first = true;
141  0 for (String chain : toShow)
142    {
143  0 String[] tokens = chain.split(":");
144  0 if (tokens.length == 2)
145    {
146  0 if (!first)
147    {
148  0 atomSpec.append(" or ");
149    }
150  0 first = false;
151  0 atomSpec.append(":").append(tokens[1]).append(" /")
152    .append(tokens[0]);
153    }
154    }
155   
156  0 String spec = atomSpec.toString();
157  0 String command = "select *;restrict " + spec + ";cartoon;center "
158    + spec;
159  0 return Arrays.asList(new StructureCommand(command));
160    }
161   
162    /**
163    * Returns a command to superpose atoms in {@code atomSpec} to those in
164    * {@code refAtoms}, restricted to alpha carbons only (Phosphorous for rna).
165    * For example
166    *
167    * <pre>
168    * compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1}
169    * ATOMS {1-87:A}{2-54:A|61-94:A} ROTATE TRANSLATE 1.0;
170    * </pre>
171    *
172    * where {@code conformation=1} excludes ALTLOC atom locations, and 1.0 is the
173    * time in seconds to animate the action. For this example, atoms in model 2
174    * are moved towards atoms in model 1.
175    * <p>
176    * The two atomspecs should each be for one model only, but may have more than
177    * one chain. The number of atoms specified should be the same for both
178    * models, though if not, Jmol may make a 'best effort' at superposition.
179    *
180    * @see https://chemapps.stolaf.edu/jmol/docs/#compare
181    */
 
182  2 toggle @Override
183    public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
184    AtomSpecModel atomSpec, AtomSpecType backbone)
185    {
186  2 StringBuilder sb = new StringBuilder(64);
187  2 String refModel = refAtoms.getModels().iterator().next();
188  2 String model2 = atomSpec.getModels().iterator().next();
189  2 sb.append(String.format("compare {%s.1} {%s.1}", model2, refModel));
190  2 sb.append(" SUBSET {(*.CA | *.P) and conformation=1} ATOMS {");
191   
192    /*
193    * command examples don't include modelspec with atoms, getAtomSpec does;
194    * it works, so leave it as it is for simplicity
195    */
196  2 sb.append(getAtomSpec(atomSpec, backbone)).append("}{");
197  2 sb.append(getAtomSpec(refAtoms, backbone)).append("}");
198  2 sb.append(" ROTATE TRANSLATE ");
199  2 sb.append(getCommandSeparator());
200   
201    /*
202    * show residues used for superposition as ribbon
203    */
204  2 sb.append("select ")
205    .append(getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY))
206    .append("|");
207  2 sb.append(getAtomSpec(refAtoms, AtomSpecType.RESIDUE_ONLY))
208    .append(getCommandSeparator()).append("cartoons");
209  2 return Arrays.asList(new StructureCommand(sb.toString()));
210    }
211   
 
212  1 toggle @Override
213    public List<StructureCommandI> centerViewOn(List<AtomSpecModel> residues)
214    {
215  1 StringBuilder sb = new StringBuilder(64);
216  1 sb.append("center ");
217  1 for (AtomSpecModel ranges : residues)
218    {
219  2 if (sb.length() > 9)
220    {
221  1 sb.append(" or ");
222    }
223  2 sb.append(getAtomSpec(ranges, AtomSpecType.RESIDUE_ONLY));
224    }
225  1 return Arrays.asList(new StructureCommand(sb.toString()));
226    }
227   
 
228  0 toggle @Override
229    public StructureCommandI openCommandFile(String path)
230    {
231    /*
232    * https://chemapps.stolaf.edu/jmol/docs/#script
233    * not currently used in Jalview
234    */
235  0 return new StructureCommand("script " + path);
236    }
237   
 
238  3 toggle @Override
239    public StructureCommandI saveSession(String filepath)
240    {
241    /*
242    * https://chemapps.stolaf.edu/jmol/docs/#writemodel
243    */
244  3 StructureCommand sc = new StructureCommand(
245    "write STATE \"" + filepath + "\"");
246  3 sc.setWaitNeeded(true);
247  3 return sc;
248    }
249   
 
250  1602 toggle @Override
251    protected StructureCommandI colourResidues(String atomSpec, Color colour)
252    {
253  1602 StringBuilder sb = new StringBuilder(atomSpec.length() + 20);
254  1602 sb.append("select ").append(atomSpec).append(getCommandSeparator())
255    .append("color").append(getColourString(colour));
256  1602 return new StructureCommand(sb.toString());
257    }
258   
 
259  0 toggle @Override
260    protected String getResidueSpec(String residue)
261    {
262  0 return residue;
263    }
264   
265    /**
266    * Generates a Jmol atomspec string like
267    *
268    * <pre>
269    * 2-5:A/1.1,8:A/1.1,5-10:B/2.1
270    * </pre>
271    *
272    * Parameter {@code alphaOnly} is not used here - this restriction is made by
273    * a separate clause in the {@code compare} (superposition) command.
274    */
 
275  1622 toggle @Override
276    public String getAtomSpec(AtomSpecModel model, AtomSpecType specType)
277    {
278  1622 StringBuilder sb = new StringBuilder(128);
279   
280  1622 boolean first = true;
281  1622 for (String modelNo : model.getModels())
282    {
283  1632 for (String chain : model.getChains(modelNo))
284    {
285  1651 for (int[] range : model.getRanges(modelNo, chain))
286    {
287  7638 if (!first)
288    {
289  6016 sb.append(PIPE);
290    }
291  7637 first = false;
292  7639 if (range[0] == range[1])
293    {
294  6583 sb.append(range[0]);
295    }
296    else
297    {
298  1056 sb.append(range[0]).append(HYPHEN).append(range[1]);
299    }
300  7637 sb.append(COLON).append(chain.trim()).append(SLASH);
301  7638 sb.append(String.valueOf(modelNo)).append(".1");
302    }
303    }
304    }
305   
306  1623 return sb.toString();
307    }
308   
 
309  2 toggle @Override
310    public List<StructureCommandI> showBackbone()
311    {
312  2 return Arrays.asList(SHOW_BACKBONE);
313    }
314   
 
315  4 toggle @Override
316    public StructureCommandI loadFile(String file)
317    {
318    // https://chemapps.stolaf.edu/jmol/docs/#loadfiles
319  4 return new StructureCommand(
320    "load FILES \"" + Platform.escapeBackslashes(file) + "\"");
321    }
322   
 
323  0 toggle @Override
324    public StructureCommandI restoreSession(String filePath)
325    {
326  0 return new StructureCommand("restore STATE \""
327    + Platform.escapeBackslashes(filePath) + "\"");
328    }
329   
 
330  0 toggle @Override
331    public List<StructureCommandI> showHetatms(List<String> toShow)
332    {
333    // always clear the current hetero cpk display
334   
335  0 StringBuilder sb = new StringBuilder();
336  0 sb.append("select hetero; cpk off;");
337   
338  0 if (toShow != null && !toShow.isEmpty())
339    {
340    // select what was requested
341  0 sb.append("select ");
342  0 boolean or = false;
343  0 for (String k : toShow)
344    {
345  0 sb.append(or ? " or " : " ");
346  0 sb.append(k);
347  0 or = true;
348    }
349    // and show as
350  0 sb.append("; cpk;");
351    }
352   
353  0 return Arrays.asList(new StructureCommand(sb.toString()));
354    }
355   
356    /**
357    * Obsolete method, only referenced from
358    * jalview.javascript.MouseOverStructureListener
359    *
360    * @param ssm
361    * @param files
362    * @param sequence
363    * @param sr
364    * @param viewPanel
365    * @return
366    */
 
367  1 toggle @Deprecated
368    public String[] colourBySequence(StructureSelectionManager ssm,
369    String[] files, SequenceI[][] sequence, SequenceRenderer sr,
370    AlignmentViewPanel viewPanel)
371    {
372    // TODO delete method
373   
374  1 FeatureRenderer fr = viewPanel.getFeatureRenderer();
375  1 FeatureColourFinder finder = new FeatureColourFinder(fr);
376  1 AlignViewportI viewport = viewPanel.getAlignViewport();
377  1 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
378  1 AlignmentI al = viewport.getAlignment();
379  1 List<String> cset = new ArrayList<>();
380   
381  3 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
382    {
383  2 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
384  2 StringBuilder command = new StringBuilder(128);
385  2 List<String> str = new ArrayList<>();
386   
387  2 if (mapping == null || mapping.length < 1)
388    {
389  0 continue;
390    }
391   
392  4 for (int s = 0; s < sequence[pdbfnum].length; s++)
393    {
394  4 for (int sp, m = 0; m < mapping.length; m++)
395    {
396  ? if (mapping[m].getSequence() == sequence[pdbfnum][s]
397    && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
398    {
399  2 int lastPos = StructureMapping.UNASSIGNED_VALUE;
400  2 SequenceI asp = al.getSequenceAt(sp);
401  22 for (int r = 0; r < asp.getLength(); r++)
402    {
403    // no mapping to gaps in sequence
404  20 if (Comparison.isGap(asp.getCharAt(r)))
405    {
406  0 continue;
407    }
408  20 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
409   
410  20 if (pos == lastPos)
411    {
412  0 continue;
413    }
414  20 if (pos == StructureMapping.UNASSIGNED_VALUE)
415    {
416    // terminate current colour op
417  0 if (command.length() > 0
418    && command.charAt(command.length() - 1) != ';')
419    {
420  0 command.append(";");
421    }
422    // reset lastPos
423  0 lastPos = StructureMapping.UNASSIGNED_VALUE;
424  0 continue;
425    }
426   
427  20 lastPos = pos;
428   
429  20 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
430    finder);
431   
432    /*
433    * shade hidden regions darker
434    */
435  20 if (!cs.isVisible(r))
436    {
437  6 col = Color.GRAY;
438    }
439   
440  20 String newSelcom = (mapping[m].getChain() != " "
441    ? ":" + mapping[m].getChain()
442    : "") + "/" + (pdbfnum + 1) + ".1" + ";color"
443    + getColourString(col);
444  20 if (command.length() > newSelcom.length() && command
445    .substring(command.length() - newSelcom.length())
446    .equals(newSelcom))
447    {
448  12 command = JmolCommands.condenseCommand(command, pos);
449  12 continue;
450    }
451    // TODO: deal with case when buffer is too large for Jmol to parse
452    // - execute command and flush
453   
454  8 if (command.length() > 0
455    && command.charAt(command.length() - 1) != ';')
456    {
457  6 command.append(";");
458    }
459   
460  8 if (command.length() > 51200)
461    {
462    // add another chunk
463  0 str.add(command.toString());
464  0 command.setLength(0);
465    }
466  8 command.append("select " + pos);
467  8 command.append(newSelcom);
468    }
469    // break;
470    }
471    }
472    }
473    {
474    // add final chunk
475  2 str.add(command.toString());
476  2 command.setLength(0);
477    }
478  2 cset.addAll(str);
479   
480    }
481  1 return cset.toArray(new String[cset.size()]);
482    }
483   
484    /**
485    * Helper method
486    *
487    * @param command
488    * @param pos
489    * @return
490    */
 
491  12 toggle @Deprecated
492    private static StringBuilder condenseCommand(StringBuilder command,
493    int pos)
494    {
495   
496    // work back to last 'select'
497  12 int p = command.length(), q = p;
498  12 do
499    {
500  74 p -= 6;
501  74 if (p < 1)
502    {
503  0 p = 0;
504    }
505  74 ;
506  ? } while ((q = command.indexOf("select", p)) == -1 && p > 0);
507   
508  12 StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
509   
510  12 command = command.delete(0, q + 7);
511   
512  12 String start;
513   
514  12 if (command.indexOf("-") > -1)
515    {
516  8 start = command.substring(0, command.indexOf("-"));
517    }
518    else
519    {
520  4 start = command.substring(0, command.indexOf(":"));
521    }
522   
523  12 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
524   
525  12 return sb;
526    }
527   
 
528  2 toggle @Override
529    public StructureCommandI openSession(String filepath)
530    {
531  2 return loadFile(filepath);
532    }
533   
 
534  1 toggle @Override
535    public StructureCommandI closeViewer()
536    {
537  1 return null; // not an external viewer
538    }
539    }