Merge branch 'develop' into developtomchmmer
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index b3ccb45..39a1c18 100644 (file)
@@ -22,6 +22,7 @@ package jalview.viewmodel;
 
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.Conservation;
+import jalview.analysis.TreeModel;
 import jalview.api.AlignCalcManagerI;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
@@ -33,12 +34,9 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.Annotation;
-import jalview.datamodel.CigarArray;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.HiddenSequences;
-import jalview.datamodel.ProfileI;
-import jalview.datamodel.Profiles;
 import jalview.datamodel.ProfilesI;
 import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.Sequence;
@@ -70,6 +68,7 @@ import java.util.BitSet;
 import java.util.Deque;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -80,10 +79,10 @@ import java.util.Map;
  * @author jimp
  * 
  */
-public abstract class AlignmentViewport implements AlignViewportI,
-        CommandListener, VamsasSource
+public abstract class AlignmentViewport
+        implements AlignViewportI, CommandListener, VamsasSource
 {
-  final protected ViewportRanges ranges;
+  protected ViewportRanges ranges;
 
   protected ViewStyleI viewStyle = new ViewStyle();
 
@@ -98,12 +97,70 @@ public abstract class AlignmentViewport implements AlignViewportI,
   protected Deque<CommandI> historyList = new ArrayDeque<>();
 
   protected Deque<CommandI> redoList = new ArrayDeque<>();
-  
+
+  protected String sequenceSetID;
+
+  /*
+   * probably unused indicator that view is of a dataset rather than an
+   * alignment
+   */
+  protected boolean isDataset = false;
+
+  private Map<SequenceI, SequenceCollectionI> hiddenRepSequences;
+
+  protected ColumnSelection colSel = new ColumnSelection();
+
+  public boolean autoCalculateConsensus = true;
+
+  protected boolean autoCalculateStrucConsensus = true;
+
+  protected boolean ignoreGapsInConsensusCalculation = false;
+
+  protected boolean ignoreBelowBackGroundFrequencyCalculation = false;
+
+  protected boolean infoLetterHeight = false;
+
+  protected ResidueShaderI residueShading = new ResidueShader();
+
+  protected AlignmentAnnotation consensus;
+
+  protected AlignmentAnnotation complementConsensus;
+
+  protected AlignmentAnnotation occupancy;
+
+  protected AlignmentAnnotation strucConsensus;
+
+  protected AlignmentAnnotation conservation;
+
+  protected AlignmentAnnotation quality;
 
   /**
-   * alignment displayed in the viewport. Please use get/setter
+   * alignment displayed in the viewport
+   */
+  private AlignmentI alignment;
+
+  /**
+   * results of alignment consensus analysis for visible portion of view
+   */
+  protected ProfilesI consensusProfiles;
+
+  /**
+   * HMM profile for the alignment
+   */
+  protected ProfilesI hmmProfiles;
+
+  /**
+   * results of cDNA complement consensus visible portion of view
+   */
+  protected Hashtable[] hcomplementConsensus;
+
+  /**
+   * results of secondary structure base pair consensus for visible portion of
+   * view
    */
-  protected AlignmentI alignment;
+  protected Hashtable[] hStrucConsensus;
+
+  protected Conservation hconservation;
 
   public AlignmentViewport(AlignmentI al)
   {
@@ -409,6 +466,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void setWrapAlignment(boolean state)
   {
     viewStyle.setWrapAlignment(state);
+    ranges.setWrappedMode(state);
   }
 
   /**
@@ -571,8 +629,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     viewStyle.setSeqNameItalics(default1);
   }
 
-
-
   @Override
   public AlignmentI getAlignment()
   {
@@ -585,14 +641,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return alignment.getGapCharacter();
   }
 
-  protected String sequenceSetID;
-
-  /**
-   * probably unused indicator that view is of a dataset rather than an
-   * alignment
-   */
-  protected boolean isDataset = false;
-
   public void setDataset(boolean b)
   {
     isDataset = b;
@@ -603,22 +651,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return isDataset;
   }
 
-  private Map<SequenceI, SequenceCollectionI> hiddenRepSequences;
-
-  protected ColumnSelection colSel = new ColumnSelection();
-
-  public boolean autoCalculateConsensus = true;
-
-  public boolean autoCalculateInformation = true;
-
-  protected boolean autoCalculateStrucConsensus = true;
-
-  protected boolean ignoreGapsInConsensusCalculation = false;
-
-  protected boolean ignoreBelowBackGroundFrequencyCalculation = false;
-
-  protected ResidueShaderI residueShading = new ResidueShader();
-
   @Override
   public void setGlobalColourScheme(ColourSchemeI cs)
   {
@@ -650,6 +682,11 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         residueShading.setConservation(hconservation);
       }
+      /*
+       * reset conservation flag in case just set to false if
+       * Conservation was null (calculation still in progress)
+       */
+      residueShading.setConservationApplied(getConservationSelected());
       residueShading.alignmentChanged(alignment, hiddenRepSequences);
     }
 
@@ -665,11 +702,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
          * retain any colour thresholds per group while
          * changing choice of colour scheme (JAL-2386)
          */
-        sg.setColourScheme(cs);
+        sg.setColourScheme(
+                cs == null ? null : cs.getInstance(this, sg));
         if (cs != null)
         {
-          sg.getGroupColourScheme()
-                  .alignmentChanged(sg, hiddenRepSequences);
+          sg.getGroupColourScheme().alignmentChanged(sg,
+                  hiddenRepSequences);
         }
       }
     }
