Merge branch 'bug/JAL-1726_External-integration-test_PDB' into Release_2_9_Branch
[jalview.git] / src / jalview / gui / AlignmentPanel.java
index beafa8c..5bc46f4 100644 (file)
  */
 package jalview.gui;
 
-import jalview.analysis.AnnotationSorter;
-import jalview.api.AlignViewportI;
-import jalview.api.AlignmentViewPanel;
-import jalview.bin.Cache;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.SearchResults;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceGroup;
-import jalview.datamodel.SequenceI;
-import jalview.jbgui.GAlignmentPanel;
-import jalview.math.AlignmentDimension;
-import jalview.schemes.ResidueProperties;
-import jalview.structure.StructureSelectionManager;
-import jalview.util.MessageManager;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Container;
@@ -52,9 +37,25 @@ import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.PrintWriter;
+import java.util.List;
 
 import javax.swing.SwingUtilities;
 
+import jalview.analysis.AnnotationSorter;
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.jbgui.GAlignmentPanel;
+import jalview.math.AlignmentDimension;
+import jalview.schemes.ResidueProperties;
+import jalview.structure.StructureSelectionManager;
+import jalview.util.MessageManager;
+
 /**
  * DOCUMENT ME!
  * 
@@ -91,6 +92,12 @@ public class AlignmentPanel extends GAlignmentPanel implements
 
   int vextent = 0;
 
+  /*
+   * Flag set while scrolling to follow complementary cDNA/protein scroll. When
+   * true, suppresses invoking the same method recursively.
+   */
+  private boolean followingComplementScroll;
+
   /**
    * Creates a new AlignmentPanel object.
    * 
@@ -290,33 +297,48 @@ public class AlignmentPanel extends GAlignmentPanel implements
   }
 
   /**
-   * scroll the view to show the position of the highlighted region in results
+   * Scroll the view to show the position of the highlighted region in results
    * (if any) and redraw the overview
    * 
    * @param results
    */
   public boolean scrollToPosition(SearchResults results)
   {
-    return scrollToPosition(results, true);
+    return scrollToPosition(results, true, false);
   }
 
   /**
-   * scroll the view to show the position of the highlighted region in results
+   * Scroll the view to show the position of the highlighted region in results
+   * (if any)
+   * 
+   * @param searchResults
+   * @param redrawOverview
+   * @return
+   */
+  public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
+  {
+    return scrollToPosition(searchResults, redrawOverview, false);
+  }
+
+  /**
+   * Scroll the view to show the position of the highlighted region in results
    * (if any)
    * 
    * @param results
    * @param redrawOverview
    *          - when set, the overview will be recalculated (takes longer)
+   * @param centre
+   *          if true, try to centre the search results horizontally in the view
    * @return false if results were not found
    */
   public boolean scrollToPosition(SearchResults results,
-          boolean redrawOverview)
+          boolean redrawOverview, boolean centre)
   {
-    int startv, endv, starts, ends, width;
+    int startv, endv, starts, ends;
     // TODO: properly locate search results in view when large numbers of hidden
     // columns exist before highlighted region
     // do we need to scroll the panel?
-    // TODO: tons of nullpointereexceptions raised here.
+    // TODO: tons of nullpointerexceptions raised here.
     if (results != null && results.getSize() > 0 && av != null
             && av.getAlignment() != null)
     {
@@ -336,6 +358,17 @@ public class AlignmentPanel extends GAlignmentPanel implements
       int end = r[1];
       // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
       // DEBUG
+
+      /*
+       * To centre results, scroll to positions half the visible width
+       * left/right of the start/end positions
+       */
+      if (centre)
+      {
+        int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
+        start = Math.max(start - offset, 0);
+        end = Math.min(end + offset, seq.getEnd() - 1);
+      }
       if (start < 0)
       {
         return false;
@@ -361,20 +394,37 @@ public class AlignmentPanel extends GAlignmentPanel implements
       {
         if ((startv = av.getStartRes()) >= start)
         {
-          setScrollValues(start - 1, seqIndex);
+          /*
+           * Scroll left to make start of search results visible
+           */
+          // setScrollValues(start - 1, seqIndex); // plus one residue
+          setScrollValues(start, seqIndex);
         }
         else if ((endv = av.getEndRes()) <= end)
         {
-          setScrollValues(startv + 1 + end - endv, seqIndex);
+          /*
+           * Scroll right to make end of search results visible
+           */
+          // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
+          setScrollValues(startv + end - endv, seqIndex);
         }
         else if ((starts = av.getStartSeq()) > seqIndex)
         {
+          /*
+           * Scroll up to make start of search results visible
+           */
           setScrollValues(av.getStartRes(), seqIndex);
         }
         else if ((ends = av.getEndSeq()) <= seqIndex)
         {
+          /*
+           * Scroll down to make end of search results visible
+           */
           setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
         }
+        /*
+         * Else results are already visible - no need to scroll
+         */
       }
       else
       {
@@ -756,6 +806,18 @@ public class AlignmentPanel extends GAlignmentPanel implements
         }
       }
     }
+    /*
+     * If there is one, scroll the (Protein/cDNA) complementary alignment to
+     * match, unless we are ourselves doing that.
+     */
+    if (isFollowingComplementScroll())
+    {
+      setFollowingComplementScroll(false);
+    }
+    else
+    {
+      av.scrollComplementaryAlignment();
+    }
   }
 
   /**
@@ -1554,11 +1616,17 @@ public class AlignmentPanel extends GAlignmentPanel implements
   {
     try
     {
+      if (alignFrame.getSplitViewContainer() != null)
+      {
+        /*
+         * bring enclosing SplitFrame to front first if there is one
+         */
+        ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
+      }
       alignFrame.setSelected(b);
     } catch (Exception ex)
     {
     }
