refactored VamsasListener to allow the source of the event to be passed to handlers
[jalview.git] / src / jalview / gui / AlignViewport.java
index 0cbbdb4..d0c5674 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
@@ -48,6 +48,7 @@ import jalview.datamodel.*;
 import jalview.schemes.*;
 import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
+import jalview.structure.VamsasSource;
 
 /**
  * DOCUMENT ME!
@@ -55,7 +56,7 @@ import jalview.structure.StructureSelectionManager;
  * @author $author$
  * @version $Revision$
  */
-public class AlignViewport implements SelectionSource
+public class AlignViewport implements SelectionSource, VamsasSource
 {
   private static final int RIGHT_JUSTIFY = 1;
 
@@ -433,6 +434,8 @@ public class AlignViewport implements SelectionSource
     showUnconserved = jalview.bin.Cache.getDefault("SHOW_UNCONSERVED",
             false);
     sortByTree = jalview.bin.Cache.getDefault("SORT_BY_TREE", false);
+    followSelection = jalview.bin.Cache.getDefault("FOLLOW_SELECTIONS",
+            true);
   }
 
   /**
@@ -476,11 +479,6 @@ public class AlignViewport implements SelectionSource
   private boolean shownpfeats;
 
   /**
-   * consensus annotation includes all percentage for all symbols in column
-   */
-  private boolean includeAllConsensusSymbols = true;
-
-  /**
    * trigger update of conservation annotation
    */
   public void updateConservation(final AlignmentPanel ap)
@@ -542,12 +540,14 @@ public class AlignViewport implements SelectionSource
 
       try
       {
-        int aWidth = (alignment != null) ? alignment.getWidth() : 0; // null
+        int aWidth = (alignment != null) ? alignment.getWidth() : -1; // null
         // pointer
         // possibility
         // here.
-        if (aWidth < 0)
+        if (aWidth <= 0)
         {
+          updatingConsensus = false;
+          UPDATING_CONSENSUS = false;
           return;
         }
 
@@ -555,11 +555,9 @@ public class AlignViewport implements SelectionSource
         consensus.annotations = new Annotation[aWidth];
 
         hconsensus = new Hashtable[aWidth];
-        AAFrequency.calculate(alignment.getSequencesArray(), 0, alignment
-                .getWidth(), hconsensus, includeAllConsensusSymbols);
-        AAFrequency.completeConsensus(consensus, hconsensus, 0, aWidth,
-                ignoreGapsInConsensusCalculation,
-                includeAllConsensusSymbols);
+        AAFrequency.calculate(alignment.getSequencesArray(), 0,
+                alignment.getWidth(), hconsensus, true);
+        updateAnnotation(true);
 
         if (globalColourScheme != null)
         {
@@ -582,6 +580,28 @@ public class AlignViewport implements SelectionSource
         ap.paintAlignment(true);
       }
     }
+
+    /**
+     * update the consensus annotation from the sequence profile data using
+     * current visualization settings.
+     */
+    public void updateAnnotation()
+    {
+      updateAnnotation(false);
+    }
+
+    protected void updateAnnotation(boolean immediate)
+    {
+      // TODO: make calls thread-safe, so if another thread calls this method,
+      // it will either return or wait until one calculation is finished.
+      if (immediate
+              || (!updatingConsensus && consensus != null && hconsensus != null))
+      {
+        AAFrequency.completeConsensus(consensus, hconsensus, 0,
+                hconsensus.length, ignoreGapsInConsensusCalculation,
+                showSequenceLogo);
+      }
+    }
   }
 
   /**
@@ -1546,90 +1566,7 @@ public class AlignViewport implements SelectionSource
   public jalview.datamodel.CigarArray getViewAsCigars(
           boolean selectedRegionOnly)
   {
-    CigarArray selection = null;
-    SequenceI[] seqs = null;
-    int i, iSize;
-    int start = 0, end = 0;
-    if (selectedRegionOnly && selectionGroup != null)
-    {
-      iSize = selectionGroup.getSize();
-      seqs = selectionGroup.getSequencesInOrder(alignment);
-      start = selectionGroup.getStartRes();
-      end = selectionGroup.getEndRes(); // inclusive for start and end in
-      // SeqCigar constructor
-    }
-    else
-    {
-      iSize = alignment.getHeight();
-      seqs = alignment.getSequencesArray();
-      end = alignment.getWidth() - 1;
-    }
-    SeqCigar[] selseqs = new SeqCigar[iSize];
-    for (i = 0; i < iSize; i++)
-    {
-      selseqs[i] = new SeqCigar(seqs[i], start, end);
-    }
-    selection = new CigarArray(selseqs);
-    // now construct the CigarArray operations
-    if (hasHiddenColumns)
-    {
-      Vector regions = colSel.getHiddenColumns();
-      int[] region;
-      int hideStart, hideEnd;
-      int last = start;
-      for (int j = 0; last < end & j < regions.size(); j++)
-      {
-        region = (int[]) regions.elementAt(j);
-        hideStart = region[0];
-        hideEnd = region[1];
-        // edit hidden regions to selection range
-        if (hideStart < last)
-        {
-          if (hideEnd > last)
-          {
-            hideStart = last;
-          }
-          else
-          {
-            continue;
-          }
-        }
-
-        if (hideStart > end)
-        {
-          break;
-        }
-
-        if (hideEnd > end)
-        {
-          hideEnd = end;
-        }
-
-        if (hideStart > hideEnd)
-        {
-          break;
-        }
-        /**
-         * form operations...
-         */
-        if (last < hideStart)
-        {
-          selection.addOperation(CigarArray.M, hideStart - last);
-        }
-        selection.addOperation(CigarArray.D, 1 + hideEnd - hideStart);
-        last = hideEnd + 1;
-      }
-      // Final match if necessary.
-      if (last < end)
-      {
-        selection.addOperation(CigarArray.M, end - last + 1);
-      }
-    }
-    else
-    {
-      selection.addOperation(CigarArray.M, end - start + 1);
-    }
-    return selection;
+    return new jalview.datamodel.CigarArray(alignment, (hasHiddenColumns ? colSel : null), (selectedRegionOnly ? selectionGroup : null));
   }
 
   /**
@@ -1640,21 +1577,24 @@ public class AlignViewport implements SelectionSource
    *          boolean true to just return the selected view
    * @return AlignmentView
    */
