Merge branch 'develop' into feature/JAL-3390hideUnmappedStructure
[jalview.git] / src / jalview / structures / models / AAStructureBindingModel.java
index 88c181c..7b1485c 100644 (file)
@@ -26,7 +26,9 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -42,8 +44,12 @@ import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.MappedFeatures;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
+import jalview.gui.Desktop;
 import jalview.gui.StructureViewer.ViewerType;
 import jalview.io.DataSourceType;
 import jalview.io.StructureFile;
@@ -164,13 +170,24 @@ public abstract class AAStructureBindingModel
 
   private boolean finishedInit = false;
 
-  /**
-   * current set of model filenames loaded in the viewer
+  /*
+   * current set of model filenames loaded in the Jmol instance 
+   * array index 0, 1, 2... corresponds to Jmol model numbers 1, 2, 3...
    */
   protected String[] modelFileNames = null;
 
   public String fileLoadingError;
 
+  private boolean showAlignmentOnly;
+
+  /*
+   * a list of chains "pdbid:chainid" to hide in the viewer
+   */
+  // TODO make private once deprecated JalviewJmolBinding.centerViewer removed
+  protected List<String> chainsToHide;
+
+  private boolean hideHiddenRegions;
+
   /**
    * Constructor
    * 
@@ -182,6 +199,7 @@ public abstract class AAStructureBindingModel
   {
     this.ssm = ssm;
     this.sequence = seqs;
+    chainsToHide = new ArrayList<>();
     chainNames = new ArrayList<>();
     chainFile = new HashMap<>();
   }
@@ -202,6 +220,8 @@ public abstract class AAStructureBindingModel
     this.nucleotide = Comparison.isNucleotide(sequenceIs);
     this.pdbEntry = pdbentry;
     this.protocol = protocol;
+    chainsToHide = new ArrayList<>();
+
     resolveChains();
   }
 
@@ -404,7 +424,7 @@ public abstract class AAStructureBindingModel
 
   /**
    * Instruct the Jalview binding to update the pdbentries vector if necessary
-   * prior to matching the jmol view's contents to the list of structure files
+   * prior to matching the viewer's contents to the list of structure files
    * Jalview knows about. By default does nothing, override as required.
    */
   public void refreshPdbEntries()
@@ -961,15 +981,37 @@ public abstract class AAStructureBindingModel
           AlignmentViewPanel alignment);
 
   /**
+   * Recolours mapped residues in the structure viewer to match colours in the
+   * given alignment panel, provided colourBySequence is selected. Colours
+   * should also be applied to any hidden mapped residues (so that they are
+   * shown correctly if these get unhidden).
+   * 
+   * @param viewPanel
+   */
+  protected void colourBySequence(AlignmentViewPanel viewPanel)
+  {
+
+    if (!colourBySequence || !isLoadingFinished() || getSsm() == null)
+    {
+      return;
+    }
+    Map<Object, AtomSpecModel> colourMap = buildColoursMap(ssm, sequence,
+            viewPanel);
+
+    List<StructureCommandI> colourBySequenceCommands = commandGenerator
+            .colourBySequence(colourMap);
+    executeCommands(colourBySequenceCommands, false, null);
+
+  }
+
+  /**
    * Sends a command to the structure viewer to colour each chain with a
    * distinct colour (to the extent supported by the viewer)
    */
   public void colourByChain()
   {
     colourBySequence = false;
-
     // TODO: JAL-628 colour chains distinctly across all visible models
-
     executeCommand(commandGenerator.colourByChain(), false,
             COLOURING_STRUCTURES);
   }
@@ -1111,7 +1153,8 @@ public abstract class AAStructureBindingModel
    * @param getReply
    * @param msg
    */
