Merge branch 'features/sequenceFeatureRefactor' into develop
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index 67db6fc..2cfc1e6 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -26,6 +26,8 @@ import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeaturesDisplayedI;
 import jalview.api.ViewStyleI;
+import jalview.commands.CommandI;
+import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
@@ -40,18 +42,25 @@ import jalview.schemes.Blosum62ColourScheme;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.PIDColourScheme;
 import jalview.schemes.ResidueProperties;
+import jalview.structure.CommandListener;
+import jalview.structure.StructureSelectionManager;
+import jalview.structure.VamsasSource;
 import jalview.viewmodel.styles.ViewStyle;
 import jalview.workers.AlignCalcManager;
+import jalview.workers.ComplementConsensusThread;
 import jalview.workers.ConsensusThread;
 import jalview.workers.StrucConsensusThread;
 
 import java.awt.Color;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * base class holding visualization and analysis attributes and common logic for
@@ -61,7 +70,7 @@ import java.util.Map;
  * 
  */
 public abstract class AlignmentViewport implements AlignViewportI,
-        ViewStyleI
+        ViewStyleI, CommandListener, VamsasSource
 {
   protected ViewStyleI viewStyle = new ViewStyle();
 
@@ -71,6 +80,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
    */
   AlignViewportI codingComplement = null;
 
+  FeaturesDisplayedI featuresDisplayed = null;
+
+  protected Deque<CommandI> historyList = new ArrayDeque<CommandI>();
+
+  protected Deque<CommandI> redoList = new ArrayDeque<CommandI>();
+
   /**
    * @param name
    * @see jalview.api.ViewStyleI#setFontName(java.lang.String)
@@ -630,6 +645,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   protected AlignmentAnnotation consensus;
 
+  protected AlignmentAnnotation complementConsensus;
+
   protected AlignmentAnnotation strucConsensus;
 
   protected AlignmentAnnotation conservation;
@@ -646,6 +663,11 @@ public abstract class AlignmentViewport implements AlignViewportI,
   protected Hashtable[] hconsensus = null;
 
   /**
+   * 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
    */
@@ -675,7 +697,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void setSequenceConsensusHash(Hashtable[] hconsensus)
   {
     this.hconsensus = hconsensus;
+  }
 
+  @Override
+  public void setComplementConsensusHash(Hashtable[] hconsensus)
+  {
+    this.hcomplementConsensus = hconsensus;
   }
 
   @Override
@@ -685,6 +712,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
+  public Hashtable[] getComplementConsensusHash()
+  {
+    return hcomplementConsensus;
+  }
+
+  @Override
   public Hashtable[] getRnaStructureConsensusHash()
   {
     return hStrucConsensus;
@@ -716,6 +749,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
+  public AlignmentAnnotation getComplementConsensusAnnotation()
+  {
+    return complementConsensus;
+  }
+
+  @Override
   public AlignmentAnnotation getAlignmentStrucConsensusAnnotation()
   {
     return strucConsensus;
@@ -756,6 +795,20 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       calculator.registerWorker(new ConsensusThread(this, ap));
     }
+
+    /*
+     * A separate thread to compute cDNA consensus for a protein alignment
+     */
+    final AlignmentI al = this.getAlignment();
+    if (!al.isNucleotide() && al.getCodonFrames() != null
+            && !al.getCodonFrames().isEmpty())
+    {
+      if (calculator
+              .getRegisteredWorkersOfClass(ComplementConsensusThread.class) == null)
+      {
+        calculator.registerWorker(new ComplementConsensusThread(this, ap));
+      }
+    }
   }
 
   // --------START Structure Conservation
@@ -860,6 +913,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
       // annotation update method from alignframe to viewport
       this.showSequenceLogo = showSequenceLogo;
       calculator.updateAnnotationFor(ConsensusThread.class);
+      calculator.updateAnnotationFor(ComplementConsensusThread.class);
       calculator.updateAnnotationFor(StrucConsensusThread.class);
     }
     this.showSequenceLogo = showSequenceLogo;
@@ -1042,6 +1096,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
    */
   protected String viewId = null;
 
+  @Override
   public String getViewId()
   {
     if (viewId == null)
@@ -1391,10 +1446,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
       AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
       for (int i = 0; i < sequences.length; i++)
       {
-        sequences[i] = new Sequence(sequences[i], annots); // construct new
-        // sequence with
-        // subset of visible
-        // annotation
+        // construct new sequence with subset of visible annotation
+        sequences[i] = new Sequence(sequences[i], annots);
       }
     }
     else
@@ -1661,21 +1714,42 @@ public abstract class AlignmentViewport implements AlignViewportI,
       {
         initRNAStructure();
       }
-      initConsensus();
+      consensus = new AlignmentAnnotation("Consensus", "PID",
+              new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
+      initConsensus(consensus);
+
+      initComplementConsensus();
     }
   }
 
