Merge remote-tracking branch 'origin/develop' into
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index 3fedb92..364222e 100644 (file)
@@ -29,6 +29,7 @@ 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.Sequence;
 import jalview.datamodel.SequenceCollectionI;
@@ -38,7 +39,6 @@ import jalview.schemes.Blosum62ColourScheme;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.PIDColourScheme;
 import jalview.schemes.ResidueProperties;
-import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
 import jalview.workers.AlignCalcManager;
 import jalview.workers.ConsensusThread;
 import jalview.workers.StrucConsensusThread;
@@ -46,6 +46,7 @@ import jalview.workers.StrucConsensusThread;
 import java.awt.Color;
 import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
@@ -60,6 +61,12 @@ import java.util.Vector;
  */
 public abstract class AlignmentViewport implements AlignViewportI
 {
+  /*
+   * A viewport that hosts the cDna view of this (protein), or vice versa (if
+   * set).
+   */
+  AlignViewportI codingComplement = null;
+
   /**
    * alignment displayed in the viewport. Please use get/setter
    */
@@ -447,7 +454,9 @@ public abstract class AlignmentViewport implements AlignViewportI
           AlignmentAnnotation alignmentAnnotation)
   {
     if (!alignmentAnnotation.autoCalculated)
+    {
       return false;
+    }
     if (calculator.workingInvolvedWith(alignmentAnnotation))
     {
       // System.err.println("grey out ("+alignmentAnnotation.label+")");
@@ -636,10 +645,6 @@ public abstract class AlignmentViewport implements AlignViewportI
   public void setHiddenColumns(ColumnSelection colsel)
   {
     this.colSel = colsel;
-    if (colSel.getHiddenColumns() != null)
-    {
-      hasHiddenColumns = true;
-    }
   }
 
   @Override
@@ -648,9 +653,14 @@ public abstract class AlignmentViewport implements AlignViewportI
     return colSel;
   }
 
+  @Override
   public void setColumnSelection(ColumnSelection colSel)
   {
     this.colSel = colSel;
+    if (colSel != null)
+    {
+      updateHiddenColumns();
+    }
   }
 
   /**
@@ -670,15 +680,22 @@ public abstract class AlignmentViewport implements AlignViewportI
     this.hiddenRepSequences = hiddenRepSequences;
   }
 
-  protected boolean hasHiddenColumns = false;
+  @Override
+  public boolean hasHiddenColumns()
+  {
+    return colSel != null && colSel.hasHiddenColumns();
+  }
 
   public void updateHiddenColumns()
   {
-    hasHiddenColumns = colSel.getHiddenColumns() != null;
+    // this method doesn't really do anything now. But - it could, since a
+    // column Selection could be in the process of modification
+    // hasHiddenColumns = colSel.hasHiddenColumns();
   }
 
   protected boolean hasHiddenRows = false;
 
+  @Override
   public boolean hasHiddenRows()
   {
     return hasHiddenRows;
@@ -804,7 +821,7 @@ public abstract class AlignmentViewport implements AlignViewportI
 
   protected boolean showConsensus = true;
 
-  Hashtable sequenceColours;
+  private Map<SequenceI, Color> sequenceColours = new HashMap<SequenceI, Color>();
 
   /**
    * Property change listener for changes in alignment
@@ -858,7 +875,6 @@ public abstract class AlignmentViewport implements AlignViewportI
     colSel.hideSelectedColumns();
     setSelectionGroup(null);
 
-    hasHiddenColumns = true;
   }
 
   public void hideColumns(int start, int end)
@@ -871,23 +887,17 @@ public abstract class AlignmentViewport implements AlignViewportI
     {
       colSel.hideColumns(start, end);
     }
-
-    hasHiddenColumns = true;
   }
 
   public void showColumn(int col)
   {
     colSel.revealHiddenColumns(col);
-    if (colSel.getHiddenColumns() == null)
-    {
-      hasHiddenColumns = false;
-    }
+
   }
 
   public void showAllHiddenColumns()
   {
     colSel.revealAllHiddenColumns();
-    hasHiddenColumns = false;
   }
 
   // common hide/show seq stuff
@@ -900,11 +910,12 @@ public abstract class AlignmentViewport implements AlignViewportI
         selectionGroup = new SequenceGroup();
         selectionGroup.setEndRes(alignment.getWidth() - 1);
       }
-      Vector tmp = alignment.getHiddenSequences().showAll(
+      List<SequenceI> tmp = alignment.getHiddenSequences().showAll(
               hiddenRepSequences);
-      for (int t = 0; t < tmp.size(); t++)
+      for (SequenceI seq : tmp)
       {
-        selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
+        selectionGroup.addSequence(seq, false);
+        setSequenceAnnotationsVisible(seq, true);
       }
 
       hasHiddenRows = false;
@@ -919,7 +930,8 @@ public abstract class AlignmentViewport implements AlignViewportI
 
   public void showSequence(int index)
   {
-    Vector tmp = alignment.getHiddenSequences().showSequence(index,
+    List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(
+            index,
             hiddenRepSequences);
     if (tmp.size() > 0)
     {
@@ -929,9 +941,10 @@ public abstract class AlignmentViewport implements AlignViewportI
         selectionGroup.setEndRes(alignment.getWidth() - 1);
       }
 
-      for (int t = 0; t < tmp.size(); t++)
+      for (SequenceI seq : tmp)
       {
-        selectionGroup.addSequence((SequenceI) tmp.elementAt(t), false);
+        selectionGroup.addSequence(seq, false);
+        setSequenceAnnotationsVisible(seq, true);
       }
       // JBPNote: refactor: only update flag if we modified visiblity (used to
       // do this regardless)
@@ -965,12 +978,30 @@ public abstract class AlignmentViewport implements AlignViewportI
       for (int i = 0; i < seq.length; i++)
       {
         alignment.getHiddenSequences().hideSequence(seq[i]);
+        setSequenceAnnotationsVisible(seq[i], false);
       }
       hasHiddenRows = true;
       firePropertyChange("alignment", null, alignment.getSequences());
     }
   }
 
+  /**
+   * Set visibility for any annotations for the given sequence.
+   * 
+   * @param sequenceI
+   */
+  protected void setSequenceAnnotationsVisible(SequenceI sequenceI,
+          boolean visible)
+  {
+    for (AlignmentAnnotation ann : alignment.getAlignmentAnnotation())
+    {
+      if (ann.sequenceRef == sequenceI)
+      {
+        ann.visible = visible;
+      }
+    }
+  }
+
   public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
   {
     int sSize = sg.getSize();
@@ -1009,8 +1040,8 @@ public abstract class AlignmentViewport implements AlignViewportI
 
   public boolean isHiddenRepSequence(SequenceI seq)
   {
-    return hiddenRepSequences != null
-            && hiddenRepSequences.containsKey(seq);
+    return alignment.getSeqrep()==seq || (hiddenRepSequences != null
+            && hiddenRepSequences.containsKey(seq));
   }
 
   public SequenceGroup getRepresentedSequences(SequenceI seq)
@@ -1019,32 +1050,21 @@ public abstract class AlignmentViewport implements AlignViewportI
             : hiddenRepSequences.get(seq));
   }
 
