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 protected String getColourCommand(AtomSpecModel colourData,
62 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/color.html
63 String colourCode = getColourString(colour);
65 return "color " + getAtomSpec(colourData, false) + " " + colourCode;
69 public String focusView()
71 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/view.html
81 public int getModelStartNo()
87 * Returns a viewer command to set the given residue attribute value on
88 * residues specified by the AtomSpecModel, for example
91 * setattr #0/A:3-9,14-20,39-43 res jv_strand 'strand' create true
94 * @param attributeName
95 * @param attributeValue
96 * @param atomSpecModel
100 protected String getSetAttributeCommand(String attributeName,
101 String attributeValue, AtomSpecModel atomSpecModel)
103 StringBuilder sb = new StringBuilder(128);
104 sb.append("setattr ").append(getAtomSpec(atomSpecModel, false));
105 sb.append(" res ").append(attributeName).append(" '")
106 .append(attributeValue).append("'");
107 sb.append(" create true");
108 return sb.toString();
112 public String openCommandFile(String path)
114 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/open.html
115 return "open " + path;
119 public String saveSession(String filepath)
121 // https://www.cgl.ucsf.edu/chimerax/docs/user/commands/save.html
122 return "save session " + filepath;
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 (Integer model : atomSpec.getModels())
144 appendModel(sb, model, atomSpec);
149 // todo: is there ChimeraX syntax to exclude altlocs?
151 return sb.toString();
155 * A helper method to append an atomSpec string for atoms in the given model
161 protected void appendModel(StringBuilder sb, Integer model,
162 AtomSpecModel atomSpec)
164 sb.append("#").append(model);
166 for (String chain : atomSpec.getChains(model))
168 boolean firstPositionForChain = true;
169 chain = " ".equals(chain) ? chain : chain.trim();
170 sb.append("/").append(chain).append(":");
171 List<int[]> rangeList = atomSpec.getRanges(model, chain);
174 * sort ranges into ascending start position order
176 Collections.sort(rangeList, IntRangeComparator.ASCENDING);
178 int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
179 int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
181 Iterator<int[]> iterator = rangeList.iterator();
182 while (iterator.hasNext())
184 int[] range = iterator.next();
185 if (range[0] <= end + 1)
188 * range overlaps or is contiguous with the last one
189 * - so just extend the end position, and carry on
190 * (unless this is the last in the list)
192 end = Math.max(end, range[1]);
197 * we have a break so append the last range
199 appendRange(sb, start, end, chain, firstPositionForChain, true);
202 firstPositionForChain = false;
207 * and append the last range
209 if (!rangeList.isEmpty())
211 appendRange(sb, start, end, chain, firstPositionForChain, true);
213 firstPositionForChain = false;
218 public String showBackbone()
220 return "~display all;show @CA|P pbonds";
224 public String superposeStructures(AtomSpecModel spec, AtomSpecModel ref)
227 * Form ChimeraX match command to match spec to ref
229 * match #1/A:2-94 toAtoms #2/A:1-93
232 * https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/midas/match.html
234 StringBuilder cmd = new StringBuilder();
235 String atomSpec = getAtomSpec(spec, true);
236 String refSpec = getAtomSpec(ref, true);
237 cmd.append("align ").append(atomSpec).append(" toAtoms ")
241 * show superposed residues as ribbon, others as chain
243 cmd.append("; ribbon ");
244 cmd.append(getAtomSpec(spec, false)).append("|");
245 cmd.append(getAtomSpec(ref, false)).append("; view");
247 return cmd.toString();