JAL-974 refactored creation of Annotation rows from JABAWS2 services for use by disor...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Tue, 28 Aug 2012 13:14:19 +0000 (14:14 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Tue, 28 Aug 2012 13:14:19 +0000 (14:14 +0100)
src/jalview/ws/jws2/AAConsClient.java
src/jalview/ws/jws2/JabawsAlignCalcWorker.java

index d1bf303..42a7bdb 100644 (file)
@@ -2,7 +2,6 @@ package jalview.ws.jws2;
 
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Annotation;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignmentPanel;
 import jalview.ws.jws2.dm.AAConsSettings;
@@ -10,9 +9,9 @@ import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.WsParamSetI;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeSet;
 
 import compbio.data.sequence.Score;
@@ -56,16 +55,12 @@ public class AAConsClient extends JabawsAlignCalcWorker
   {
     if (immediate || !calcMan.isWorking(this) && scoremanager != null)
     {
-      AlignmentAnnotation annotation;
       Map<String, TreeSet<Score>> scoremap = scoremanager.asMap();
       int alWidth = alignViewport.getAlignment().getWidth();
-      AlignmentI alignment;
-      int ann = (alignment = alignViewport.getAlignment())
-              .getAlignmentAnnotation().length;
       ArrayList<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
       for (String score : scoremap.keySet())
       {
-        TreeSet<Score> scores = scoremap.get(score);
+        Set<Score> scores = scoremap.get(score);
         for (Score scr : scores)
         {
           if (scr.getRanges() != null && scr.getRanges().size() > 0)
@@ -82,90 +77,16 @@ public class AAConsClient extends JabawsAlignCalcWorker
           }
           else
           {
-            // simple annotation row
-            annotation = alignViewport.getAlignment()
-                    .findOrCreateAnnotation(scr.getMethod(), getCalcId(), true,
-                            null, null);
-
-            Annotation[] elm = new Annotation[alWidth];
-            if (alWidth == gapMap.length) // scr.getScores().size())
-            {
-              Iterator<Float> vals = scr.getScores().iterator();
-              float m = 0f, x = 0f;
-              for (int i = 0; vals.hasNext(); i++)
-              {
-                float val = vals.next().floatValue();
-                if (i == 0)
-                {
-                  m = val;
-                  x = val;
-                }
-                else
-                {
-                  if (m > val)
-                  {
-                    m = val;
-                  }
-                  ;
-                  if (x < val)
-                  {
-                    x = val;
-                  }
-                }
-                // if we're at a gapped column then skip to next ungapped position
-                if (gapMap != null && gapMap.length > 0)
-                {
-                  while (!gapMap[i])
-                  {
-                    elm[i++]=new Annotation("", "" , ' ', Float.NaN);
-                  }
-                }
-                elm[i] = new Annotation("", "" + val, ' ', val);
-              }
-
-              annotation.annotations = elm;
-              annotation.belowAlignment = true;
-              if (x < 0)
-              {
-                x = 0;
-              }
-              x += (x - m) * 0.1;
-              annotation.graphMax = x;
-              annotation.graphMin = m;
-              annotation.validateRangeAndDisplay();
-              ourAnnot.add(annotation);
-            }
+            createAnnotationRowsForScores(ourAnnot, getCalcId(), alWidth,
+                    scr);
           }
         }
       }
+
       if (ourAnnot.size() > 0)
       {
-        List<AlignmentAnnotation> our = ourAnnots;
-        ourAnnots = ourAnnot;
-        if (our != null)
-        {
-          if (our.size() > 0)
-          {
-            for (AlignmentAnnotation an : our)
-            {
-              if (!ourAnnots.contains(an))
-              {
-                // remove the old annotation
-                alignment.deleteAnnotation(an);
-              }
-            }
-          }
-          our.clear();
-        }
-      }
-      // if (ann !=
-      // alignViewport.getAlignment().getAlignmentAnnotation().length)
-      {
-        ap.adjustAnnotationHeight();
+        updateOurAnnots(ourAnnot);
       }
-      /*
-       * else { ap.paintAlignment(true); }
-       */
     }
   }
 