+  @Override
   public int adjustForHiddenSeqs(int alignmentIndex)
   {
     return alignment.getHiddenSequences().adjustForHiddenSeqs(
             alignmentIndex);
   }
 
-  // Selection manipulation
-  /**
-   * broadcast selection to any interested parties
-   */
-  public abstract void sendSelection();
-
+  @Override
   public void invertColumnSelection()
   {
     colSel.invertColumnSelection(0, alignment.getWidth());
   }
 
-  /**
-   * This method returns an array of new SequenceI objects derived from the
-   * whole alignment or just the current selection with start and end points
-   * adjusted
-   * 
-   * @note if you need references to the actual SequenceI objects in the
-   *       alignment or currently selected then use getSequenceSelection()
-   * @return selection as new sequenceI objects
-   */
+
+  @Override
   public SequenceI[] getSelectionAsNewSequence()
   {
     SequenceI[] sequences;
@@ -1073,12 +1093,7 @@ public abstract class AlignmentViewport implements AlignViewportI
     return sequences;
   }
 
-  /**
-   * get the currently selected sequence objects or all the sequences in the
-   * alignment.
-   * 
-   * @return array of references to sequence objects
-   */
+
   @Override
   public SequenceI[] getSequenceSelection()
   {
@@ -1094,31 +1109,16 @@ public abstract class AlignmentViewport implements AlignViewportI
     return sequences;
   }
 
-  /**
-   * This method returns the visible alignment as text, as seen on the GUI, ie
-   * if columns are hidden they will not be returned in the result. Use this for
-   * calculating trees, PCA, redundancy etc on views which contain hidden
-   * columns.
-   * 
-   * @return String[]
-   */
+
   @Override
