JAL-1933 basic hack to get gap counts shown as an annotation row.
authorJim Procter <jprocter@issues.jalview.org>
Thu, 1 Dec 2016 10:34:07 +0000 (10:34 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 1 Dec 2016 10:34:07 +0000 (10:34 +0000)
src/jalview/analysis/AAFrequency.java
src/jalview/api/AlignViewportI.java
src/jalview/viewmodel/AlignmentViewport.java
src/jalview/workers/ConsensusThread.java

index 17874e6..b5dab6f 100755 (executable)
@@ -289,6 +289,55 @@ public class AAFrequency
   }
 
   /**
+   * Derive the gap count annotation row.
+   * 
+   * @param consensus
+   *          the annotation row to add annotations to
+   * @param profiles
+   *          the source consensus data
+   * @param startCol
+   *          start column (inclusive)
+   * @param endCol
+   *          end column (exclusive)
+   */
+  public static void completeGapAnnot(AlignmentAnnotation consensus,
+          ProfilesI profiles, int startCol, int endCol, long nseq)
+  {
+    // long now = System.currentTimeMillis();
+    if (consensus == null || consensus.annotations == null
+            || consensus.annotations.length < endCol)
+    {
+      /*
+       * called with a bad alignment annotation row 
+       * wait for it to be initialised properly
+       */
+      return;
+    }
+
+    for (int i = startCol; i < endCol; i++)
+    {
+      ProfileI profile = profiles.get(i);
+      if (profile == null)
+      {
+        /*
+         * happens if sequences calculated over were 
+         * shorter than alignment width
+         */
+        consensus.annotations[i] = null;
+        return;
+      }
+
+      final int gapped = profile.getGapped();
+
+      String description = "" + gapped;
+
+      consensus.annotations[i] = new Annotation(gapped);
+    }
+    // long elapsed = System.currentTimeMillis() - now;
+    // System.out.println(-elapsed);
+  }
+
+  /**
    * Returns a tooltip showing either
    * <ul>
    * <li>the full profile (percentages of all residues present), if
index 72542b3..7067328 100644 (file)
@@ -110,6 +110,13 @@ public interface AlignViewportI extends ViewStyleI
   AlignmentAnnotation getAlignmentConsensusAnnotation();
 
   /**
+   * get the container for alignment gap annotation
+   * 
+   * @return
+   */
+  AlignmentAnnotation getAlignmentGapAnnotation();
+
+  /**
    * get the container for cDNA complement consensus annotation
    * 
    * @return
index fecccb0..560ff59 100644 (file)
@@ -688,6 +688,8 @@ public abstract class AlignmentViewport implements AlignViewportI,
 
   protected AlignmentAnnotation complementConsensus;
 
+  protected AlignmentAnnotation gapcounts;
+
   protected AlignmentAnnotation strucConsensus;
 
   protected AlignmentAnnotation conservation;
@@ -790,6 +792,12 @@ public abstract class AlignmentViewport implements AlignViewportI,
   }
 
   @Override
+  public AlignmentAnnotation getAlignmentGapAnnotation()
+  {
+    return gapcounts;
+  }
+
+  @Override
   public AlignmentAnnotation getComplementConsensusAnnotation()
   {
     return complementConsensus;
@@ -829,7 +837,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
   public void updateConsensus(final AlignmentViewPanel ap)
   {
     // see note in mantis : issue number 8585
-    if (consensus == null || !autoCalculateConsensus)
+    if ((consensus == null || gapcounts == null) || !autoCalculateConsensus)
     {
       return;
     }
@@ -1903,6 +1911,10 @@ public abstract class AlignmentViewport implements AlignViewportI,
       consensus = new AlignmentAnnotation("Consensus", "PID",
               new Annotation[1], 0f, 100f, AlignmentAnnotation.BAR_GRAPH);
       initConsensus(consensus);
+      gapcounts = new AlignmentAnnotation("Gaps", "Number of Gaps",
+              new Annotation[1], 0f, alignment.getHeight(),
+              AlignmentAnnotation.LINE_GRAPH);
+      initGapCounts(gapcounts);
 
       initComplementConsensus();
     }
@@ -1955,6 +1967,20 @@ public abstract class AlignmentViewport implements AlignViewportI,
     }
   }
 
+  // these should be extracted from the view model - style and settings for
+  // derived annotation
+  private void initGapCounts(AlignmentAnnotation gapcounts)
+  {
+    gapcounts.hasText = false;
+    gapcounts.autoCalculated = true;
+    gapcounts.graph = AlignmentAnnotation.BAR_GRAPH;
+
+    if (showConsensus)
+    {
+      alignment.addAnnotation(gapcounts);
+    }
+  }
+
   private void initConservation()
   {
     if (showConservation)
index debe45d..5c55d48 100644 (file)
@@ -50,7 +50,8 @@ public class ConsensusThread extends AlignCalcWorker
     try
     {
       AlignmentAnnotation consensus = getConsensusAnnotation();
-      if (consensus == null || calcMan.isPending(this))
+      AlignmentAnnotation gap = getGapAnnotation();
+      if ((consensus == null && gap == null) || calcMan.isPending(this))
       {
         calcMan.workerComplete(this);
         return;
@@ -117,6 +118,10 @@ public class ConsensusThread extends AlignCalcWorker
   {
     AlignmentAnnotation consensus = getConsensusAnnotation();
     consensus.annotations = new Annotation[aWidth];
+    AlignmentAnnotation gap = getGapAnnotation();
+    if (gap!=null) {
+      gap.annotations = new Annotation[aWidth];
+    }
   }
 
   /**
@@ -166,6 +171,16 @@ public class ConsensusThread extends AlignCalcWorker
   }
 
   /**
+   * Get the Gap annotation for the alignment
+   * 
+   * @return
+   */
+  protected AlignmentAnnotation getGapAnnotation()
+  {
+    return alignViewport.getAlignmentGapAnnotation();
+  }
+
+  /**
    * update the consensus annotation from the sequence profile data using
    * current visualization settings.
    */
@@ -183,6 +198,11 @@ public class ConsensusThread extends AlignCalcWorker
             && hconsensus != null)
     {
       deriveConsensus(consensus, hconsensus);
+      AlignmentAnnotation gap = getGapAnnotation();
+      if (gap != null)
+      {
+        deriveGap(gap, hconsensus);
+      }
     }
   }
 
@@ -206,6 +226,25 @@ public class ConsensusThread extends AlignCalcWorker
             alignViewport.isIgnoreGapsConsensus(),
             alignViewport.isShowSequenceLogo(), nseq);
   }
+    /**
+     * Convert the computed consensus data into a gap annotation row for
+     * display.
+     * 
+     * @param gapAnnotation
+     *          the annotation to be populated
+     * @param hconsensus
+     *          the computed consensus data
+     */
+    protected void deriveGap(AlignmentAnnotation gapAnnotation,
+            ProfilesI hconsensus)
+    {
+
+      long nseq = getSequences().length;
+      AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
+            hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
+            nseq);
+
+  }
 
   /**
    * Get the consensus data stored on the viewport.