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.rbvi.chimera;
23 import java.util.Locale;
25 import java.awt.Color;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
31 import jalview.structure.AtomSpecModel;
32 import jalview.structure.StructureCommand;
33 import jalview.structure.StructureCommandI;
34 import jalview.structure.StructureCommandsBase;
35 import jalview.util.ColorUtils;
38 * Routines for generating Chimera commands for Jalview/Chimera binding
43 public class ChimeraCommands extends StructureCommandsBase
45 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/focus.html
46 private static final StructureCommand FOCUS_VIEW = new StructureCommand("focus");
48 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/listen.html#listresattr
49 private static final StructureCommand LIST_RESIDUE_ATTRIBUTES = new StructureCommand("list resattr");
51 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/stop.html
52 private static final StructureCommand CLOSE_CHIMERA = new StructureCommand("stop really");
54 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/listen.html
55 private static final StructureCommand STOP_NOTIFY_SELECTION = new StructureCommand("listen stop selection");
57 private static final StructureCommand STOP_NOTIFY_MODELS = new StructureCommand("listen stop models");
59 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/listen.html#listselection
60 private static final StructureCommand GET_SELECTION = new StructureCommand("list selection level residue");
62 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
63 "~display all;~ribbon;chain @CA|P");
65 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
66 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
68 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/rainbow.html
69 private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
72 // Chimera clause to exclude alternate locations in atom selection
73 private static final String NO_ALTLOCS = "&~@.B-Z&~@.2-9";
76 public StructureCommandI colourResidues(String atomSpec, Color colour)
78 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/color.html
79 String colourCode = getColourString(colour);
80 return new StructureCommand("color " + colourCode + " " + atomSpec);
84 * Returns a colour formatted suitable for use in viewer command syntax
89 protected String getColourString(Color colour)
91 return ColorUtils.toTkCode(colour);
95 * Traverse the map of features/values/models/chains/positions to construct a
96 * list of 'setattr' commands (one per distinct feature type and value).
98 * The format of each command is
101 * <blockquote> setattr r <featureName> " " #modelnumber:range.chain
102 * e.g. setattr r jv_chain <value> #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
110 public List<StructureCommandI> setAttributes(
111 Map<String, Map<Object, AtomSpecModel>> featureMap)
113 List<StructureCommandI> commands = new ArrayList<>();
114 for (String featureType : featureMap.keySet())
116 String attributeName = makeAttributeName(featureType);
119 * clear down existing attributes for this feature
121 // 'problem' - sets attribute to None on all residues - overkill?
122 // commands.add("~setattr r " + attributeName + " :*");
124 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
125 for (Object value : values.keySet())
128 * for each distinct value recorded for this feature type,
129 * add a command to set the attribute on the mapped residues
130 * Put values in single quotes, encoding any embedded single quotes
132 AtomSpecModel atomSpecModel = values.get(value);
133 String featureValue = value.toString();
134 featureValue = featureValue.replaceAll("\\'", "'");
135 StructureCommandI cmd = setAttribute(attributeName, featureValue,
145 * Returns a viewer command to set the given residue attribute value on
146 * residues specified by the AtomSpecModel, for example
149 * setatr res jv_chain 'primary' #1:12-34,48-55.B
152 * @param attributeName
153 * @param attributeValue
154 * @param atomSpecModel
157 protected StructureCommandI setAttribute(String attributeName,
158 String attributeValue,
159 AtomSpecModel atomSpecModel)
161 StringBuilder sb = new StringBuilder(128);
162 sb.append("setattr res ").append(attributeName).append(" '")
163 .append(attributeValue).append("' ");
164 sb.append(getAtomSpec(atomSpecModel, false));
165 return new StructureCommand(sb.toString());
169 * Makes a prefixed and valid Chimera attribute name. A jv_ prefix is applied
170 * for a 'Jalview' namespace, and any non-alphanumeric character is converted
175 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/setattr.html
178 protected String makeAttributeName(String featureType)
180 String attName = super.makeAttributeName(featureType);
183 * Chimera treats an attribute name ending in 'color' as colour-valued;
184 * Jalview doesn't, so prevent this by appending an underscore
186 if (attName.toUpperCase(Locale.ROOT).endsWith("COLOR"))
195 public StructureCommandI colourByChain()
197 return COLOUR_BY_CHAIN;
201 public List<StructureCommandI> colourByCharge()
203 return Arrays.asList(COLOUR_BY_CHARGE);
207 public String getResidueSpec(String residue)
209 return "::" + residue;
213 public StructureCommandI setBackgroundColour(Color col)
215 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/set.html#bgcolor
216 return new StructureCommand("set bgColor " + ColorUtils.toTkCode(col));
220 public StructureCommandI focusView()
226 public List<StructureCommandI> showChains(List<String> toShow)
229 * Construct a chimera command like
231 * ~display #*;~ribbon #*;ribbon :.A,:.B
233 StringBuilder cmd = new StringBuilder(64);
234 boolean first = true;
235 for (String chain : toShow)
237 String[] tokens = chain.split(":");
238 if (tokens.length == 2)
240 String showChainCmd = tokens[0] + ":." + tokens[1];
245 cmd.append(showChainCmd);
251 * could append ";focus" to this command to resize the display to fill the
252 * window, but it looks more helpful not to (easier to relate chains to the
255 final String command = "~display #*; ~ribbon #*; ribbon :"
257 return Arrays.asList(new StructureCommand(command));
261 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
265 * Form Chimera match command to match spec to ref
266 * (the first set of atoms are moved on to the second)
268 * match #1:1-30.B,81-100.B@CA #0:21-40.A,61-90.A@CA
270 * @see https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
272 StringBuilder cmd = new StringBuilder();
273 String atomSpecAlphaOnly = getAtomSpec(spec, true);
274 String refSpecAlphaOnly = getAtomSpec(ref, true);
275 cmd.append("match ").append(atomSpecAlphaOnly).append(" ").append(refSpecAlphaOnly);
278 * show superposed residues as ribbon
280 String atomSpec = getAtomSpec(spec, false);
281 String refSpec = getAtomSpec(ref, false);
282 cmd.append("; ribbon ");
283 cmd.append(atomSpec).append("|").append(refSpec).append("; focus");
285 return Arrays.asList(new StructureCommand(cmd.toString()));
289 public StructureCommandI openCommandFile(String path)
291 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
292 return new StructureCommand("open cmd:" + path);
296 public StructureCommandI saveSession(String filepath)
298 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/save.html
299 return new StructureCommand("save " + filepath);
303 * Returns the range(s) modelled by {@code atomSpec} formatted as a Chimera
304 * atomspec string, e.g.
307 * #0:15.A,28.A,54.A,70-72.A|#1:2.A,6.A,11.A,13-14.A
312 * <li>#0 is a model number</li>
313 * <li>15 or 70-72 is a residue number, or range of residue numbers</li>
314 * <li>.A is a chain identifier</li>
315 * <li>residue ranges are separated by comma</li>
316 * <li>atomspecs for distinct models are separated by | (or)</li>
324 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/frameatom_spec.html
327 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
329 StringBuilder sb = new StringBuilder(128);
330 boolean firstModel = true;
331 for (String model : atomSpec.getModels())
338 appendModel(sb, model, atomSpec, alphaOnly);
340 return sb.toString();
344 * A helper method to append an atomSpec string for atoms in the given model
351 protected void appendModel(StringBuilder sb, String model,
352 AtomSpecModel atomSpec, boolean alphaOnly)
354 sb.append("#").append(model).append(":");
356 boolean firstPositionForModel = true;
358 for (String chain : atomSpec.getChains(model))
360 chain = " ".equals(chain) ? chain : chain.trim();
362 List<int[]> rangeList = atomSpec.getRanges(model, chain);
363 for (int[] range : rangeList)
365 appendRange(sb, range[0], range[1], chain, firstPositionForModel,
367 firstPositionForModel = false;
373 * restrict to alpha carbon, no alternative locations
374 * (needed to ensuring matching atom counts for superposition)
376 // TODO @P instead if RNA - add nucleotide flag to AtomSpecModel?
377 sb.append("@CA").append(NO_ALTLOCS);
382 public List<StructureCommandI> showBackbone()
384 return Arrays.asList(SHOW_BACKBONE);
388 public StructureCommandI loadFile(String file)
390 return new StructureCommand("open " + file);
394 public StructureCommandI openSession(String filepath)
396 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
397 // this version of the command has no dependency on file extension
398 return new StructureCommand("open chimera:" + filepath);
402 public StructureCommandI closeViewer()
404 return CLOSE_CHIMERA;
408 public List<StructureCommandI> startNotifications(String uri)
410 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/listen.html
411 List<StructureCommandI> cmds = new ArrayList<>();
412 cmds.add(new StructureCommand("listen start models url " + uri));
413 cmds.add(new StructureCommand("listen start select prefix SelectionChanged url " + uri));
418 public List<StructureCommandI> stopNotifications()
420 List<StructureCommandI> cmds = new ArrayList<>();
421 cmds.add(STOP_NOTIFY_MODELS);
422 cmds.add(STOP_NOTIFY_SELECTION);
427 public StructureCommandI getSelectedResidues()
429 return GET_SELECTION;
433 public StructureCommandI listResidueAttributes()
435 return LIST_RESIDUE_ATTRIBUTES;
439 public StructureCommandI getResidueAttributes(String attName)
441 // this alternative command
442 // list residues spec ':*/attName' attr attName
443 // doesn't report 'None' values (which is good), but
444 // fails for 'average.bfactor' (which is bad):
445 return new StructureCommand("list residues attr '" + attName + "'");