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.awt.Color;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
29 import jalview.structure.AtomSpecModel;
30 import jalview.structure.StructureCommand;
31 import jalview.structure.StructureCommandI;
32 import jalview.structure.StructureCommandsBase;
33 import jalview.util.ColorUtils;
36 * Routines for generating Chimera commands for Jalview/Chimera binding
41 public class ChimeraCommands extends StructureCommandsBase
43 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
44 "~display all;~ribbon;chain @CA|P");
46 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
47 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
49 private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
52 // Chimera clause to exclude alternate locations in atom selection
53 private static final String NO_ALTLOCS = "&~@.B-Z&~@.2-9";
56 public StructureCommandI colourResidues(String atomSpec, Color colour)
58 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/color.html
59 String colourCode = getColourString(colour);
60 return new StructureCommand("color " + colourCode + " " + atomSpec);
64 * Returns a colour formatted suitable for use in viewer command syntax
69 protected String getColourString(Color colour)
71 return ColorUtils.toTkCode(colour);
75 * Traverse the map of features/values/models/chains/positions to construct a
76 * list of 'setattr' commands (one per distinct feature type and value).
78 * The format of each command is
81 * <blockquote> setattr r <featureName> " " #modelnumber:range.chain
82 * e.g. setattr r jv_chain <value> #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
90 public List<StructureCommandI> setAttributes(
91 Map<String, Map<Object, AtomSpecModel>> featureMap)
93 List<StructureCommandI> commands = new ArrayList<>();
94 for (String featureType : featureMap.keySet())
96 String attributeName = makeAttributeName(featureType);
99 * clear down existing attributes for this feature
101 // 'problem' - sets attribute to None on all residues - overkill?
102 // commands.add("~setattr r " + attributeName + " :*");
104 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
105 for (Object value : values.keySet())
108 * for each distinct value recorded for this feature type,
109 * add a command to set the attribute on the mapped residues
110 * Put values in single quotes, encoding any embedded single quotes
112 AtomSpecModel atomSpecModel = values.get(value);
113 String featureValue = value.toString();
114 featureValue = featureValue.replaceAll("\\'", "'");
115 StructureCommandI cmd = setAttribute(attributeName, featureValue,
125 * Returns a viewer command to set the given residue attribute value on
126 * residues specified by the AtomSpecModel, for example
129 * setatr res jv_chain 'primary' #1:12-34,48-55.B
132 * @param attributeName
133 * @param attributeValue
134 * @param atomSpecModel
137 protected StructureCommandI setAttribute(String attributeName,
138 String attributeValue,
139 AtomSpecModel atomSpecModel)
141 StringBuilder sb = new StringBuilder(128);
142 sb.append("setattr res ").append(attributeName).append(" '")
143 .append(attributeValue).append("' ");
144 sb.append(getAtomSpec(atomSpecModel, false));
145 return new StructureCommand(sb.toString());
149 * Makes a prefixed and valid Chimera attribute name. A jv_ prefix is applied
150 * for a 'Jalview' namespace, and any non-alphanumeric character is converted
155 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/setattr.html
157 protected String makeAttributeName(String featureType)
159 String attName = super.makeAttributeName(featureType);
162 * Chimera treats an attribute name ending in 'color' as colour-valued;
163 * Jalview doesn't, so prevent this by appending an underscore
165 if (attName.toUpperCase().endsWith("COLOR"))
174 public StructureCommandI colourByChain()
176 return COLOUR_BY_CHAIN;
180 public List<StructureCommandI> colourByCharge()
182 return Arrays.asList(COLOUR_BY_CHARGE);
186 public String getResidueSpec(String residue)
188 return "::" + residue;
192 public StructureCommandI setBackgroundColour(Color col)
194 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/set.html#bgcolor
195 return new StructureCommand("set bgColor " + ColorUtils.toTkCode(col));
199 public StructureCommandI focusView()
201 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/focus.html
202 return new StructureCommand("focus");
206 public List<StructureCommandI> showChains(List<String> toShow)
209 * Construct a chimera command like
211 * ~display #*;~ribbon #*;ribbon :.A,:.B
213 StringBuilder cmd = new StringBuilder(64);
214 boolean first = true;
215 for (String chain : toShow)
217 String[] tokens = chain.split(":");
218 if (tokens.length == 2)
220 String showChainCmd = tokens[0] + ":." + tokens[1];
225 cmd.append(showChainCmd);
231 * could append ";focus" to this command to resize the display to fill the
232 * window, but it looks more helpful not to (easier to relate chains to the
235 final String command = "~display #*; ~ribbon #*; ribbon :"
237 return Arrays.asList(new StructureCommand(command));
241 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
245 * Form Chimera match command to match spec to ref
246 * (the first set of atoms are moved on to the second)
248 * match #1:1-30.B,81-100.B@CA #0:21-40.A,61-90.A@CA
250 * @see https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
252 StringBuilder cmd = new StringBuilder();
253 String atomSpecAlphaOnly = getAtomSpec(spec, true);
254 String refSpecAlphaOnly = getAtomSpec(ref, true);
255 cmd.append("match ").append(atomSpecAlphaOnly).append(" ").append(refSpecAlphaOnly);
258 * show superposed residues as ribbon
260 String atomSpec = getAtomSpec(spec, false);
261 String refSpec = getAtomSpec(ref, false);
262 cmd.append("; ribbon ");
263 cmd.append(atomSpec).append("|").append(refSpec).append("; focus");
265 return Arrays.asList(new StructureCommand(cmd.toString()));
269 public StructureCommandI openCommandFile(String path)
271 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/filetypes.html
272 return new StructureCommand("open cmd:" + path);
276 public StructureCommandI saveSession(String filepath)
278 // https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/save.html
279 return new StructureCommand("save " + filepath);
283 * Returns the range(s) modelled by {@code atomSpec} formatted as a Chimera
284 * atomspec string, e.g.
287 * #0:15.A,28.A,54.A,70-72.A|#1:2.A,6.A,11.A,13-14.A
292 * <li>#0 is a model number</li>
293 * <li>15 or 70-72 is a residue number, or range of residue numbers</li>
294 * <li>.A is a chain identifier</li>
295 * <li>residue ranges are separated by comma</li>
296 * <li>atomspecs for distinct models are separated by | (or)</li>
304 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/frameatom_spec.html
307 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
309 StringBuilder sb = new StringBuilder(128);
310 boolean firstModel = true;
311 for (String model : atomSpec.getModels())
318 appendModel(sb, model, atomSpec, alphaOnly);
320 return sb.toString();
324 * A helper method to append an atomSpec string for atoms in the given model
331 protected void appendModel(StringBuilder sb, String model,
332 AtomSpecModel atomSpec, boolean alphaOnly)
334 sb.append("#").append(model).append(":");
336 boolean firstPositionForModel = true;
338 for (String chain : atomSpec.getChains(model))
340 chain = " ".equals(chain) ? chain : chain.trim();
342 List<int[]> rangeList = atomSpec.getRanges(model, chain);
343 for (int[] range : rangeList)
345 appendRange(sb, range[0], range[1], chain, firstPositionForModel,
347 firstPositionForModel = false;
353 * restrict to alpha carbon, no alternative locations
354 * (needed to ensuring matching atom counts for superposition)
356 // TODO @P instead if RNA - add nucleotide flag to AtomSpecModel?
357 sb.append("@CA").append(NO_ALTLOCS);
362 public List<StructureCommandI> showBackbone()
364 return Arrays.asList(SHOW_BACKBONE);
368 public StructureCommandI loadFile(String file)
370 return new StructureCommand("open " + file);
374 * Overrides the default method to concatenate colour commands into one
377 public List<StructureCommandI> colourBySequence(
378 Map<Object, AtomSpecModel> colourMap)
380 List<StructureCommandI> commands = new ArrayList<>();
381 StringBuilder sb = new StringBuilder(colourMap.size() * 20);
382 boolean first = true;
383 for (Object key : colourMap.keySet())
385 Color colour = (Color) key;
386 final AtomSpecModel colourData = colourMap.get(colour);
387 StructureCommandI command = getColourCommand(colourData, colour);
390 sb.append(getCommandSeparator());
393 sb.append(command.getCommand());
396 commands.add(new StructureCommand(sb.toString()));