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 jalview.structure.AtomSpecModel;
24 import jalview.util.ColorUtils;
25 import jalview.util.IntRangeComparator;
27 import java.awt.Color;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
33 * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
35 public class ChimeraXCommands extends ChimeraCommands
37 private static final String CMD_COLOUR_BY_CHARGE = "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow";
40 public String colourByCharge()
42 return CMD_COLOUR_BY_CHARGE;
46 public String getResidueSpec(String residue)
52 public String setBackgroundColour(Color col)
54 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/set.html
55 return "set bgColor " + ColorUtils.toTkCode(col);
59 public String getColourCommand(String atomSpec, Color colour)
61 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
62 String colourCode = getColourString(colour);
64 return "color " + atomSpec + " " + colourCode;
68 public String 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 String 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 sb.toString();
111 public String openCommandFile(String path)
113 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
114 return "open " + path;
118 public String saveSession(String filepath)
120 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
121 return "save session " + filepath;
125 * Returns the range(s) formatted as a ChimeraX atomspec, for example
127 * #1/A:2-20,30-40/B:10-20|#2/A:12-30
132 public String getAtomSpec(AtomSpecModel atomSpec, boolean alphaOnly)
134 StringBuilder sb = new StringBuilder(128);
135 boolean firstModel = true;
136 for (Integer model : atomSpec.getModels())
143 appendModel(sb, model, atomSpec);
148 // todo: is there ChimeraX syntax to exclude altlocs?
150 return sb.toString();
154 * A helper method to append an atomSpec string for atoms in the given model
160 protected void appendModel(StringBuilder sb, Integer model,
161 AtomSpecModel atomSpec)
163 sb.append("#").append(model);
165 for (String chain : atomSpec.getChains(model))
167 boolean firstPositionForChain = true;
168 sb.append("/").append(chain.trim()).append(":");
169 List<int[]> rangeList = atomSpec.getRanges(model, chain);
172 * sort ranges into ascending start position order
174 Collections.sort(rangeList, IntRangeComparator.ASCENDING);
176 int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
177 int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
179 Iterator<int[]> iterator = rangeList.iterator();
180 while (iterator.hasNext())
182 int[] range = iterator.next();
183 if (range[0] <= end + 1)
186 * range overlaps or is contiguous with the last one
187 * - so just extend the end position, and carry on
188 * (unless this is the last in the list)
190 end = Math.max(end, range[1]);
195 * we have a break so append the last range
197 appendRange(sb, start, end, chain, firstPositionForChain, true);
200 firstPositionForChain = false;
205 * and append the last range
207 if (!rangeList.isEmpty())
209 appendRange(sb, start, end, chain, firstPositionForChain, true);
211 firstPositionForChain = false;
216 public String showBackbone()
218 return "~display all;show @CA|P pbonds";
222 public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
225 * Form ChimeraX match command to match spec to ref
227 * match #1/A:2-94 toAtoms #2/A:1-93
230 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
232 StringBuilder cmd = new StringBuilder();
233 String atomSpec = getAtomSpec(spec, true);
234 String refSpec = getAtomSpec(ref, true);
235 cmd.append("align ").append(atomSpec).append(" toAtoms ")
239 * show superposed residues as ribbon, others as chain
241 cmd.append("; ribbon ");
242 cmd.append(getAtomSpec(spec, false)).append("|");
243 cmd.append(getAtomSpec(ref, false)).append("; view");
245 return cmd.toString();