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;
28 import jalview.structure.AtomSpecModel;
29 import jalview.structure.StructureCommand;
30 import jalview.structure.StructureCommandI;
33 * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
35 public class ChimeraXCommands extends ChimeraCommands
37 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#resattr
38 private static final StructureCommand LIST_RESIDUE_ATTRIBUTES = new StructureCommand(
41 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/exit.html
42 private static final StructureCommand CLOSE_CHIMERAX = new StructureCommand(
45 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#notify
46 private static final StructureCommand STOP_NOTIFY_SELECTION = new StructureCommand(
47 "info notify stop selection jalview");
49 private static final StructureCommand STOP_NOTIFY_MODELS = new StructureCommand(
50 "info notify stop models jalview");
52 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#selection
53 private static final StructureCommand GET_SELECTION = new StructureCommand(
54 "info selection level residue");
56 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
57 "~display all;~ribbon;show @CA|P atoms");
59 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
60 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
63 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
64 "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
67 public List<StructureCommandI> colourByCharge()
69 return Arrays.asList(COLOUR_BY_CHARGE);
73 public String getResidueSpec(String residue)
79 public StructureCommandI colourResidues(String atomSpec, Color colour)
81 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
82 String colourCode = getColourString(colour);
84 return new StructureCommand("color " + atomSpec + " " + colourCode);
88 public StructureCommandI focusView()
94 * Returns a viewer command to set the given residue attribute value on
95 * residues specified by the AtomSpecModel, for example
98 * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
101 * @param attributeName
102 * @param attributeValue
103 * @param atomSpecModel
107 protected StructureCommandI setAttribute(String attributeName,
108 String attributeValue, AtomSpecModel atomSpecModel)
110 StringBuilder sb = new StringBuilder(128);
111 sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
112 sb.append(" res ").append(attributeName).append(" '")
113 .append(attributeValue).append("'");
114 sb.append(" create true");
115 return new StructureCommand(sb.toString());
119 public StructureCommandI openCommandFile(String path)
121 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
122 return new StructureCommand("open " + path);
126 public StructureCommandI saveSession(String filepath)
128 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
129 // note ChimeraX will append ".cxs" to the filepath!
130 return new StructureCommand("save " + filepath + " format session");
134 * Returns the range(s) formatted as a ChimeraX atomspec, for example
136 * #1/A:2-20,30-40/B:10-20|#2/A:12-30
141 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
143 StringBuilder sb = new StringBuilder(128);
144 boolean firstModel = true;
145 for (String model : atomSpec.getModels())
152 appendModel(sb, model, atomSpec);
155 // TODO @P if RNA - add nucleotide flag to AtomSpecModel?
158 // todo: is there ChimeraX syntax to exclude altlocs?
160 return sb.toString();
164 * A helper method to append an atomSpec string for atoms in the given model
170 protected void appendModel(StringBuilder sb, String model,
171 AtomSpecModel atomSpec)
173 sb.append("#").append(model);
175 for (String chain : atomSpec.getChains(model))
177 boolean firstPositionForChain = true;
178 sb.append("/").append(chain.trim()).append(":");
179 List<int[]> rangeList = atomSpec.getRanges(model, chain);
180 boolean first = true;
181 for (int[] range : rangeList)
188 appendRange(sb, range[0], range[1], chain, firstPositionForChain,
195 public List<StructureCommandI> showBackbone()
197 return Arrays.asList(SHOW_BACKBONE);
201 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
205 * Form ChimeraX match command to match spec to ref
207 * match #1/A:2-94 toAtoms #2/A:1-93
209 * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
211 StringBuilder cmd = new StringBuilder();
212 String atomSpec = getAtomSpec(spec, true);
213 String refSpec = getAtomSpec(ref, true);
214 cmd.append("align ").append(atomSpec).append(" toAtoms ")
218 * show superposed residues as ribbon, others as chain
220 cmd.append("; ribbon ");
221 cmd.append(getAtomSpec(spec, false)).append("|");
222 cmd.append(getAtomSpec(ref, false)).append("; view");
224 return Arrays.asList(new StructureCommand(cmd.toString()));
228 public StructureCommandI openSession(String filepath)
230 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
231 // this version of the command has no dependency on file extension
232 return new StructureCommand("open " + filepath + " format session");
236 public StructureCommandI hideChain(String modelId, String chainId)
238 String cmd = "~ribbon #" + modelId + "/" + chainId;
239 return new StructureCommand(cmd);
242 public StructureCommandI closeViewer()
244 return CLOSE_CHIMERAX;
248 public List<StructureCommandI> startNotifications(String uri)
250 List<StructureCommandI> cmds = new ArrayList<>();
251 cmds.add(new StructureCommand(
252 "info notify start models prefix ModelChanged jalview url "
254 cmds.add(new StructureCommand(
255 "info notify start selection jalview prefix SelectionChanged url "
261 public List<StructureCommandI> stopNotifications()
263 List<StructureCommandI> cmds = new ArrayList<>();
264 cmds.add(STOP_NOTIFY_MODELS);
265 cmds.add(STOP_NOTIFY_SELECTION);
270 public StructureCommandI getSelectedResidues()
272 return GET_SELECTION;
276 public StructureCommandI listResidueAttributes()
278 return LIST_RESIDUE_ATTRIBUTES;