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.util.ColorUtils;
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("info resattr");
41 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/exit.html
42 private static final StructureCommand CLOSE_CHIMERAX = new StructureCommand("exit");
44 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#notify
45 private static final StructureCommand STOP_NOTIFY_SELECTION = new StructureCommand("info notify stop selection jalview");
47 private static final StructureCommand STOP_NOTIFY_MODELS = new StructureCommand("info notify stop models jalview");
49 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/info.html#selection
50 private static final StructureCommand GET_SELECTION = new StructureCommand(
51 "info selection level residue");
53 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
54 "~display all;~ribbon;show @CA|P atoms");
56 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
57 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
60 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
61 "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
64 public List<StructureCommandI> colourByCharge()
66 return Arrays.asList(COLOUR_BY_CHARGE);
70 public String getResidueSpec(String residue)
76 public StructureCommandI colourResidues(String atomSpec, Color colour)
78 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
79 String colourCode = getColourString(colour);
81 return new StructureCommand("color " + atomSpec + " " + colourCode);
85 public StructureCommandI focusView()
96 public int getModelStartNo()
102 * Returns a viewer command to set the given residue attribute value on
103 * residues specified by the AtomSpecModel, for example
106 * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
109 * @param attributeName
110 * @param attributeValue
111 * @param atomSpecModel
115 protected StructureCommandI setAttribute(String attributeName,
116 String attributeValue, AtomSpecModel atomSpecModel)
118 StringBuilder sb = new StringBuilder(128);
119 sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
120 sb.append(" res ").append(attributeName).append(" '")
121 .append(attributeValue).append("'");
122 sb.append(" create true");
123 return new StructureCommand(sb.toString());
127 public StructureCommandI openCommandFile(String path)
129 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
130 return new StructureCommand("open " + path);
134 public StructureCommandI saveSession(String filepath)
136 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
137 // note ChimeraX will append ".cxs" to the filepath!
138 return new StructureCommand("save " + filepath + " format session");
142 * Returns the range(s) formatted as a ChimeraX atomspec, for example
144 * #1/A:2-20,30-40/B:10-20|#2/A:12-30
149 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
151 StringBuilder sb = new StringBuilder(128);
152 boolean firstModel = true;
153 for (String model : atomSpec.getModels())
160 appendModel(sb, model, atomSpec);
163 // TODO @P if RNA - add nucleotide flag to AtomSpecModel?
166 // todo: is there ChimeraX syntax to exclude altlocs?
168 return sb.toString();
172 * A helper method to append an atomSpec string for atoms in the given model
178 protected void appendModel(StringBuilder sb, String model,
179 AtomSpecModel atomSpec)
181 sb.append("#").append(model);
183 for (String chain : atomSpec.getChains(model))
185 boolean firstPositionForChain = true;
186 sb.append("/").append(chain.trim()).append(":");
187 List<int[]> rangeList = atomSpec.getRanges(model, chain);
188 boolean first = true;
189 for (int[] range : rangeList)
196 appendRange(sb, range[0], range[1], chain, firstPositionForChain,
203 public List<StructureCommandI> showBackbone()
205 return Arrays.asList(SHOW_BACKBONE);
209 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
213 * Form ChimeraX match command to match spec to ref
215 * match #1/A:2-94 toAtoms #2/A:1-93
217 * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
219 StringBuilder cmd = new StringBuilder();
220 String atomSpec = getAtomSpec(spec, true);
221 String refSpec = getAtomSpec(ref, true);
222 cmd.append("align ").append(atomSpec).append(" toAtoms ")
226 * show superposed residues as ribbon, others as chain
228 cmd.append("; ribbon ");
229 cmd.append(getAtomSpec(spec, false)).append("|");
230 cmd.append(getAtomSpec(ref, false)).append("; view");
232 return Arrays.asList(new StructureCommand(cmd.toString()));
236 public StructureCommandI openSession(String filepath)
238 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
239 // this version of the command has no dependency on file extension
240 return new StructureCommand("open " + filepath + " format session");
244 public StructureCommandI closeViewer()
246 return CLOSE_CHIMERAX;
250 public List<StructureCommandI> startNotifications(String uri)
252 List<StructureCommandI> cmds = new ArrayList<>();
253 cmds.add(new StructureCommand("info notify start models prefix ModelChanged jalview url " + uri));
254 cmds.add(new StructureCommand("info notify start selection jalview prefix SelectionChanged url " + uri));
259 public List<StructureCommandI> stopNotifications()
261 List<StructureCommandI> cmds = new ArrayList<>();
262 cmds.add(STOP_NOTIFY_MODELS);
263 cmds.add(STOP_NOTIFY_SELECTION);
268 public StructureCommandI getSelectedResidues()
270 return GET_SELECTION;
274 public StructureCommandI listResidueAttributes()
276 return LIST_RESIDUE_ATTRIBUTES;