validCharWidth moved to viewport
[jalview.git] / src / jalview / gui / AlignViewport.java
index e98fb9f..d4ba5ea 100755 (executable)
@@ -1,4 +1,4 @@
-/*\r
+ /*\r
  * Jalview - A Sequence Alignment Editor and Viewer\r
  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
  *\r
@@ -43,7 +43,7 @@ public class AlignViewport
     int endRes;\r
     int startSeq;\r
     int endSeq;\r
-    boolean showFullId = false;\r
+    boolean showJVSuffix = true;\r
     boolean showText = true;\r
     boolean showColourText = false;\r
     boolean showBoxes = true;\r
@@ -61,9 +61,9 @@ public class AlignViewport
     SequenceGroup selectionGroup;\r
     int charHeight;\r
     int charWidth;\r
-    int chunkWidth;\r
-    int chunkHeight;\r
-    Font font = new Font("SansSerif", Font.PLAIN, 10);\r
+    boolean validCharWidth;\r
+    int wrappedWidth;\r
+    Font font;\r
     AlignmentI alignment;\r
     ColumnSelection colSel = new ColumnSelection();\r
     int threshold;\r
@@ -72,12 +72,22 @@ public class AlignViewport
     boolean scaleAboveWrapped = false;\r
     boolean scaleLeftWrapped = true;\r
     boolean scaleRightWrapped = true;\r
+    boolean hasHiddenColumns = false;\r
+    boolean hasHiddenRows = false;\r
+\r
+    boolean cursorMode = false;\r
+\r
+    // The following vector holds the features which are\r
+    // currently visible, in the correct order or rendering\r
+    Hashtable featuresDisplayed = null;\r
+\r
 \r
     /** DOCUMENT ME!! */\r
     public Vector vconsensus;\r
     AlignmentAnnotation consensus;\r
     AlignmentAnnotation conservation;\r
     AlignmentAnnotation quality;\r
+    boolean autoCalculateConsensus = true;\r
 \r
     /** DOCUMENT ME!! */\r
     public int ConsPercGaps = 25; // JBPNote : This should be a scalable property!\r
@@ -87,6 +97,19 @@ public class AlignViewport
 \r
     boolean ignoreGapsInConsensusCalculation = false;\r
 \r
