JAL-2629 revising hmmer annotation updating (wip)
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index b260cab..ecca699 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;
@@ -36,6 +37,8 @@ import jalview.datamodel.Annotation;
 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;
@@ -56,6 +59,7 @@ import jalview.viewmodel.styles.ViewStyle;
 import jalview.workers.AlignCalcManager;
 import jalview.workers.ComplementConsensusThread;
 import jalview.workers.ConsensusThread;
+import jalview.workers.InformationThread;
 import jalview.workers.StrucConsensusThread;
 
 import java.awt.Color;
@@ -79,7 +83,7 @@ import java.util.Map;
 public abstract class AlignmentViewport
         implements AlignViewportI, CommandListener, VamsasSource
 {
-  final protected ViewportRanges ranges;
+  protected ViewportRanges ranges;
 
   protected ViewStyleI viewStyle = new ViewStyle();
 
@@ -95,10 +99,73 @@ public abstract class AlignmentViewport
 
   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;
+
+  public boolean autoCalculateInformation = 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;
+
+  protected List<AlignmentAnnotation> information = new ArrayList<>();
+
+  /**
+   * alignment displayed in the viewport
+   */
+  private AlignmentI alignment;
+
+  /**
+   * results of alignment consensus analysis for visible portion of view
+   */
+  protected ProfilesI hconsensus = null;
+
   /**
-   * alignment displayed in the viewport. Please use get/setter
+   * results of information annotation analysis for the visible portion of view
    */
-  protected AlignmentI alignment;
+  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;
 
   public AlignmentViewport(AlignmentI al)
   {
@@ -579,14 +646,6 @@ public abstract class AlignmentViewport
     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;
@@ -597,18 +656,6 @@ public abstract class AlignmentViewport
     return isDataset;
   }
 
-  private Map<SequenceI, SequenceCollectionI> hiddenRepSequences;
-
-  protected ColumnSelection colSel = new ColumnSelection();
-
-  public boolean autoCalculateConsensus = true;
-
-  protected boolean autoCalculateStrucConsensus = true;
-
-  protected boolean ignoreGapsInConsensusCalculation = false;
-
-  protected ResidueShaderI residueShading = new ResidueShader();
-
   @Override
   public void setGlobalColourScheme(ColourSchemeI cs)
   {
@@ -681,41 +728,6 @@ public abstract class AlignmentViewport
   {
     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;
-
-  /**
-   * results of alignment consensus analysis for visible portion of view
-   */
-  protected ProfilesI 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
-   */
-  protected Hashtable[] hStrucConsensus = null;
-
-  protected Conservation hconservation = null;
-
   @Override
   public void setConservation(Conservation cons)
   {
@@ -753,6 +765,34 @@ public abstract class AlignmentViewport
   }
 
   @Override
+  public void setSequenceInformationHashes(List<ProfilesI> info)
+  {
+    hinformation = info;
+  }
+
+  @Override
+  public void setSequenceInformationHash(ProfilesI info, int index)
+  {
+    if (hinformation.size() < index + 1)
+    {
+      return;
+    }
+    hinformation.set(index, info);
+  }
+
+  @Override
+  public List<ProfilesI> getSequenceInformationHashes()
+  {
+    return hinformation;
+  }
+
+  @Override
+  public ProfilesI getSequenceInformationHash(int index)
+  {
+    return hinformation.get(index);
+  }
+
+  @Override
   public Hashtable[] getComplementConsensusHash()
   {
     return hcomplementConsensus;
@@ -790,9 +830,15 @@ public abstract class AlignmentViewport
   }
 
   @Override
+  public List<AlignmentAnnotation> getInformationAnnotations()
+  {
+    return information;
+  }
+
+  @Override
   public AlignmentAnnotation getAlignmentGapAnnotation()
   {
-    return gapcounts;
+    return occupancy;
   }
 
   @Override
@@ -881,6 +927,20 @@ public abstract class AlignmentViewport
     }
   }
 
+  /**
+   * trigger update of information annotation
+   */
+  @Override
+  public void updateInformation(final AlignmentViewPanel ap)
+  {
+    if (calculator
+            .getRegisteredWorkersOfClass(InformationThread.class) == null)
+    {
+      calculator.registerWorker(new InformationThread(this, ap));
+    }
+
+  }
+
   // --------START Structure Conservation
   public void updateStrucConsensus(final AlignmentViewPanel ap)
   {
@@ -944,14 +1004,16 @@ public abstract class AlignmentViewport
     strucConsensus = null;
     conservation = null;
     quality = null;
-    groupConsensus = null;
-    groupConservation = null;
     hconsensus = 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);
   }
 
