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.renderer.seqfeatures.FeatureColourFinder;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureMappingcommandSet;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.util.StructureCommands;
37 import java.awt.Color;
38 import java.util.ArrayList;
39 import java.util.List;
43 * Routines for generating Jmol commands for Jalview/Jmol binding
48 public class JmolCommands extends StructureCommands
50 private static final String COMMA = ",";
53 * Get commands to colour structure by sequence
61 * @deprecated only called by applet code
62 * @see #getColourBySequenceCommand(Map)
65 public static StructureMappingcommandSet[] getColourBySequenceCommand(
66 StructureSelectionManager ssm, String[] files,
67 SequenceI[][] sequence, SequenceRenderer sr,
68 AlignmentViewPanel viewPanel)
70 FeatureRenderer fr = viewPanel.getFeatureRenderer();
71 FeatureColourFinder finder = new FeatureColourFinder(fr);
72 AlignViewportI viewport = viewPanel.getAlignViewport();
73 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
74 AlignmentI al = viewport.getAlignment();
75 List<StructureMappingcommandSet> cset = new ArrayList<>();
77 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
79 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
80 StringBuffer command = new StringBuffer();
81 StructureMappingcommandSet smc;
82 ArrayList<String> str = new ArrayList<>();
84 if (mapping == null || mapping.length < 1)
89 for (int s = 0; s < sequence[pdbfnum].length; s++)
91 for (int sp, m = 0; m < mapping.length; m++)
93 if (mapping[m].getSequence() == sequence[pdbfnum][s]
94 && (sp = al.findIndex(sequence[pdbfnum][s])) > -1)
96 int lastPos = StructureMapping.UNASSIGNED_VALUE;
97 SequenceI asp = al.getSequenceAt(sp);
98 for (int r = 0; r < asp.getLength(); r++)
100 // no mapping to gaps in sequence
101 if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
105 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
111 if (pos == StructureMapping.UNASSIGNED_VALUE)
113 // terminate current colour op
114 if (command.length() > 0
115 && command.charAt(command.length() - 1) != ';')
120 lastPos = StructureMapping.UNASSIGNED_VALUE;
126 Color col = sr.getResidueColour(sequence[pdbfnum][s], r,
130 * shade hidden regions darker
132 if (!cs.isVisible(r))
137 String newSelcom = (mapping[m].getChain() != " "
138 ? ":" + mapping[m].getChain()
139 : "") + "/" + (pdbfnum + 1) + ".1" + ";color["
140 + col.getRed() + COMMA + col.getGreen() + COMMA
141 + col.getBlue() + "]";
142 if (command.length() > newSelcom.length() && command
143 .substring(command.length() - newSelcom.length())
146 command = JmolCommands.condenseCommand(command, pos);
149 // TODO: deal with case when buffer is too large for Jmol to parse
150 // - execute command and flush
152 if (command.length() > 0
153 && command.charAt(command.length() - 1) != ';')
158 if (command.length() > 51200)
161 str.add(command.toString());
162 command.setLength(0);
164 command.append("select " + pos);
165 command.append(newSelcom);
173 str.add(command.toString());
174 command.setLength(0);
176 // Finally, add the command set ready to be returned.
177 cset.add(new StructureMappingcommandSet(JmolCommands.class,
178 files[pdbfnum], str.toArray(new String[str.size()])));
181 return cset.toArray(new StructureMappingcommandSet[cset.size()]);
184 public static StringBuffer condenseCommand(StringBuffer command, int pos)
187 // work back to last 'select'
188 int p = command.length(), q = p;
197 } while ((q = command.indexOf("select", p)) == -1 && p > 0);
199 StringBuffer sb = new StringBuffer(command.substring(0, q + 7));
201 command = command.delete(0, q + 7);
205 if (command.indexOf("-") > -1)
207 start = command.substring(0, command.indexOf("-"));
211 start = command.substring(0, command.indexOf(":"));
214 sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
220 * Answers a Jmol 'color' command to colour residues as described by the given
221 * map of {@code <Color, AtomSpecModel>}
226 public static String[] getColourBySequenceCommand(
227 Map<Object, AtomSpecModel> map)
229 String[] cmds = new String[map.keySet().size()];
232 for (Object o : map.keySet())
234 StringBuilder cmd = new StringBuilder(128);
236 String atomSpec = getAtomSpec(map.get(o));
237 cmd.append("color (").append(atomSpec).append(") [")
238 .append(c.getRed()).append(COMMA).append(c.getGreen())
239 .append(COMMA).append(c.getBlue()).append("];");
240 cmds[i] = cmd.toString();
248 * Builds a Jmol syntax selection expression from the given model, for example
251 * (61-64,70)&:A/1.1,(12-25,41-44)&:B/1.1,12:A/2.1
252 * for model 1, chain A, residues 61-64 and 70, chain B residues 12-25 and 41-44, model 2 chain A residue 12
255 * Note the brackets to group multiple residue ranges for the same chain
256 * (without bracketing, ranges would apply to all chains)
258 * @param atomSpecModel
261 public static String getAtomSpec(AtomSpecModel atomSpecModel)
263 StringBuilder sb = new StringBuilder(64);
264 for (int model : atomSpecModel.getModels())
266 for (String chain : atomSpecModel.getChains(model))
272 boolean firstRange = true;
274 List<int[]> rangeList = atomSpecModel.getRanges(model, chain);
276 if (rangeList.size() > 1)
280 appendResidueRange(sb, rangeList, null, firstRange);
281 if (rangeList.size() > 1)
285 sb.append(":").append(chain.trim()).append("/")
286 .append(String.valueOf(model + 1))
291 return sb.toString();