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;
31 import jalview.structure.StructureCommandsI.AtomSpecType;
34 * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
36 public class ChimeraXCommands extends ChimeraCommands
38 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#resattr
39 private static final StructureCommand LIST_RESIDUE_ATTRIBUTES = new StructureCommand(
42 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/exit.html
43 private static final StructureCommand CLOSE_CHIMERAX = new StructureCommand(
46 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#notify
47 private static final StructureCommand STOP_NOTIFY_SELECTION = new StructureCommand(
48 "info notify stop selection jalview");
50 private static final StructureCommand STOP_NOTIFY_MODELS = new StructureCommand(
51 "info notify stop models jalview");
53 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#selection
54 private static final StructureCommand GET_SELECTION = new StructureCommand(
55 "info selection level residue");
57 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
58 "~display all;~ribbon;show @CA|P atoms");
60 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
61 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
64 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
65 "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
68 public List<StructureCommandI> colourByCharge()
70 return Arrays.asList(COLOUR_BY_CHARGE);
74 public String getResidueSpec(String residue)
80 public StructureCommandI colourResidues(String atomSpec, Color colour)
82 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
83 String colourCode = getColourString(colour);
85 return new StructureCommand("color " + atomSpec + " " + colourCode);
89 public StructureCommandI focusView()
100 public int getModelStartNo()
106 * Returns a viewer command to set the given residue attribute value on
107 * residues specified by the AtomSpecModel, for example
110 * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
113 * @param attributeName
114 * @param attributeValue
115 * @param atomSpecModel
119 protected StructureCommandI setAttribute(String attributeName,
120 String attributeValue, AtomSpecModel atomSpecModel)
122 StringBuilder sb = new StringBuilder(128);
123 sb.append("setattr ")
124 .append(getAtomSpec(atomSpecModel, AtomSpecType.RESIDUE_ONLY));
125 sb.append(" res ").append(attributeName).append(" '")
126 .append(attributeValue).append("'");
127 sb.append(" create true");
128 return new StructureCommand(sb.toString());
132 public StructureCommandI openCommandFile(String path)
134 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
135 return new StructureCommand("open " + path);
139 public StructureCommandI saveSession(String filepath)
141 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
142 // note ChimeraX will append ".cxs" to the filepath!
143 return new StructureCommand("save " + filepath + " format session");
147 * Returns the range(s) formatted as a ChimeraX atomspec, for example
149 * #1/A:2-20,30-40/B:10-20|#2/A:12-30
151 * Note there is no need to explicitly exclude ALTLOC atoms when
152 * {@code alphaOnly == true}, as this is the default behaviour of ChimeraX (a
153 * change from Chimera)
158 public String getAtomSpec(AtomSpecModel atomSpec, AtomSpecType specType)
160 StringBuilder sb = new StringBuilder(128);
161 boolean firstModel = true;
162 for (String model : atomSpec.getModels())
169 appendModel(sb, model, atomSpec);
170 if (specType == AtomSpecType.ALPHA)
174 if (specType == AtomSpecType.PHOSPHATE)
179 return sb.toString();
183 * A helper method to append an atomSpec string for atoms in the given model
189 protected void appendModel(StringBuilder sb, String model,
190 AtomSpecModel atomSpec)
192 sb.append("#").append(model);
194 for (String chain : atomSpec.getChains(model))
196 boolean firstPositionForChain = true;
197 sb.append("/").append(chain.trim()).append(":");
198 List<int[]> rangeList = atomSpec.getRanges(model, chain);
199 boolean first = true;
200 for (int[] range : rangeList)
207 appendRange(sb, range[0], range[1], chain, firstPositionForChain,
214 public List<StructureCommandI> showBackbone()
216 return Arrays.asList(SHOW_BACKBONE);
220 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
221 AtomSpecModel spec, AtomSpecType backbone)
224 * Form ChimeraX match command to match spec to ref
226 * match #1/A:2-94 toAtoms #2/A:1-93
228 * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
230 StringBuilder cmd = new StringBuilder();
231 String atomSpec = getAtomSpec(spec, backbone);
232 String refSpec = getAtomSpec(ref, backbone);
233 cmd.append("align ").append(atomSpec).append(" toAtoms ")
237 * show superposed residues as ribbon, others as chain
239 cmd.append("; ribbon ");
240 cmd.append(getAtomSpec(spec, AtomSpecType.RESIDUE_ONLY)).append("|");
241 cmd.append(getAtomSpec(ref, AtomSpecType.RESIDUE_ONLY))
244 return Arrays.asList(new StructureCommand(cmd.toString()));
248 public StructureCommandI openSession(String filepath)
250 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
251 // this version of the command has no dependency on file extension
252 return new StructureCommand("open " + filepath + " format session");
256 public StructureCommandI closeViewer()
258 return CLOSE_CHIMERAX;
262 public List<StructureCommandI> startNotifications(String uri)
264 List<StructureCommandI> cmds = new ArrayList<>();
265 cmds.add(new StructureCommand(
266 "info notify start models jalview prefix ModelChanged url "
268 cmds.add(new StructureCommand(
269 "info notify start selection jalview prefix SelectionChanged url "
275 public List<StructureCommandI> stopNotifications()
277 List<StructureCommandI> cmds = new ArrayList<>();
278 cmds.add(STOP_NOTIFY_MODELS);
279 cmds.add(STOP_NOTIFY_SELECTION);
284 public StructureCommandI getSelectedResidues()
286 return GET_SELECTION;
290 public StructureCommandI listResidueAttributes()
292 return LIST_RESIDUE_ATTRIBUTES;