-  public jalview.datamodel.CigarArray getViewAsCigars(
+  public CigarArray getViewAsCigars(
           boolean selectedRegionOnly)
   {
-    return new jalview.datamodel.CigarArray(alignment,
-            (hasHiddenColumns ? colSel : null),
+    return new CigarArray(alignment, colSel,
             (selectedRegionOnly ? selectionGroup : null));
   }
 
-  /**
-   * 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
-   * @return AlignmentView
-   */
+
   @Override
   public jalview.datamodel.AlignmentView getAlignmentView(
           boolean selectedOnly)
@@ -1126,34 +1126,17 @@ public abstract class AlignmentViewport implements AlignViewportI
     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
-   */
+
   @Override
   public jalview.datamodel.AlignmentView getAlignmentView(
           boolean selectedOnly, boolean markGroups)
   {
     return new AlignmentView(alignment, colSel, selectionGroup,
-            hasHiddenColumns, selectedOnly, markGroups);
+            colSel != null && colSel.hasHiddenColumns(), selectedOnly,
+            markGroups);
   }
 
-  /**
-   * This method returns the visible alignment as text, as seen on the GUI, ie
-   * if columns are hidden they will not be returned in the result. Use this for
-   * calculating trees, PCA, redundancy etc on views which contain hidden
-   * columns.
-   * 
-   * @return String[]
-   */
+
   @Override
   public String[] getViewAsString(boolean selectedRegionOnly)
   {
@@ -1176,7 +1159,7 @@ public abstract class AlignmentViewport implements AlignViewportI
     }
 
     selection = new String[iSize];
-    if (hasHiddenColumns)
+    if (colSel != null && colSel.hasHiddenColumns())
     {
       selection = colSel.getVisibleSequenceStrings(start, end, seqs);
     }
@@ -1191,15 +1174,8 @@ public abstract class AlignmentViewport implements AlignViewportI
     return selection;
   }
 
