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;
46 * Routines for generating Jmol commands for Jalview/Jmol binding
51 public class JmolCommands extends StructureCommandsBase
53 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
54 "select *; cartoons off; backbone");
56 private static final StructureCommand FOCUS_VIEW = new StructureCommand("zoom 0");
58 private static final StructureCommand COLOUR_ALL_WHITE = new StructureCommand(
59 "select *;color white;");
61 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
62 "select *;color white;select ASP,GLU;color red;"
63 + "select LYS,ARG;color blue;select CYS;color yellow");
65 private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
66 "select *;color chain");
68 private static final String PIPE = "|";
70 private static final String HYPHEN = "-";
72 private static final String COLON = ":";
74 private static final String SLASH = "/";
82 public int getModelStartNo()
88 * Returns a string representation of the given colour suitable for inclusion
94 protected String getColourString(Color c)
96 return c == null ? null
97 : String.format("[%d,%d,%d]", c.getRed(), c.getGreen(),
102 public String[] colourBySequence(StructureSelectionManager ssm,
104 SequenceI[][] sequence, SequenceRenderer sr,
105 AlignmentViewPanel viewPanel)
107 // TODO delete method
109 FeatureRenderer fr = viewPanel.getFeatureRenderer();
110 FeatureColourFinder finder = new FeatureColourFinder(fr);
111 AlignViewportI viewport = viewPanel.getAlignViewport();
112 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
113 AlignmentI al = viewport.getAlignment();
114 List<String> cset = new ArrayList<>();
116 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
118 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
119 StringBuilder command = new StringBuilder(128);
120 List<String> str = new ArrayList<>();
122 if (mapping == null || mapping.length < 1)
127 for (int s = 0; s < sequence[pdbfnum].length; s++)
129 for (int sp, m = 0; m < mapping.length; m++)
131 if (mapping[m].getSequence() == sequence[pdbfnum][s]
132 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
134 int lastPos = StructureMapping.UNASSIGNED_VALUE;
135 SequenceI asp = al.getSequenceAt(sp);
136 for (int r = 0; r < asp.getLength(); r++)
138 // no mapping to gaps in sequence
139 if (Comparison.isGap(asp.getCharAt(r)))
143 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
149 if (pos == StructureMapping.UNASSIGNED_VALUE)
151 // terminate current colour op
152 if (command.length() > 0
153 && command.charAt(command.length() - 1) != ';')
158 lastPos = StructureMapping.UNASSIGNED_VALUE;
164 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
168 * shade hidden regions darker
170 if (!cs.isVisible(r))
175 String newSelcom = (mapping[m].getChain() != " "
176 ? ":" + mapping[m].getChain()
177 : "") + "/" + (pdbfnum + 1) + ".1" + ";color"
178 + getColourString(col);
179 if (command.length() > newSelcom.length() && command
180 .substring(command.length() - newSelcom.length())
183 command = JmolCommands.condenseCommand(command, pos);
186 // TODO: deal with case when buffer is too large for Jmol to parse
187 // - execute command and flush
189 if (command.length() > 0
190 && command.charAt(command.length() - 1) != ';')
195 if (command.length() > 51200)
198 str.add(command.toString());
199 command.setLength(0);
201 command.append("select " + pos);
202 command.append(newSelcom);
210 str.add(command.toString());
211 command.setLength(0);
216 return cset.toArray(new String[cset.size()]);
219 public static StringBuilder condenseCommand(StringBuilder command,
223 // work back to last 'select'
224 int p = command.length(), q = p;
233 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
235 StringBuilder sb = new StringBuilder(command.substring(0, q + 7));
237 command = command.delete(0, q + 7);
241 if (command.indexOf("-") > -1)
243 start = command.substring(0, command.indexOf("-"));
247 start = command.substring(0, command.indexOf(":"));
250 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
256 public StructureCommandI colourByChain()
258 return COLOUR_BY_CHAIN;
262 public List<StructureCommandI> colourByCharge()
264 return Arrays.asList(COLOUR_BY_CHARGE);
268 public List<StructureCommandI> colourByResidues(Map<String, Color> colours)
270 List<StructureCommandI> cmds = super.colourByResidues(colours);
271 cmds.add(0, COLOUR_ALL_WHITE);
276 public StructureCommandI setBackgroundColour(Color col)
278 return new StructureCommand("background " + getColourString(col));
282 public StructureCommandI focusView()
288 public List<StructureCommandI> showChains(List<String> toShow)
290 StringBuilder atomSpec = new StringBuilder(128);
291 boolean first = true;
292 for (String chain : toShow)
294 String[] tokens = chain.split(":");
295 if (tokens.length == 2)
299 atomSpec.append(" or ");
302 atomSpec.append(":").append(tokens[1]).append(" /").append(tokens[0]);
306 String spec = atomSpec.toString();
307 String command = "select *;restrict " + spec + ";cartoon;center "
309 return Arrays.asList(new StructureCommand(command));
313 * Returns a command to superpose atoms in {@code atomSpec} to those in
314 * {@code refAtoms}, restricted to alpha carbons only (Phosphorous for rna).
318 * compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1}
319 * ATOMS {1-87:A}{2-54:A|61-94:A} ROTATE TRANSLATE 1.0;
322 * where {@code conformation=1} excludes ALTLOC atom locations, and 1.0 is the
323 * time in seconds to animate the action. For this example, atoms in model 2
324 * are moved towards atoms in model 1.
326 * The two atomspecs should each be for one model only, but may have more than
327 * one chain. The number of atoms specified should be the same for both
328 * models, though if not, Jmol may make a 'best effort' at superposition.
330 * @see https://chemapps.stolaf.edu/jmol/docs/#compare
333 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
334 AtomSpecModel atomSpec)
336 StringBuilder sb = new StringBuilder(64);
337 String refModel = refAtoms.getModels().iterator().next();
338 String model2 = atomSpec.getModels().iterator().next();
339 sb.append(String.format("compare {%s.1} {%s.1}", model2, refModel));
340 sb.append(" SUBSET {(*.CA | *.P) and conformation=1} ATOMS {");
343 * command examples don't include modelspec with atoms, getAtomSpec does;
344 * it works, so leave it as it is for simplicity
346 sb.append(getAtomSpec(atomSpec, true)).append("}{");
347 sb.append(getAtomSpec(refAtoms, true)).append("}");
348 sb.append(" ROTATE TRANSLATE ");
349 sb.append(getCommandSeparator());
352 * show residues used for superposition as ribbon
354 sb.append("select ").append(getAtomSpec(atomSpec, false)).append("|");
355 sb.append(getAtomSpec(refAtoms, false)).append(getCommandSeparator())
358 return Arrays.asList(new StructureCommand(sb.toString()));
362 public StructureCommandI openCommandFile(String path)
365 * https://chemapps.stolaf.edu/jmol/docs/#script
366 * not currently used in Jalview
368 return new StructureCommand("script " + path);
372 public StructureCommandI saveSession(String filepath)
375 * https://chemapps.stolaf.edu/jmol/docs/#writemodel
377 return new StructureCommand("write STATE \"" + filepath + "\"");
381 protected StructureCommandI getColourCommand(String atomSpec, Color colour)
383 StringBuilder sb = new StringBuilder(atomSpec.length()+20);
384 sb.append("select ").append(atomSpec).append(getCommandSeparator())
385 .append("color").append(getColourString(colour));
386 return new StructureCommand(sb.toString());
390 protected String getResidueSpec(String residue)
396 * Generates a Jmol atomspec string like
399 * 2-5:A/1.1,8:A/1.1,5-10:B/2.1
402 * Parameter {@code alphaOnly} is not used here - this restriction is made by
403 * a separate clause in the {@code compare} (superposition) command.
406 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
408 StringBuilder sb = new StringBuilder(128);
410 boolean first = true;
411 for (String modelNo : model.getModels())
413 for (String chain : model.getChains(modelNo))
415 for (int[] range : model.getRanges(modelNo, chain))
422 if (range[0] == range[1])
428 sb.append(range[0]).append(HYPHEN).append(range[1]);
430 sb.append(COLON).append(chain.trim()).append(SLASH);
431 sb.append(String.valueOf(modelNo)).append(".1");
436 return sb.toString();
440 public List<StructureCommandI> showBackbone()
442 return Arrays.asList(SHOW_BACKBONE);
446 public StructureCommandI loadFile(String file)