use fontchooser to change fonts
[jalview.git] / src / jalview / analysis / Conservation.java
index bfb2e00..b9b68ce 100755 (executable)
@@ -20,12 +20,17 @@ package jalview.analysis;
 \r
 import jalview.datamodel.*;\r
 \r
-import jalview.gui.*;\r
-\r
 import java.util.*;\r
 \r
 \r
-public class Conservation {\r
+/**\r
+ * Calculates conservation values for a given set of sequences\r
+ *\r
+ * @author $author$\r
+ * @version $Revision$\r
+ */\r
+public class Conservation\r
+{\r
     Vector sequences;\r
     int start;\r
     int end;\r
@@ -33,7 +38,11 @@ public class Conservation {
     int maxLength = 0; //  used by quality calcs\r
     boolean seqNumsChanged = false; // updated after any change via calcSeqNum;\r
     Vector total = new Vector();\r
+\r
+    /** Stores calculated quality values */\r
     public Vector quality;\r
+\r
+    /** Stores maximum and minimum values of quality values  */\r
     public Double[] qualityRange = new Double[2];\r
     String consString = "";\r
     Sequence consSequence;\r
@@ -42,8 +51,19 @@ public class Conservation {
     String name = "";\r
     int[][] cons2;\r
 \r
+    /**\r
+     * Creates a new Conservation object.\r
+     *\r
+     * @param name Name of conservation\r
+     * @param propHash DOCUMENT ME!\r
+     * @param threshold to count the residues in residueHash(). commonly used value is 3\r
+     * @param sequences sequences to be used in calculation\r
+     * @param start start residue position\r
+     * @param end end residue position\r
+     */\r
     public Conservation(String name, Hashtable propHash, int threshold,\r
-        Vector sequences, int start, int end) {\r
+        Vector sequences, int start, int end)\r
+    {\r
         this.name = name;\r
         this.propHash = propHash;\r
         this.threshold = threshold;\r
@@ -54,81 +74,118 @@ public class Conservation {
         calcSeqNums();\r
     }\r
 \r
-    private void calcSeqNums() {\r
-        for (int i = 0; i < sequences.size(); i++) {\r
+    /**\r
+     * DOCUMENT ME!\r
+     */\r
+    private void calcSeqNums()\r
+    {\r
+      int i=0, iSize=sequences.size();\r
+        for (i=0; i < iSize; i++)\r
+        {\r
             calcSeqNum(i);\r
         }\r
     }\r
 \r
-    private void calcSeqNum(int i) {\r
+    /**\r
+     * DOCUMENT ME!\r
+     *\r
+     * @param i DOCUMENT ME!\r
+     */\r
+    private void calcSeqNum(int i)\r
+    {\r
         String sq = null; // for dumb jbuilder not-inited exception warning\r
         int[] sqnum = null;\r
 \r
-        if ((i > -1) && (i < sequences.size())) {\r
+        if ((i > -1) && (i < sequences.size()))\r
+        {\r
             sq = ((SequenceI) sequences.elementAt(i)).getSequence();\r
 \r
-            if (seqNums.size() <= i) {\r
+            if (seqNums.size() <= i)\r
+            {\r
                 seqNums.addElement(new int[sq.length() + 1]);\r
             }\r
 \r
-            if (sq.hashCode() != ((int[]) seqNums.elementAt(i))[0]) {\r
+            if (sq.hashCode() != ((int[]) seqNums.elementAt(i))[0])\r
+            {\r
                 int j;\r
                 int len;\r
                 seqNumsChanged = true;\r
                 sq = ((SequenceI) sequences.elementAt(i)).getSequence();\r
                 len = sq.length();\r
 \r
-                if (maxLength < len) {\r
+                if (maxLength < len)\r
+                {\r
                     maxLength = len;\r
                 }\r
 \r
                 sqnum = new int[len + 1]; // better to always make a new array - sequence can change its length\r
                 sqnum[0] = sq.hashCode();\r
 \r
-                for (j = 1; j <= len; j++) {\r
-                    sqnum[j] = ((Integer) jalview.schemes.ResidueProperties.aaHash.get(new String(\r
-                                sq.substring(j - 1, j)))).intValue(); // yuk\r
+                for (j = 1; j <= len; j++)\r
+                {\r
+                    sqnum[j] = ((Integer) jalview.schemes.ResidueProperties.aaHash.get(String.valueOf(\r
+                                sq.charAt(j - 1)))).intValue(); // yuk - JBPNote - case taken care of in aaHash\r
                 }\r
 \r
                 seqNums.setElementAt(sqnum, i);\r
             }\r
-        } else {\r
+        }\r
+        else\r
+        {\r
             // JBPNote INFO level debug\r
             System.err.println(\r
                 "ERROR: calcSeqNum called with out of range sequence index for Alignment\n");\r
         }\r
     }\r
 \r
-    public void calculate() {\r
-        for (int i = start; i <= end; i++) {\r
-            Hashtable resultHash = null;\r
-            Hashtable residueHash = null;\r
-\r
+    /**\r
+     * Calculates the conservation values for given set of sequences\r
+     */\r
+    public void calculate()\r
+    {\r
+      Hashtable resultHash, residueHash, ht;\r
+      int count, thresh, j, jSize = sequences.size();\r
+      String sequence, res, type;\r
+      Enumeration enumeration, enumeration2;\r
+\r
+      for (int i = start; i <= end; i++)\r
+        {\r
             resultHash = new Hashtable();\r
             residueHash = new Hashtable();\r
 \r
-            for (int j = 0; j < sequences.size(); j++) {\r
+            for (j = 0; j < jSize; j++)\r
+            {\r
                 // JBPNote - have to make sure elements of the sequences vector\r
                 //  are tested like this everywhere...\r
-                if (sequences.elementAt(j) instanceof Sequence) {\r
-                    Sequence s = (Sequence) sequences.elementAt(j);\r
+                if (sequences.elementAt(j) instanceof Sequence)\r
+                {\r
+                    sequence = ((Sequence) sequences.elementAt(j)).getSequence();\r
 \r
-                    if (s.getLength() > i) {\r
-                        String res = s.getSequence().substring(i, i + 1);\r
+                    if (sequence.length() > i)\r
+                    {\r
+                        res = String.valueOf(Character.toUpperCase(sequence.charAt(i)));\r
 \r
-                        if (residueHash.containsKey(res)) {\r
-                            int count = ((Integer) residueHash.get(res)).intValue();\r
+                        if (residueHash.containsKey(res))\r
+                        {\r
+                            count = ((Integer) residueHash.get(res)).intValue();\r
                             count++;\r
                             residueHash.put(res, new Integer(count));\r
-                        } else {\r
+                        }\r
+                        else\r
+                        {\r
                             residueHash.put(res, new Integer(1));\r
                         }\r
-                    } else {\r
-                        if (residueHash.containsKey("-")) {\r
-                            int count = ((Integer) residueHash.get("-")).intValue();\r
+                    }\r
+                    else\r
+                    {\r
+                        if (residueHash.containsKey("-"))\r
+                        {\r
+                            count = ((Integer) residueHash.get("-")).intValue();\r
                             count++;\r
                             residueHash.put("-", new Integer(count));\r
-                        } else {\r
+                        }\r
+                        else\r
+                        {\r
                             residueHash.put("-", new Integer(1));\r
                         }\r
                     }\r
@@ -136,31 +193,40 @@ public class Conservation {
             }\r
 \r
             //What is the count threshold to count the residues in residueHash()\r
-            int thresh = (threshold * (sequences.size())) / 100;\r
+            thresh = (threshold * (sequences.size())) / 100;\r
 \r
             //loop over all the found residues\r
-            Enumeration e = residueHash.keys();\r
+            enumeration = residueHash.keys();\r
 \r
-            while (e.hasMoreElements()) {\r
-                String res = (String) e.nextElement();\r
+            while (enumeration.hasMoreElements())\r
+            {\r
+                res = (String) enumeration.nextElement();\r
 \r
-                if (((Integer) residueHash.get(res)).intValue() > thresh) {\r
+                if (((Integer) residueHash.get(res)).intValue() > thresh)\r
+                {\r
                     //Now loop over the properties\r
-                    Enumeration e2 = propHash.keys();\r
+                    enumeration2 = propHash.keys();\r
 \r
-                    while (e2.hasMoreElements()) {\r
-                        String type = (String) e2.nextElement();\r
-                        Hashtable ht = (Hashtable) propHash.get(type);\r
+                    while (enumeration2.hasMoreElements())\r
+                    {\r
+                        type = (String) enumeration2.nextElement();\r
+                        ht = (Hashtable) propHash.get(type);\r
 \r
                         //Have we ticked this before?\r
-                        if (!resultHash.containsKey(type)) {\r
-                            if (ht.containsKey(res)) {\r
+                        if (!resultHash.containsKey(type))\r
+                        {\r
+                            if (ht.containsKey(res))\r
+                            {\r
                                 resultHash.put(type, ht.get(res));\r
-                            } else {\r
+                            }\r
+                            else\r
+                            {\r
                                 resultHash.put(type, ht.get("-"));\r
                             }\r
-                        } else if (((Integer) resultHash.get(type)).equals(\r
-                                    (Integer) ht.get(res)) == false) {\r
+                        }\r
+                        else if (((Integer) resultHash.get(type)).equals(\r
+                                    (Integer) ht.get(res)) == false)\r
+                        {\r
                             resultHash.put(type, new Integer(-1));\r
                         }\r
                     }\r
@@ -171,56 +237,46 @@ public class Conservation {
         }\r
     }\r
 \r
-    public int countGaps(int j) {\r
-        int count = 0;\r
-\r
-        for (int i = 0; i < sequences.size(); i++) {\r
-            if ((j + 1) > ((Sequence) sequences.elementAt(i)).getSequence()\r
-                               .length()) {\r
-                count++;\r
-\r
-                continue;\r
-            }\r
-\r
-            char c = ((Sequence) sequences.elementAt(i)).getSequence().charAt(j);\r
-\r
-            if (jalview.util.Comparison.isGap((c))) {\r
-                count++;\r
-            }\r
-        }\r
-\r
-        return count;\r
-    }\r
 \r
     /***\r
      * countConsNGaps\r
      * returns gap count in int[0], and conserved residue count in int[1]\r
      */\r
-    public int[] countConsNGaps(int j) {\r
+    public int[] countConsNGaps(int j)\r
+    {\r
         int count = 0;\r
         int cons = 0;\r
         int nres = 0;\r
         int[] r = new int[2];\r
         char f = '$';\r
+        int i, iSize = sequences.size();\r
+        char c;\r
 \r
-        for (int i = 0; i < sequences.size(); i++) {\r
-            if (j >= ((Sequence) sequences.elementAt(i)).getSequence().length()) {\r
+        for (i = 0; i < iSize; i++)\r
+        {\r
+            if (j >= ((Sequence) sequences.elementAt(i)).getLength())\r
+            {\r
                 count++;\r
-\r
                 continue;\r
             }\r
 \r
-            char c = ((Sequence) sequences.elementAt(i)).getSequence().charAt(j);\r
+            c = ((Sequence) sequences.elementAt(i)).getCharAt(j); // gaps do not have upper/lower case\r
 \r
-            if (jalview.util.Comparison.isGap((c))) {\r
+            if (jalview.util.Comparison.isGap((c)))\r
+            {\r
                 count++;\r
-            } else {\r
+            }\r
+            else\r
+            {\r
                 nres++;\r
 \r
-                if (nres == 1) {\r
+                if (nres == 1)\r
+                {\r
                     f = c;\r
                     cons++;\r
-                } else if (f == c) {\r
+                }\r
+                else if (f == c)\r
+                {\r
                     cons++;\r
                 }\r
             }\r
@@ -232,70 +288,110 @@ public class Conservation {
         return r;\r
     }\r
 \r
-    public void verdict(boolean consflag, float percentageGaps) {\r
-        String consString = "";\r
-\r
-        for (int i = start; i <= end; i++) {\r
-            int[] gapcons = countConsNGaps(i);\r
-            boolean cons = (gapcons[0] == 1) ? true : false;\r
-            int totGaps = gapcons[1];\r
-            float pgaps = ((float) totGaps * 100) / (float) sequences.size();\r
-\r
-            //      System.out.println("percentage gaps = "+pgaps+"\n");\r
-            if (percentageGaps > pgaps) {\r
-                Hashtable resultHash = (Hashtable) total.elementAt(i - start);\r
+    /**\r
+     * Calculates the conservation sequence\r
+     *\r
+     * @param consflag if true, poitiveve conservation; false calculates negative conservation\r
+     * @param percentageGaps commonly used value is 25\r
+     */\r
+    public void verdict(boolean consflag, float percentageGaps)\r
+    {\r
+        StringBuffer consString = new StringBuffer();\r
+        String type;\r
+        Integer result;\r
+        int[] gapcons;\r
+        int totGaps, count;\r
+        float pgaps;\r
+        Hashtable resultHash ;\r
+        Enumeration enumeration;\r
+\r
+\r
+        for (int i = start; i <= end; i++)\r
+        {\r
+            gapcons = countConsNGaps(i);\r
+            totGaps = gapcons[1];\r
+            pgaps = ((float) totGaps * 100) / (float) sequences.size();\r
+\r
+            if (percentageGaps > pgaps)\r
+            {\r
+                resultHash = (Hashtable) total.elementAt(i - start);\r
 \r
                 //Now find the verdict\r
-                int count = 0;\r
-                Enumeration e3 = resultHash.keys();\r
+                count = 0;\r
+                enumeration = resultHash.keys();\r
 \r
-                while (e3.hasMoreElements()) {\r
-                    String type = (String) e3.nextElement();\r
-                    Integer result = (Integer) resultHash.get(type);\r
+                while (enumeration.hasMoreElements())\r
+                {\r
+                   type = (String) enumeration.nextElement();\r
+                   result = (Integer) resultHash.get(type);\r
 \r
                     //Do we want to count +ve conservation or +ve and -ve cons.?\r
-                    if (consflag) {\r
-                        if (result.intValue() == 1) {\r
+                    if (consflag)\r
+                    {\r
+                        if (result.intValue() == 1)\r
+                        {\r
                             count++;\r
                         }\r
-                    } else {\r
-                        if (result.intValue() != -1) {\r
+                    }\r
+                    else\r
+                    {\r
+                        if (result.intValue() != -1)\r
+                        {\r
                             count++;\r
                         }\r
                     }\r
                 }\r
 \r
-                if (count < 10) {\r
-                    consString = consString + String.valueOf(count); // Conserved props!=Identity\r
-                } else {\r
-                    consString = consString + ((gapcons[0] == 1) ? "*" : "+");\r
+                if (count < 10)\r
+                {\r
+                    consString.append(count); // Conserved props!=Identity\r
+                }\r
+                else\r
+                {\r
+                    consString.append((gapcons[0] == 1) ? "*" : "+");\r
                 }\r
-            } else {\r
-                consString = consString + "-";\r
+            }\r
+            else\r
+            {\r
+                consString.append("-");\r
             }\r
         }\r
 \r
-        consSequence = new Sequence(name, consString, start, end);\r
+        consSequence = new Sequence(name, consString.toString(), start, end);\r
     }\r
 \r
-    public Sequence getConsSequence() {\r
+    /**\r
+     *\r
+     *\r
+     * @return Conservation sequence\r
+     */\r
+    public Sequence getConsSequence()\r
+    {\r
         return consSequence;\r
     }\r
 \r
     // From Alignment.java in jalview118\r
-    public void findQuality() {\r
+    public void findQuality()\r
+    {\r
         findQuality(0, maxLength - 1);\r
     }\r
 \r
-    private void percentIdentity2() {\r
+    /**\r
+     * DOCUMENT ME!\r
+     */\r
+    private void percentIdentity2()\r
+    {\r
         calcSeqNums(); // updates maxLength, too.\r
 \r
-        if ((cons2 == null) || seqNumsChanged) {\r
+        if ((cons2 == null) || seqNumsChanged)\r
+        {\r
             cons2 = new int[maxLength][24];\r
 \r
             // Initialize the array\r
-            for (int j = 0; j < 24; j++) {\r
-                for (int i = 0; i < maxLength; i++) {\r
+            for (int j = 0; j < 24; j++)\r
+            {\r
+                for (int i = 0; i < maxLength; i++)\r
+                {\r
                     cons2[i][j] = 0;\r
                 }\r
             }\r
@@ -303,14 +399,17 @@ public class Conservation {
             int[] sqnum;\r
             int j = 0;\r
 \r
-            while (j < sequences.size()) {\r
+            while (j < sequences.size())\r
+            {\r
                 sqnum = (int[]) seqNums.elementAt(j);\r
 \r
-                for (int i = 1; i < sqnum.length; i++) {\r
+                for (int i = 1; i < sqnum.length; i++)\r
+                {\r
                     cons2[i - 1][sqnum[i]]++;\r
                 }\r
 \r
-                for (int i = sqnum.length - 1; i < maxLength; i++) {\r
+                for (int i = sqnum.length - 1; i < maxLength; i++)\r
+                {\r
                     cons2[i][23]++; // gap count\r
                 }\r
 \r
@@ -336,11 +435,17 @@ public class Conservation {
         }\r
     }\r
 \r
-    public void findQuality(int start, int end) {\r
+    /**\r
+     * Calculates the quality of the set of sequences\r
+     *\r
+     * @param start Start residue\r
+     * @param end End residue\r
+     */\r
+    public void findQuality(int start, int end)\r
+    {\r
         quality = new Vector();\r
 \r
         double max = -10000;\r
-        String s = "";\r
         int[][] BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62();\r
 \r
         //Loop over columns // JBPNote Profiling info\r
@@ -350,25 +455,35 @@ public class Conservation {
 \r
         int size = seqNums.size();\r
         int[] lengths = new int[size];\r
+        double tot, bigtot, sr, tmp;\r
+        double [] x, xx;\r
+        int l, j, i, ii, seqNum;\r
 \r
-        for (int l = 0; l < size; l++)\r
+        for (l = 0; l < size; l++)\r
             lengths[l] = ((int[]) seqNums.elementAt(l)).length - 1;\r
 \r
-        for (int j = start; j <= end; j++) {\r
-            double bigtot = 0;\r
+\r
+        for (j = start; j <= end; j++)\r
+        {\r
+            bigtot = 0;\r
 \r
             // First Xr = depends on column only\r
-            double[] x = new double[24];\r
+            x = new double[24];\r
 \r
-            for (int ii = 0; ii < 24; ii++) {\r
+            for (ii = 0; ii < 24; ii++)\r
+            {\r
                 x[ii] = 0;\r
 \r
-                try {\r
-                    for (int i2 = 0; i2 < 24; i2++) {\r
+                try\r
+                {\r
+                    for (int i2 = 0; i2 < 24; i2++)\r
+                    {\r
                         x[ii] += (((double) cons2[j][i2] * BLOSUM62[ii][i2]) +\r
                         4);\r
                     }\r
-                } catch (Exception e) {\r
+                }\r
+                catch (Exception e)\r
+                {\r
                     System.err.println("Exception during quality calculation.");\r
                     e.printStackTrace();\r
                 }\r
@@ -380,19 +495,24 @@ public class Conservation {
             }\r
 \r
             // Now calculate D for each position and sum\r
-            for (int k = 0; k < size; k++) {\r
-                double tot = 0;\r
-                double[] xx = new double[24];\r
-                int seqNum = (j < lengths[k])\r
+            for (int k = 0; k < size; k++)\r
+            {\r
+                tot = 0;\r
+                xx = new double[24];\r
+                seqNum = (j < lengths[k])\r
                     ? ((int[]) seqNums.elementAt(k))[j + 1] : 23; // Sequence, or gap at the end\r
 \r
                 // This is a loop over r\r
-                for (int i = 0; i < 23; i++) {\r
-                    double sr = 0;\r
+                for (i = 0; i < 23; i++)\r
+                {\r
+                    sr = 0;\r
 \r
-                    try {\r
+                    try\r
+                    {\r
                         sr = (double) BLOSUM62[i][seqNum] + 4;\r
-                    } catch (Exception e) {\r
+                    }\r
+                    catch (Exception e)\r
+                    {\r
                         System.out.println("Exception in sr: " + e);\r
                         e.printStackTrace();\r
                     }\r
@@ -408,28 +528,30 @@ public class Conservation {
             }\r
 \r
             // This is the quality for one column\r
-            if (max < bigtot) {\r
+            if (max < bigtot)\r
+            {\r
                 max = bigtot;\r
             }\r
 \r
             //      bigtot  = bigtot * (size-cons2[j][23])/size;\r
             quality.addElement(new Double(bigtot));\r
 \r
-            s += "-";\r
 \r
             // Need to normalize by gaps\r
         }\r
 \r
         double newmax = -10000;\r
 \r
-        for (int j = start; j <= end; j++) {\r
-            double tmp = ((Double) quality.elementAt(j)).doubleValue();\r
+        for (j = start; j <= end; j++)\r
+        {\r
+            tmp = ((Double) quality.elementAt(j)).doubleValue();\r
             tmp = ((max - tmp) * (size - cons2[j][23])) / size;\r
 \r
             //     System.out.println(tmp+ " " + j);\r
             quality.setElementAt(new Double(tmp), j);\r
 \r
-            if (tmp > newmax) {\r
+            if (tmp > newmax)\r
+            {\r
                 newmax = tmp;\r
             }\r
         }\r