@@ -995,6 +1057,21 @@ public abstract class AlignmentViewport
   protected boolean showConsensusHistogram = true;
 
   /**
+   * should hmm profile be rendered by default
+   */
+  protected boolean showHMMSequenceLogo = false;
+
+  /**
+   * should hmm profile be rendered normalised to row height
+   */
+  protected boolean normaliseHMMSequenceLogo = false;
+
+  /**
+   * should information histograms be rendered by default
+   */
+  protected boolean showInformationHistogram = true;
+
+  /**
    * @return the showConsensusProfile
    */
   @Override
@@ -1004,6 +1081,15 @@ public abstract class AlignmentViewport
   }
 
   /**
+   * @return the showInformationProfile
+   */
+  @Override
+  public boolean isShowHMMSequenceLogo()
+  {
+    return showHMMSequenceLogo;
+  }
+
+  /**
    * @param showSequenceLogo
    *          the new value
    */
@@ -1021,6 +1107,16 @@ public abstract class AlignmentViewport
     this.showSequenceLogo = showSequenceLogo;
   }
 
+  public void setShowHMMSequenceLogo(boolean showHMMSequenceLogo)
+  {
+    if (showHMMSequenceLogo != this.showHMMSequenceLogo)
+    {
+      this.showHMMSequenceLogo = showHMMSequenceLogo;
+      calculator.updateAnnotationFor(InformationThread.class);
+    }
+    this.showHMMSequenceLogo = showHMMSequenceLogo;
+  }
+
   /**
    * @param showConsensusHistogram
    *          the showConsensusHistogram to set
@@ -1031,6 +1127,15 @@ public abstract class AlignmentViewport
   }
 
   /**
+   * @param showInformationHistogram
+   *          the showInformationHistogram to set
+   */
+  public void setShowInformationHistogram(boolean showInformationHistogram)
+  {
+    this.showInformationHistogram = showInformationHistogram;
+  }
+
+  /**
    * @return the showGroupConservation
    */
   public boolean isShowGroupConservation()
@@ -1076,6 +1181,17 @@ public abstract class AlignmentViewport
   }
 
   /**
+   * 
+   * @return flag to indicate if the information content histogram should be
+   *         rendered by default
+   */
+  @Override
+  public boolean isShowInformationHistogram()
+  {
+    return this.showInformationHistogram;
+  }
+
+  /**
    * when set, updateAlignment will always ensure sequences are of equal length
    */
   private boolean padGaps = false;
@@ -1234,6 +1350,26 @@ public abstract class AlignmentViewport
 
   }
 
