JAL-3390 new View menu option to hide hidden regions in structure
[jalview.git] / src / jalview / structures / models / AAStructureBindingModel.java
index 84475fe..ef4fed3 100644 (file)
  */
 package jalview.structures.models;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.SequenceRenderer;
 import jalview.api.StructureSelectionManagerProvider;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.io.DataSourceType;
@@ -53,9 +54,9 @@ import java.util.List;
  * @author gmcarstairs
  *
  */
-public abstract class AAStructureBindingModel extends
-        SequenceStructureBindingModel implements StructureListener,
-        StructureSelectionManagerProvider
+public abstract class AAStructureBindingModel
+        extends SequenceStructureBindingModel
+        implements StructureListener, StructureSelectionManagerProvider
 {
 
   private StructureSelectionManager ssm;
@@ -94,6 +95,17 @@ public abstract class AAStructureBindingModel extends
 
   public String fileLoadingError;
 
+  private boolean showAlignmentOnly;
+
+  /*
+   * a list of chains "pdbid:chainid" to show in the viewer;
+   * empty means show all
+   */
+  // TODO make private once showStructures() deals with this
+  protected List<String> chainsToShow;
+
+  private boolean hideHiddenRegions;
+
   /**
    * Data bean class to simplify parameterisation in superposeStructures
    */
@@ -134,6 +146,7 @@ public abstract class AAStructureBindingModel extends
   {
     this.ssm = ssm;
     this.sequence = seqs;
+    chainsToShow = new ArrayList<>();
   }
 
   /**
@@ -142,7 +155,6 @@ public abstract class AAStructureBindingModel extends
    * @param ssm
    * @param pdbentry
    * @param sequenceIs
-   * @param chains
    * @param protocol
    */
   public AAStructureBindingModel(StructureSelectionManager ssm,
@@ -154,8 +166,62 @@ public abstract class AAStructureBindingModel extends
     this.nucleotide = Comparison.isNucleotide(sequenceIs);
     this.pdbEntry = pdbentry;
     this.protocol = protocol;
+    chainsToShow = new ArrayList<>();
+
+    resolveChains();
   }
 
+  private boolean resolveChains()
+  {
+    /**
+     * final count of chain mappings discovered
+     */
+    int chainmaps = 0;
+    // JBPNote: JAL-2693 - this should be a list of chain mappings per
+    // [pdbentry][sequence]
+    String[][] newchains = new String[pdbEntry.length][];
+    int pe = 0;
+    for (PDBEntry pdb : pdbEntry)
+    {
+      SequenceI[] seqsForPdb = sequence[pe];
+      if (seqsForPdb != null)
+      {
+        newchains[pe] = new String[seqsForPdb.length];
+        int se = 0;
+        for (SequenceI asq : seqsForPdb)
+        {
+          String chain = (chains != null && chains[pe] != null)
+                  ? chains[pe][se]
+                  : null;
+          SequenceI sq = (asq.getDatasetSequence() == null) ? asq
+                  : asq.getDatasetSequence();
+          if (sq.getAllPDBEntries() != null)
+          {
+            for (PDBEntry pdbentry : sq.getAllPDBEntries())
+            {
+              if (pdb.getFile() != null && pdbentry.getFile() != null
+                      && pdb.getFile().equals(pdbentry.getFile()))
+              {
+                String chaincode = pdbentry.getChainCode();
+                if (chaincode != null && chaincode.length() > 0)
+                {
+                  chain = chaincode;
+                  chainmaps++;
+                  break;
+                }
+              }
+            }
+          }
+          newchains[pe][se] = chain;
+          se++;
+        }
+        pe++;
+      }
+    }
+
+    chains = newchains;
+    return chainmaps > 0;
+  }
   public StructureSelectionManager getSsm()
   {
     return ssm;
@@ -300,11 +366,12 @@ public abstract class AAStructureBindingModel extends
     {
       throw new Error(MessageManager.formatMessage(
               "error.implementation_error_no_pdbentry_from_index",
-              new Object[] { Integer.valueOf(pe).toString() }));
+              new Object[]
+              { Integer.valueOf(pe).toString() }));
     }
     final String nullChain = "TheNullChain";