@@ -678,8 +716,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public ColourSchemeI getGlobalColourScheme()
   {
-    return residueShading == null ? null : residueShading
-            .getColourScheme();
+    return residueShading == null ? null : residueShading.getColourScheme();
   }
 
   @Override
@@ -687,47 +724,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
   {
     return residueShading;
   }
-
-  protected AlignmentAnnotation consensus;
-
-  protected AlignmentAnnotation complementConsensus;
-
-  protected AlignmentAnnotation gapcounts;
-
-  protected AlignmentAnnotation strucConsensus;
-
-  protected AlignmentAnnotation conservation;
-
-  protected AlignmentAnnotation quality;
-
-  protected AlignmentAnnotation[] groupConsensus;
-
-  protected AlignmentAnnotation[] groupConservation;
-
-  protected List<AlignmentAnnotation> groupInformation = new ArrayList<>();
-
-  protected List<AlignmentAnnotation> information = new ArrayList<>();
-
-  /**
-   * results of alignment consensus analysis for visible portion of view
-   */
-  protected ProfilesI hconsensus = null;
-
-  protected List<ProfilesI> hinformation = new ArrayList<>();
-
-  /**
-   * results of cDNA complement consensus visible portion of view
-   */
-  protected Hashtable[] hcomplementConsensus = null;
-
-  /**
-   * results of secondary structure base pair consensus for visible portion of
-   * view
-   */
-  protected Hashtable[] hStrucConsensus = null;
-
-  protected Conservation hconservation = null;
-
   @Override
   public void setConservation(Conservation cons)
   {
@@ -747,9 +743,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
-  public void setSequenceConsensusHash(ProfilesI hconsensus)
+  public void setConsensusProfiles(ProfilesI hconsensus)
   {
-    this.hconsensus = hconsensus;
+    this.consensusProfiles = hconsensus;
   }
 
   @Override
@@ -759,33 +755,21 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
-  public ProfilesI getSequenceConsensusHash()
-  {
-    return hconsensus;
-  }
-
-  @Override
-  public void setSequenceInformationHashes(List<ProfilesI> info)
-  {
-    hinformation = info;
-  }
-
-  @Override
-  public void setSequenceInformationHash(ProfilesI info, int index)
+  public ProfilesI getConsensusProfiles()
   {
-    hinformation.set(index, info);
+    return consensusProfiles;
   }
 
   @Override
-  public List<ProfilesI> getSequenceInformationHashes()
+  public void setHmmProfiles(ProfilesI info)
   {
-    return hinformation;
+    hmmProfiles = info;
   }
 
   @Override
-  public ProfilesI getSequenceInformationHash(int index)
+  public ProfilesI getHmmProfiles()
   {
-    return hinformation.get(index);
+    return hmmProfiles;
   }
 
   @Override
@@ -826,22 +810,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
-  public List<AlignmentAnnotation> getInformationAnnotations()
-  {
-    return information;
-  }
-
-  @Override
-  public AlignmentAnnotation getInformationAnnotation(int index)
-  {
-    return information.get(index);
-  }
-
-
-  @Override
-  public AlignmentAnnotation getAlignmentGapAnnotation()
+  public AlignmentAnnotation getOccupancyAnnotation()
   {
-    return gapcounts;
+    return occupancy;
   }
 
   @Override
@@ -870,11 +841,11 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       return;
     }
