2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.jmol;
23 import java.awt.Color;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
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;
47 * Routines for generating Jmol commands for Jalview/Jmol binding
52 public class JmolCommands extends StructureCommandsBase
54 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
55 "select *; cartoons off; backbone");
57 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
60 private static final StructureCommand COLOUR_ALL_WHITE = new StructureCommand(
61 "select *;color white;");
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");
67 private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
68 "select *;color chain");
70 private static final String PIPE = "|";
72 private static final String HYPHEN = "-";
74 private static final String COLON = ":";
76 private static final String SLASH = "/";
84 public int getModelStartNo()
90 * Returns a string representation of the given colour suitable for inclusion
96 protected String getColourString(Color c)
98 return c == null ? null
99 : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
104 public StructureCommandI colourByChain()
106 return COLOUR_BY_CHAIN;
110 public List<StructureCommandI> colourByCharge()
112 return Arrays.asList(COLOUR_BY_CHARGE);
116 public List<StructureCommandI> colourByResidues(
117 Map<String, Color> colours)
119 List<StructureCommandI> cmds = super.colourByResidues(colours);
120 cmds.add(0, COLOUR_ALL_WHITE);
125 public StructureCommandI setBackgroundColour(Color col)
127 return new StructureCommand("background " + getColourString(col));
131 public StructureCommandI focusView()
137 public List<StructureCommandI> showChains(List<String> toShow)
139 StringBuilder atomSpec = new StringBuilder(128);
140 boolean first = true;
141 for (String chain : toShow)
143 String[] tokens = chain.split(":");
144 if (tokens.length == 2)
148 atomSpec.append(" or ");
151 atomSpec.append(":").append(tokens[1]).append(" /")
156 String spec = atomSpec.toString();
157 String command = "select *;restrict " + spec + ";cartoon;center "
159 return Arrays.asList(new StructureCommand(command));
163 * Returns a command to superpose atoms in {@code atomSpec} to those in
164 * {@code refAtoms}, restricted to alpha carbons only (Phosphorous for rna).
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;
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.
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.
180 * @see https://chemapps.stolaf.edu/jmol/docs/#compare
183 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
184 AtomSpecModel atomSpec, AtomSpecType backbone)
186 StringBuilder sb = new StringBuilder(64);
187 String refModel = refAtoms.getModels().iterator().next();
188 String model2 = atomSpec.getModels().iterator().next();
189 sb.append(String.format("compare {%s.1} {%s.1}", model2, refModel));
190 sb.append(" SUBSET {(*.CA | *.P) and conformation=1} ATOMS {");
193 * command examples don't include modelspec with atoms, getAtomSpec does;
194 * it works, so leave it as it is for simplicity
196 sb.append(getAtomSpec(atomSpec, backbone)).append("}{");
197 sb.append(getAtomSpec(refAtoms, backbone)).append("}");
198 sb.append(" ROTATE TRANSLATE ");
199 sb.append(getCommandSeparator());
202 * show residues used for superposition as ribbon
205 .append(getAtomSpec(atomSpec, AtomSpecType.RESIDUE_ONLY))
207 sb.append(getAtomSpec(refAtoms, AtomSpecType.RESIDUE_ONLY))
208 .append(getCommandSeparator()).append("cartoons");
209 return Arrays.asList(new StructureCommand(sb.toString()));
213 public List<StructureCommandI> centerViewOn(List<AtomSpecModel> residues)
215 StringBuilder sb = new StringBuilder(64);
216 sb.append("center ");
217 for (AtomSpecModel ranges : residues)
223 sb.append(getAtomSpec(ranges, AtomSpecType.RESIDUE_ONLY));
225 return Arrays.asList(new StructureCommand(sb.toString()));
229 public StructureCommandI openCommandFile(String path)
232 * https://chemapps.stolaf.edu/jmol/docs/#script
233 * not currently used in Jalview
235 return new StructureCommand("script " + path);
239 public StructureCommandI saveSession(String filepath)
242 * https://chemapps.stolaf.edu/jmol/docs/#writemodel
244 StructureCommand sc = new StructureCommand(
245 "write STATE \"" + filepath + "\"");
246 sc.setWaitNeeded(true);
251 protected StructureCommandI colourResidues(String atomSpec, Color colour)
253 StringBuilder sb = new StringBuilder(atomSpec.length() + 20);
254 sb.append("select ").append(atomSpec).append(getCommandSeparator())
255 .append("color").append(getColourString(colour));
256 return new StructureCommand(sb.toString());
260 protected String getResidueSpec(String residue)
266 * Generates a Jmol atomspec string like
269 * 2-5:A/1.1,8:A/1.1,5-10:B/2.1
272 * Parameter {@code alphaOnly} is not used here - this restriction is made by
273 * a separate clause in the {@code compare} (superposition) command.
276 public String getAtomSpec(AtomSpecModel model, AtomSpecType specType)
278 StringBuilder sb = new StringBuilder(128);
280 boolean first = true;
281 for (String modelNo : model.getModels())
283 for (String chain : model.getChains(modelNo))
285 for (int[] range : model.getRanges(modelNo, chain))
292 if (range[0] == range[1])
298 sb.append(range[0]).append(HYPHEN).append(range[1]);
300 sb.append(COLON).append(chain.trim()).append(SLASH);
301 sb.append(String.valueOf(modelNo)).append(".1");
306 return sb.toString();
310 public List<StructureCommandI> showBackbone()
312 return Arrays.asList(SHOW_BACKBONE);
316 public StructureCommandI loadFile(String file)
318 // https://chemapps.stolaf.edu/jmol/docs/#loadfiles
319 return new StructureCommand(
320 "load FILES \"" + Platform.escapeBackslashes(file) + "\"");
324 public StructureCommandI restoreSession(String filePath)
326 return new StructureCommand("restore STATE \""
327 + Platform.escapeBackslashes(filePath) + "\"");
331 public List<StructureCommandI> showHetatms(List<String> toShow)
333 // always clear the current hetero cpk display
335 StringBuilder sb = new StringBuilder();
336 sb.append("select hetero; cpk off;");
338 if (toShow != null && !toShow.isEmpty())
340 // select what was requested
341 sb.append("select ");
343 for (String k : toShow)
345 sb.append(or ? " or " : " ");
353 return Arrays.asList(new StructureCommand(sb.toString()));
357 * Obsolete method, only referenced from
358 * jalview.javascript.MouseOverStructureListener
368 public String[] colourBySequence(StructureSelectionManager ssm,
369 String[] files, SequenceI[][] sequence, SequenceRenderer sr,
370 AlignmentViewPanel viewPanel)
372 // TODO delete method
374 FeatureRenderer fr = viewPanel.getFeatureRenderer();
375 FeatureColourFinder finder = new FeatureColourFinder(fr);
376 AlignViewportI viewport = viewPanel.getAlignViewport();
377 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
378 AlignmentI al = viewport.getAlignment();
379 List<String> cset = new ArrayList<>();
381 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
383 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
384 StringBuilder command = new StringBuilder(128);
385 List<String> str = new ArrayList<>();
387 if (mapping == null || mapping.length < 1)
392 for (int s = 0; s < sequence[pdbfnum].length; s++)
394 for (int sp, m = 0; m < mapping.length; m++)
396 if (mapping[m].getSequence() == sequence[pdbfnum][s]
397 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
399 int lastPos = StructureMapping.UNASSIGNED_VALUE;
400 SequenceI asp = al.getSequenceAt(sp);
401 for (int r = 0; r < asp.getLength(); r++)
403 // no mapping to gaps in sequence
404 if (Comparison.isGap(asp.getCharAt(r)))
408 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
414 if (pos == StructureMapping.UNASSIGNED_VALUE)
416 // terminate current colour op
417 if (command.length() > 0
418 && command.charAt(command.length() - 1) != ';')
423 lastPos = StructureMapping.UNASSIGNED_VALUE;
429 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
433 * shade hidden regions darker
435 if (!cs.isVisible(r))
440 String newSelcom = (mapping[m].getChain() != " "
441 ? ":" + mapping[m].getChain()
442 : "") + "/" + (pdbfnum + 1) + ".1" + ";color"
443 + getColourString(col);
444 if (command.length() > newSelcom.length() && command
445 .substring(command.length() - newSelcom.length())
448 command = JmolCommands.condenseCommand(command, pos);
451 // TODO: deal with case when buffer is too large for Jmol to parse
452 // - execute command and flush
454 if (command.length() > 0
455 && command.charAt(command.length() - 1) != ';')
460 if (command.length() > 51200)
463 str.add(command.toString());
464 command.setLength(0);
466 command.append("select " + pos);
467 command.append(newSelcom);
475 str.add(command.toString());
476 command.setLength(0);
481 return cset.toArray(new String[cset.size()]);
492 private static StringBuilder condenseCommand(StringBuilder command,
496 // work back to last 'select'
497 int p = command.length(), q = p;
506 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
508 StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
510 command = command.delete(0, q + 7);
514 if (command.indexOf("-") > -1)
516 start = command.substring(0, command.indexOf("-"));
520 start = command.substring(0, command.indexOf(":"));
523 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
529 public StructureCommandI openSession(String filepath)
531 return loadFile(filepath);
535 public StructureCommandI closeViewer()
537 return null; // not an external viewer