+    boolean isDataset = false;\r
+\r
+    boolean antiAlias = false;\r
+\r
+    boolean padGaps = false;\r
+\r
+\r
+    public AlignViewport(AlignmentI al, boolean dataset)\r
+    {\r
+      isDataset = dataset;\r
+      setAlignment(al);\r
+      init();\r
+    }\r
     /**\r
      * Creates a new AlignViewport object.\r
      *\r
@@ -95,26 +118,29 @@ public class AlignViewport
     public AlignViewport(AlignmentI al)\r
     {\r
         setAlignment(al);\r
+        init();\r
+    }\r
+\r
+    void init()\r
+    {\r
         this.startRes = 0;\r
-        this.endRes = al.getWidth() - 1;\r
+        this.endRes = alignment.getWidth() - 1;\r
         this.startSeq = 0;\r
-        this.endSeq = al.getHeight() - 1;\r
+        this.endSeq = alignment.getHeight() - 1;\r
 \r
-        updateFromPreferences();\r
-    }\r
+      antiAlias = Cache.getDefault("ANTI_ALIAS", false);\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     */\r
-    public void updateFromPreferences()\r
-    {\r
-      showFullId = Cache.getDefault("SHOW_FULL_ID", true);\r
+      showJVSuffix = Cache.getDefault("SHOW_JVSUFFIX", true);\r
       showAnnotation = Cache.getDefault("SHOW_ANNOTATIONS", true);\r
       showConservation = Cache.getDefault("SHOW_CONSERVATION", true);\r
 \r
       showQuality = Cache.getDefault("SHOW_QUALITY", true);\r
       showIdentity = Cache.getDefault("SHOW_IDENTITY", true);\r
 \r
+      autoCalculateConsensus = Cache.getDefault("AUTO_CALC_CONSENSUS", true);\r
+\r
+      padGaps = Cache.getDefault("PAD_GAPS", false);\r
+\r
        String fontName = Cache.getDefault("FONT_NAME", "SansSerif");\r
        String fontStyle = Cache.getDefault("FONT_STYLE", Font.PLAIN + "") ;\r
        String fontSize = Cache.getDefault("FONT_SIZE", "10");\r
@@ -138,8 +164,11 @@ public class AlignViewport
 \r
         // We must set conservation and consensus before setting colour,\r
         // as Blosum and Clustal require this to be done\r
-        updateConservation();\r
-        updateConsensus();\r
+        if(vconsensus==null && !isDataset)\r
+        {\r
+          updateConservation();\r
+          updateConsensus();\r
+        }\r
 \r
         if (jalview.bin.Cache.getProperty("DEFAULT_COLOUR") != null)\r
         {\r
@@ -166,19 +195,28 @@ public class AlignViewport
      *\r
      * @param b DOCUMENT ME!\r
      */\r
-    public void showSequenceFeatures(boolean b)\r
+    public void setShowSequenceFeatures(boolean b)\r
     {\r
         showSequenceFeatures = b;\r
     }\r
 \r
+    public boolean getShowSequenceFeatures()\r
+    {\r
+      return showSequenceFeatures;\r
+    }\r
+\r
     /**\r
      * DOCUMENT ME!\r
      */\r
     public void updateConservation()\r
     {\r
+      if(alignment.isNucleotide())\r
+          return;\r
+\r
+      try{\r
         Conservation cons = new jalview.analysis.Conservation("All",\r
-                jalview.schemes.ResidueProperties.propHash, 3,\r
-                alignment.getSequences(), 0, alignment.getWidth() - 1);\r
+            jalview.schemes.ResidueProperties.propHash, 3,\r
+            alignment.getSequences(), 0, alignment.getWidth() - 1);\r
         cons.calculate();\r
         cons.verdict(false, ConsPercGaps);\r
         cons.findQuality();\r
@@ -207,71 +245,96 @@ public class AlignViewport
 \r
         for (int i = 0; i < alWidth; i++)\r
         {\r
-            float value = 0;\r
-\r
-            try\r
+          float value = 0;\r
+\r
+          try\r
+          {\r
+            value = Integer.parseInt(sequence.charAt(i) + "");\r
+          }\r
+          catch (Exception ex)\r
+          {\r
+            if (sequence.charAt(i) == '*')\r
             {\r
-                value = Integer.parseInt(sequence.charAt(i) + "");\r
+              value = 11;\r
             }\r
-            catch (Exception ex)\r
-            {\r
-                if (sequence.charAt(i) == '*')\r
-                {\r
-                    value = 11;\r
-                }\r
 \r
-                if (sequence.charAt(i) == '+')\r
-                {\r
-                    value = 10;\r
-                }\r
+            if (sequence.charAt(i) == '+')\r
+            {\r
+              value = 10;\r
             }\r
-\r
-            float vprop = value - min;\r
-            vprop /= max;\r
-            annotations[i] = new Annotation(sequence.charAt(i) + "",\r
-                    String.valueOf(value), ' ', value,\r
-                    new Color(minR + (maxR * vprop), minG + (maxG * vprop),\r
-                        minB + (maxB * vprop)));\r
-\r
-            // Quality calc\r
-            value = ((Double) cons.quality.get(i)).floatValue();\r
-            vprop = value - qmin;\r
-            vprop /= qmax;\r
-            qannotations[i] = new Annotation(" ", String.valueOf(value), ' ',\r
-                    value,\r
-                    new Color(minR + (maxR * vprop), minG + (maxG * vprop),\r
-                        minB + (maxB * vprop)));\r
+          }\r
+\r
+          float vprop = value - min;\r
+          vprop /= max;\r
+          annotations[i] = new Annotation(sequence.charAt(i) + "",\r
+                                          String.valueOf(value), ' ', value,\r
+                                          new Color(minR + (maxR * vprop),\r
+              minG + (maxG * vprop),\r
+              minB + (maxB * vprop)));\r
+\r
+          // Quality calc\r
+          value = ( (Double) cons.quality.get(i)).floatValue();\r
+          vprop = value - qmin;\r
+          vprop /= qmax;\r
+          qannotations[i] = new Annotation(" ", String.valueOf(value), ' ',\r
+                                           value,\r
+                                           new Color(minR + (maxR * vprop),\r
+              minG + (maxG * vprop),\r
+              minB + (maxB * vprop)));\r
         }\r
 \r
         if (conservation == null)\r
         {\r
-            conservation = new AlignmentAnnotation("Conservation",\r
-                    "Conservation of total alignment less than " +\r
-                    ConsPercGaps + "% gaps", annotations, 0f, // cons.qualityRange[0].floatValue(),\r
-                    11f, // cons.qualityRange[1].floatValue()\r
-                    1);\r
-\r
-            if (showConservation)\r
-            {\r
-                alignment.addAnnotation(conservation);\r
-            }\r
-\r
-            quality = new AlignmentAnnotation("Quality",\r
-                    "Alignment Quality based on Blosum62 scores", qannotations,\r
-                    cons.qualityRange[0].floatValue(),\r
-                    cons.qualityRange[1].floatValue(), 1);\r
-\r
-            if (showQuality)\r
-            {\r
-                alignment.addAnnotation(quality);\r
-            }\r
+          conservation = new AlignmentAnnotation("Conservation",\r
+                                                 "Conservation of total alignment less than " +\r
+                                                 ConsPercGaps + "% gaps",\r
+                                                 annotations, 0f, // cons.qualityRange[0].floatValue(),\r
+                                                 11f, // cons.qualityRange[1].floatValue()\r
+                                                 AlignmentAnnotation.BAR_GRAPH);\r
+\r
+          if (showConservation)\r
+          {\r
+            alignment.addAnnotation(conservation);\r
+          }\r
+\r
+          quality = new AlignmentAnnotation("Quality",\r
+                                            "Alignment Quality based on Blosum62 scores",\r
+                                            qannotations,\r
+                                            cons.qualityRange[0].floatValue(),\r
+                                            cons.qualityRange[1].floatValue(),\r
+                                            AlignmentAnnotation.BAR_GRAPH);\r
+\r
+          if (showQuality)\r
+          {\r
+            alignment.addAnnotation(quality);\r
+          }\r
         }\r
         else\r
         {\r
-            conservation.annotations = annotations;\r
-            quality.annotations = qannotations;\r
-            quality.graphMax = cons.qualityRange[1].floatValue();\r
+          conservation.annotations = annotations;\r
+          quality.annotations = qannotations;\r
+          quality.graphMax = cons.qualityRange[1].floatValue();\r
         }\r
+      }\r
+      catch (OutOfMemoryError error)\r
+      {\r
+        javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+        {\r
+          public void run()\r
+          {\r
+            javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                "Out of memory calculating conservation!!"\r
+                +\r
+                "\nSee help files for increasing Java Virtual Machine memory."\r
+                , "Out of memory",\r
+                javax.swing.JOptionPane.WARNING_MESSAGE);\r
+          }\r
+        });\r
+\r
+        System.out.println("Conservation calculation: " + error);\r
+        System.gc();\r
+\r
+      }\r
     }\r
 \r
     /**\r
@@ -279,6 +342,7 @@ public class AlignViewport
      */\r
     public void updateConsensus()\r
     {\r
+      try{\r
         Annotation[] annotations = new Annotation[alignment.getWidth()];\r
 \r
         // this routine prevents vconsensus becoming a new object each time\r
@@ -286,63 +350,84 @@ public class AlignViewport
         // and PID colouring of alignment\r
         if (vconsensus == null)\r
         {\r
-            vconsensus = alignment.getAAFrequency();\r
+          vconsensus = alignment.getAAFrequency();\r
         }\r
         else\r
         {\r
-            Vector temp = alignment.getAAFrequency();\r
-            vconsensus.clear();\r
+          Vector temp = alignment.getAAFrequency();\r
+          vconsensus.clear();\r
 \r
-            Enumeration e = temp.elements();\r
+          Enumeration e = temp.elements();\r
 \r
-            while (e.hasMoreElements())\r
-            {\r
-                vconsensus.add(e.nextElement());\r
-            }\r
+          while (e.hasMoreElements())\r
+          {\r
+            vconsensus.add(e.nextElement());\r
+          }\r
         }\r
 \r
         Hashtable hash = null;\r
 \r
         for (int i = 0; i < alignment.getWidth(); i++)\r
         {\r
-            hash = (Hashtable) vconsensus.elementAt(i);\r
+          hash = (Hashtable) vconsensus.elementAt(i);\r
 \r
-            float value = 0;\r
-            if(ignoreGapsInConsensusCalculation)\r
-              value = ((Float)hash.get("pid_nogaps")).floatValue();\r
-            else\r
-              value = ((Float)hash.get("pid_gaps")).floatValue();\r
+          float value = 0;\r
+          if (ignoreGapsInConsensusCalculation)\r
+            value = ( (Float) hash.get("pid_nogaps")).floatValue();\r
+          else\r
+            value = ( (Float) hash.get("pid_gaps")).floatValue();\r
 \r
-            String maxRes = hash.get("maxResidue").toString();\r
-            String mouseOver = hash.get("maxResidue")+" ";\r
+          String maxRes = hash.get("maxResidue").toString();\r
+          String mouseOver = hash.get("maxResidue") + " ";\r
 \r
-            if (maxRes.length() > 1)\r
-            {\r
-                mouseOver = "[" + maxRes + "] ";\r
-                maxRes = "+";\r
-            }\r
+          if (maxRes.length() > 1)\r
+          {\r
+            mouseOver = "[" + maxRes + "] ";\r
+            maxRes = "+";\r
+          }\r
 \r
-            mouseOver += ((int) value + "%");\r
-            annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
+          mouseOver += ( (int) value + "%");\r
+          annotations[i] = new Annotation(maxRes, mouseOver, ' ', value);\r
         }\r
 \r
         if (consensus == null)\r
         {\r
-            consensus = new AlignmentAnnotation("Consensus", "PID",\r
-                    annotations, 0f, 100f, 1);\r
+          consensus = new AlignmentAnnotation("Consensus", "PID",\r
+                                              annotations, 0f, 100f,AlignmentAnnotation.BAR_GRAPH);\r
 \r
-            if (showIdentity)\r
-            {\r
-                alignment.addAnnotation(consensus);\r
-            }\r
+          if (showIdentity)\r
+          {\r
+            alignment.addAnnotation(consensus);\r
+          }\r
         }\r
         else\r
         {\r
-            consensus.annotations = annotations;\r
+          consensus.annotations = annotations;\r
         }\r
 \r
-        if(globalColourScheme!=null)\r
+        if (globalColourScheme != null)\r
           globalColourScheme.setConsensus(vconsensus);\r
+\r
+      }catch(OutOfMemoryError error)\r
+      {\r
+        javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+        {\r
+          public void run()\r
+          {\r
+            javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                "Out of memory calc45ulating consensus!!"\r
+                +\r
+                "\nSee help files for increasing Java Virtual Machine memory."\r
+                , "Out of memory",\r
+                javax.swing.JOptionPane.WARNING_MESSAGE);\r
+          }\r
+        });\r
+\r
+\r
+        System.out.println("Consensus calculation: " + error);\r
+        System.gc();\r
+      }\r
+\r
     }\r
 \r
     /**\r
@@ -540,6 +625,7 @@ public class AlignViewport
         java.awt.FontMetrics fm = c.getFontMetrics(font);\r
         setCharHeight(fm.getHeight());\r
         setCharWidth(fm.charWidth('M'));\r
+        validCharWidth = true;\r
     }\r
 \r
     /**\r
@@ -597,9 +683,9 @@ public class AlignViewport
      *\r
      * @param w DOCUMENT ME!\r
      */\r