-  /**
-   * return visible region boundaries within given column range
-   * 
-   * @param min
-   *          first column (inclusive, from 0)
-   * @param max
-   *          last column (exclusive)
-   * @return int[][] range of {start,end} visible positions
-   */
+
+  @Override
   public int[][] getVisibleRegionBoundaries(int min, int max)
   {
     Vector regions = new Vector();
@@ -1208,7 +1184,7 @@ public abstract class AlignmentViewport implements AlignViewportI
 
     do
     {
-      if (hasHiddenColumns)
+      if (colSel != null && colSel.hasHiddenColumns())
       {
         if (start == 0)
         {
@@ -1229,7 +1205,7 @@ public abstract class AlignmentViewport implements AlignViewportI
       regions.addElement(new int[]
       { start, end });
 
-      if (hasHiddenColumns)
+      if (colSel != null && colSel.hasHiddenColumns())
       {
         start = colSel.adjustForHiddenColumns(end);
         start = colSel.getHiddenBoundaryLeft(start) + 1;
@@ -1266,18 +1242,15 @@ public abstract class AlignmentViewport implements AlignViewportI
     return ala;
   }
 
-  /**
-   * @return the padGaps
-   */
+
+  @Override
   public boolean isPadGaps()
   {
     return padGaps;
   }
 
-  /**
-   * @param padGaps
-   *          the padGaps to set
-   */
+
+  @Override
   public void setPadGaps(boolean padGaps)
   {
     this.padGaps = padGaps;
@@ -1289,6 +1262,7 @@ public abstract class AlignmentViewport implements AlignViewportI
    * 
    * @param ap
    */
+  @Override
   public void alignmentChanged(AlignmentViewPanel ap)
   {
     if (isPadGaps())
@@ -1450,6 +1424,7 @@ public abstract class AlignmentViewport implements AlignViewportI
    * 
    * @see jalview.api.AlignViewportI#calcPanelHeight()
    */
+  @Override
   public int calcPanelHeight()
   {
     // setHeight of panels
@@ -1581,30 +1556,49 @@ public abstract class AlignmentViewport implements AlignViewportI
     }
     oldrfs.clear();
   }
+  /**
+   * show the reference sequence in the alignment view
+   */
+  private boolean displayReferenceSeq=false;
+  /**
+   * colour according to the reference sequence defined on the alignment
+   */
+  private boolean colourByReferenceSeq=false;
+
+  @Override
+  public boolean isDisplayReferenceSeq()
+  {
+    return alignment.hasSeqrep() && displayReferenceSeq;
+  }
+
+  @Override
+  public void setDisplayReferenceSeq(boolean displayReferenceSeq)
+  {
+    this.displayReferenceSeq = displayReferenceSeq;
+  }
+
+  @Override
+  public boolean isColourByReferenceSeq()
+  {
+    return alignment.hasSeqrep() && colourByReferenceSeq;
+  }
+
+  @Override
+  public void setColourByReferenceSeq(boolean colourByReferenceSeq)
+  {
+    this.colourByReferenceSeq = colourByReferenceSeq;
+  }
 
   @Override
   public Color getSequenceColour(SequenceI seq)
   {
-    Color sqc = Color.white;
-    if (sequenceColours != null)
-    {
-      sqc = (Color) sequenceColours.get(seq);
-      if (sqc == null)
-      {
-        sqc = Color.white;
-      }
-    }
-    return sqc;
+    Color sqc = sequenceColours.get(seq);
+    return (sqc == null ? Color.white : sqc);
   }
 
   @Override
   public void setSequenceColour(SequenceI seq, Color col)
   {
-    if (sequenceColours == null)
-    {
-      sequenceColours = new Hashtable();
-    }
-
     if (col == null)
     {
       sequenceColours.remove(seq);
@@ -1618,10 +1612,6 @@ public abstract class AlignmentViewport implements AlignViewportI
   @Override
   public void updateSequenceIdColours()
   {
-    if (sequenceColours == null)
-    {
-      sequenceColours = new Hashtable();
-    }
     for (SequenceGroup sg : alignment.getGroups())
     {
       if (sg.idColour != null)
@@ -1637,9 +1627,43 @@ public abstract class AlignmentViewport implements AlignViewportI
   @Override
   public void clearSequenceColours()
   {
-    sequenceColours = null;
+    sequenceColours.clear();
   };
 
+  @Override
+  public AlignViewportI getCodingComplement()
+  {
+    return this.codingComplement;
+  }
+
+  /**
+   * Set this as the (cDna/protein) complement of the given viewport. Also
+   * ensures the reverse relationship is set on the given viewport.
+   */
+  @Override
+  public void setCodingComplement(AlignViewportI av)
+  {
+    if (this == av)
+    {
+      System.err.println("Ignoring recursive setCodingComplement request");
+    }
+    else
+    {
+      this.codingComplement = av;
+      // avoid infinite recursion!
+      if (av.getCodingComplement() != this)
+      {
+        av.setCodingComplement(this);
+      }
+    }
+  }
+
+  @Override
+  public boolean isNucleotide()
+  {
+    return getAlignment() == null ? false : getAlignment().isNucleotide();
+  }
+
   FeaturesDisplayedI featuresDisplayed = null;
 
   @Override
@@ -1648,11 +1672,13 @@ public abstract class AlignmentViewport implements AlignViewportI
     return featuresDisplayed;
   }
 
+  @Override
   public void setFeaturesDisplayed(FeaturesDisplayedI featuresDisplayedI)
   {
     featuresDisplayed = featuresDisplayedI;
   }
 
+  @Override
   public boolean areFeaturesDisplayed()
   {
     return featuresDisplayed != null && featuresDisplayed.getRegisterdFeaturesCount()>0;
@@ -1682,15 +1708,44 @@ public abstract class AlignmentViewport implements AlignViewportI
 
   boolean showSeqFeaturesHeight;
 
+  @Override
   public void setShowSequenceFeaturesHeight(boolean selected)
   {
     showSeqFeaturesHeight = selected;
   }
 
+  @Override
   public boolean isShowSequenceFeaturesHeight()
   {
     return showSeqFeaturesHeight;
   }
 
+  private boolean showAnnotation = true;
+
+  private boolean rightAlignIds = false;
+
+
+  @Override
+  public void setShowAnnotation(boolean b)
+  {
+    showAnnotation = b;
+  }
+
+  @Override
+  public boolean isShowAnnotation()
+  {
+    return showAnnotation;
+  }
+
+  @Override
+  public boolean isRightAlignIds()
+  {
+    return rightAlignIds;
+  }
 
+  @Override
+  public void setRightAlignIds(boolean rightAlignIds)
+  {
+    this.rightAlignIds = rightAlignIds;
+  }
 }