Remove consensus annotation if out of memory
[jalview.git] / src / jalview / gui / AlignViewport.java
index c0e9dc9..0519569 100755 (executable)
@@ -109,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,7 +148,7 @@ public class AlignViewport
 
       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 + "") ;
@@ -174,7 +167,6 @@ public class AlignViewport
 
        setFont(new Font(fontName, style, Integer.parseInt(fontSize)));
 
-
        alignment.setGapCharacter( Cache.getDefault("GAP_SYMBOL", "-").charAt(0) );
 
 
@@ -221,7 +213,6 @@ public class AlignViewport
            {
              alignment.addAnnotation(consensus);
            }
-
         }
 
         if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)
@@ -273,6 +264,21 @@ public class AlignViewport
       {
         try
         {
+          while (updatingConservation)
+          {
+            try
+            {
+              Thread.sleep(200);
+            }
+            catch (Exception ex)
+            {
+              ex.printStackTrace();
+            }
+          }
+
+          updatingConservation = true;
+
+
           int alWidth = alignment.getWidth();
           if(alWidth<0)
             return;
@@ -289,7 +295,6 @@ public class AlignViewport
             cons.findQuality();
           }
 
-
           String sequence = cons.getConsSequence().getSequence();
           float minR;
           float minG;
@@ -397,49 +402,29 @@ public class AlignViewport
 
     boolean consUpdateNeeded = false;
 
-    boolean updatingConsensus = false;
+    static boolean updatingConsensus = false;
 
-    boolean updatingConservation = false;
+    static boolean updatingConservation = false;
 
     /**
      * 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");
-      }
+      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");
-      }
+      consensusThread = new ConsensusThread(ap);
+      consensusThread.start();
     }
 
 
@@ -452,6 +437,20 @@ public class AlignViewport
       }
       public void run()
       {
+        while (updatingConsensus)
+        {
+          try
+          {
+            Thread.sleep(200);
+          }
+          catch (Exception ex)
+          {
+            ex.printStackTrace();
+          }
+        }
+
+        updatingConsensus = true;
+
         try
         {
           int aWidth = alignment.getWidth();
@@ -498,6 +497,8 @@ public class AlignViewport
         }
         catch (OutOfMemoryError error)
         {
+          alignment.deleteAnnotation(consensus);
+
           consensus = null;
           hconsensus = null;
           javax.swing.SwingUtilities.invokeLater(new Runnable()
@@ -544,6 +545,7 @@ public class AlignViewport
             seqs.append(consensus.annotations[i].displayCharacter);
         }
       }
+
       SequenceI sq = new Sequence("Consensus", seqs.toString());
       sq.setDescription("Percentage Identity Consensus "+((ignoreGapsInConsensusCalculation) ? " without gaps" : ""));
       return sq;
@@ -1499,4 +1501,64 @@ 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();
+      }
+    }
+
+
+
 }