Refactored alignment viewport to use common base, extended viewport API with getters...
[jalview.git] / src / jalview / appletgui / AlignViewport.java
index 9af5ac2..e48c0ea 100644 (file)
@@ -22,14 +22,16 @@ import java.util.*;
 import java.awt.*;
 
 import jalview.analysis.*;
+import jalview.api.AlignCalcManagerI;
 import jalview.api.AlignViewportI;
 import jalview.bin.*;
 import jalview.datamodel.*;
 import jalview.schemes.*;
 import jalview.structure.SelectionSource;
 import jalview.structure.VamsasSource;
+import jalview.workers.ConservationThread;
 
-public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSource
+public class AlignViewport extends AlignmentViewport implements AlignViewportI, SelectionSource, VamsasSource 
 {
   int startRes;
 
@@ -125,8 +127,6 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
 
   boolean autocalculateConsensus = true;
 
-  public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!
-
   private java.beans.PropertyChangeSupport changeSupport = new java.beans.PropertyChangeSupport(
           this);
 
@@ -146,6 +146,8 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
 
   Hashtable hiddenRepSequences;
   
+  AlignCalcManagerI calculator=new jalview.workers.AlignCalcManager();
+  
   public void finalize() {
     applet=null;
     quality=null;
@@ -288,7 +290,7 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
       if (!alignment.isNucleotide())
       {
         conservation = new AlignmentAnnotation("Conservation",
-                "Conservation of total alignment less than " + ConsPercGaps
+                "Conservation of total alignment less than " + getConsPercGaps()
                         + "% gaps", new Annotation[1], 0f, 11f,
                 AlignmentAnnotation.BAR_GRAPH);
         conservation.hasText = true;
@@ -334,149 +336,6 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
     return showSequenceFeatures;
   }
 
-  class ConservationThread extends Thread
-  {
-    AlignmentPanel ap;
-
-    public ConservationThread(AlignmentPanel ap)
-    {
-      this.ap = ap;
-    }
-
-    public void run()
-    {
-      try
-      {
-        updatingConservation = true;
-
-        while (UPDATING_CONSERVATION)
-        {
-          try
-          {
-            if (ap != null)
-            {
-              ap.paintAlignment(false);
-            }
-            Thread.sleep(200);
-          } catch (Exception ex)
-          {
-            ex.printStackTrace();
-          }
-        }
-
-        UPDATING_CONSERVATION = true;
-
-        int alWidth = (alignment==null) ? -1 : alignment.getWidth();
-        if (alWidth < 0)
-        {
-          updatingConservation = false;
-          UPDATING_CONSERVATION = false;
-          return;
-        }
-
-        Conservation cons = new jalview.analysis.Conservation("All",
-                jalview.schemes.ResidueProperties.propHash, 3,
-                alignment.getSequences(), 0, alWidth - 1);
-
-        cons.calculate();
-        cons.verdict(false, ConsPercGaps);
-
-        if (quality != null)
-        {
-          cons.findQuality();
-        }
-
-        char[] sequence = cons.getConsSequence().getSequence();
-        float minR;
-        float minG;
-        float minB;
-        float maxR;
-        float maxG;
-        float maxB;
-        minR = 0.3f;
-        minG = 0.0f;
-        minB = 0f;
-        maxR = 1.0f - minR;
-        maxG = 0.9f - minG;
-        maxB = 0f - minB; // scalable range for colouring both Conservation and
-        // Quality
-
-        float min = 0f;
-        float max = 11f;
-        float qmin = 0f;
-        float qmax = 0f;
-
-        char c;
-
-        conservation.annotations = new Annotation[alWidth];
-
-        if (quality != null)
-        {
-          quality.graphMax = cons.qualityRange[1].floatValue();
-          quality.annotations = new Annotation[alWidth];
-          qmin = cons.qualityRange[0].floatValue();
-          qmax = cons.qualityRange[1].floatValue();
-        }
-
-        for (int i = 0; i < alWidth; i++)
-        {
-          float value = 0;
-
-          c = sequence[i];
-
-          if (Character.isDigit(c))
-          {
-            value = (int) (c - '0');
-          }
-          else if (c == '*')
-          {
-            value = 11;
-          }
-          else if (c == '+')
-          {
-            value = 10;
-          }
-          // TODO - refactor to use a graduatedColorScheme to calculate the
-          // histogram colors.
-          float vprop = value - min;
-          vprop /= max;
-          conservation.annotations[i] = new Annotation(String.valueOf(c),
-                  String.valueOf(value), ' ', value, new Color(minR
-                          + (maxR * vprop), minG + (maxG * vprop), minB
-                          + (maxB * vprop)));
-
-          // Quality calc
-          if (quality != null)
-          {
-            value = ((Double) cons.quality.elementAt(i)).floatValue();
-            vprop = value - qmin;
-            vprop /= qmax;
-            quality.annotations[i] = new Annotation(" ",
-                    String.valueOf(value), ' ', value, new Color(minR
-                            + (maxR * vprop), minG + (maxG * vprop), minB
-                            + (maxB * vprop)));
-          }
-        }
-      } catch (OutOfMemoryError error)
-      {
-        System.out.println("Out of memory calculating conservation!!");
-        conservation = null;
-        quality = null;
-        System.gc();
-      }
-
-      UPDATING_CONSERVATION = false;
-      updatingConservation = false;
-
-      if (ap != null)
-      {
-        ap.paintAlignment(true);
-      }
-
-    }
-  }
-
-  ConservationThread conservationThread;
 
   ConsensusThread consensusThread;
   
@@ -507,8 +366,7 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
       return;
     }
 
-    conservationThread = new ConservationThread(ap);
-    conservationThread.start();
+    calculator.startWorker(new ConservationThread(this, ap));
   }
 
   /**
@@ -1496,14 +1354,9 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
       cs.setConsensus(hconsensus);
       if (cs.conservationApplied())
       {
-        Alignment al = (Alignment) alignment;
-        Conservation c = new Conservation("All",
-                ResidueProperties.propHash, 3, al.getSequences(), 0,
-                al.getWidth() - 1);
-        c.calculate();
-        c.verdict(false, ConsPercGaps);
-
-        cs.setConservation(c);
+        cs.setConservation(Conservation.calculateConservation("All",
+                ResidueProperties.propHash, 3, alignment.getSequences(), 0,
+                alignment.getWidth(), false, getConsPercGaps(), false));
       }
     }
 
@@ -1829,4 +1682,32 @@ public class AlignViewport implements AlignViewportI, SelectionSource, VamsasSou
   {
     return null; // TODO: JAL-891 port to jvlite : refactor and introduce hStrucConsensus;
   }
+
+  @Override
+  public AlignmentAnnotation getAlignmentQualityAnnot()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public AlignmentAnnotation getAlignmentConservationAnnotation()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  @Override
+  public boolean isClosed()
+  {
+    System.err.println("Alignment is "+alignment==null ? "closed" : "open");
+    return alignment==null;
+  }
+
+  @Override
+  public AlignCalcManagerI getCalcManager()
+  {
+    // TODO Auto-generated method stub
+    return null;
+  }
 }