Merge branch 'develop' into features/hmmer
[jalview.git] / src / jalview / viewmodel / AlignmentViewport.java
index 5e88c87..0ec07c0 100644 (file)
@@ -37,6 +37,8 @@ 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;
@@ -57,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;
@@ -77,8 +80,8 @@ 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;
 
@@ -96,6 +99,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   protected Deque<CommandI> redoList = new ArrayDeque<>();
 
+
   /**
    * alignment displayed in the viewport. Please use get/setter
    */
@@ -568,8 +572,6 @@ public abstract class AlignmentViewport implements AlignViewportI,
     viewStyle.setSeqNameItalics(default1);
   }
 
-
-
   @Override
   public AlignmentI getAlignment()
   {
@@ -606,10 +608,14 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   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
@@ -643,6 +649,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);
     }
 
@@ -661,8 +672,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
         sg.setColourScheme(cs);
         if (cs != null)
         {
-          sg.getGroupColourScheme()
-                  .alignmentChanged(sg, hiddenRepSequences);
+          sg.getGroupColourScheme().alignmentChanged(sg,
+                  hiddenRepSequences);
         }
       }
     }
@@ -671,8 +682,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public ColourSchemeI getGlobalColourScheme()
   {
-    return residueShading == null ? null : residueShading
-            .getColourScheme();
+    return residueShading == null ? null : residueShading.getColourScheme();
   }
 
   @Override
@@ -697,12 +707,21 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   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;
 
   /**
+   * results of information annotation analysis for the visible portion of view
+   */
+  protected List<ProfilesI> hinformation = new ArrayList<>();
+
+  /**
    * results of cDNA complement consensus visible portion of view
    */
   protected Hashtable[] hcomplementConsensus = null;
@@ -752,6 +771,30 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
+  public void setSequenceInformationHashes(List<ProfilesI> info)
+  {
+    hinformation = info;
+  }
+
+  @Override
+  public void setSequenceInformationHash(ProfilesI info, int index)
+  {
+    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;
@@ -789,6 +832,18 @@ 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()
   {
     return gapcounts;
@@ -820,11 +875,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));
     }
   }
 
@@ -838,7 +893,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));
     }
@@ -869,8 +925,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));
@@ -879,6 +935,20 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
   }
 
+  /**
+   * 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)
   {
@@ -894,7 +964,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));
     }
@@ -992,6 +1063,21 @@ public abstract class AlignmentViewport implements AlignViewportI,
   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
@@ -1001,6 +1087,15 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   /**
+   * @return the showInformationProfile
+   */
+  @Override
+  public boolean isShowHMMSequenceLogo()
+  {
+    return showHMMSequenceLogo;
+  }
+
+  /**
    * @param showSequenceLogo
    *          the new value
    */
@@ -1018,6 +1113,16 @@ public abstract class AlignmentViewport implements AlignViewportI,
     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
@@ -1028,6 +1133,15 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   /**
+   * @param showInformationHistogram
+   *          the showInformationHistogram to set
+   */
+  public void setShowInformationHistogram(boolean showInformationHistogram)
+  {
+    this.showInformationHistogram = showInformationHistogram;
+  }
+
+  /**
    * @return the showGroupConservation
    */
   public boolean isShowGroupConservation()