index 1d23ec0..d8807e7 100644 (file)
@@ -487,4 +487,112 @@ public abstract class JabawsAlignCalcWorker extends AlignCalcWorker
     return false;
   }
 
+  protected void createAnnotationRowsForScores(
+          List<AlignmentAnnotation> ourAnnot, String calcId, int alWidth,
+          Score scr)
+  {
+    // simple annotation row
+    AlignmentAnnotation annotation = alignViewport.getAlignment()
+            .findOrCreateAnnotation(scr.getMethod(), calcId, true, null,
+                    null);
+    if (alWidth == gapMap.length) // scr.getScores().size())
+    {
+      constructAnnotationFromScore(annotation, 0, alWidth, scr);
+      ourAnnot.add(annotation);
+    }
+  }
+
+  protected AlignmentAnnotation createAnnotationRowsForScores(
+          List<AlignmentAnnotation> ourAnnot, String typeName,
+          String calcId, SequenceI dseq, int base, Score scr)
+  {
+    System.out.println("Creating annotation on dseq:" + dseq.getStart()
+            + " base is " + base + " and length=" + dseq.getLength()
+            + " == " + scr.getScores().size());
+    // AlignmentAnnotation annotation = new AlignmentAnnotation(
+    // scr.getMethod(), typeName, new Annotation[]
+    // {}, 0, -1, AlignmentAnnotation.LINE_GRAPH);
+    // annotation.setCalcId(calcId);
+    AlignmentAnnotation annotation = alignViewport.getAlignment()
+            .findOrCreateAnnotation(typeName, calcId, false, dseq, null);
+    constructAnnotationFromScore(annotation, base, dseq.getLength(), scr);
+    annotation.createSequenceMapping(dseq, dseq.findPosition(base), false);
+    annotation.adjustForAlignment();
+    dseq.addAlignmentAnnotation(annotation);
+    ourAnnot.add(annotation);
+    return annotation;
+  }
+
+  private void constructAnnotationFromScore(AlignmentAnnotation annotation,
+          int base, int alWidth, Score scr)
+  {
+    Annotation[] elm = new Annotation[alWidth];
+    Iterator<Float> vals = scr.getScores().iterator();
+    float m = 0f, x = 0f;
+    for (int i = base; vals.hasNext(); i++)
+    {
+      float val = vals.next().floatValue();
+      if (i == 0)
+      {
+        m = val;
+        x = val;
+      }
+      else
+      {
+        if (m > val)
+        {
+          m = val;
+        }
+        ;
+        if (x < val)
+        {
+          x = val;
+        }
+      }
+      // if we're at a gapped column then skip to next ungapped position
+      if (gapMap != null && gapMap.length > 0)
+      {
+        while (!gapMap[i])
+        {
+          elm[i++] = new Annotation("", "", ' ', Float.NaN);
+        }
+      }
+      elm[i] = new Annotation("", "" + val, ' ', val);
+    }
+
+    annotation.annotations = elm;
+    annotation.belowAlignment = true;
+    if (x < 0)
+    {
+      x = 0;
+    }
+    x += (x - m) * 0.1;
+    annotation.graphMax = x;
+    annotation.graphMin = m;
+    annotation.validateRangeAndDisplay();
+  }
+
+  protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
+  {
+    List<AlignmentAnnotation> our = ourAnnots;
+    ourAnnots = ourAnnot;
+    AlignmentI alignment = alignViewport.getAlignment();
+    if (our != null)
+    {
+      if (our.size() > 0)
+      {
+        for (AlignmentAnnotation an : our)
+        {
+          if (!ourAnnots.contains(an))
+          {
+            // remove the old annotation
+            alignment.deleteAnnotation(an);
+          }
+        }
+      }
+      our.clear();
+
+      ap.adjustAnnotationHeight();
+    }
+  }
 }