-    ;
 
     if (b)
     {
@@ -1651,4 +1719,68 @@ public class AlignmentPanel extends GAlignmentPanel implements
   {
     this.idPanel = idPanel;
   }
+
+  /**
+   * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
+   * The aim is to keep the two alignments 'lined up' on their centre columns.
+   * 
+   * @param sr
+   *          holds mapped region(s) of this alignment that we are scrolling
+   *          'to'; may be modified for sequence offset by this method
+   * @param seqOffset
+   *          the number of visible sequences to show above the mapped region
+   */
+  public void scrollToCentre(SearchResults sr, int seqOffset)
+  {
+    /*
+     * To avoid jumpy vertical scrolling (if some sequences are gapped or not
+     * mapped), we can make the scroll-to location a sequence above the one
+     * actually mapped.
+     */
+    SequenceI mappedTo = sr.getResultSequence(0);
+    List<SequenceI> seqs = av.getAlignment().getSequences();
+
+    /*
+     * This is like AlignmentI.findIndex(seq) but here we are matching the
+     * dataset sequence not the aligned sequence
+     */
+    int sequenceIndex = 0;
+    boolean matched = false;
+    for (SequenceI seq : seqs)
+    {
+      if (mappedTo == seq.getDatasetSequence())
+      {
+        matched = true;
+        break;
+      }
+      sequenceIndex++;
+    }
+    if (!matched)
+    {
+      return; // failsafe, shouldn't happen
+    }
+    sequenceIndex = Math.max(0, sequenceIndex - seqOffset);
+    sr.getResults().get(0)
+            .setSequence(av.getAlignment().getSequenceAt(sequenceIndex));
+
+    /*
+     * Scroll to position but centring the target residue.
+     */
+    scrollToPosition(sr, true, true);
+  }
+
+  /**
+   * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
+   * 
+   * @param b
+   */
+  protected void setFollowingComplementScroll(boolean b)
+  {
+    this.followingComplementScroll = b;
+  }
+
+  protected boolean isFollowingComplementScroll()
+  {
+    return this.followingComplementScroll;
+  }
 }