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.api.FeatureRenderer;
24 import jalview.api.SequenceRenderer;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.structure.StructureMapping;
29 import jalview.structure.StructureMappingcommandSet;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.util.ColorUtils;
32 import jalview.util.Comparison;
34 import java.awt.Color;
35 import java.util.ArrayList;
36 import java.util.LinkedHashMap;
37 import java.util.List;
41 * Routines for generating Chimera commands for Jalview/Chimera binding
46 public class ChimeraCommands
50 * utility to construct the commands to colour chains by the given alignment
51 * for passing to Chimera
53 * @returns Object[] { Object[] { <model being coloured>,
56 public static StructureMappingcommandSet getColourBySequenceCommand(
57 StructureSelectionManager ssm, String[] files,
58 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
61 Map<Object, AtomSpecModel> colourMap = buildColoursMap(
62 ssm, files, sequence, sr, fr, alignment);
64 List<String> colourCommands = buildColourCommands(colourMap);
66 StructureMappingcommandSet cs = new StructureMappingcommandSet(
67 ChimeraCommands.class, null,
68 colourCommands.toArray(new String[colourCommands.size()]));
74 * Traverse the map of colours/models/chains/positions to construct a list of
75 * 'color' commands (one per distinct colour used). The format of each command
80 * color colorname #modelnumber:range.chain
81 * e.g. color #00ff00 #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
88 protected static List<String> buildColourCommands(
89 Map<Object, AtomSpecModel> colourMap)
92 * This version concatenates all commands into a single String (semi-colon
93 * delimited). If length limit issues arise, refactor to return one color
96 List<String> commands = new ArrayList<String>();
97 StringBuilder sb = new StringBuilder(256);
98 boolean firstColour = true;
99 for (Object key : colourMap.keySet())
101 Color colour = (Color) key;
102 String colourCode = ColorUtils.toTkCode(colour);
107 sb.append("color ").append(colourCode).append(" ");
109 final AtomSpecModel colourData = colourMap
111 sb.append(colourData.getAtomSpec());
113 commands.add(sb.toString());
118 * Traverses a map of { modelNumber, {chain, {list of from-to ranges} } } and
119 * builds a Chimera format atom spec
121 * @param modelAndChainRanges
123 protected static String getAtomSpec(
124 Map<Integer, Map<String, List<int[]>>> modelAndChainRanges)
126 StringBuilder sb = new StringBuilder(128);
127 boolean firstModelForColour = true;
128 for (Integer model : modelAndChainRanges.keySet())
130 boolean firstPositionForModel = true;
131 if (!firstModelForColour)
135 firstModelForColour = false;
136 sb.append("#").append(model).append(":");
138 final Map<String, List<int[]>> modelData = modelAndChainRanges
140 for (String chain : modelData.keySet())
142 boolean hasChain = !"".equals(chain.trim());
143 for (int[] range : modelData.get(chain))
145 if (!firstPositionForModel)
149 if (range[0] == range[1])
155 sb.append(range[0]).append("-").append(range[1]);
159 sb.append(".").append(chain);
161 firstPositionForModel = false;
165 return sb.toString();
170 * Build a data structure which maps contiguous subsequences for each colour.
171 * This generates a data structure from which we can easily generate the
172 * Chimera command for colour by sequence.
176 * list of start/end ranges
177 * Ordering is by order of addition (for colours and positions), natural ordering (for models and chains)
180 protected static Map<Object, AtomSpecModel> buildColoursMap(
181 StructureSelectionManager ssm, String[] files,
182 SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
183 AlignmentI alignment)
185 Map<Object, AtomSpecModel> colourMap = new LinkedHashMap<Object, AtomSpecModel>();
186 Color lastColour = null;
187 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
189 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
191 if (mapping == null || mapping.length < 1)
196 int startPos = -1, lastPos = -1;
197 String lastChain = "";
198 for (int s = 0; s < sequence[pdbfnum].length; s++)
200 for (int sp, m = 0; m < mapping.length; m++)
202 final SequenceI seq = sequence[pdbfnum][s];
203 if (mapping[m].getSequence() == seq
204 && (sp = alignment.findIndex(seq)) > -1)
206 SequenceI asp = alignment.getSequenceAt(sp);
207 for (int r = 0; r < asp.getLength(); r++)
209 // no mapping to gaps in sequence
210 if (Comparison.isGap(asp.getCharAt(r)))
214 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
216 if (pos < 1 || pos == lastPos)
221 Color colour = sr.getResidueColour(seq, r, fr);
222 final String chain = mapping[m].getChain();
225 * Just keep incrementing the end position for this colour range
226 * _unless_ colour, PDB model or chain has changed, or there is a
227 * gap in the mapped residue sequence
229 final boolean newColour = !colour.equals(lastColour);
230 final boolean nonContig = lastPos + 1 != pos;
231 final boolean newChain = !chain.equals(lastChain);
232 if (newColour || nonContig || newChain)
236 addRange(colourMap, lastColour, pdbfnum, startPos,
245 // final colour range
246 if (lastColour != null)
248 addRange(colourMap, lastColour, pdbfnum, startPos,
260 * Helper method to add one contiguous colour range to the colour map.
269 protected static void addRange(Map<Object, AtomSpecModel> map,
270 Object key, int model, int startPos, int endPos, String chain)
273 * Get/initialize map of data for the colour
275 AtomSpecModel atomSpec = map.get(key);
276 if (atomSpec == null)
278 atomSpec = new AtomSpecModel();
279 map.put(key, atomSpec);
282 atomSpec.addRange(model, startPos, endPos, chain);
286 * Constructs and returns a set of Chimera commands to set attributes on
287 * residues corresponding to features in Jalview
296 public static StructureMappingcommandSet getSetAttributeCommandsForFeatures(
297 StructureSelectionManager ssm, String[] files,
298 SequenceI[][] seqs, FeatureRenderer fr, AlignmentI alignment)
300 Map<String, AtomSpecModel> featureMap = buildFeaturesMap(
301 ssm, files, seqs, fr, alignment);
303 List<String> commands = buildSetAttributeCommands(featureMap);
305 StructureMappingcommandSet cs = new StructureMappingcommandSet(
306 ChimeraCommands.class, null,
307 commands.toArray(new String[commands.size()]));
313 * Helper method to build a map of { featureType, AtomSpecModel }
322 protected static Map<String, AtomSpecModel> buildFeaturesMap(
323 StructureSelectionManager ssm, String[] files,
324 SequenceI[][] seqs, FeatureRenderer fr, AlignmentI alignment)
326 Map<String, AtomSpecModel> theMap = new LinkedHashMap<String, AtomSpecModel>();
328 List<String> visibleFeatures = fr.getDisplayedFeatureTypes();
329 if (visibleFeatures.isEmpty())
334 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
336 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
338 if (mapping == null || mapping.length < 1)
343 for (int seqNo = 0; seqNo < seqs[pdbfnum].length; seqNo++)
345 for (int m = 0; m < mapping.length; m++)
347 final SequenceI seq = seqs[pdbfnum][seqNo];
348 int sp = alignment.findIndex(seq);
349 if (mapping[m].getSequence() == seq && sp > -1)
352 * found a sequence with a mapping to a structure;
353 * now scan its features
355 SequenceI asp = alignment.getSequenceAt(sp);
357 scanSequenceFeatures(visibleFeatures, mapping[m], asp, theMap,
367 * Inspect features on the sequence; for each feature that is visible,
368 * determine its mapped ranges in the structure (if any) according to the
369 * given mapping, and add them to the map
371 * @param visibleFeatures
377 protected static void scanSequenceFeatures(List<String> visibleFeatures,
378 StructureMapping mapping, SequenceI seq,
379 Map<String, AtomSpecModel> theMap, int modelNumber)
381 SequenceFeature[] sfs = seq.getSequenceFeatures();
387 for (SequenceFeature sf : sfs)
389 String type = sf.getType();
390 if (!visibleFeatures.contains(type))
394 List<int[]> mappedRanges = mapping.getPDBResNumRanges(sf.getBegin(),
397 if (!mappedRanges.isEmpty())
399 AtomSpecModel atomSpec = theMap.get(type);
400 if (atomSpec == null)
402 atomSpec = new AtomSpecModel();
403 theMap.put(type, atomSpec);
405 for (int[] range : mappedRanges)
407 atomSpec.addRange(modelNumber, range[0], range[1],
415 * Traverse the map of features/models/chains/positions to construct a list of
416 * 'setattr' commands (one per feature type). The format of each command is
419 * <blockquote> setattr r <featureName> " " #modelnumber:range.chain
420 * e.g. setattr r jv:chain " " #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
424 * Note we are not (currently) setting attribute values, only the type
425 * (presence) of each attribute. This is to avoid overloading the Chimera REST
426 * interface by sending too many distinct commands. Analysis by feature values
427 * may still be performed in Jalview, on selections created in Chimera.
432 protected static List<String> buildSetAttributeCommands(
433 Map<String, AtomSpecModel> featureMap)
435 List<String> commands = new ArrayList<String>();
436 for (String featureType : featureMap.keySet())
438 StringBuilder sb = new StringBuilder(128);
439 String sanitised = featureType.replace(" ", "_").replace("-", "_");
440 sb.append("setattr r jv_").append(sanitised).append(" \" \" ");
441 sb.append(featureMap.get(featureType).getAtomSpec());
442 commands.add(sb.toString());