-    if (calculator
-            .getRegisteredWorkersOfClass(jalview.workers.ConservationThread.class) == null)
+    if (calculator.getRegisteredWorkersOfClass(
+            jalview.workers.ConservationThread.class) == null)
     {
-      calculator.registerWorker(new jalview.workers.ConservationThread(
-              this, ap));
+      calculator.registerWorker(
+              new jalview.workers.ConservationThread(this, ap));
     }
   }
 
@@ -888,7 +859,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       return;
     }
-    if (calculator.getRegisteredWorkersOfClass(ConsensusThread.class) == null)
+    if (calculator
+            .getRegisteredWorkersOfClass(ConsensusThread.class) == null)
     {
       calculator.registerWorker(new ConsensusThread(this, ap));
     }
@@ -919,8 +891,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
       }
       if (doConsensus)
       {
-        if (calculator
-                .getRegisteredWorkersOfClass(ComplementConsensusThread.class) == null)
+        if (calculator.getRegisteredWorkersOfClass(
+                ComplementConsensusThread.class) == null)
         {
           calculator
                   .registerWorker(new ComplementConsensusThread(this, ap));
@@ -929,17 +901,14 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
   }
 
-  /**
-   * trigger update of information annotation
-   */
-  public void updateInformation(final AlignmentViewPanel ap)
+  @Override
+  public void initInformationWorker(final AlignmentViewPanel ap)
   {
     if (calculator
             .getRegisteredWorkersOfClass(InformationThread.class) == null)
     {
       calculator.registerWorker(new InformationThread(this, ap));
     }
-
   }
 
   // --------START Structure Conservation
@@ -957,7 +926,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       return;
     }
-    if (calculator.getRegisteredWorkersOfClass(StrucConsensusThread.class) == null)
+    if (calculator.getRegisteredWorkersOfClass(
+            StrucConsensusThread.class) == null)
     {
       calculator.registerWorker(new StrucConsensusThread(this, ap));
     }
@@ -1004,14 +974,16 @@ public abstract class AlignmentViewport implements AlignViewportI,
     strucConsensus = null;
     conservation = null;
     quality = null;
-    groupConsensus = null;
-    groupConservation = null;
-    hconsensus = null;
+    consensusProfiles = null;
+    hconservation = null;
     hcomplementConsensus = null;
-    // colour scheme may hold reference to consensus
-    residueShading = null;
-    // TODO remove listeners from changeSupport?
+    occupancy = null;
+    calculator = null;
+    residueShading = null; // may hold a reference to Consensus
     changeSupport = null;
+    ranges = null;
+    currentTree = null;
+    selectionGroup = null;
     setAlignment(null);
   }
 
@@ -1057,17 +1029,17 @@ public abstract class AlignmentViewport implements AlignViewportI,
   /**
    * should hmm profile be rendered by default
    */
-  protected boolean showHMMSequenceLogo = false;
+  protected boolean hmmShowSequenceLogo = false;
 
   /**
    * should hmm profile be rendered normalised to row height
    */
-  protected boolean normaliseHMMSequenceLogo = false;
+  protected boolean hmmNormaliseSequenceLogo = false;
 
   /**
    * should information histograms be rendered by default
    */