-    List<SequenceI> s = new ArrayList<SequenceI>();
-    List<String> c = new ArrayList<String>();
+    List<SequenceI> s = new ArrayList<>();
+    List<String> c = new ArrayList<>();
     if (getChains() == null)
     {
       setChains(new String[getPdbCount()][]);
@@ -373,8 +440,8 @@ public abstract class AAStructureBindingModel extends
   public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
           SequenceI[][] seq, String[][] chns)
   {
-    List<PDBEntry> v = new ArrayList<PDBEntry>();
-    List<int[]> rtn = new ArrayList<int[]>();
+    List<PDBEntry> v = new ArrayList<>();
+    List<int[]> rtn = new ArrayList<>();
     for (int i = 0; i < getPdbCount(); i++)
     {
       v.add(getPdbEntry(i));
@@ -532,7 +599,7 @@ public abstract class AAStructureBindingModel extends
           BitSet matched, SuperposeData[] structures)
   {
     int refStructure = -1;
-    String[] files = getPdbFile();
+    String[] files = getStructureFiles();
     if (files == null)
     {
       return -1;
@@ -638,8 +705,8 @@ public abstract class AAStructureBindingModel extends
 
     if (waiting)
     {
-      System.err
-              .println("Timed out waiting for structure viewer to load file "
+      System.err.println(
+              "Timed out waiting for structure viewer to load file "
                       + notLoaded);
       return false;
     }
@@ -657,10 +724,8 @@ public abstract class AAStructureBindingModel extends
         {
           for (SequenceI s : seqs)
           {
-            if (s == seq
-                    || (s.getDatasetSequence() != null && s
-                            .getDatasetSequence() == seq
-                            .getDatasetSequence()))
+            if (s == seq || (s.getDatasetSequence() != null
+                    && s.getDatasetSequence() == seq.getDatasetSequence()))
             {
               return true;
             }
@@ -716,13 +781,13 @@ public abstract class AAStructureBindingModel extends
    *          an array of corresponding hidden columns for each alignment
    * @return
    */
-  public abstract String superposeStructures(AlignmentI[] alignments, int[] structureIndices,
-          ColumnSelection[] hiddenCols);
+  public abstract String superposeStructures(AlignmentI[] alignments,
+          int[] structureIndices, HiddenColumns[] hiddenCols);
 
   public abstract void setBackgroundColour(Color col);
 
   protected abstract StructureMappingcommandSet[] getColourBySequenceCommands(
-          String[] files, SequenceRenderer sr, AlignmentViewPanel avp);
+          String[] files, AlignmentViewPanel avp);
 
   /**
    * returns the current sequenceRenderer that should be used to colour the
@@ -732,7 +797,8 @@ public abstract class AAStructureBindingModel extends
    * 
    * @return
    */
-  public abstract SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment);
+  public abstract SequenceRenderer getSequenceRenderer(
+          AlignmentViewPanel alignment);
 
   protected abstract void colourBySequence(
           StructureMappingcommandSet[] colourBySequenceCommands);
@@ -742,13 +808,20 @@ public abstract class AAStructureBindingModel extends
   public abstract void colourByCharge();
 
   /**
-   * 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)
   {
-    if (!colourBySequence || !isLoadingFinished())
+    if (!isLoadingFinished())
+    {
+      return;
+    }
+    // todo: property change event for visibleAlignment
+    // to avoid unnecessary redraws here
+    if (!colourBySequence && !isShowAlignmentOnly())
     {
       return;
     }
@@ -756,12 +829,10 @@ public abstract class AAStructureBindingModel extends
     {
       return;
     }
-    String[] files = getPdbFile();
-  
-    SequenceRenderer sr = getSequenceRenderer(alignmentv);
-  
+    String[] files = getStructureFiles();
+
     StructureMappingcommandSet[] colourBySequenceCommands = getColourBySequenceCommands(
-            files, sr, alignmentv);
+            files, alignmentv);
     colourBySequence(colourBySequenceCommands);
   }
 
@@ -772,4 +843,107 @@ public abstract class AAStructureBindingModel extends
 
   public abstract jalview.api.FeatureRenderer getFeatureRenderer(
           AlignmentViewPanel alignment);
+
+  /**
+   * 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
+  }
+
+  @Override
+  public void updateColours(Object source)
+  {
+    AlignmentViewPanel ap = (AlignmentViewPanel) source;
+    // ignore events from panels not used to colour this view
+    if (!getViewer().isUsedforcolourby(ap))
+    {
+      return;
+    }
+    if (!isLoadingFromArchive())
+    {
+      colourBySequence(ap);
+    }
+  }
+
+  /**
+   * Sets the list of chains to display (as "pdbid:chain"), where an empty list
+   * means show all
+   * 
+   * @param chains
+   */
+  public void setChainsToShow(List<String> chains)
+  {
+    chainsToShow = 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 (chainsToShow.isEmpty())
+    {
+      return true;
+    }
+    return chainsToShow.contains(pdbId + ":" + chainId);
+  }
 }