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.Arrays;
25 import java.util.List;
27 import jalview.structure.AtomSpecModel;
28 import jalview.structure.StructureCommand;
29 import jalview.structure.StructureCommandI;
30 import jalview.util.ColorUtils;
33 * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
35 public class ChimeraXCommands extends ChimeraCommands
37 private static final StructureCommand SHOW_BACKBONE = new StructureCommand(
38 "~display all;~ribbon;show @CA|P atoms");
40 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
43 private static final StructureCommandI COLOUR_BY_CHARGE = new StructureCommand(
44 "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
47 public List<StructureCommandI> colourByCharge()
49 return Arrays.asList(COLOUR_BY_CHARGE);
53 public String getResidueSpec(String residue)
59 public StructureCommandI colourResidues(String atomSpec, Color colour)
61 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
62 String colourCode = getColourString(colour);
64 return new StructureCommand("color " + atomSpec + " " + colourCode);
68 public StructureCommandI focusView()
70 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
80 public int getModelStartNo()
86 * Returns a viewer command to set the given residue attribute value on
87 * residues specified by the AtomSpecModel, for example
90 * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
93 * @param attributeName
94 * @param attributeValue
95 * @param atomSpecModel
99 protected StructureCommandI setAttribute(String attributeName,
100 String attributeValue, AtomSpecModel atomSpecModel)
102 StringBuilder sb = new StringBuilder(128);
103 sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
104 sb.append(" res ").append(attributeName).append(" '")
105 .append(attributeValue).append("'");
106 sb.append(" create true");
107 return new StructureCommand(sb.toString());
111 public StructureCommandI openCommandFile(String path)
113 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
114 return new StructureCommand("open " + path);
118 public StructureCommandI saveSession(String filepath)
120 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
121 // note ChimeraX will append ".cxs" to the filepath!
122 return new StructureCommand("save " + filepath + " format session");
126 * Returns the range(s) formatted as a ChimeraX atomspec, for example
128 * #1/A:2-20,30-40/B:10-20|#2/A:12-30
133 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
135 StringBuilder sb = new StringBuilder(128);
136 boolean firstModel = true;
137 for (String model : atomSpec.getModels())
144 appendModel(sb, model, atomSpec);
147 // TODO @P if RNA - add nucleotide flag to AtomSpecModel?
150 // todo: is there ChimeraX syntax to exclude altlocs?
152 return sb.toString();
156 * A helper method to append an atomSpec string for atoms in the given model
162 protected void appendModel(StringBuilder sb, String model,
163 AtomSpecModel atomSpec)
165 sb.append("#").append(model);
167 for (String chain : atomSpec.getChains(model))
169 boolean firstPositionForChain = true;
170 sb.append("/").append(chain.trim()).append(":");
171 List<int[]> rangeList = atomSpec.getRanges(model, chain);
172 boolean first = true;
173 for (int[] range : rangeList)
180 appendRange(sb, range[0], range[1], chain, firstPositionForChain,
187 public List<StructureCommandI> showBackbone()
189 return Arrays.asList(SHOW_BACKBONE);
193 public List<StructureCommandI> superposeStructures(AtomSpecModel ref,
197 * Form ChimeraX match command to match spec to ref
199 * match #1/A:2-94 toAtoms #2/A:1-93
201 * @see https://www.cgl.ucsf.edu/chimerax/docs/user/commands/align.html
203 StringBuilder cmd = new StringBuilder();
204 String atomSpec = getAtomSpec(spec, true);
205 String refSpec = getAtomSpec(ref, true);
206 cmd.append("align ").append(atomSpec).append(" toAtoms ")
210 * show superposed residues as ribbon, others as chain
212 cmd.append("; ribbon ");
213 cmd.append(getAtomSpec(spec, false)).append("|");
214 cmd.append(getAtomSpec(ref, false)).append("; view");
216 return Arrays.asList(new StructureCommand(cmd.toString()));
220 public StructureCommandI openSession(String filepath)
222 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html#composite
223 // this version of the command has no dependency on file extension
224 return new StructureCommand("open " + filepath + " format session");
228 public StructureCommandI closeViewer()
230 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/exit.html
231 return new StructureCommand("exit");