-  private List<String> executeCommands(List<StructureCommandI> commands,
+  protected List<String> executeCommands(
+          List<StructureCommandI> commands,
           boolean getReply, String msg)
   {
     return executeCommands(getReply, msg,
@@ -1160,29 +1203,36 @@ public abstract class AAStructureBindingModel
   }
 
   /**
-   * colour any structures associated with sequences in the given alignment
-   * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
-   * if colourBySequence is enabled.
+   * Recolours the displayed structures, if they are coloured by
+   * sequence, or 'show only visible alignment' is selected. This supports
+   * updating structure colours on either change of alignment colours, or change
+   * to the visible region of the alignment.
    */
-  public void colourBySequence(AlignmentViewPanel alignmentv)
+  public void updateStructureColours(AlignmentViewPanel alignmentv)
   {
-    if (!colourBySequence || !isLoadingFinished())
+    if (!isLoadingFinished())
+    {
+      return;
+    }
+
+    /*
+     * if structure is not coloured by sequence, but restricted to the alignment,
+     * then redraw it (but don't recolour it) in case hidden regions have changed
+     * (todo: specific messaging for change of hidden region only)
+     */
+    if (!colourBySequence)
     {
+      if (isShowAlignmentOnly())
+      {
+        showStructures(alignmentv.getAlignViewport(), false);
+      }
       return;
     }
     if (getSsm() == null)
     {
       return;
     }
-    String[] files = getStructureFiles();
-
-    SequenceRenderer sr = getSequenceRenderer(alignmentv);
-    Map<Object, AtomSpecModel> colourMap = buildColoursMap(ssm, files,
-            sequence, sr, alignmentv);
-
-    List<StructureCommandI> colourBySequenceCommands = commandGenerator
-            .colourBySequence(colourMap);
-    executeCommands(colourBySequenceCommands, false, null);
+    colourBySequence(alignmentv);
   }
 
   /**
@@ -1238,6 +1288,202 @@ public abstract class AAStructureBindingModel
   }
 
   /**
+   * Sets the flag for whether only mapped visible residues in the alignment
+   * should be visible in the structure viewer
+   * 
+   * @param b
+   */
+  public void setShowAlignmentOnly(boolean b)
+  {
+    showAlignmentOnly = b;
+  }
+
+  /**
+   * Answers true if only residues mapped to the alignment should be shown in the
+   * structure viewer, else false
+   * 
+   * @return
+   */
+  public boolean isShowAlignmentOnly()
+  {
+    return showAlignmentOnly;
+  }
+
+  /**
+   * Sets the flag for hiding regions of structure which are hidden in the
+   * alignment (only applies when the structure viewer is restricted to the
+   * alignment only)
+   * 
+   * @param b
+   */
+  public void setHideHiddenRegions(boolean b)
+  {
+    hideHiddenRegions = b;
+  }
+
+  /**
+   * Answers true if regions hidden in the alignment should also be hidden in the
+   * structure viewer, else false (only applies when the structure viewer is
+   * restricted to the alignment only)
+   * 
+   * @return
+   */
+  public boolean isHideHiddenRegions()
+  {
+    return hideHiddenRegions;
+  }
+
+  /**
+   * Shows the structures in the viewer, without changing their colouring. This is
+   * to support toggling of whether the whole structure is shown, or only residues
+   * mapped to visible regions of the alignment.
+   * 
+   * @param alignViewportI
+   * @param refocus
+   *                         if true, refit the display to the viewer
+   */
+  public void showStructures(AlignViewportI alignViewportI, boolean refocus)
+  {
+    // override with implementation
+  }
+
+  /**
+   * Sets the list of chains to hide (as "pdbid:chain")
+   * 
+   * @param chains
+   */
+  public void setChainsToHide(List<String> chains)
+  {
+    chainsToHide = chains;
+  }
+
+  /**
+   * Answers true if the specified structure and chain are selected to be shown in
+   * the viewer, else false
+   * 
+   * @param pdbId
+   * @param chainId
+   * @return
+   */
+  protected boolean isShowChain(String pdbId, String chainId)
+  {
+    if (chainsToHide.isEmpty())
+    {
+      return true;
+    }
+    return !chainsToHide.contains(pdbId + ":" + chainId);
+  }
+
+  @Override
+  public abstract String[] getStructureFiles();
+
+  /**
+   * Builds a model of residues mapped from sequences to show on structure, taking
+   * into account user choices of
+   * <ul>
+   * <li>which chains are shown</li>
+   * <li>whether all structure is shown, or only that mapped to the alignment</li>
+   * <li>whether hidden regions of the alignment are hidden (excluded) or grayed
+   * out (included)</li>
+   * </ul>
+   * 
+   * @param av
+   * @return
+   */
+  protected AtomSpecModel getShownResidues(AlignViewportI av)
+  {
+    AlignmentI alignment = av.getAlignment();
+    final int width = alignment.getWidth();
+  
+    String[] files = getStructureFiles();
+  
+    AtomSpecModel model = new AtomSpecModel();
+  
+    for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
+    {
+      String fileName = files[pdbfnum];
+      final String modelId = getModelIdForFile(files[pdbfnum]);
+      StructureMapping[] mappings = getSsm().getMapping(fileName);
+  
+      /*
+       * Find the first mapped sequence (if any) for this PDB entry which is in
+       * the alignment
+       */
+      final int seqCountForPdbFile = getSequence()[pdbfnum].length;
+      for (int s = 0; s < seqCountForPdbFile; s++)
+      {
+        for (StructureMapping mapping : mappings)
+        {
+          final SequenceI theSequence = getSequence()[pdbfnum][s];
+          if (mapping.getSequence() == theSequence
+                  && alignment.findIndex(theSequence) > -1)
+          {
+            String chainCd = mapping.getChain();
+            if (!isShowChain(mapping.getPdbId(), chainCd))
+            {
+              // continue;
+            }
+            Iterator<int[]> visible;
+            if (isShowAlignmentOnly() && isHideHiddenRegions())
+            {
+              visible = alignment.getHiddenColumns()
+                    .getVisContigsIterator(0, width, true);
+            }
+            else
+            {
+              visible = Collections.singletonList(new int[] { 0, width })
+                      .iterator();
+            }
+            while (visible.hasNext())
+            {
+              int[] visibleRegion = visible.next();
+              int seqStartPos = theSequence.findPosition(visibleRegion[0]);
+              int seqEndPos = theSequence.findPosition(visibleRegion[1]);
+              List<int[]> residueRanges = mapping
+                      .getPDBResNumRanges(seqStartPos, seqEndPos);
+              if (!residueRanges.isEmpty())
+              {
+                for (int[] range : residueRanges)
+                {
+                  model.addRange(modelId, range[0], range[1], chainCd);
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  
+    return model;
+  }
+
+  /**
+   * Answers the structure viewer's model number for the given PDB file, or -1 if
+   * not found
+   * 
+   * @param fileName
+   * @param fileIndex
+   *                    index of the file in the stored array of file names
+   * @return
+   */
+  public int getModelForPdbFile(String fileName, int fileIndex)
+  {
+    return fileIndex;
+  }
+
+  /**
+   * Answers a default structure model specification which is simply the string
+   * form of the model number. Override if needed to specify submodels.
+   * 
+   * @param model
+   * @return
+   */
+  public String getModelSpec(int model)
+  {
+    return String.valueOf(model);
+  }
+
+  /**
    * Returns the FeatureRenderer for the given alignment view, or null if
    * feature display is turned off in the view.
    * 
@@ -1248,6 +1494,10 @@ public abstract class AAStructureBindingModel
   {
     AlignmentViewPanel ap = (avp == null) ? getViewer().getAlignmentPanel()
             : avp;
+    if (ap == null)
+    {
+      return null;
+    }
     return ap.getAlignViewport().isShowSequenceFeatures()
             ? ap.getFeatureRenderer()
             : null;
@@ -1293,7 +1543,7 @@ public abstract class AAStructureBindingModel
     }
     if (!isLoadingFromArchive())
     {
-      colourBySequence(ap);
+      updateStructureColours(ap);
     }
   }
 
@@ -1354,16 +1604,16 @@ public abstract class AAStructureBindingModel
    * models and chains)
    * 
    * @param ssm
-   * @param files
    * @param sequence
-   * @param sr
    * @param viewPanel
    * @return
    */
   protected Map<Object, AtomSpecModel> buildColoursMap(
-          StructureSelectionManager ssm, String[] files,
-          SequenceI[][] sequence, SequenceRenderer sr, AlignmentViewPanel viewPanel)
+          StructureSelectionManager ssm, SequenceI[][] sequence,
+          AlignmentViewPanel viewPanel)
   {
+    String[] files = getStructureFiles();
+    SequenceRenderer sr = getSequenceRenderer(viewPanel);
     FeatureRenderer fr = viewPanel.getFeatureRenderer();
     FeatureColourFinder finder = new FeatureColourFinder(fr);
     AlignViewportI viewport = viewPanel.getAlignViewport();
@@ -1566,4 +1816,305 @@ public abstract class AAStructureBindingModel
       executeCommand(cmd, false);
     }
   }
+
+  /**
+   * Returns true if the viewer is an external structure viewer for which the
+   * process is still alive, else false (for Jmol, or an external viewer which
+   * the user has independently closed)
+   * 
+   * @return
+   */
+  public boolean isViewerRunning()
+  {
+    return false;
+  }
+
+  /**
+   * Closes Jalview's structure viewer panel and releases associated resources.
+   * If it is managing an external viewer program, and {@code forceClose} is
+   * true, also shuts down that program.
+   * 
+   * @param forceClose
+   */
+  public void closeViewer(boolean forceClose)
+  {
+    getSsm().removeStructureViewerListener(this, this.getStructureFiles());
+    releaseUIResources();
+
+    // add external viewer shutdown in overrides
+    // todo - or can maybe pull up to here
+  }
+
+  /**
+   * Returns the URL of a help page for the structure viewer, or null if none is
+   * known
+   * 
+   * @return
+   */
+  public String getHelpURL()
+  {
+    return null;
+  }
+
+  /**
+   * <pre>
+   * Helper method to build a map of 
+   *   { featureType, { feature value, AtomSpecModel } }
+   * </pre>
+   * 
+   * @param viewPanel
+   * @return
+   */
+  protected Map<String, Map<Object, AtomSpecModel>> buildFeaturesMap(
+          AlignmentViewPanel viewPanel)
+  {
+    Map<String, Map<Object, AtomSpecModel>> theMap = new LinkedHashMap<>();
+    String[] files = getStructureFiles();
+    if (files == null)
+    {
+      return theMap;
+    }
+
+    FeatureRenderer fr = viewPanel.getFeatureRenderer();
+    if (fr == null)
+    {
+      return theMap;
+    }
+  
+    AlignViewportI viewport = viewPanel.getAlignViewport();
+    List<String> visibleFeatures = fr.getDisplayedFeatureTypes();
+  
+    /*
+     * if alignment is showing features from complement, we also transfer
+     * these features to the corresponding mapped structure residues
+     */
+    boolean showLinkedFeatures = viewport.isShowComplementFeatures();
+    List<String> complementFeatures = new ArrayList<>();
+    FeatureRenderer complementRenderer = null;
+    if (showLinkedFeatures)
+    {
+      AlignViewportI comp = fr.getViewport().getCodingComplement();
+      if (comp != null)
+      {
+        complementRenderer = Desktop.getAlignFrameFor(comp)
+                .getFeatureRenderer();
+        complementFeatures = complementRenderer.getDisplayedFeatureTypes();
+      }
+    }
+    if (visibleFeatures.isEmpty() && complementFeatures.isEmpty())
+    {
+      return theMap;
+    }
+  
+    AlignmentI alignment = viewPanel.getAlignment();
+    SequenceI[][] seqs = getSequence();
+
+    for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
+    {
+      String modelId = getModelIdForFile(files[pdbfnum]);
+      StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
+  
+      if (mapping == null || mapping.length < 1)
+      {
+        continue;
+      }
+  
+      for (int seqNo = 0; seqNo < seqs[pdbfnum].length; seqNo++)
+      {
+        for (int m = 0; m < mapping.length; m++)
+        {
+          final SequenceI seq = seqs[pdbfnum][seqNo];
+          int sp = alignment.findIndex(seq);
+          StructureMapping structureMapping = mapping[m];
+          if (structureMapping.getSequence() == seq && sp > -1)
+          {
+            /*
+             * found a sequence with a mapping to a structure;
+             * now scan its features
+             */
+            if (!visibleFeatures.isEmpty())
+            {
+              scanSequenceFeatures(visibleFeatures, structureMapping, seq,
+                      theMap, modelId);
+            }
+            if (showLinkedFeatures)
+            {
+              scanComplementFeatures(complementRenderer, structureMapping,
+                      seq, theMap, modelId);
+            }
+          }
+        }
+      }
+    }
+    return theMap;
+  }
+
+  /**
+   * Ask the structure viewer to open a session file. Returns true if
+   * successful, else false (or not supported).
+   * 
+   * @param filepath
+   * @return
+   */
+  public boolean openSession(String filepath)
+  {
+    StructureCommandI cmd = getCommandGenerator().openSession(filepath);
+    if (cmd == null)
+    {
+      return false;
+    }
+    executeCommand(cmd, true);
+    // todo: test for failure - how?
+    return true;
+  }
+
+  /**
+   * Scans visible features in mapped positions of the CDS/peptide complement, and
+   * adds any found to the map of attribute values/structure positions
+   * 
+   * @param complementRenderer
+   * @param structureMapping
+   * @param seq
+   * @param theMap
+   * @param modelNumber
+   */
+  protected static void scanComplementFeatures(
+          FeatureRenderer complementRenderer,
+          StructureMapping structureMapping, SequenceI seq,
+          Map<String, Map<Object, AtomSpecModel>> theMap,
+          String modelNumber)
+  {
+    /*
+     * for each sequence residue mapped to a structure position...
+     */
+    for (int seqPos : structureMapping.getMapping().keySet())
+    {
+      /*
+       * find visible complementary features at mapped position(s)
+       */
+      MappedFeatures mf = complementRenderer
+              .findComplementFeaturesAtResidue(seq, seqPos);
+      if (mf != null)
+      {
+        for (SequenceFeature sf : mf.features)
+        {
+          String type = sf.getType();
+  
+          /*
+           * Don't copy features which originated from Chimera
+           */
+          if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
+                  .equals(sf.getFeatureGroup()))
+          {
+            continue;
+          }
+  
+          /*
+           * record feature 'value' (score/description/type) as at the
+           * corresponding structure position
+           */
+          List<int[]> mappedRanges = structureMapping
+                  .getPDBResNumRanges(seqPos, seqPos);
+  
+          if (!mappedRanges.isEmpty())
+          {
+            String value = sf.getDescription();
+            if (value == null || value.length() == 0)
+            {
+              value = type;
+            }
+            float score = sf.getScore();
+            if (score != 0f && !Float.isNaN(score))
+            {
+              value = Float.toString(score);
+            }
+            Map<Object, AtomSpecModel> featureValues = theMap.get(type);
+            if (featureValues == null)
+            {
+              featureValues = new HashMap<>();
+              theMap.put(type, featureValues);
+            }
+            for (int[] range : mappedRanges)
+            {
+              addAtomSpecRange(featureValues, value, modelNumber, range[0],
+                      range[1], structureMapping.getChain());
+            }
+          }
+        }
+      }
+    }
+  }
+
+  /**
+   * Inspect features on the sequence; for each feature that is visible,
+   * determine its mapped ranges in the structure (if any) according to the
+   * given mapping, and add them to the map.
+   * 
+   * @param visibleFeatures
+   * @param mapping
+   * @param seq
+   * @param theMap
+   * @param modelId
+   */
+  protected static void scanSequenceFeatures(List<String> visibleFeatures,
+          StructureMapping mapping, SequenceI seq,
+          Map<String, Map<Object, AtomSpecModel>> theMap, String modelId)
+  {
+    List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures(
+            visibleFeatures.toArray(new String[visibleFeatures.size()]));
+    for (SequenceFeature sf : sfs)
+    {
+      String type = sf.getType();
+  
+      /*
+       * Don't copy features which originated from Chimera
+       */
+      if (JalviewChimeraBinding.CHIMERA_FEATURE_GROUP
+              .equals(sf.getFeatureGroup()))
+      {
+        continue;
+      }
+  
+      List<int[]> mappedRanges = mapping.getPDBResNumRanges(sf.getBegin(),
+              sf.getEnd());
+  
+      if (!mappedRanges.isEmpty())
+      {
+        String value = sf.getDescription();
+        if (value == null || value.length() == 0)
+        {
+          value = type;
+        }
+        float score = sf.getScore();
+        if (score != 0f && !Float.isNaN(score))
+        {
+          value = Float.toString(score);
+        }
+        Map<Object, AtomSpecModel> featureValues = theMap.get(type);
+        if (featureValues == null)
+        {
+          featureValues = new HashMap<>();
+          theMap.put(type, featureValues);
+        }
+        for (int[] range : mappedRanges)
+        {
+          addAtomSpecRange(featureValues, value, modelId, range[0],
+                  range[1], mapping.getChain());
+        }
+      }
+    }
+  }
+
+  /**
+   * Returns the number of structure files in the structure viewer and mapped to
+   * Jalview. This may be zero if the files are still in the process of loading
+   * in the viewer.
+   * 
+   * @return
+   */
+  public int getMappedStructureCount()
+  {
+    String[] files = getStructureFiles();
+    return files == null ? 0 : files.length;
+  }
 }