more bugfixes.
[jalview.git] / src / jalview / gui / AlignViewport.java
index 36f69a5..6c8e0e4 100755 (executable)
@@ -61,6 +61,7 @@ public class AlignViewport
     boolean validCharWidth;
     int wrappedWidth;
     Font font;
+    boolean seqNameItalics;
     AlignmentI alignment;
     ColumnSelection colSel = new ColumnSelection();
     int threshold;
@@ -109,6 +110,20 @@ public class AlignViewport
 
     boolean gatherViewsHere = false;
 
+    Stack historyList = new Stack();
+    Stack redoList = new Stack();
+
+    Hashtable sequenceColours;
+
+    int thresholdTextColour = 0;
+    Color textColour = Color.black;
+    Color textColour2 = Color.white;
+
+    boolean rightAlignIds = false;
+
+    Hashtable hiddenRepSequences;
+
+
     /**
      * Creates a new AlignViewport object.
      *
@@ -146,6 +161,8 @@ public class AlignViewport
       showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);
       showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);
 
+      rightAlignIds = Cache.getDefault("RIGHT_ALIGN_IDS", false);
+
       autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);
 
       padGaps = Cache.getDefault("PAD_GAPS", true);
@@ -154,6 +171,8 @@ public class AlignViewport
        String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "") ;
        String fontSize = Cache.getDefault("FONT_SIZE", "10");
 
+       seqNameItalics = Cache.getDefault("ID_ITALICS", true);
+
        int style = 0;
 
        if (fontStyle.equals("bold"))
@@ -183,6 +202,7 @@ public class AlignViewport
                 11f,
                 AlignmentAnnotation.BAR_GRAPH);
             conservation.hasText = true;
+            conservation.autoCalculated = true;
 
 
             if (Cache.getDefault("SHOW_CONSERVATION", true))
@@ -199,6 +219,7 @@ public class AlignViewport
                                                 11f,
                                                 AlignmentAnnotation.BAR_GRAPH);
               quality.hasText = true;
+              quality.autoCalculated = true;
 
               alignment.addAnnotation(quality);
             }
@@ -208,6 +229,7 @@ public class AlignViewport
                                                new Annotation[1], 0f, 100f,
                                                AlignmentAnnotation.BAR_GRAPH);
           consensus.hasText = true;
+          consensus.autoCalculated = true;
 
            if (Cache.getDefault("SHOW_IDENTITY", true))
            {
@@ -231,6 +253,8 @@ public class AlignViewport
                 globalColourScheme.setConsensus(hconsensus);
             }
         }
+
+        wrapAlignment = jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false);
     }
 
 
@@ -264,10 +288,16 @@ public class AlignViewport
       {
         try
         {
-          while (updatingConservation)
+          updatingConservation = true;
+
+          while (UPDATING_CONSERVATION)
           {
             try
             {
+              if (ap != null)
+              {
+                ap.repaint();
+              }
               Thread.sleep(200);
             }
             catch (Exception ex)
@@ -276,7 +306,7 @@ public class AlignViewport
             }
           }
 
-          updatingConservation = true;
+          UPDATING_CONSERVATION = true;
 
 
           int alWidth = alignment.getWidth();
@@ -295,7 +325,7 @@ public class AlignViewport
             cons.findQuality();
           }
 
-          String sequence = cons.getConsSequence().getSequence();
+          char [] sequence = cons.getConsSequence().getSequence();
           float minR;
           float minG;
           float minB;
@@ -330,7 +360,7 @@ public class AlignViewport
           {
             float value = 0;
 
-            c = sequence.charAt(i);
+            c = sequence[i];
 
             if (Character.isDigit(c))
               value = (int) (c - '0');
@@ -387,11 +417,14 @@ public class AlignViewport
 
         }
 
+        UPDATING_CONSERVATION = false;
+        updatingConservation = false;
+
         if(ap!=null)
         {
           ap.repaint();
         }
-        updatingConservation = false;
+
       }
     }
 
@@ -402,9 +435,13 @@ public class AlignViewport
 
     boolean consUpdateNeeded = false;
 
-    static boolean updatingConsensus = false;
+    static boolean UPDATING_CONSENSUS = false;
+
+    static boolean UPDATING_CONSERVATION = false;
+
+    boolean updatingConsensus = false;
 
-    static boolean updatingConservation = false;
+    boolean updatingConservation = false;
 
     /**
      * DOCUMENT ME!
@@ -437,10 +474,16 @@ public class AlignViewport
       }
       public void run()
       {
-        while (updatingConsensus)
+        updatingConsensus = true;
+        while (UPDATING_CONSENSUS)
         {
           try
           {
+            if (ap != null)
+            {
+              ap.repaint();
+            }
+
             Thread.sleep(200);
           }
           catch (Exception ex)
@@ -449,7 +492,8 @@ public class AlignViewport
           }
         }
 
-        updatingConsensus = true;
+
+        UPDATING_CONSENSUS = true;
 
         try
         {
@@ -497,6 +541,8 @@ public class AlignViewport
         }
         catch (OutOfMemoryError error)
         {
+          alignment.deleteAnnotation(consensus);
+
           consensus = null;
           hconsensus = null;
           javax.swing.SwingUtilities.invokeLater(new Runnable()
@@ -515,14 +561,13 @@ public class AlignViewport
           System.out.println("Consensus calculation: " + error);
           System.gc();
         }
+        UPDATING_CONSENSUS = false;
+        updatingConsensus = false;
 
         if (ap != null)
         {
           ap.repaint();
         }
-
-
-        updatingConsensus = false;
       }
     }
     /**
@@ -543,6 +588,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;
@@ -1224,6 +1270,33 @@ public class AlignViewport
       hasHiddenColumns = true;
     }
 
+    public void hideRepSequences(SequenceI repSequence, SequenceGroup sg)
+    {
+      int sSize = sg.getSize();
+      if(sSize < 2)
+        return;
+
+      if(hiddenRepSequences==null)
+        hiddenRepSequences = new Hashtable();
+
+       hiddenRepSequences.put(repSequence, sg);
+
+      //Hide all sequences except the repSequence
+      SequenceI [] seqs = new SequenceI[sSize-1];
+      int index = 0;
+      for(int i=0; i<sSize; i++)
+        if(sg.getSequenceAt(i)!=repSequence)
+        {
+          if(index==sSize-1)
+            return;
+
+          seqs[index++] = sg.getSequenceAt(i);
+        }
+
+      hideSequence(seqs);
+
+    }
+
     public void hideAllSelectedSeqs()
     {
       if (selectionGroup == null)
@@ -1241,8 +1314,9 @@ public class AlignViewport
       if(seq!=null)
       {
         for (int i = 0; i < seq.length; i++)
+        {
           alignment.getHiddenSequences().hideSequence(seq[i]);
-
+        }
         hasHiddenRows = true;
         firePropertyChange("alignment", null, alignment.getSequences());
       }
@@ -1250,7 +1324,8 @@ public class AlignViewport
 
     public void showSequence(int index)
     {
-      Vector tmp = alignment.getHiddenSequences().showSequence(index);
+      Vector tmp = alignment.getHiddenSequences().showSequence(index
+          , hiddenRepSequences);
       if(tmp.size()>0)
       {
         if(selectionGroup==null)
@@ -1294,7 +1369,7 @@ public class AlignViewport
           selectionGroup = new SequenceGroup();
           selectionGroup.setEndRes(alignment.getWidth()-1);
         }
-        Vector tmp = alignment.getHiddenSequences().showAll();
+        Vector tmp = alignment.getHiddenSequences().showAll(hiddenRepSequences);
         for(int t=0; t<tmp.size(); t++)
         {
           selectionGroup.addSequence(
@@ -1303,23 +1378,26 @@ public class AlignViewport
         }
         firePropertyChange("alignment", null, alignment.getSequences());
         hasHiddenRows = false;
+        hiddenRepSequences = null;
       }
     }
 
+
+
     public void invertColumnSelection()
     {
-      int column;
       for(int i=0; i<alignment.getWidth(); i++)
       {
-        column = i;
-
-        if(colSel.contains(column))
-          colSel.removeElement(column);
+        if(colSel.contains(i))
+          colSel.removeElement(i);
         else
-          colSel.addElement(column);
-
+        {
+          if (!hasHiddenColumns || colSel.isVisible(i))
+          {
+            colSel.addElement(i);
+          }
+        }
       }
-
     }
 
     public int adjustForHiddenSeqs(int alignmentIndex)
@@ -1360,7 +1438,7 @@ public class AlignViewport
       int start = 0, end = 0;
       if(selectedRegionOnly && selectionGroup!=null)
       {
-        iSize = selectionGroup.getSize(false);
+        iSize = selectionGroup.getSize();
         seqs = selectionGroup.getSequencesInOrder(alignment);
         start = selectionGroup.getStartRes();
         end = selectionGroup.getEndRes(); // inclusive for start and end in SeqCigar constructor
@@ -1455,7 +1533,7 @@ public class AlignViewport
       int start = 0, end = 0;
       if(selectedRegionOnly && selectionGroup!=null)
       {
-        iSize = selectionGroup.getSize(false);
+        iSize = selectionGroup.getSize();
         seqs = selectionGroup.getSequencesInOrder(alignment);
         start = selectionGroup.getStartRes();
         end = selectionGroup.getEndRes()+1;
@@ -1473,7 +1551,7 @@ public class AlignViewport
       } else {
         for(i=0; i<iSize; i++)
         {
-          selection[i] = seqs[i].getSequence(start, end);
+          selection[i] = seqs[i].getSequenceAsString(start, end);
         }
 
       }
@@ -1509,10 +1587,25 @@ public class AlignViewport
           updateConservation(ap);
         }
 
-        resetAllColourSchemes();
+        //Reset endRes of groups if beyond alignment width
+        int alWidth = alignment.getWidth();
+        Vector groups = alignment.getGroups();
+        if(groups!=null)
+        {
+          for(int i=0; i<groups.size(); i++)
+          {
+            SequenceGroup sg = (SequenceGroup)groups.elementAt(i);
+            if(sg.getEndRes()>alWidth)
+              sg.setEndRes(alWidth-1);
+          }
+        }
 
-        alignment.adjustSequenceAnnotations();
+        if(selectionGroup!=null && selectionGroup.getEndRes()>alWidth)
+          selectionGroup.setEndRes(alWidth-1);
+
+        resetAllColourSchemes();
 
+       // alignment.adjustSequenceAnnotations();
     }
 
 
@@ -1550,12 +1643,31 @@ public class AlignViewport
         if(sg.cs!=null && sg.cs instanceof ClustalxColourScheme)
         {
           ((ClustalxColourScheme)sg.cs).resetClustalX(
-              sg.getSequences(true), sg.getWidth());
+              sg.getSequences(hiddenRepSequences), sg.getWidth());
         }
         sg.recalcConservation();
       }
     }
 
 
+    public Color getSequenceColour(SequenceI seq)
+    {
+      if(sequenceColours==null || !sequenceColours.containsKey(seq))
+        return Color.white;
+      else
+        return (Color)sequenceColours.get(seq);
+    }
+
+    public void setSequenceColour(SequenceI seq, Color col)
+    {
+      if(sequenceColours==null)
+        sequenceColours = new Hashtable();
+
+      if(col == null)
+        sequenceColours.remove(seq);
+      else
+        sequenceColours.put(seq, col);
+    }
+
 
 }