-    public void setChunkWidth(int w)\r
+    public void setWrappedWidth(int w)\r
     {\r
-        this.chunkWidth = w;\r
+        this.wrappedWidth = w;\r
     }\r
 \r
     /**\r
@@ -607,30 +693,11 @@ public class AlignViewport
      *\r
      * @return DOCUMENT ME!\r
      */\r
-    public int getChunkWidth()\r
+    public int getWrappedWidth()\r
     {\r
-        return chunkWidth;\r
+        return wrappedWidth;\r
     }\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param h DOCUMENT ME!\r
-     */\r
-    public void setChunkHeight(int h)\r
-    {\r
-        this.chunkHeight = h;\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @return DOCUMENT ME!\r
-     */\r
-    public int getChunkHeight()\r
-    {\r
-        return chunkHeight;\r
-    }\r
 \r
     /**\r
      * DOCUMENT ME!\r
@@ -851,15 +918,6 @@ public class AlignViewport
         return colSel;\r
     }\r
 \r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param height DOCUMENT ME!\r
-     */\r
-    public void resetSeqLimits(int height)\r
-    {\r
-        setEndSeq(height / getCharHeight());\r
-    }\r
 \r
     /**\r
      * DOCUMENT ME!\r
@@ -906,9 +964,9 @@ public class AlignViewport
      *\r
      * @return DOCUMENT ME!\r
      */\r
-    public boolean getShowFullId()\r
+    public boolean getShowJVSuffix()\r
     {\r
-        return showFullId;\r
+        return showJVSuffix;\r
     }\r
 \r
     /**\r
@@ -916,11 +974,12 @@ public class AlignViewport
      *\r
      * @param b DOCUMENT ME!\r
      */\r
-    public void setShowFullId(boolean b)\r
+    public void setShowJVSuffix(boolean b)\r
     {\r
-        showFullId = b;\r
+        showJVSuffix = b;\r
     }\r
 \r
+\r
     /**\r
      * DOCUMENT ME!\r
      *\r
@@ -1002,7 +1061,7 @@ public class AlignViewport
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
+     * Property change listener for changes in alignment\r
      *\r
      * @param listener DOCUMENT ME!\r
      */\r
@@ -1024,7 +1083,7 @@ public class AlignViewport
     }\r
 \r
     /**\r
-     * DOCUMENT ME!\r
+     * Property change listener for changes in alignment\r
      *\r
      * @param prop DOCUMENT ME!\r
      * @param oldvalue DOCUMENT ME!\r
@@ -1039,18 +1098,43 @@ public class AlignViewport
     {\r
       ignoreGapsInConsensusCalculation = b;\r
       updateConsensus();\r
-      if(globalColourScheme!=null && globalColourScheme instanceof ResidueColourScheme)\r
+      if(globalColourScheme!=null)\r
       {\r
-        ((ResidueColourScheme) globalColourScheme).setThreshold(\r
-            ((ResidueColourScheme) globalColourScheme).getThreshold(), ignoreGapsInConsensusCalculation);\r
-\r
-\r
+        globalColourScheme.setThreshold(globalColourScheme.getThreshold(), ignoreGapsInConsensusCalculation);\r
       }\r
-\r
     }\r
 \r
     public boolean getIgnoreGapsConsensus()\r
     {\r
      return ignoreGapsInConsensusCalculation;\r
     }\r
+\r
+    public void setDataset(boolean b)\r
+    {\r
+      isDataset = b;\r
+    }\r
+\r
+    public boolean isDataset()\r
+    {\r
+      return isDataset;\r
+    }\r
+\r
+    public void hideSequence(SequenceI seq)\r
+    {\r
+      alignment.getHiddenSequences().hideSequence(seq);\r
+      hasHiddenRows = true;\r
+    }\r
+\r
+    public void showSequence(int index)\r
+    {\r
+      alignment.getHiddenSequences().showSequence(index);\r
+\r
+      if(alignment.getHiddenSequences().getSize()<1)\r
+        hasHiddenRows = false;\r
+    }\r
+\r
+    public int adjustForHiddenSeqs(int alignmentIndex)\r
+    {\r
+      return alignment.getHiddenSequences().adjustForHiddenSeqs(alignmentIndex);\r
+    }\r
 }\r