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.StructureMapping;
35 import jalview.structure.StructureMappingcommandSet;
36 import jalview.structure.StructureSelectionManager;
37 import jalview.util.ColorUtils;
38 import jalview.util.Comparison;
40 import java.awt.Color;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.LinkedHashMap;
44 import java.util.List;
46 import java.util.Map.Entry;
49 * Routines for generating ChimeraX commands for Jalview/ChimeraX binding
51 public class ChimeraXCommands extends ChimeraCommands
53 public static final String NAMESPACE_PREFIX = "jv_";
55 private static final String CMD_COLOUR_BY_CHARGE = "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow";
58 * Traverses a map of { modelNumber, {chain, {list of from-to ranges} } } and
59 * builds a Chimera format atom spec
61 * @param modelAndChainRanges
63 protected static String getAtomSpec(
64 Map<Integer, Map<String, List<int[]>>> modelAndChainRanges)
66 StringBuilder sb = new StringBuilder(128);
67 boolean firstModelForColour = true;
68 for (Integer model : modelAndChainRanges.keySet())
70 boolean firstPositionForModel = true;
71 if (!firstModelForColour)
75 firstModelForColour = false;
76 sb.append("#").append(model).append(":");
78 final Map<String, List<int[]>> modelData = modelAndChainRanges
80 for (String chain : modelData.keySet())
82 boolean hasChain = !"".equals(chain.trim());
83 for (int[] range : modelData.get(chain))
85 if (!firstPositionForModel)
89 if (range[0] == range[1])
95 sb.append(range[0]).append("-").append(range[1]);
99 sb.append(".").append(chain);
101 firstPositionForModel = false;
105 return sb.toString();
110 * Build a data structure which records contiguous subsequences for each colour.
111 * From this we can easily generate the Chimera command for colour by sequence.
115 * list of start/end ranges
116 * Ordering is by order of addition (for colours and positions), natural ordering (for models and chains)
119 protected static Map<Object, AtomSpecModel> buildColoursMap(
120 StructureSelectionManager ssm, String[] files,
121 SequenceI[][] sequence, SequenceRenderer sr,
122 AlignmentViewPanel viewPanel)
124 FeatureRenderer fr = viewPanel.getFeatureRenderer();
125 FeatureColourFinder finder = new FeatureColourFinder(fr);
126 AlignViewportI viewport = viewPanel.getAlignViewport();
127 HiddenColumns cs = viewport.getAlignment().getHiddenColumns();
128 AlignmentI al = viewport.getAlignment();
129 Map<Object, AtomSpecModel> colourMap = new LinkedHashMap<>();
130 Color lastColour = null;
132 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
134 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
136 if (mapping == null || mapping.length < 1)
141 int startPos = -1, lastPos = -1;
142 String lastChain = "";
143 for (int s = 0; s < sequence[pdbfnum].length; s++)
145 for (int sp, m = 0; m < mapping.length; m++)
147 final SequenceI seq = sequence[pdbfnum][s];
148 if (mapping[m].getSequence() == seq
149 && (sp = al.findIndex(seq)) > -1)
151 SequenceI asp = al.getSequenceAt(sp);
152 for (int r = 0; r < asp.getLength(); r++)
154 // no mapping to gaps in sequence
155 if (Comparison.isGap(asp.getCharAt(r)))
159 int pos = mapping[m].getPDBResNum(asp.findPosition(r));
161 if (pos < 1 || pos == lastPos)
166 Color colour = sr.getResidueColour(seq, r, finder);
169 * darker colour for hidden regions
171 if (!cs.isVisible(r))
176 final String chain = mapping[m].getChain();
179 * Just keep incrementing the end position for this colour range
180 * _unless_ colour, PDB model or chain has changed, or there is a
181 * gap in the mapped residue sequence
183 final boolean newColour = !colour.equals(lastColour);
184 final boolean nonContig = lastPos + 1 != pos;
185 final boolean newChain = !chain.equals(lastChain);
186 if (newColour || nonContig || newChain)
190 addAtomSpecRange(colourMap, lastColour, pdbfnum, startPos,
199 // final colour range
200 if (lastColour != null)
202 addAtomSpecRange(colourMap, lastColour, pdbfnum, startPos,
214 * Helper method to add one contiguous range to the AtomSpec model for the given
215 * value (creating the model if necessary). As used by Jalview, {@code value} is
217 * <li>a colour, when building a 'colour structure by sequence' command</li>
218 * <li>a feature value, when building a 'set Chimera attributes from features'
229 protected static void addAtomSpecRange(Map<Object, AtomSpecModel> map,
230 Object value, int model, int startPos, int endPos, String chain)
233 * Get/initialize map of data for the colour
235 AtomSpecModel atomSpec = map.get(value);
236 if (atomSpec == null)
238 atomSpec = new AtomSpecModel();
239 map.put(value, atomSpec);
242 atomSpec.addRange(model, startPos, endPos, chain);
246 * Constructs and returns Chimera commands to set attributes on residues
247 * corresponding to features in Jalview. Attribute names are the Jalview
248 * feature type, with a "jv_" prefix.
256 public static StructureMappingcommandSet getSetAttributeCommandsForFeatures(
257 StructureSelectionManager ssm, String[] files, SequenceI[][] seqs,
258 AlignmentViewPanel viewPanel)
260 Map<String, Map<Object, AtomSpecModel>> featureMap = buildFeaturesMap(
261 ssm, files, seqs, viewPanel);
263 List<String> commands = buildSetAttributeCommands(featureMap);
265 StructureMappingcommandSet cs = new StructureMappingcommandSet(
266 ChimeraXCommands.class, null,
267 commands.toArray(new String[commands.size()]));
274 * Helper method to build a map of
275 * { featureType, { feature value, AtomSpecModel } }
284 protected static Map<String, Map<Object, AtomSpecModel>> buildFeaturesMap(
285 StructureSelectionManager ssm, String[] files, SequenceI[][] seqs,
286 AlignmentViewPanel viewPanel)
288 Map<String, Map<Object, AtomSpecModel>> theMap = new LinkedHashMap<>();
290 FeatureRenderer fr = viewPanel.getFeatureRenderer();
296 AlignViewportI viewport = viewPanel.getAlignViewport();
297 List<String> visibleFeatures = fr.getDisplayedFeatureTypes();
300 * if alignment is showing features from complement, we also transfer
301 * these features to the corresponding mapped structure residues
303 boolean showLinkedFeatures = viewport.isShowComplementFeatures();
304 List<String> complementFeatures = new ArrayList<>();
305 FeatureRenderer complementRenderer = null;
306 if (showLinkedFeatures)
308 AlignViewportI comp = fr.getViewport().getCodingComplement();
311 complementRenderer = Desktop.getAlignFrameFor(comp)
312 .getFeatureRenderer();
313 complementFeatures = complementRenderer.getDisplayedFeatureTypes();
316 if (visibleFeatures.isEmpty() && complementFeatures.isEmpty())
321 AlignmentI alignment = viewPanel.getAlignment();
322 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
324 StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
326 if (mapping == null || mapping.length < 1)
331 for (int seqNo = 0; seqNo < seqs[pdbfnum].length; seqNo++)
333 for (int m = 0; m < mapping.length; m++)
335 final SequenceI seq = seqs[pdbfnum][seqNo];
336 int sp = alignment.findIndex(seq);
337 StructureMapping structureMapping = mapping[m];
338 if (structureMapping.getSequence() == seq && sp > -1)
341 * found a sequence with a mapping to a structure;
342 * now scan its features
344 if (!visibleFeatures.isEmpty())
346 scanSequenceFeatures(visibleFeatures, structureMapping, seq,
349 if (showLinkedFeatures)
351 scanComplementFeatures(complementRenderer, structureMapping,
352 seq, theMap, pdbfnum);
362 * Scans visible features in mapped positions of the CDS/peptide complement, and
363 * adds any found to the map of attribute values/structure positions
365 * @param complementRenderer
366 * @param structureMapping
371 protected static void scanComplementFeatures(
372 FeatureRenderer complementRenderer,
373 StructureMapping structureMapping, SequenceI seq,
374 Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
377 * for each sequence residue mapped to a structure position...
379 for (int seqPos : structureMapping.getMapping().keySet())
382 * find visible complementary features at mapped position(s)
384 MappedFeatures mf = complementRenderer
385 .findComplementFeaturesAtResidue(seq, seqPos);
388 for (SequenceFeature sf : mf.features)
390 String type = sf.getType();
393 * Don't copy features which originated from Chimera
395 if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
396 .equals(sf.getFeatureGroup()))
402 * record feature 'value' (score/description/type) as at the
403 * corresponding structure position
405 List<int[]> mappedRanges = structureMapping
406 .getPDBResNumRanges(seqPos, seqPos);
408 if (!mappedRanges.isEmpty())
410 String value = sf.getDescription();
411 if (value == null || value.length() == 0)
415 float score = sf.getScore();
416 if (score != 0f && !Float.isNaN(score))
418 value = Float.toString(score);
420 Map<Object, AtomSpecModel> featureValues = theMap.get(type);
421 if (featureValues == null)
423 featureValues = new HashMap<>();
424 theMap.put(type, featureValues);
426 for (int[] range : mappedRanges)
428 addAtomSpecRange(featureValues, value, modelNumber, range[0],
429 range[1], structureMapping.getChain());
438 * Inspect features on the sequence; for each feature that is visible, determine
439 * its mapped ranges in the structure (if any) according to the given mapping,
440 * and add them to the map.
442 * @param visibleFeatures
448 protected static void scanSequenceFeatures(List<String> visibleFeatures,
449 StructureMapping mapping, SequenceI seq,
450 Map<String, Map<Object, AtomSpecModel>> theMap, int modelNumber)
452 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures(
453 visibleFeatures.toArray(new String[visibleFeatures.size()]));
454 for (SequenceFeature sf : sfs)
456 String type = sf.getType();
459 * Don't copy features which originated from Chimera
461 if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
462 .equals(sf.getFeatureGroup()))
467 List<int[]> mappedRanges = mapping.getPDBResNumRanges(sf.getBegin(),
470 if (!mappedRanges.isEmpty())
472 String value = sf.getDescription();
473 if (value == null || value.length() == 0)
477 float score = sf.getScore();
478 if (score != 0f && !Float.isNaN(score))
480 value = Float.toString(score);
482 Map<Object, AtomSpecModel> featureValues = theMap.get(type);
483 if (featureValues == null)
485 featureValues = new HashMap<>();
486 theMap.put(type, featureValues);
488 for (int[] range : mappedRanges)
490 addAtomSpecRange(featureValues, value, modelNumber, range[0],
491 range[1], mapping.getChain());
498 * Traverse the map of features/values/models/chains/positions to construct a
499 * list of 'setattr' commands (one per distinct feature type and value).
501 * The format of each command is
504 * <blockquote> setattr r <featureName> " " #modelnumber:range.chain
505 * e.g. setattr r jv:chain <value> #0:2.B,4.B,9-12.B|#1:1.A,2-6.A,...
512 protected static List<String> buildSetAttributeCommands(
513 Map<String, Map<Object, AtomSpecModel>> featureMap)
515 List<String> commands = new ArrayList<>();
516 for (String featureType : featureMap.keySet())
518 String attributeName = makeAttributeName(featureType);
521 * clear down existing attributes for this feature
523 // 'problem' - sets attribute to None on all residues - overkill?
524 // commands.add("~setattr r " + attributeName + " :*");
526 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
527 for (Object value : values.keySet())
530 * for each distinct value recorded for this feature type,
531 * add a command to set the attribute on the mapped residues
532 * Put values in single quotes, encoding any embedded single quotes
534 StringBuilder sb = new StringBuilder(128);
535 String featureValue = value.toString();
536 featureValue = featureValue.replaceAll("\\'", "'");
537 sb.append("setattr r ").append(attributeName).append(" '")
538 .append(featureValue).append("' ");
539 sb.append(values.get(value).getAtomSpec());
540 commands.add(sb.toString());
548 * Makes a prefixed and valid Chimera attribute name. A jv_ prefix is applied
549 * for a 'Jalview' namespace, and any non-alphanumeric character is converted
556 * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/setattr.html
559 protected static String makeAttributeName(String featureType)
561 StringBuilder sb = new StringBuilder();
562 if (featureType != null)
564 for (char c : featureType.toCharArray())
566 sb.append(Character.isLetterOrDigit(c) ? c : '_');
569 String attName = NAMESPACE_PREFIX + sb.toString();
572 * Chimera treats an attribute name ending in 'color' as colour-valued;
573 * Jalview doesn't, so prevent this by appending an underscore
575 if (attName.toUpperCase().endsWith("COLOR"))
584 public String colourByCharge()
586 return CMD_COLOUR_BY_CHARGE;
590 public String colourByResidues(Map<String, Color> colours)
592 StringBuilder cmd = new StringBuilder(12 * colours.size());
595 * concatenate commands like
598 for (Entry<String, Color> entry : colours.entrySet())
600 String colorSpec = ColorUtils.toTkCode(entry.getValue());
601 String resCode = entry.getKey();
602 cmd.append("color :").append(resCode).append(" ").append(colorSpec)
603 .append(CMD_SEPARATOR);
605 return cmd.toString();
609 public String setBackgroundColour(Color col)
611 return "set bgColor " + ColorUtils.toTkCode(col);
615 protected String getColourCommand(AtomSpecModel colourData,
618 return "color " + colourData.getAtomSpecX() + " " + colourCode;
622 public String focusView()