Reset colour schemes and alignmentChanged moved to viewport
[jalview.git] / src / jalview / gui / AlignViewport.java
index 743c8dd..efce038 100755 (executable)
@@ -51,9 +51,6 @@ public class AlignViewport
     boolean renderGaps = true;
     boolean showSequenceFeatures = false;
     boolean showAnnotation = true;
-    boolean showConservation = true;
-    boolean showQuality = true;
-    boolean showIdentity = true;
     boolean colourAppliesToAllGroups = true;
     ColourSchemeI globalColourScheme = null;
     boolean conservationColourSelected = false;
@@ -112,13 +109,6 @@ public class AlignViewport
 
     boolean gatherViewsHere = false;
 
-
-    public AlignViewport(AlignmentI al, boolean dataset)
-    {
-      isDataset = dataset;
-      setAlignment(al);
-      init();
-    }
     /**
      * Creates a new AlignViewport object.
      *
@@ -155,14 +145,10 @@ public class AlignViewport
 
       showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
       showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
-      showConservation = Cache.getDefault("SHOW_CONSERVATION", true);
-
-      showQuality = Cache.getDefault("SHOW_QUALITY", true);
-      showIdentity = Cache.getDefault("SHOW_IDENTITY", true);
 
       autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
 
-      padGaps = Cache.getDefault("PAD_GAPS", false);
+      padGaps = Cache.getDefault("PAD_GAPS", true);
 
        String fontName = Cache.getDefault("FONT_NAME", "SansSerif");
        String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "") ;
@@ -181,7 +167,6 @@ public class AlignViewport
 
        setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
 
-
        alignment.setGapCharacter( Cache.getDefault("GAP_SYMBOL", "-").charAt(0) );
 
 
@@ -199,12 +184,13 @@ public class AlignViewport
                 AlignmentAnnotation.BAR_GRAPH);
             conservation.hasText = true;
 
-            if (showConservation)
+
+            if (Cache.getDefault("SHOW_CONSERVATION", true))
             {
               alignment.addAnnotation(conservation);
             }
 
-            if (showQuality)
+            if (Cache.getDefault("SHOW_QUALITY", true))
             {
               quality = new AlignmentAnnotation("Quality",
                                                 "Alignment Quality based on Blosum62 scores",
@@ -212,6 +198,7 @@ public class AlignViewport
                                                 0f,
                                                 11f,
                                                 AlignmentAnnotation.BAR_GRAPH);
+              quality.hasText = true;
 
               alignment.addAnnotation(quality);
             }
@@ -222,11 +209,10 @@ public class AlignViewport
                                                AlignmentAnnotation.BAR_GRAPH);
           consensus.hasText = true;
 
-           if (showIdentity)
+           if (Cache.getDefault("SHOW_IDENTITY", true))
            {
              alignment.addAnnotation(consensus);
            }
-
         }
 
         if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
@@ -278,24 +264,22 @@ public class AlignViewport
       {
         try
         {
-          long t = System.currentTimeMillis();
+          int alWidth = alignment.getWidth();
+          if(alWidth<0)
+            return;
 
           Conservation cons = new jalview.analysis.Conservation("All",
               jalview.schemes.ResidueProperties.propHash, 3,
-              alignment.getSequences(), 0, alignment.getWidth() - 1);
+              alignment.getSequences(), 0, alWidth -1);
 
           cons.calculate();
           cons.verdict(false, ConsPercGaps);
 
-          if (showQuality)
+          if (quality!=null)
           {
             cons.findQuality();
           }
 
-          System.out.println("Conservation took " + (System.currentTimeMillis() - t) +
-                             "ms");
-
-          int alWidth = alignment.getWidth();
           String sequence = cons.getConsSequence().getSequence();
           float minR;
           float minG;
@@ -319,8 +303,9 @@ public class AlignViewport
 
           conservation.annotations = new Annotation[alWidth];
 
-          if (showQuality)
+          if (quality!=null)
           {
+            quality.graphMax = cons.qualityRange[1].floatValue();
             quality.annotations = new Annotation[alWidth];
             qmin = cons.qualityRange[0].floatValue();
             qmax = cons.qualityRange[1].floatValue();
@@ -349,7 +334,7 @@ public class AlignViewport
                                          minB + (maxB * vprop)));
 
             // Quality calc
-            if (showQuality)
+            if (quality!=null)
             {
               value = ( (Double) cons.quality.get(i)).floatValue();
               vprop = value - qmin;
@@ -361,16 +346,13 @@ public class AlignViewport
                   minB + (maxB * vprop)));
             }
           }
-
-          if (quality != null)
-          {
-            quality.graphMax = cons.qualityRange[1].floatValue();
-          }
         }
         catch (OutOfMemoryError error)
         {
           javax.swing.SwingUtilities.invokeLater(new Runnable()
           {
+
+
             public void run()
             {
               javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
@@ -382,6 +364,9 @@ public class AlignViewport
             }
           });
 
+          conservation = null;
+          quality = null;
+
           System.out.println("Conservation calculation: " + error);
           System.gc();
 
@@ -396,9 +381,9 @@ public class AlignViewport
     }
 
 
-    ConservationThread conservationThread;
+    static ConservationThread conservationThread;
 
-    ConsensusThread consensusThread;
+    static ConsensusThread consensusThread;
 
     boolean consUpdateNeeded = false;
 
@@ -409,42 +394,49 @@ public class AlignViewport
     /**
      * DOCUMENT ME!
      */