-  protected boolean showInformationHistogram = true;
+  protected boolean hmmShowHistogram = true;
 
   /**
    * @return the showConsensusProfile
@@ -1084,7 +1056,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public boolean isShowHMMSequenceLogo()
   {
-    return showHMMSequenceLogo;
+    return hmmShowSequenceLogo;
   }
 
   /**
@@ -1107,12 +1079,14 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   public void setShowHMMSequenceLogo(boolean showHMMSequenceLogo)
   {
-    if (showHMMSequenceLogo != this.showHMMSequenceLogo)
+    if (showHMMSequenceLogo != this.hmmShowSequenceLogo)
     {
-      this.showHMMSequenceLogo = showHMMSequenceLogo;
-      calculator.updateAnnotationFor(InformationThread.class);
+      this.hmmShowSequenceLogo = showHMMSequenceLogo;
+      // TODO: updateAnnotation if description (tooltip) will show
+      // profile in place of information content?
+      // calculator.updateAnnotationFor(InformationThread.class);
     }
-    this.showHMMSequenceLogo = showHMMSequenceLogo;
+    this.hmmShowSequenceLogo = showHMMSequenceLogo;
   }
 
   /**
@@ -1126,11 +1100,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   /**
    * @param showInformationHistogram
-   *          the showInformationHistogram to set
    */
   public void setShowInformationHistogram(boolean showInformationHistogram)
   {
-    this.showInformationHistogram = showInformationHistogram;
+    this.hmmShowHistogram = showInformationHistogram;
   }
 
   /**
@@ -1186,7 +1159,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public boolean isShowInformationHistogram()
   {
-    return this.showInformationHistogram;
+    return this.hmmShowHistogram;
   }
 
   /**
@@ -1277,7 +1250,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public boolean hasHiddenColumns()
   {
-    return colSel != null
+    return alignment.getHiddenColumns() != null
             && alignment.getHiddenColumns().hasHiddenColumns();
   }
 
@@ -1300,8 +1273,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
   {
     if (sequenceSetID != null)
     {
-      System.err
-              .println("Warning - overwriting a sequenceSetId for a viewport!");
+      System.err.println(
+              "Warning - overwriting a sequenceSetId for a viewport!");
     }
     sequenceSetID = new String(newid);
   }
@@ -1338,24 +1311,22 @@ public abstract class AlignmentViewport implements AlignViewportI,
     ignoreGapsInConsensusCalculation = b;
     if (ap != null)
     {
-      updateConsensus(ap);
       if (residueShading != null)
       {
         residueShading.setThreshold(residueShading.getThreshold(),
                 ignoreGapsInConsensusCalculation);
       }
     }
-
   }
 
   public void setIgnoreBelowBackground(boolean b, AlignmentViewPanel ap)
   {
     ignoreBelowBackGroundFrequencyCalculation = b;
-    if (ap != null)
-    {
-      updateInformation(ap);
-    }
+  }
 
+  public void setInfoLetterHeight(boolean b, AlignmentViewPanel ap)
+  {
+    infoLetterHeight = b;
   }
 
   private long sgrouphash = -1, colselhash = -1;
@@ -1418,6 +1389,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return ignoreBelowBackGroundFrequencyCalculation;
   }
 
+  @Override
+  public boolean isInfoLetterHeight()
+  {
+    return infoLetterHeight;
+  }
+
   // property change stuff
   // JBPNote Prolly only need this in the applet version.
   private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
@@ -1463,7 +1440,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void removePropertyChangeListener(
           java.beans.PropertyChangeListener listener)
   {
-    changeSupport.removePropertyChangeListener(listener);
+    if (changeSupport != null)
+    {
+      changeSupport.removePropertyChangeListener(listener);
+    }
   }
 
   /**
@@ -1524,6 +1504,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
   // common hide/show seq stuff
   public void showAllHiddenSeqs()
   {
+    int startSeq = ranges.getStartSeq();
+    int endSeq = ranges.getEndSeq();
+
     if (alignment.getHiddenSequences().getSize() > 0)
     {
       if (selectionGroup == null)
@@ -1531,8 +1514,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
         selectionGroup = new SequenceGroup();
         selectionGroup.setEndRes(alignment.getWidth() - 1);
       }
-      List<SequenceI> tmp = alignment.getHiddenSequences().showAll(
-              hiddenRepSequences);
+      List<SequenceI> tmp = alignment.getHiddenSequences()
+              .showAll(hiddenRepSequences);
       for (SequenceI seq : tmp)
       {
         selectionGroup.addSequence(seq, false);
@@ -1541,6 +1524,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
       hiddenRepSequences = null;
 
+      ranges.setStartEndSeq(startSeq, endSeq + tmp.size());
+
       firePropertyChange("alignment", null, alignment.getSequences());
       // used to set hasHiddenRows/hiddenRepSequences here, after the property
       // changed event
@@ -1550,8 +1535,11 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   public void showSequence(int index)
   {
-    List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(
-            index, hiddenRepSequences);
+    int startSeq = ranges.getStartSeq();
+    int endSeq = ranges.getEndSeq();
+
+    List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(index,
+            hiddenRepSequences);
     if (tmp.size() > 0)
     {
       if (selectionGroup == null)
@@ -1565,6 +1553,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
         selectionGroup.addSequence(seq, false);
         setSequenceAnnotationsVisible(seq, true);
       }
+
+      ranges.setStartEndSeq(startSeq, endSeq + tmp.size());
+
       firePropertyChange("alignment", null, alignment.getSequences());
       sendSelection();
     }
@@ -1586,6 +1577,11 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   public void hideSequence(SequenceI[] seq)
   {
+    /*
+     * cache offset to first visible sequence
+     */
+    int startSeq = ranges.getStartSeq();
+
     if (seq != null)
     {
       for (int i = 0; i < seq.length; i++)
@@ -1593,6 +1589,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
         alignment.getHiddenSequences().hideSequence(seq[i]);
         setSequenceAnnotationsVisible(seq[i], false);
       }