@@ -1073,6 +1187,17 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   /**
+   * 
+   * @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;
@@ -1183,8 +1308,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);
   }
@@ -1231,6 +1356,16 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   }
 
+  public void setIgnoreBelowBackground(boolean b, AlignmentViewPanel ap)
+  {
+    ignoreBelowBackGroundFrequencyCalculation = b;
+    if (ap != null)
+    {
+      updateInformation(ap);
+    }
+
+  }
+
   private long sgrouphash = -1, colselhash = -1;
 
   /**
@@ -1285,6 +1420,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return ignoreGapsInConsensusCalculation;
   }
 
+  @Override
+  public boolean isIgnoreBelowBackground()
+  {
+    return ignoreBelowBackGroundFrequencyCalculation;
+  }
+
   // property change stuff
   // JBPNote Prolly only need this in the applet version.
   private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
@@ -1392,6 +1533,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void showAllHiddenSeqs()
   {
     int startSeq = ranges.getStartSeq();
+    int endSeq = ranges.getEndSeq();
 
     if (alignment.getHiddenSequences().getSize() > 0)
     {
@@ -1400,8 +1542,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);
@@ -1410,7 +1552,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
       hiddenRepSequences = null;
 
-      ranges.setStartSeq(startSeq);
+      ranges.setStartEndSeq(startSeq, endSeq + tmp.size());
 
       firePropertyChange("alignment", null, alignment.getSequences());
       // used to set hasHiddenRows/hiddenRepSequences here, after the property
@@ -1422,9 +1564,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void showSequence(int index)
   {
     int startSeq = ranges.getStartSeq();
+    int endSeq = ranges.getEndSeq();
 
-    List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(
-            index, hiddenRepSequences);
+    List<SequenceI> tmp = alignment.getHiddenSequences().showSequence(index,
+            hiddenRepSequences);
     if (tmp.size() > 0)
     {
       if (selectionGroup == null)
@@ -1438,7 +1581,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
         selectionGroup.addSequence(seq, false);
         setSequenceAnnotationsVisible(seq, true);
       }
-      ranges.setStartSeq(startSeq);
+
+      ranges.setStartEndSeq(startSeq, endSeq + tmp.size());
+
       firePropertyChange("alignment", null, alignment.getSequences());
       sendSelection();
     }
@@ -1502,8 +1647,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);
@@ -1593,8 +1738,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
    */
   public boolean isHiddenRepSequence(SequenceI seq)
   {
-    return (hiddenRepSequences != null && hiddenRepSequences
-            .containsKey(seq));
+    return (hiddenRepSequences != null
+            && hiddenRepSequences.containsKey(seq));
   }
 
   /**
@@ -1612,8 +1757,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
   @Override
   public int adjustForHiddenSeqs(int alignmentIndex)
   {
-    return alignment.getHiddenSequences().adjustForHiddenSeqs(
-            alignmentIndex);
+    return alignment.getHiddenSequences()
+            .adjustForHiddenSeqs(alignmentIndex);
   }
 
   @Override
@@ -1683,10 +1828,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
@@ -1732,8 +1877,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     if (alignment.getHiddenColumns() != null
             && alignment.getHiddenColumns().hasHiddenColumns())
     {
-      selection = alignment.getHiddenColumns().getVisibleSequenceStrings(
-              start, end, seqs);
+      selection = alignment.getHiddenColumns()
+              .getVisibleSequenceStrings(start, end, seqs);
     }
     else
     {
@@ -1802,8 +1947,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
         if (selectedOnly && selectionGroup != null)
         {
           alignment.getHiddenColumns().makeVisibleAnnotation(
-                  selectionGroup.getStartRes(),
-                  selectionGroup.getEndRes(), clone);
+                  selectionGroup.getStartRes(), selectionGroup.getEndRes(),
+                  clone);
         }
         else
         {
@@ -1852,6 +1997,15 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       updateStrucConsensus(ap);
     }
+    updateInformation(ap);
+
+    List<SequenceI> hmmSequences;
+    hmmSequences = alignment.getHMMConsensusSequences(false);
+
+    for (SequenceI seq : hmmSequences)
+    {
+      seq.updateHMMMapping();
+    }
 
     // Reset endRes of groups if beyond alignment width
     int alWidth = alignment.getWidth();
@@ -1927,6 +2081,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
               MessageManager.getString("label.consensus_descr"),
               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
       initConsensus(consensus);
+
       initGapCounts();
 
       initComplementConsensus();
@@ -1984,6 +2139,35 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
   }
 
+  @Override
+  public void initInformation()
+  {
+    for (SequenceI seq : alignment.getHMMConsensusSequences(false))
+    {
+      if (!seq.hasHMMAnnotation())
+      {
+        AlignmentAnnotation information;
+        information = new AlignmentAnnotation(seq.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 = seq;
+        information.setCalcId("HMM annotation");
+        this.information.add(information);
+        hinformation.add(new Profiles(new ProfileI[1]));
+        alignment.addAnnotation(information);
+        seq.updateHMMMapping();
+        seq.setHasInfo(true);
+        seq.addAlignmentAnnotation(information);
+      }
+    }
+
+  }
+
   // these should be extracted from the view model - style and settings for
   // derived annotation
   private void initGapCounts()
@@ -1992,8 +2176,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     {
       gapcounts = new AlignmentAnnotation("Occupancy",
               MessageManager.getString("label.occupancy_descr"),
-              new Annotation[1], 0f,
-              alignment.getHeight(), AlignmentAnnotation.BAR_GRAPH);
+              new Annotation[1], 0f, alignment.getHeight(),
+              AlignmentAnnotation.BAR_GRAPH);
       gapcounts.hasText = true;
       gapcounts.autoCalculated = true;
       gapcounts.scaleColLabel = true;
@@ -2011,8 +2195,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);
@@ -2133,6 +2317,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
     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 +2357,9 @@ public abstract class AlignmentViewport implements AlignViewportI,
           sg.setshowSequenceLogo(showprf);
           sg.setShowConsensusHistogram(showConsHist);
           sg.setNormaliseSequenceLogo(normLogo);
+          sg.setshowHMMSequenceLogo(showHMMPrf);
+          sg.setShowInformationHistogram(showInfoHist);
+          sg.setNormaliseHMMSequenceLogo(normHMMLogo);
         }
         if (conv)
         {
@@ -2492,8 +2682,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     viewStyle = new ViewStyle(settingsForView);
     if (residueShading != null)
     {
-      residueShading.setConservationApplied(settingsForView
-              .isConservationColourSelected());
+      residueShading.setConservationApplied(
+              settingsForView.isConservationColourSelected());
     }
   }
 
@@ -2661,7 +2851,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
     return sortAnnotationsBy;
   }
 
-  public void setSortAnnotationsBy(SequenceAnnotationOrder sortAnnotationsBy)
+  public void setSortAnnotationsBy(
+          SequenceAnnotationOrder sortAnnotationsBy)
   {
     this.sortAnnotationsBy = sortAnnotationsBy;
   }
@@ -2739,8 +2930,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;
@@ -2771,7 +2962,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))
@@ -2782,9 +2974,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;
@@ -2919,7 +3111,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
         }
       }
     }
-  
+
     SequenceI sq = new Sequence("Consensus", seqs.toString());
     sq.setDescription("Percentage Identity Consensus "
             + ((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));