-  private void initConsensus()
+  /**
+   * If this is a protein alignment and there are mappings to cDNA, add the cDNA
+   * consensus annotation.
+   */
+  protected void initComplementConsensus()
   {
+    if (!alignment.isNucleotide())
+    {
+      final Set<AlignedCodonFrame> codonMappings = alignment
+              .getCodonFrames();
+      if (codonMappings != null && !codonMappings.isEmpty())
+      {
+        complementConsensus = new AlignmentAnnotation("cDNA Consensus",
+                "PID for cDNA", new Annotation[1], 0f, 100f,
+                AlignmentAnnotation.BAR_GRAPH);
+        initConsensus(complementConsensus);
+      }
+    }
+  }
 
-    consensus = new AlignmentAnnotation("Consensus", "PID",
-            new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
-    consensus.hasText = true;
-    consensus.autoCalculated = true;
+  private void initConsensus(AlignmentAnnotation aa)
+  {
+    aa.hasText = true;
+    aa.autoCalculated = true;
 
     if (showConsensus)
     {
-      alignment.addAnnotation(consensus);
+      alignment.addAnnotation(aa);
     }
   }
 
@@ -1737,57 +1811,57 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public int calcPanelHeight()
   {
     // setHeight of panels
-    AlignmentAnnotation[] aa = getAlignment().getAlignmentAnnotation();
+    AlignmentAnnotation[] anns = getAlignment().getAlignmentAnnotation();
     int height = 0;
     int charHeight = getCharHeight();
-    if (aa != null)
+    if (anns != null)
     {
       BitSet graphgrp = new BitSet();
-      for (int i = 0; i < aa.length; i++)
+      for (AlignmentAnnotation aa : anns)
       {
-        if (aa[i] == null)
+        if (aa == null)
         {
           System.err.println("Null annotation row: ignoring.");
           continue;
         }
-        if (!aa[i].visible)
+        if (!aa.visible)
         {
           continue;
         }
-        if (aa[i].graphGroup > -1)
+        if (aa.graphGroup > -1)
         {
-          if (graphgrp.get(aa[i].graphGroup))
+          if (graphgrp.get(aa.graphGroup))
           {
             continue;
           }
           else
           {
-            graphgrp.set(aa[i].graphGroup);
+            graphgrp.set(aa.graphGroup);
           }
         }
-        aa[i].height = 0;
+        aa.height = 0;
 
-        if (aa[i].hasText)
+        if (aa.hasText)
         {
-          aa[i].height += charHeight;
+          aa.height += charHeight;
         }
 
-        if (aa[i].hasIcons)
+        if (aa.hasIcons)
         {
-          aa[i].height += 16;
+          aa.height += 16;
         }
 
-        if (aa[i].graph > 0)
+        if (aa.graph > 0)
         {
-          aa[i].height += aa[i].graphHeight;
+          aa.height += aa.graphHeight;
         }
 
-        if (aa[i].height == 0)
+        if (aa.height == 0)
         {
-          aa[i].height = 20;
+          aa.height = 20;
         }
 
-        height += aa[i].height;
+        height += aa.height;
       }
     }
     if (height == 0)
@@ -1958,8 +2032,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return getAlignment() == null ? false : getAlignment().isNucleotide();
   }
 
-  FeaturesDisplayedI featuresDisplayed = null;
-
   @Override
   public FeaturesDisplayedI getFeaturesDisplayed()
   {
@@ -2242,4 +2314,76 @@ public abstract class AlignmentViewport implements AlignViewportI,
   {
     viewStyle.setShowNPFeats(shownpfeats);
   }
+
+  public abstract StructureSelectionManager getStructureSelectionManager();
+
+  /**
+   * Add one command to the command history list.
+   * 
+   * @param command
+   */
+  public void addToHistoryList(CommandI command)
+  {
+    if (this.historyList != null)
+    {
+      this.historyList.push(command);
+      broadcastCommand(command, false);
+    }
+  }
+
+  protected void broadcastCommand(CommandI command, boolean undo)
+  {
+    getStructureSelectionManager().commandPerformed(command, undo, getVamsasSource());
+  }
+
+  /**
+   * Add one command to the command redo list.
+   * 
+   * @param command
+   */
+  public void addToRedoList(CommandI command)
+  {
+    if (this.redoList != null)
+    {
+      this.redoList.push(command);
+    }
+    broadcastCommand(command, true);
+  }
+
+  /**
+   * Clear the command redo list.
+   */
+  public void clearRedoList()
+  {
+    if (this.redoList != null)
+    {
+      this.redoList.clear();
+    }
+  }
+
+  public void setHistoryList(Deque<CommandI> list)
+  {
+    this.historyList = list;
+  }
+
+  public Deque<CommandI> getHistoryList()
+  {
+    return this.historyList;
+  }
+
+  public void setRedoList(Deque<CommandI> list)
+  {
+    this.redoList = list;
+  }
+
+  public Deque<CommandI> getRedoList()
+  {
+    return this.redoList;
+  }
+
+  @Override
+  public VamsasSource getVamsasSource()
+  {
+    return this;
+  }
 }