+      ranges.setStartSeq(startSeq);
       firePropertyChange("alignment", null, alignment.getSequences());
     }
   }
@@ -1622,8 +1619,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
 
     int gsize = selectionGroup.getSize();
-    SequenceI[] hseqs = selectionGroup.getSequences().toArray(
-            new SequenceI[gsize]);
+    SequenceI[] hseqs = selectionGroup.getSequences()
+            .toArray(new SequenceI[gsize]);
 
     hideSequence(hseqs);
     setSelectionGroup(null);
@@ -1713,8 +1710,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
    */
   public boolean isHiddenRepSequence(SequenceI seq)
   {
-    return (hiddenRepSequences != null && hiddenRepSequences
-            .containsKey(seq));
+    return (hiddenRepSequences != null
+            && hiddenRepSequences.containsKey(seq));
   }
 
   /**
@@ -1732,14 +1729,15 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public int adjustForHiddenSeqs(int alignmentIndex)
   {
-    return alignment.getHiddenSequences().adjustForHiddenSeqs(
-            alignmentIndex);
+    return alignment.getHiddenSequences()
+            .adjustForHiddenSeqs(alignmentIndex);
   }
 
   @Override
   public void invertColumnSelection()
   {
     colSel.invertColumnSelection(0, alignment.getWidth(), alignment);
+    isColSelChanged(true);
   }
 
   @Override
@@ -1763,7 +1761,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
     else
     {
-      sequences = selectionGroup.getSelectionAsNewSequences(alignment);
+      sequences = selectionGroup.getSelectionAsNewSequences(alignment,
+              true);
     }
 
     return sequences;
@@ -1785,13 +1784,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
-  public CigarArray getViewAsCigars(boolean selectedRegionOnly)
-  {
-    return new CigarArray(alignment, alignment.getHiddenColumns(),
-            (selectedRegionOnly ? selectionGroup : null));
-  }
-
-  @Override
   public jalview.datamodel.AlignmentView getAlignmentView(
           boolean selectedOnly)
   {
@@ -1803,10 +1795,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
           boolean selectedOnly, boolean markGroups)
   {
     return new AlignmentView(alignment, alignment.getHiddenColumns(),
-            selectionGroup, alignment.getHiddenColumns() != null
+            selectionGroup,
+            alignment.getHiddenColumns() != null
                     && alignment.getHiddenColumns().hasHiddenColumns(),
-            selectedOnly,
-            markGroups);
+            selectedOnly, markGroups);
   }
 
   @Override
@@ -1852,8 +1844,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
     if (alignment.getHiddenColumns() != null
             && alignment.getHiddenColumns().hasHiddenColumns())
     {
-      selection = alignment.getHiddenColumns().getVisibleSequenceStrings(
-              start, end, seqs);
+      for (i = 0; i < iSize; i++)
+      {
+        Iterator<int[]> blocks = alignment.getHiddenColumns()
+                .getVisContigsIterator(start, end + 1, false);
+        selection[i] = seqs[i].getSequenceStringFromIterator(blocks);
+      }
     }
     else
     {
@@ -1880,10 +1876,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         if (start == 0)
         {
-          start = hidden.adjustForHiddenColumns(start);
+          start = hidden.visibleToAbsoluteColumn(start);
         }
 
-        end = hidden.getHiddenBoundaryRight(start);
+        end = hidden.getNextHiddenBoundary(false, start);
         if (start == end)
         {
           end = max;
@@ -1898,8 +1894,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
       if (hidden != null && hidden.hasHiddenColumns())
       {
-        start = hidden.adjustForHiddenColumns(end);
-        start = hidden.getHiddenBoundaryLeft(start) + 1;
+        start = hidden.visibleToAbsoluteColumn(end);
+        start = hidden.getNextHiddenBoundary(true, start) + 1;
       }
     } while (end < max);
 
@@ -1921,13 +1917,13 @@ public abstract class AlignmentViewport implements AlignViewportI,
         AlignmentAnnotation clone = new AlignmentAnnotation(annot);
         if (selectedOnly && selectionGroup != null)
         {
-          alignment.getHiddenColumns().makeVisibleAnnotation(
-                  selectionGroup.getStartRes(),
-                  selectionGroup.getEndRes(), clone);
+          clone.makeVisibleAnnotation(
+                  selectionGroup.getStartRes(), selectionGroup.getEndRes(),
+                  alignment.getHiddenColumns());
         }
         else
         {
-          alignment.getHiddenColumns().makeVisibleAnnotation(clone);
+          clone.makeVisibleAnnotation(alignment.getHiddenColumns());
         }
         ala.add(clone);
       }
@@ -1960,28 +1956,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       alignment.padGaps();
     }
-    if (autoCalculateConsensus)
-    {
-      updateConsensus(ap);
-    }
-    if (hconsensus != null && autoCalculateConsensus)
-    {
-      updateConservation(ap);
-    }
-    if (autoCalculateStrucConsensus)
-    {
-      updateStrucConsensus(ap);
-    }
-    updateInformation(ap);
-
-    Map<Integer, SequenceI> hmmSequences;
-    hmmSequences = alignment.getHMMConsensusSequences(false);
-
-    for (Map.Entry<Integer, SequenceI> entry : hmmSequences.entrySet())
-    {
-      SequenceI seq = entry.getValue();
-      seq.updateHMMMapping();
-    }
 
     // Reset endRes of groups if beyond alignment width
     int alWidth = alignment.getWidth();
@@ -2004,7 +1978,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
     updateAllColourSchemes();
     calculator.restartWorkers();
-    // alignment.adjustSequenceAnnotations();
   }
 
   /**
@@ -2017,7 +1990,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       rs.alignmentChanged(alignment, hiddenRepSequences);
 
-      rs.setConsensus(hconsensus);
+      rs.setConsensus(consensusProfiles);
       if (rs.conservationApplied())
       {
         rs.setConservation(Conservation.calculateConservation("All",
@@ -2042,7 +2015,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
     // depending on if the user wants to see the annotation or not in a
     // specific alignment
 
-    if (hconsensus == null && !isDataset)
+    if (consensusProfiles == null && !isDataset)
     {
       if (!alignment.isNucleotide())
       {
@@ -2058,13 +2031,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
       initConsensus(consensus);
 
-      initGapCounts();
+      initOccupancy();
 
       initComplementConsensus();
     }
   }
 
-
   /**
    * If this is a protein alignment and there are mappings to cDNA, adds the
    * cDNA consensus annotation and returns true, else returns false.
@@ -2116,38 +2088,22 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
   }
 
-  public void initInformation(SequenceI hmmSequence)
-  {
-    AlignmentAnnotation information;
-    information = new AlignmentAnnotation(hmmSequence.getName(),
-            MessageManager.getString("label.information_description"),
-            new Annotation[1], 0f, 6.52f, AlignmentAnnotation.BAR_GRAPH);
-    information.hasText = true;
-    information.autoCalculated = true;
-    information.hasText = true;
-    information.autoCalculated = false;
-    information.sequenceRef = hmmSequence;
-    this.information.add(information);
-    hinformation.add(new Profiles(new ProfileI[1]));
-    alignment.addAnnotation(information);
-  }
-
   // these should be extracted from the view model - style and settings for
   // derived annotation
-  private void initGapCounts()
+  private void initOccupancy()
   {
     if (showOccupancy)
     {
-      gapcounts = new AlignmentAnnotation("Occupancy",
+      occupancy = new AlignmentAnnotation("Occupancy",
               MessageManager.getString("label.occupancy_descr"),
-              new Annotation[1], 0f,
-              alignment.getHeight(), AlignmentAnnotation.BAR_GRAPH);
-      gapcounts.hasText = true;
-      gapcounts.autoCalculated = true;
-      gapcounts.scaleColLabel = true;
-      gapcounts.graph = AlignmentAnnotation.BAR_GRAPH;
+              new Annotation[1], 0f, alignment.getHeight(),
+              AlignmentAnnotation.BAR_GRAPH);
+      occupancy.hasText = true;
+      occupancy.autoCalculated = true;
+      occupancy.scaleColLabel = true;
+      occupancy.graph = AlignmentAnnotation.BAR_GRAPH;
 
-      alignment.addAnnotation(gapcounts);
+      alignment.addAnnotation(occupancy);
     }
   }
 
@@ -2159,8 +2115,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         conservation = new AlignmentAnnotation("Conservation",
                 MessageManager.formatMessage("label.conservation_descr",
-                        getConsPercGaps()), new Annotation[1],
-                0f, 11f, AlignmentAnnotation.BAR_GRAPH);
+                        getConsPercGaps()),
+                new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
         conservation.hasText = true;
         conservation.autoCalculated = true;
         alignment.addAnnotation(conservation);
@@ -2321,7 +2277,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
           sg.setshowSequenceLogo(showprf);
           sg.setShowConsensusHistogram(showConsHist);
           sg.setNormaliseSequenceLogo(normLogo);
-          sg.setshowHMMSequenceLogo(showHMMPrf);
+          sg.setShowHMMSequenceLogo(showHMMPrf);
           sg.setShowInformationHistogram(showInfoHist);
           sg.setNormaliseHMMSequenceLogo(normHMMLogo);
         }
@@ -2646,8 +2602,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     viewStyle = new ViewStyle(settingsForView);
     if (residueShading != null)
     {
-      residueShading.setConservationApplied(settingsForView
-              .isConservationColourSelected());
+      residueShading.setConservationApplied(
+              settingsForView.isConservationColourSelected());
     }
   }
 
@@ -2815,7 +2771,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return sortAnnotationsBy;
   }
 
-  public void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
+  public void setSortAnnotationsBy(
+          SequenceAnnotationOrder sortAnnotationsBy)
   {
     this.sortAnnotationsBy = sortAnnotationsBy;
   }
@@ -2893,8 +2850,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
       return 0;
     }
     boolean iAmProtein = !getAlignment().isNucleotide();
-    AlignmentI proteinAlignment = iAmProtein ? getAlignment() : complement
-            .getAlignment();
+    AlignmentI proteinAlignment = iAmProtein ? getAlignment()
+            : complement.getAlignment();
     if (proteinAlignment == null)
     {
       return 0;
@@ -2925,7 +2882,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
      */
     int lastSeq = alignment.getHeight() - 1;
     List<AlignedCodonFrame> seqMappings = null;
