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.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.MappedFeatures;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceI;
32 import jalview.gui.Desktop;
33 import jalview.renderer.seqfeatures.FeatureColourFinder;
34 import jalview.structure.StructureCommandsBase;
35 import jalview.structure.StructureMapping;
36 import jalview.structure.StructureMappingcommandSet;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.util.ColorUtils;
39 import jalview.util.Comparison;
41 import java.awt.Color;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.LinkedHashMap;
45 import java.util.List;
47 import java.util.Map.Entry;
50 * Routines for generating Chimera commands for Jalview/Chimera binding
55 public class ChimeraCommands extends StructureCommandsBase
57 public static final String NAMESPACE_PREFIX = "jv_";
59 protected static final String CMD_SEPARATOR = ";";
61 private static final String CMD_COLOUR_BY_CHARGE = "color white;color red ::ASP;color red ::GLU;color blue ::LYS;color blue ::ARG;color yellow ::CYS";
63 private static final String CMD_COLOUR_BY_CHAIN = "rainbow chain";
66 public String[] colourBySequence(
67 StructureSelectionManager ssm, String[] files,
68 SequenceI[][] sequence, SequenceRenderer sr,
69 AlignmentViewPanel viewPanel)
71 Map<Object, AtomSpecModel> colourMap = buildColoursMap(ssm, files,
72 sequence, sr, viewPanel);
74 List<String> colourCommands = buildColourCommands(colourMap);
76 return colourCommands.toArray(new String[colourCommands.size()]);
80 * Traverse the map of colours/models/chains/positions to construct a list of
81 * 'color' commands (one per distinct colour used). The format of each command
86 * color colorname #modelnumber:range.chain
87 * e.g. color #00ff00 #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
94 protected List<String> buildColourCommands(
95 Map<Object, AtomSpecModel> colourMap)
98 * This version concatenates all commands into a single String (semi-colon
99 * delimited). If length limit issues arise, refactor to return one color
100 * command per colour.
102 List<String> commands = new ArrayList<>();
103 StringBuilder sb = new StringBuilder(256);
104 boolean firstColour = true;
105 for (Object key : colourMap.keySet())
107 Color colour = (Color) key;
108 String colourCode = ColorUtils.toTkCode(colour);
114 final AtomSpecModel colourData = colourMap.get(colour);
115 sb.append(getColourCommand(colourData, colourCode));
117 commands.add(sb.toString());
121 protected String getColourCommand(AtomSpecModel colourData,
124 return "color " + colourCode + " " + colourData.getAtomSpec();
128 * Traverses a map of { modelNumber, {chain, {list of from-to ranges} } } and
129 * builds a Chimera format atom spec
131 * @param modelAndChainRanges
133 protected static String getAtomSpec(
134 Map<Integer, Map<String, List<int[]>>> modelAndChainRanges)
136 StringBuilder sb = new StringBuilder(128);
137 boolean firstModelForColour = true;
138 for (Integer model : modelAndChainRanges.keySet())
140 boolean firstPositionForModel = true;
141 if (!firstModelForColour)
145 firstModelForColour = false;
146 sb.append("#").append(model).append(":");
148 final Map<String, List<int[]>> modelData = modelAndChainRanges
150 for (String chain : modelData.keySet())
152 boolean hasChain = !"".equals(chain.trim());
153 for (int[] range : modelData.get(chain))
155 if (!firstPositionForModel)
159 if (range[0] == range[1])
165 sb.append(range[0]).append("-").append(range[1]);
169 sb.append(".").append(chain);
171 firstPositionForModel = false;
175 return sb.toString();
180 * Build a data structure which records contiguous subsequences for each colour.
181 * From this we can easily generate the Chimera command for colour by sequence.
185 * list of start/end ranges
186 * Ordering is by order of addition (for colours and positions), natural ordering (for models and chains)
196 protected static Map<Object, AtomSpecModel> buildColoursMap(
197 StructureSelectionManager ssm, String[] files,
198 SequenceI[][] sequence, SequenceRenderer sr,
199 AlignmentViewPanel viewPanel)
201 FeatureRenderer fr = viewPanel.getFeatureRenderer();
202 FeatureColourFinder finder = new FeatureColourFinder(fr);
203 AlignViewportI viewport = viewPanel.getAlignViewport();
204 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
205 AlignmentI al = viewport.getAlignment();
206 Map<Object, AtomSpecModel> colourMap = new LinkedHashMap<>();
207 Color lastColour = null;
209 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
211 final int modelNumber = pdbfnum + getModelStartNo();
212 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
214 if (mapping == null || mapping.length < 1)
219 int startPos = -1, lastPos = -1;
220 String lastChain = "";
221 for (int s = 0; s < sequence[pdbfnum].length; s++)
223 for (int sp, m = 0; m < mapping.length; m++)
225 final SequenceI seq = sequence[pdbfnum][s];
226 if (mapping[m].getSequence() == seq
227 && (sp = al.findIndex(seq)) > -1)
229 SequenceI asp = al.getSequenceAt(sp);
230 for (int r = 0; r < asp.getLength(); r++)
232 // no mapping to gaps in sequence
233 if (Comparison.isGap(asp.getCharAt(r)))
237 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
239 if (pos < 1 || pos == lastPos)
244 Color colour = sr.getResidueColour(seq, r, finder);
247 * darker colour for hidden regions
249 if (!cs.isVisible(r))
254 final String chain = mapping[m].getChain();
257 * Just keep incrementing the end position for this colour range
258 * _unless_ colour, PDB model or chain has changed, or there is a
259 * gap in the mapped residue sequence
261 final boolean newColour = !colour.equals(lastColour);
262 final boolean nonContig = lastPos + 1 != pos;
263 final boolean newChain = !chain.equals(lastChain);
264 if (newColour || nonContig || newChain)
268 addAtomSpecRange(colourMap, lastColour, modelNumber,
269 startPos, lastPos, lastChain);
277 // final colour range
278 if (lastColour != null)
280 addAtomSpecRange(colourMap, lastColour, modelNumber, startPos,
291 protected static int getModelStartNo()
297 * Helper method to add one contiguous range to the AtomSpec model for the given
298 * value (creating the model if necessary). As used by Jalview, {@code value} is
300 * <li>a colour, when building a 'colour structure by sequence' command</li>
301 * <li>a feature value, when building a 'set Chimera attributes from features'
312 protected static void addAtomSpecRange(Map<Object, AtomSpecModel> map,
313 Object value, int model, int startPos, int endPos, String chain)
316 * Get/initialize map of data for the colour
318 AtomSpecModel atomSpec = map.get(value);
319 if (atomSpec == null)
321 atomSpec = new AtomSpecModel();
322 map.put(value, atomSpec);
325 atomSpec.addRange(model, startPos, endPos, chain);
329 * Constructs and returns Chimera commands to set attributes on residues
330 * corresponding to features in Jalview. Attribute names are the Jalview feature
331 * type, with a "jv_" prefix.
340 public static StructureMappingcommandSet getSetAttributeCommandsForFeatures(
341 StructureSelectionManager ssm, String[] files, SequenceI[][] seqs,
342 AlignmentViewPanel viewPanel, boolean isChimeraX)
344 Map<String, Map<Object, AtomSpecModel>> featureMap = buildFeaturesMap(
345 ssm, files, seqs, viewPanel, isChimeraX);
347 List<String> commands = buildSetAttributeCommands(featureMap,
350 StructureMappingcommandSet cs = new StructureMappingcommandSet(
351 ChimeraCommands.class, null,
352 commands.toArray(new String[commands.size()]));
359 * Helper method to build a map of
360 * { featureType, { feature value, AtomSpecModel } }
370 protected static Map<String, Map<Object, AtomSpecModel>> buildFeaturesMap(
371 StructureSelectionManager ssm, String[] files, SequenceI[][] seqs,
372 AlignmentViewPanel viewPanel, boolean isChimeraX)
374 Map<String, Map<Object, AtomSpecModel>> theMap = new LinkedHashMap<>();
376 FeatureRenderer fr = viewPanel.getFeatureRenderer();
382 AlignViewportI viewport = viewPanel.getAlignViewport();
383 List<String> visibleFeatures = fr.getDisplayedFeatureTypes();
386 * if alignment is showing features from complement, we also transfer
387 * these features to the corresponding mapped structure residues
389 boolean showLinkedFeatures = viewport.isShowComplementFeatures();
390 List<String> complementFeatures = new ArrayList<>();
391 FeatureRenderer complementRenderer = null;
392 if (showLinkedFeatures)
394 AlignViewportI comp = fr.getViewport().getCodingComplement();
397 complementRenderer = Desktop.getAlignFrameFor(comp)
398 .getFeatureRenderer();
399 complementFeatures = complementRenderer.getDisplayedFeatureTypes();
402 if (visibleFeatures.isEmpty() && complementFeatures.isEmpty())
407 AlignmentI alignment = viewPanel.getAlignment();
408 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
410 final int modelNumber = pdbfnum + (isChimeraX ? 1 : 0);
411 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
413 if (mapping == null || mapping.length < 1)
418 for (int seqNo = 0; seqNo < seqs[pdbfnum].length; seqNo++)
420 for (int m = 0; m < mapping.length; m++)
422 final SequenceI seq = seqs[pdbfnum][seqNo];
423 int sp = alignment.findIndex(seq);
424 StructureMapping structureMapping = mapping[m];
425 if (structureMapping.getSequence() == seq && sp > -1)
428 * found a sequence with a mapping to a structure;
429 * now scan its features
431 if (!visibleFeatures.isEmpty())
433 scanSequenceFeatures(visibleFeatures, structureMapping, seq,
434 theMap, modelNumber);
436 if (showLinkedFeatures)
438 scanComplementFeatures(complementRenderer, structureMapping,
439 seq, theMap, modelNumber);
449 * Scans visible features in mapped positions of the CDS/peptide complement, and
450 * adds any found to the map of attribute values/structure positions
452 * @param complementRenderer
453 * @param structureMapping
458 protected static void scanComplementFeatures(
459 FeatureRenderer complementRenderer,
460 StructureMapping structureMapping, SequenceI seq,
461 Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
464 * for each sequence residue mapped to a structure position...
466 for (int seqPos : structureMapping.getMapping().keySet())
469 * find visible complementary features at mapped position(s)
471 MappedFeatures mf = complementRenderer
472 .findComplementFeaturesAtResidue(seq, seqPos);
475 for (SequenceFeature sf : mf.features)
477 String type = sf.getType();
480 * Don't copy features which originated from Chimera
482 if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
483 .equals(sf.getFeatureGroup()))
489 * record feature 'value' (score/description/type) as at the
490 * corresponding structure position
492 List<int[]> mappedRanges = structureMapping
493 .getPDBResNumRanges(seqPos, seqPos);
495 if (!mappedRanges.isEmpty())
497 String value = sf.getDescription();
498 if (value == null || value.length() == 0)
502 float score = sf.getScore();
503 if (score != 0f && !Float.isNaN(score))
505 value = Float.toString(score);
507 Map<Object, AtomSpecModel> featureValues = theMap.get(type);
508 if (featureValues == null)
510 featureValues = new HashMap<>();
511 theMap.put(type, featureValues);
513 for (int[] range : mappedRanges)
515 addAtomSpecRange(featureValues, value, modelNumber, range[0],
516 range[1], structureMapping.getChain());
525 * Inspect features on the sequence; for each feature that is visible, determine
526 * its mapped ranges in the structure (if any) according to the given mapping,
527 * and add them to the map.
529 * @param visibleFeatures
535 protected static void scanSequenceFeatures(List<String> visibleFeatures,
536 StructureMapping mapping, SequenceI seq,
537 Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
539 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures(
540 visibleFeatures.toArray(new String[visibleFeatures.size()]));
541 for (SequenceFeature sf : sfs)
543 String type = sf.getType();
546 * Don't copy features which originated from Chimera
548 if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
549 .equals(sf.getFeatureGroup()))
554 List<int[]> mappedRanges = mapping.getPDBResNumRanges(sf.getBegin(),
557 if (!mappedRanges.isEmpty())
559 String value = sf.getDescription();
560 if (value == null || value.length() == 0)
564 float score = sf.getScore();
565 if (score != 0f && !Float.isNaN(score))
567 value = Float.toString(score);
569 Map<Object, AtomSpecModel> featureValues = theMap.get(type);
570 if (featureValues == null)
572 featureValues = new HashMap<>();
573 theMap.put(type, featureValues);
575 for (int[] range : mappedRanges)
577 addAtomSpecRange(featureValues, value, modelNumber, range[0],
578 range[1], mapping.getChain());
585 * Traverse the map of features/values/models/chains/positions to construct a
586 * list of 'setattr' commands (one per distinct feature type and value).
588 * The format of each command is
591 * <blockquote> setattr r <featureName> " " #modelnumber:range.chain
592 * e.g. setattr r jv:chain <value> #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
600 protected static List<String> buildSetAttributeCommands(
601 Map<String, Map<Object, AtomSpecModel>> featureMap,
604 List<String> commands = new ArrayList<>();
605 for (String featureType : featureMap.keySet())
607 String attributeName = makeAttributeName(featureType);
610 * clear down existing attributes for this feature
612 // 'problem' - sets attribute to None on all residues - overkill?
613 // commands.add("~setattr r " + attributeName + " :*");
615 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
616 for (Object value : values.keySet())
619 * for each distinct value recorded for this feature type,
620 * add a command to set the attribute on the mapped residues
621 * Put values in single quotes, encoding any embedded single quotes
623 AtomSpecModel atomSpecModel = values.get(value);
624 StringBuilder sb = new StringBuilder(128);
625 sb.append("setattr");
628 sb.append(" ").append(atomSpecModel.getAtomSpecX());
630 String featureValue = value.toString();
631 featureValue = featureValue.replaceAll("\\'", "'");
632 sb.append(" res ").append(attributeName).append(" '")
633 .append(featureValue).append("' ");
636 sb.append(" create true");
640 sb.append(atomSpecModel.getAtomSpec());
642 commands.add(sb.toString());
650 * Makes a prefixed and valid Chimera attribute name. A jv_ prefix is applied
651 * for a 'Jalview' namespace, and any non-alphanumeric character is converted
658 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/setattr.html
661 protected static String makeAttributeName(String featureType)
663 StringBuilder sb = new StringBuilder();
664 if (featureType != null)
666 for (char c : featureType.toCharArray())
668 sb.append(Character.isLetterOrDigit(c) ? c : '_');
671 String attName = NAMESPACE_PREFIX + sb.toString();
674 * Chimera treats an attribute name ending in 'color' as colour-valued;
675 * Jalview doesn't, so prevent this by appending an underscore
677 if (attName.toUpperCase().endsWith("COLOR"))
686 public String colourByChain()
688 return CMD_COLOUR_BY_CHAIN;
692 public String colourByCharge()
694 return CMD_COLOUR_BY_CHARGE;
698 public String colourByResidues(Map<String, Color> colours)
700 StringBuilder cmd = new StringBuilder(12 * colours.size());
703 * concatenate commands like
704 * color #4949b6 ::VAL
706 for (Entry<String, Color> entry : colours.entrySet())
708 String colorSpec = ColorUtils.toTkCode(entry.getValue());
709 String resCode = entry.getKey();
710 cmd.append("color ").append(colorSpec).append(" ::").append(resCode)
711 .append(CMD_SEPARATOR);
713 return cmd.toString();
717 public String setBackgroundColour(Color col)
719 return "set bgColor " + ColorUtils.toTkCode(col);
723 public String focusView()
729 public String showChains(List<String> toShow)
732 * Construct a chimera command like
734 * ~display #*;~ribbon #*;ribbon :.A,:.B
736 StringBuilder cmd = new StringBuilder(64);
737 boolean first = true;
738 for (String chain : toShow)
740 String[] tokens = chain.split(":");
741 if (tokens.length == 2)
743 String showChainCmd = tokens[0] + ":." + tokens[1];
748 cmd.append(showChainCmd);
754 * could append ";focus" to this command to resize the display to fill the
755 * window, but it looks more helpful not to (easier to relate chains to the
758 final String command = "~display #*; ~ribbon #*; ribbon :"