JAL-1601 Expand secondary structure prediction tests
[jalview.git] / src / jalview / workers / InformationThread.java
index 6a86a61..c8084b9 100644 (file)
@@ -10,9 +10,9 @@ import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.ProfilesI;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
-import jalview.renderer.ResidueShaderI;
 import jalview.util.MessageManager;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -25,8 +25,6 @@ public class InformationThread extends AlignCalcWorker
 {
   public static final String HMM_CALC_ID = "HMM";
 
-  private float max = 0f;
-
   /**
    * Constructor
    * 
@@ -46,92 +44,48 @@ public class InformationThread extends AlignCalcWorker
   @Override
   public void run()
   {
-    if (calcMan.isPending(this))
+    if (alignViewport.getAlignment().getHmmSequences().isEmpty())
     {
       return;
     }
-    calcMan.notifyStart(this);
-    // long started = System.currentTimeMillis();
-
-    try
+    if (alignViewport.isClosed())
     {
-      if (calcMan.isPending(this))
-      {
-        // another instance of this is waiting to run
-        calcMan.workerComplete(this);
-        return;
-      }
-      while (!calcMan.notifyWorking(this))
-      {
-        // another thread in progress, wait my turn
-        try
-        {
-          if (ap != null)
-          {
-            ap.paintAlignment(false, false);
-          }
-          Thread.sleep(200);
-        } catch (Exception ex)
-        {
-          ex.printStackTrace();
-        }
-      }
-      if (alignViewport.isClosed())
-      {
-        abortAndDestroy();
-        return;
-      }
-      AlignmentI alignment = alignViewport.getAlignment();
-
-      int aWidth = alignment == null ? -1 : alignment.getWidth();
-
-      if (aWidth < 0)
-      {
-        calcMan.workerComplete(this);
-        return;
-      }
-
-      eraseAnnotations(alignment);
-      computeProfiles(alignment);
-      updateResultAnnotation(true);
+      abortAndDestroy();
+      return;
+    }
 
-      if (ap != null)
-      {
-        ap.adjustAnnotationHeight();
-        ap.paintAlignment(true, true);
-      }
-    } catch (OutOfMemoryError error)
-    {
-      calcMan.disableWorker(this);
-      ap.raiseOOMWarning("calculating information", error);
-    } finally
+    AlignmentI alignment = alignViewport.getAlignment();
+    int aWidth = alignment == null ? -1 : alignment.getWidth();
+    if (aWidth < 0)
     {
-      calcMan.workerComplete(this);
+      return;
     }
-  }
 
-  /**
-   * Deletes any existing information annotations. These are sequence-related
-   * annotations which relate to HMM consensus sequences for either the
-   * alignment or a subgroup.
-   * 
-   * @param alignment
-   */
-  protected void eraseAnnotations(AlignmentI alignment)
-  {
-    Iterable<AlignmentAnnotation> anns = alignment
-            .findAnnotation(HMM_CALC_ID);
-    for (AlignmentAnnotation ann : anns)
+    /*
+     * compute information profiles for any HMM consensus sequences
+     * for the alignment or sub-groups
+     */
+    computeProfiles(alignment);
+
+    /*
+     * construct the corresponding annotations
+     */
+    updateAnnotation();
+
+    if (ap != null)
     {
-      alignment.deleteAnnotation(ann);
+      ap.adjustAnnotationHeight();
+      ap.paintAlignment(true, true);
     }
   }
 
   /**
    * Computes HMM profiles for any HMM consensus sequences (for alignment or
-   * subgroups)
+   * subgroups). Any alignment profile computed is stored on the viewport, any
+   * group profile computed is stored on the respective sequence group.
    * 
    * @param alignment
+   * @see AlignViewportI#setHmmProfiles(ProfilesI)
    */
   protected void computeProfiles(AlignmentI alignment)
   {
@@ -140,15 +94,14 @@ public class InformationThread extends AlignCalcWorker
     /*
      * alignment HMM profile
      */
-    SequenceI seq = alignment.getHmmConsensus();
-    if (seq != null)
+    List<SequenceI> seqs = alignment.getHmmSequences();
+    if (!seqs.isEmpty())
     {
-      HiddenMarkovModel hmm = seq.getHMM();
+      HiddenMarkovModel hmm = seqs.get(0).getHMM();
       ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
-              0, width, true, alignViewport.isIgnoreBelowBackground(),
+              0, width, alignViewport.isIgnoreBelowBackground(),
               alignViewport.isInfoLetterHeight());
       alignViewport.setHmmProfiles(hmmProfiles);
-      // setColourSchemeInformation(hmmProfiles);
     }
 
     /*
@@ -157,15 +110,14 @@ public class InformationThread extends AlignCalcWorker
     List<SequenceGroup> groups = alignment.getGroups();
     for (SequenceGroup group : groups)
     {
-      seq = group.getHmmConsensus();
-      if (seq != null)
+      seqs = group.getHmmSequences();
+      if (!seqs.isEmpty())
       {
-        HiddenMarkovModel hmm = seq.getHMM();
+        HiddenMarkovModel hmm = seqs.get(0).getHMM();
         ProfilesI hmmProfiles = AAFrequency.calculateHMMProfiles(hmm, width,
-                0, width, true, group.isIgnoreBelowBackground(),
+                0, width, group.isIgnoreBelowBackground(),
                 group.isUseInfoLetterHeight());
         group.setHmmProfiles(hmmProfiles);
-        // setColourSchemeInformation(hmmProfiles);
       }
     }
   }
@@ -180,15 +132,6 @@ public class InformationThread extends AlignCalcWorker
     return alignViewport.getAlignment().getSequencesArray();
   }
 
-  protected void setColourSchemeInformation(ProfilesI information)
-  {
-    ResidueShaderI cs = alignViewport.getResidueShading();
-    if (cs != null)
-    {
-      cs.setInformation(information);
-    }
-  }
-
   /**
    * Get the Gap annotation for the alignment
    * 
@@ -196,39 +139,32 @@ public class InformationThread extends AlignCalcWorker
    */
   protected AlignmentAnnotation getGapAnnotation()
   {
-    return alignViewport.getOccupancyAnnotation();
+               return alignViewport.getAlignmentGapAnnotation();
   }
 
   /**
-   * Updates the information annotation from the sequence profile data using
-   * current visualisation settings
+   * Computes Information Content annotation for any HMM consensus sequences
+   * (for alignment or groups), and updates (or adds) the annotation to the
+   * sequence and the alignment
    */
   @Override
   public void updateAnnotation()
   {
-    updateResultAnnotation(false);
-  }
-
-  /**
-   * Constructs Information Content annotation for any HMM consensus sequences
-   * (for alignment or groups), and adds the annotation to the sequence and the
-   * alignment
-   * 
-   * @param immediate
-   */
-  public void updateResultAnnotation(boolean immediate)
-  {
     AlignmentI alignment = alignViewport.getAlignment();
 
+    float maxInformation = 0f;
+    List<AlignmentAnnotation> infos = new ArrayList<>();
+
     /*
      * annotation for alignment HMM consensus if present
      */
-    SequenceI hmmSeq = alignment.getHmmConsensus();
-    ProfilesI profile = alignViewport.getHmmProfiles();
-    AlignmentAnnotation ann = makeInformationAnnotation(hmmSeq, profile);
-    if (ann != null)
+    List<SequenceI> hmmSeqs = alignment.getHmmSequences();
+    if (!hmmSeqs.isEmpty())
     {
-      alignment.addAnnotation(ann);
+      ProfilesI profile = alignViewport.getHmmProfiles();
+      float m = updateInformationAnnotation(hmmSeqs.get(0), profile, null,
+              infos);
+      maxInformation = Math.max(maxInformation, m);
     }
 
     /*
@@ -236,67 +172,113 @@ public class InformationThread extends AlignCalcWorker
      */
     for (SequenceGroup group : alignment.getGroups())
     {
-      hmmSeq = group.getHmmConsensus();
-      ProfilesI profiles = group.getHmmProfiles();
-      ann = makeInformationAnnotation(hmmSeq, profiles);
-      if (ann != null)
+      hmmSeqs = group.getHmmSequences();
+      if (!hmmSeqs.isEmpty())
       {
-        ann.groupRef = group;
-        alignment.addAnnotation(ann);
+        ProfilesI profiles = group.getHmmProfiles();
+        float m = updateInformationAnnotation(hmmSeqs.get(0), profiles,
+                group, infos);
+        maxInformation = Math.max(maxInformation, m);
       }
     }
+
+    /*
+     * maxInformation holds the maximum value of information score;
+     * set this as graphMax in all annotations to scale them all the same
+     */
+    for (AlignmentAnnotation ann : infos)
+    {
+      ann.graphMax = maxInformation;
+    }
   }
 
   /**
-   * Constructs an HMM Profile information content annotation for a sequence
+   * Updates (and first constructs if necessary) an HMM Profile information
+   * content annotation for a sequence. The <code>group</code> argument is null
+   * for the whole alignment annotation, not null for a subgroup annotation. The
+   * updated annotation is added to the <code>infos</code> list. Answers the
+   * maximum information content value of any annotation (for use as a scaling
+   * factor for display).
    * 
    * @param seq
    * @param profile
+   * @param group
+   * @param infos
    * @return
    */
-  protected AlignmentAnnotation makeInformationAnnotation(SequenceI seq,
-          ProfilesI profile)
+  protected float updateInformationAnnotation(SequenceI seq,
+          ProfilesI profile, SequenceGroup group,
+          List<AlignmentAnnotation> infos)
   {
     if (seq == null || profile == null)
     {
-      return null;
+      return 0f;
     }
 
-    AlignmentI alignment = alignViewport.getAlignment();
-    int aWidth = alignment == null ? 0 : alignment.getWidth();
-    AlignmentAnnotation ann = new AlignmentAnnotation(seq.getName(),
-            MessageManager.getString("label.information_description"),
-            new Annotation[aWidth], 0f, 6.52f,
-            AlignmentAnnotation.BAR_GRAPH);
-    ann.hasText = true;
-    ann.autoCalculated = false;
-    ann.sequenceRef = seq;
-    ann.setCalcId(InformationThread.HMM_CALC_ID);
+    AlignmentAnnotation ann = findOrCreateAnnotation(seq, group);
+
     seq.addAlignmentAnnotation(ann);
+    infos.add(ann);
 
-    long nseq = getSequences().length;
-    max = AAFrequency.completeInformation(ann, profile,
-            profile.getStartColumn(), profile.getEndColumn() + 1, nseq,
-            max);
+    float max = AAFrequency.completeInformation(ann, profile,
+            profile.getStartColumn(), profile.getEndColumn() + 1);
 
-    return ann;
+    return max;
   }
 
   /**
-   * Convert the computed information data into the desired annotation for
-   * display.
+   * A helper method that first searches for the HMM annotation that matches the
+   * group reference (null for the whole alignment annotation). If found, its
+   * sequence reference is updated to the given sequence (the recomputed HMM
+   * consensus sequence). If not found, it is created. This supports both
+   * creating the annotation the first time hmmbuild is run, and updating it if
+   * hmmbuild is re-run.
    * 
-   * @param informationAnnotation
-   *          the annotation to be populated
-   * @param hinformation
-   *          the computed information data
+   * @param seq
+   * @param group
+   * @return
    */
-  protected void deriveInformation(
-          AlignmentAnnotation informationAnnotation, ProfilesI hinformation)
+  AlignmentAnnotation findOrCreateAnnotation(SequenceI seq,
+          SequenceGroup group)
   {
-    long nseq = getSequences().length;
-    max = AAFrequency.completeInformation(informationAnnotation,
-            hinformation, hinformation.getStartColumn(),
-            hinformation.getEndColumn() + 1, nseq, max);
+    /*
+     * can't use Alignment.findOrCreateAnnotation here because we 
+     * want to update, rather than match on, the sequence ref
+     */
+    AlignmentAnnotation info = null;
+
+    AlignmentI alignment = alignViewport.getAlignment();
+    AlignmentAnnotation[] anns = alignment.getAlignmentAnnotation();
+    if (anns != null)
+    {
+      for (AlignmentAnnotation ann : anns)
+      {
+        if (HMM_CALC_ID.equals(ann.getCalcId()) && group == ann.groupRef)
+        {
+          info = ann;
+          info.setSequenceRef(seq);
+          info.label = seq.getName(); // in case group name changed!
+          break;
+        }
+      }
+    }
+
+    if (info == null)
+    {
+      int aWidth = alignment.getWidth();
+      String desc = MessageManager
+              .getString("label.information_description");
+      float graphMax = 6.52f; // todo where does this value derive from?
+      info = new AlignmentAnnotation(seq.getName(), desc,
+              new Annotation[aWidth], 0f, graphMax,
+              AlignmentAnnotation.BAR_GRAPH);
+      info.setCalcId(HMM_CALC_ID);
+      info.setSequenceRef(seq);
+      info.groupRef = group;
+      info.hasText = true;
+      alignment.addAnnotation(info);
+    }
+
+    return info;
   }
 }