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.jmol;
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureRenderer;
26 import jalview.api.SequenceRenderer;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.SequenceI;
30 import jalview.ext.rbvi.chimera.AtomSpecModel;
31 import jalview.ext.rbvi.chimera.StructureCommands;
32 import jalview.renderer.seqfeatures.FeatureColourFinder;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureMappingcommandSet;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.structures.models.AAStructureBindingModel;
38 import java.awt.Color;
39 import java.util.ArrayList;
40 import java.util.List;
44 * Routines for generating Jmol commands for Jalview/Jmol binding
49 public class JmolCommands extends StructureCommands
51 private static final String COMMA = ",";
54 * Jmol utility which constructs the commands to colour chains by the given
57 * @returns Object[] { Object[] { <model being coloured>,
60 public static StructureMappingcommandSet[] getColourBySequenceCommand(
61 StructureSelectionManager ssm, String[] files,
62 AAStructureBindingModel binding, AlignmentViewPanel viewPanel)
64 SequenceRenderer sr = binding.getSequenceRenderer(viewPanel);
65 SequenceI[][] sequence = binding.getSequence();
66 return getColourBySequenceCommand(ssm, files, sequence, sr, viewPanel);
69 public static StructureMappingcommandSet[] getColourBySequenceCommand(
70 StructureSelectionManager ssm, String[] files,
71 SequenceI[][] sequence, SequenceRenderer sr,
72 AlignmentViewPanel viewPanel)
74 FeatureRenderer fr = viewPanel.getFeatureRenderer();
75 FeatureColourFinder finder = new FeatureColourFinder(fr);
76 AlignViewportI viewport = viewPanel.getAlignViewport();
77 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
78 AlignmentI al = viewport.getAlignment();
79 List<StructureMappingcommandSet> cset = new ArrayList<>();
81 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
83 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
84 StringBuffer command = new StringBuffer();
85 StructureMappingcommandSet smc;
86 ArrayList<String> str = new ArrayList<>();
88 if (mapping == null || mapping.length < 1)
93 for (int s = 0; s < sequence[pdbfnum].length; s++)
95 for (int sp, m = 0; m < mapping.length; m++)
97 if (mapping[m].getSequence() == sequence[pdbfnum][s]
98 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
100 int lastPos = StructureMapping.UNASSIGNED_VALUE;
101 SequenceI asp = al.getSequenceAt(sp);
102 for (int r = 0; r < asp.getLength(); r++)
104 // no mapping to gaps in sequence
105 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
109 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
115 if (pos == StructureMapping.UNASSIGNED_VALUE)
117 // terminate current colour op
118 if (command.length() > 0
119 && command.charAt(command.length() - 1) != ';')
124 lastPos = StructureMapping.UNASSIGNED_VALUE;
130 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
134 * shade hidden regions darker
136 if (!cs.isVisible(r))
141 String newSelcom = (mapping[m].getChain() != " "
142 ? ":" + mapping[m].getChain()
143 : "") + "/" + (pdbfnum + 1) + ".1" + ";color["
144 + col.getRed() + COMMA + col.getGreen() + COMMA
145 + col.getBlue() + "]";
146 if (command.length() > newSelcom.length() && command
147 .substring(command.length() - newSelcom.length())
150 command = JmolCommands.condenseCommand(command, pos);
153 // TODO: deal with case when buffer is too large for Jmol to parse
154 // - execute command and flush
156 if (command.length() > 0
157 && command.charAt(command.length() - 1) != ';')
162 if (command.length() > 51200)
165 str.add(command.toString());
166 command.setLength(0);
168 command.append("select " + pos);
169 command.append(newSelcom);
177 str.add(command.toString());
178 command.setLength(0);
180 // Finally, add the command set ready to be returned.
181 cset.add(new StructureMappingcommandSet(JmolCommands.class,
182 files[pdbfnum], str.toArray(new String[str.size()])));
185 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
188 public static StringBuffer condenseCommand(StringBuffer command, int pos)
191 // work back to last 'select'
192 int p = command.length(), q = p;
201 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
203 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
205 command = command.delete(0, q + 7);
209 if (command.indexOf("-") > -1)
211 start = command.substring(0, command.indexOf("-"));
215 start = command.substring(0, command.indexOf(":"));
218 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
224 * Answers a Jmol 'color' command to colour residues as described by the given
225 * map of {@code <Color, AtomSpecModel>}
230 public static String[] getColourBySequenceCommand(
231 Map<Object, AtomSpecModel> map)
233 String[] cmds = new String[map.keySet().size()];
236 for (Object o : map.keySet())
238 StringBuilder cmd = new StringBuilder(128);
240 String atomSpec = getAtomSpec(map.get(o));
241 cmd.append("select ").append(atomSpec).append(";color[")
242 .append(c.getRed()).append(COMMA).append(c.getGreen())
243 .append(COMMA).append(c.getBlue()).append("];");
244 cmds[i] = cmd.toString();
252 * Builds a Jmol syntax selection expression from the given model, for example
255 * 61-64,70:A/1.1,12-25,41-44:B/1.1,12:A/2.1
256 * for model 1, chain A, residues 61-64 and 70, chain B residues 12-25 and 41-44, model 2 chain A residue 12
259 * @param atomSpecModel
262 public static String getAtomSpec(AtomSpecModel atomSpecModel)
264 StringBuilder sb = new StringBuilder(64);
265 for (int model : atomSpecModel.getModels())
267 for (String chain : atomSpecModel.getChains(model))
273 boolean firstRange = true;
275 List<int[]> rangeList = atomSpecModel.getRanges(model, chain);
277 if (rangeList.size() > 1)
281 appendResidueRange(sb, rangeList, null, firstRange);
282 if (rangeList.size() > 1)
286 sb.append(":").append(chain.trim()).append("/")
287 .append(String.valueOf(model + 1))
292 return sb.toString();