-    for (int seqNo = ranges.getStartSeq(); seqNo < lastSeq; seqNo++, seqOffset++)
+    for (int seqNo = ranges
+            .getStartSeq(); seqNo <= lastSeq; seqNo++, seqOffset++)
     {
       sequence = getAlignment().getSequenceAt(seqNo);
       if (hiddenSequences != null && hiddenSequences.isHidden(sequence))
@@ -2936,9 +2894,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         continue;
       }
-      seqMappings = MappingUtils
-              .findMappingsForSequenceAndOthers(sequence, mappings,
-                      getCodingComplement().getAlignment().getSequences());
+      seqMappings = MappingUtils.findMappingsForSequenceAndOthers(sequence,
+              mappings,
+              getCodingComplement().getAlignment().getSequences());
       if (!seqMappings.isEmpty())
       {
         break;
@@ -3021,6 +2979,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
    */
   private SearchResultsI searchResults = null;
 
+  protected TreeModel currentTree = null;
+
   @Override
   public boolean hasSearchResults()
   {
@@ -3038,6 +2998,156 @@ public abstract class AlignmentViewport implements AlignViewportI,
   {
     return searchResults;
   }
-  
 
+  /**
+   * get the consensus sequence as displayed under the PID consensus annotation
+   * row.
+   * 
+   * @return consensus sequence as a new sequence object
+   */
+  public SequenceI getConsensusSeq()
+  {
+    if (consensus == null)
+    {
+      updateConsensus(null);
+    }
+    if (consensus == null)
+    {
+      return null;
+    }
+    StringBuffer seqs = new StringBuffer();
+    for (int i = 0; i < consensus.annotations.length; i++)
+    {
+      Annotation annotation = consensus.annotations[i];
+      if (annotation != null)
+      {
+        String description = annotation.description;
+        if (description != null && description.startsWith("["))
+        {
+          // consensus is a tie - just pick the first one
+          seqs.append(description.charAt(1));
+        }
+        else
+        {
+          seqs.append(annotation.displayCharacter);
+        }
+      }
+    }
+
+    SequenceI sq = new Sequence("Consensus", seqs.toString());
+    sq.setDescription("Percentage Identity Consensus "
+            + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
+    return sq;
+  }
+
+  public boolean hasReferenceAnnotation()
+  {
+    AlignmentAnnotation[] annots = this.alignment.getAlignmentAnnotation();
+    for (AlignmentAnnotation annot : annots)
+    {
+      if ("RF".equals(annot.label) || annot.label.contains("Reference"))
+      {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  @Override
+  public void setCurrentTree(TreeModel tree)
+  {
+    currentTree = tree;
+  }
+
+  @Override
+  public TreeModel getCurrentTree()
+  {
+    return currentTree;
+  }
+
+  @Override
+  public boolean isNormaliseSequenceLogo()
+  {
+    return normaliseSequenceLogo;
+  }
+
+  public void setNormaliseSequenceLogo(boolean state)
+  {
+    normaliseSequenceLogo = state;
+  }
+
+  @Override
+  public boolean isNormaliseHMMSequenceLogo()
+  {
+    return hmmNormaliseSequenceLogo;
+  }
+
+  public void setNormaliseHMMSequenceLogo(boolean state)
+  {
+    hmmNormaliseSequenceLogo = state;
+  }
+
+  /**
+   * flag set to indicate if structure views might be out of sync with sequences
+   * in the alignment
+   */
+
+  private boolean needToUpdateStructureViews = false;
+
+  @Override
+  public boolean isUpdateStructures()
+  {
+    return needToUpdateStructureViews;
+  }
+
+  @Override
+  public void setUpdateStructures(boolean update)
+  {
+    needToUpdateStructureViews = update;
+  }
+
+  @Override
+  public boolean needToUpdateStructureViews()
+  {
+    boolean update = needToUpdateStructureViews;
+    needToUpdateStructureViews = false;
+    return update;
+  }
+
+  @Override
+  public void addSequenceGroup(SequenceGroup sequenceGroup)
+  {
+    alignment.addGroup(sequenceGroup);
+
+    Color col = sequenceGroup.idColour;
+    if (col != null)
+    {
+      col = col.brighter();
+
+      for (SequenceI sq : sequenceGroup.getSequences())
+      {
+        setSequenceColour(sq, col);
+      }
+    }
+
+    if (codingComplement != null)
+    {
+      SequenceGroup mappedGroup = MappingUtils
+              .mapSequenceGroup(sequenceGroup, this, codingComplement);
+      if (mappedGroup.getSequences().size() > 0)
+      {
+        codingComplement.getAlignment().addGroup(mappedGroup);
+
+        if (col != null)
+        {
+          for (SequenceI seq : mappedGroup.getSequences())
+          {
+            codingComplement.setSequenceColour(seq, col);
+          }
+        }
+      }
+      // propagate the structure view update flag according to our own setting
+      codingComplement.setUpdateStructures(needToUpdateStructureViews);
+    }
+  }
 }