+  public void setIgnoreBelowBackground(boolean b, AlignmentViewPanel ap)
+  {
+    ignoreBelowBackGroundFrequencyCalculation = b;
+    if (ap != null)
+    {
+      updateInformation(ap);
+    }
+
+  }
+
+  public void setInfoLetterHeight(boolean b, AlignmentViewPanel ap)
+  {
+    infoLetterHeight = b;
+    if (ap != null)
+    {
+      updateInformation(ap);
+    }
+
+  }
+
   private long sgrouphash = -1, colselhash = -1;
 
   /**
@@ -1288,6 +1424,18 @@ public abstract class AlignmentViewport
     return ignoreGapsInConsensusCalculation;
   }
 
+  @Override
+  public boolean isIgnoreBelowBackground()
+  {
+    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(
@@ -1333,7 +1481,10 @@ public abstract class AlignmentViewport
   public void removePropertyChangeListener(
           java.beans.PropertyChangeListener listener)
   {
-    changeSupport.removePropertyChangeListener(listener);
+    if (changeSupport != null)
+    {
+      changeSupport.removePropertyChangeListener(listener);
+    }
   }
 
   /**
@@ -1852,6 +2003,16 @@ public abstract class AlignmentViewport
     {
       updateStrucConsensus(ap);
     }
+    initInformation();
+    updateInformation(ap);
+
+    List<SequenceI> hmmSequences;
+    hmmSequences = alignment.getHMMConsensusSequences();
+
+    for (SequenceI seq : hmmSequences)
+    {
+      seq.updateHMMMapping();
+    }
 
     // Reset endRes of groups if beyond alignment width
     int alWidth = alignment.getWidth();
@@ -1927,7 +2088,8 @@ public abstract class AlignmentViewport
               MessageManager.getString("label.consensus_descr"),
               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
       initConsensus(consensus);
-      initGapCounts();
+
+      initOccupancy();
 
       initComplementConsensus();
     }
@@ -1984,22 +2146,46 @@ public abstract class AlignmentViewport
     }
   }
 
+  @Override
+  public void initInformation()
+  {
+    for (SequenceI seq : alignment.getHMMConsensusSequences())
+    {
+      if (!seq.hasHMMAnnotation())
+      {
+        AlignmentAnnotation info = new AlignmentAnnotation(seq.getName(),
+                MessageManager.getString("label.information_description"),
+                new Annotation[1], 0f, 6.52f,
+                AlignmentAnnotation.BAR_GRAPH);
+        info.hasText = true;
+        info.autoCalculated = false;
+        info.sequenceRef = seq;
+        info.setCalcId(InformationThread.HMM_CALC_ID);
+        this.information.add(info);
+        hinformation.add(new Profiles(new ProfileI[1]));
+        alignment.addAnnotation(info);
+        seq.updateHMMMapping();
+        seq.addAlignmentAnnotation(info);
+      }
+    }
+  }
+
   // 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;
+      occupancy.hasText = true;
+      occupancy.autoCalculated = true;
+      occupancy.scaleColLabel = true;
+      occupancy.graph = AlignmentAnnotation.BAR_GRAPH;
 
-      alignment.addAnnotation(gapcounts);
+      alignment.addAnnotation(occupancy);
     }
   }
 
@@ -2133,6 +2319,9 @@ public abstract class AlignmentViewport
     boolean showprf = isShowSequenceLogo();
     boolean showConsHist = isShowConsensusHistogram();
     boolean normLogo = isNormaliseSequenceLogo();
+    boolean showHMMPrf = isShowHMMSequenceLogo();
+    boolean showInfoHist = isShowInformationHistogram();
+    boolean normHMMLogo = isNormaliseHMMSequenceLogo();
 
     /**
      * TODO reorder the annotation rows according to group/sequence ordering on
@@ -2170,6 +2359,9 @@ public abstract class AlignmentViewport
           sg.setshowSequenceLogo(showprf);
           sg.setShowConsensusHistogram(showConsHist);
           sg.setNormaliseSequenceLogo(normLogo);
+          sg.setshowHMMSequenceLogo(showHMMPrf);
+          sg.setShowInformationHistogram(showInfoHist);
+          sg.setNormaliseHMMSequenceLogo(normHMMLogo);
         }
         if (conv)
         {
@@ -2869,6 +3061,8 @@ public abstract class AlignmentViewport
    */
   private SearchResultsI searchResults = null;
 
+  protected TreeModel currentTree = null;
+
   @Override
   public boolean hasSearchResults()
   {
@@ -2927,4 +3121,29 @@ public abstract class AlignmentViewport
             + ((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;
+  }
 }