-    public void updateConservation(AlignmentPanel ap)
+    public void updateConservation(final AlignmentPanel ap)
     {
-      if (alignment.isNucleotide())
+      if (alignment.isNucleotide() || conservation==null)
         return;
 
       updatingConservation = true;
-
-      if (conservationThread == null || !conservationThread.isAlive())
-      {
-        conservationThread = new ConservationThread(ap);
-        conservationThread.start();
-      }
-      else
-      {
-        consUpdateNeeded = true;
-        System.out.println("come back later");
-      }
+    // javax.swing.SwingUtilities.invokeLater(new Runnable()      {        public void run()  {
+          while (conservationThread != null && conservationThread.isAlive())
+          {
+            try
+            {
+           //   Thread.sleep(100);
+              ap.repaint();
+            }
+            catch (Exception ex)
+            {}
+          }
+          conservationThread = new ConservationThread(ap);
+          conservationThread.start();
+      //  }      });
     }
 
     /**
      * DOCUMENT ME!
      */
-    public void updateConsensus(AlignmentPanel ap)
+    public void updateConsensus(final AlignmentPanel ap)
     {
       updatingConsensus = true;
 
-      if (consensusThread == null || !consensusThread.isAlive())
-      {
-        consensusThread = new ConsensusThread(ap);
-        consensusThread.start();
-      }
-      else
-      {
-        consUpdateNeeded = true;
-        System.out.println("come back later");
-      }
+     // javax.swing.SwingUtilities.invokeLater(new Runnable()      {        public void run()   {
+          while (consensusThread != null && consensusThread.isAlive())
+          {
+            try
+            {
+          //    Thread.sleep(100);
+              ap.repaint();
+            }
+            catch (Exception ex)
+            {}
+          }
+          consensusThread = new ConsensusThread(ap);
+          consensusThread.start();
+      //  }      });
     }
 
 
@@ -460,20 +452,19 @@ public class AlignViewport
         try
         {
           int aWidth = alignment.getWidth();
+          if(aWidth<0)
+            return;
 
           consensus.annotations = null;
           consensus.annotations = new Annotation[aWidth];
 
-          long t = System.currentTimeMillis();
+
           hconsensus = new Hashtable[aWidth];
           AAFrequency.calculate(alignment.getSequencesArray(),
                                 0,
                                 alignment.getWidth(),
                                 hconsensus);
 
-          System.out.println("Consensus took " +
-                             (System.currentTimeMillis() - t) + "ms");
-
           for (int i = 0; i < aWidth; i++)
           {
             float value = 0;
@@ -504,6 +495,8 @@ public class AlignViewport
         }
         catch (OutOfMemoryError error)
         {
+          consensus = null;
+          hconsensus = null;
           javax.swing.SwingUtilities.invokeLater(new Runnable()
           {
             public void run()
@@ -1503,4 +1496,63 @@ public class AlignViewport
       return sequenceSetID;
     }
 
+    public void alignmentChanged(AlignmentPanel ap)
+    {
+        if (padGaps)
+          alignment.padGaps();
+
+        if (hconsensus != null && autoCalculateConsensus)
+        {
+          updateConsensus(ap);
+          updateConservation(ap);
+        }
+
+        resetAllColourSchemes();
+
+        alignment.adjustSequenceAnnotations();
+    }
+
+
+    void resetAllColourSchemes()
+    {
+      ColourSchemeI cs = globalColourScheme;
+      if(cs!=null)
+      {
+        if (cs instanceof ClustalxColourScheme)
+        {
+          ( (ClustalxColourScheme) cs).
+              resetClustalX(alignment.getSequences(),
+                            alignment.getWidth());
+        }
+
+        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);
+        }
+      }
+
+      int s, sSize = alignment.getGroups().size();
+      for(s=0; s<sSize; s++)
+      {
+        SequenceGroup sg = (SequenceGroup)alignment.getGroups().elementAt(s);
+        if(sg.cs!=null && sg.cs instanceof ClustalxColourScheme)
+        {
+          ((ClustalxColourScheme)sg.cs).resetClustalX(
+              sg.getSequences(true), sg.getWidth());
+        }
+        sg.recalcConservation();
+      }
+    }
+
+
+
 }