-  jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly)
+  public jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly)
   {
-    // JBPNote:
-    // this is here because the AlignmentView constructor modifies the
-    // CigarArray
-    // object. Refactoring of Cigar and alignment view representation should
-    // be done to remove redundancy.
-    CigarArray aligview = getViewAsCigars(selectedOnly);
-    if (aligview != null)
-    {
-      return new AlignmentView(aligview,
-              (selectedOnly && selectionGroup != null) ? selectionGroup
-                      .getStartRes() : 0);
-    }
-    return null;
+    return getAlignmentView(selectedOnly, false);
+  }
+  
+  /**
+   * return a compact representation of the current alignment selection to pass
+   * to an analysis function
+   * 
+   * @param selectedOnly
+   *          boolean true to just return the selected view
+   * @param markGroups
+   *          boolean true to annotate the alignment view with groups on the alignment (and intersecting with selected region if selectedOnly is true) 
+   * @return AlignmentView
+   */
+  public jalview.datamodel.AlignmentView getAlignmentView(boolean selectedOnly, boolean markGroups)
+  {
+    return new AlignmentView(alignment, colSel, selectionGroup, hasHiddenColumns, selectedOnly, markGroups);
   }
 
   /**
@@ -1836,8 +1776,8 @@ public class AlignViewport implements SelectionSource
       {
         Alignment al = (Alignment) alignment;
         Conservation c = new Conservation("All",
-                ResidueProperties.propHash, 3, al.getSequences(), 0, al
-                        .getWidth() - 1);
+                ResidueProperties.propHash, 3, al.getSequences(), 0,
+                al.getWidth() - 1);
         c.calculate();
         c.verdict(false, ConsPercGaps);
 
@@ -1851,8 +1791,8 @@ public class AlignViewport implements SelectionSource
       SequenceGroup sg = (SequenceGroup) alignment.getGroups().elementAt(s);
       if (sg.cs != null && sg.cs instanceof ClustalxColourScheme)
       {
-        ((ClustalxColourScheme) sg.cs).resetClustalX(sg
-                .getSequences(hiddenRepSequences), sg.getWidth());
+        ((ClustalxColourScheme) sg.cs).resetClustalX(
+                sg.getSequences(hiddenRepSequences), sg.getWidth());
       }
       sg.recalcConservation();
     }
@@ -2205,6 +2145,16 @@ public class AlignViewport implements SelectionSource
    */
   public void setShowSequenceLogo(boolean showSequenceLogo)
   {
+    if (showSequenceLogo != this.showSequenceLogo)
+    {
+      // TODO: decouple settings setting from calculation when refactoring
+      // annotation update method from alignframe to viewport
+      this.showSequenceLogo = showSequenceLogo;
+      if (consensusThread != null)
+      {
+        consensusThread.updateAnnotation();
+      }
+    }
     this.showSequenceLogo = showSequenceLogo;
   }
 
@@ -2252,24 +2202,6 @@ public class AlignViewport implements SelectionSource
   }
 
   /**
-   * @return the includeAllConsensusSymbols
-   */
-  public boolean isIncludeAllConsensusSymbols()
-  {
-    return includeAllConsensusSymbols;
-  }
-
-  /**
-   * @param includeAllConsensusSymbols
-   *          the includeAllConsensusSymbols to set
-   */
-  public void setIncludeAllConsensusSymbols(
-          boolean includeAllConsensusSymbols)
-  {
-    this.includeAllConsensusSymbols = includeAllConsensusSymbols;
-  }
-
-  /**
    * 
    * @return flag to indicate if the consensus histogram should be rendered by
    *         default