1.1 compatible
[jalview.git] / src / jalview / analysis / Conservation.java
index f99661e..74381fd 100755 (executable)
@@ -27,14 +27,52 @@ public class Conservation {
   Vector sequences;\r
   int    start;\r
   int    end;\r
-\r
-  Vector total = new Vector();\r
-\r
-  String consString = "";\r
-\r
-  DrawableSequence consSequence;\r
-  Hashtable        propHash;\r
-  int              threshold;\r
+  Vector seqNums; // vector of int vectors where first is sequence checksum\r
+  int maxLength=0; //  used by quality calcs\r
+  boolean seqNumsChanged = false; // updated after any change via calcSeqNum;\r
+  private void calcSeqNums() {\r
+    for (int i=0; i<sequences.size(); i++) {\r
+      calcSeqNum(i);\r
+    }\r
+  }\r
+  private void calcSeqNum(int i) {\r
+    String sq=null; // for dumb jbuilder not-inited exception warning\r
+    int[] sqnum = null;\r
+    if (i>-1 && i<sequences.size()) {\r
+      sq = ((SequenceI)sequences.elementAt(i)).getSequence();\r
+      if (seqNums.size()<=i) {\r
+        seqNums.addElement(new int[sq.length()+1]);\r
+      }\r
+      if (sq.hashCode()!=((int[])seqNums.elementAt(i))[0])\r
+       {\r
+         int j, len;\r
+         seqNumsChanged = true;\r
+         sq = ((SequenceI) sequences.elementAt(i)).getSequence();\r
+         len = sq.length();\r
+         if (maxLength < len)\r
+           maxLength = len;\r
+         sqnum = new int[len + 1]; // better to always make a new array - sequence can change its length\r
+         sqnum[0] = sq.hashCode();\r
+         for (j = 1; j <= len; j++)\r
+         {\r
+           sqnum[j] = ( (Integer) jalview.schemes.ResidueProperties.aaHash.get(new\r
+               String(sq.substring(j - 1, j)))).intValue(); // yuk\r
+         }\r
+         seqNums.setElementAt(sqnum, i);\r
+       }\r
+     } else {\r
+       // JBPNote INFO level debug\r
+       System.out.println("calcSeqNum called with out of range sequence index for Alignment\n");\r
+     }\r
+   }\r
+   Vector total = new Vector();\r
+   public Vector quality;\r
+   public Double[] qualityRange = new Double[2];\r
+   String consString = "";\r
+\r
+   Sequence consSequence;\r
+   Hashtable        propHash;\r
+   int              threshold;\r
 \r
   String name = "";\r
 \r
@@ -45,6 +83,8 @@ public class Conservation {
     this.sequences = sequences;\r
     this.start     = start;\r
     this.end       = end;\r
+    seqNums = new Vector(sequences.size());\r
+    calcSeqNums();\r
   }\r
 \r
 \r
@@ -58,7 +98,8 @@ public class Conservation {
       residueHash = new Hashtable();\r
 \r
       for (int j=0; j < sequences.size(); 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
 \r
@@ -115,32 +156,74 @@ public class Conservation {
           }\r
         }\r
       }\r
+\r
       total.addElement(resultHash);\r
     }\r
   }\r
 \r
-  public int countGaps(int j) {\r
+  public int countGaps(int j)\r
+  {\r
     int count = 0;\r
 \r
-    for (int i = 0; i < sequences.size();i++) {\r
+    for (int i = 0; i < sequences.size();i++)\r
+    {\r
       if( j+1 > ((Sequence)sequences.elementAt(i)).getSequence().length())\r
       {  count++; continue;}\r
 \r
-      String tmp = ((Sequence)sequences.elementAt(i)).getSequence().substring(j,j+1);\r
-      if (tmp.equals(" ") || tmp.equals(".") || tmp.equals("-")) {\r
+      char c = ((Sequence)sequences.elementAt(i)).getSequence().charAt(j);\r
+      if (jalview.util.Comparison.isGap((c)))\r
         count++;\r
-      }\r
+\r
     }\r
     return count;\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
+  {\r
+    int count = 0;\r
+    int cons=0;\r
+    int nres = 0;\r
+    int[] r = new int[2];\r
+    char f='$';\r
+    for (int i = 0; i < sequences.size();i++)\r
+    {\r
+      if( j >= ((Sequence)sequences.elementAt(i)).getSequence().length())\r
+      {  count++;\r
+      continue;}\r
+\r
+      char c = ((Sequence)sequences.elementAt(i)).getSequence().charAt(j);\r
+      if (jalview.util.Comparison.isGap((c)))\r
+        count++;\r
+      else {\r
+        nres++;\r
+        if (nres==1) {\r
+          f = c;\r
+          cons++;\r
+        } else\r
+          if (f == c) {\r
+            cons++;\r
+          }\r
+      }\r
+    }\r
+    r[0] = (nres==cons) ? 1 : 0;\r
+    r[1] = count;\r
+\r
+\r
+    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 totGaps = countGaps(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
       {\r
         Hashtable resultHash = (Hashtable)total.elementAt(i-start);\r
@@ -169,9 +252,9 @@ public class Conservation {
         }\r
 \r
         if (count < 10)\r
-          consString = consString + String.valueOf(count);\r
+          consString = consString + String.valueOf(count);// Conserved props!=Identity\r
         else\r
-          consString = consString + "*";\r
+          consString = consString + ((gapcons[0]==1) ? "*" : "+");\r
 \r
       }\r
       else\r
@@ -180,11 +263,149 @@ public class Conservation {
       }\r
     }\r
 \r
-    consSequence = new DrawableSequence(name,consString,start,end);\r
+    consSequence = new Sequence(name,consString,start,end);\r
   }\r
 \r
-  public jalview.gui.DrawableSequence getConsSequence() {\r
+  public Sequence getConsSequence() {\r
     return consSequence;\r
   }\r
 \r
+  // From Alignment.java in jalview118\r
+\r
+  public void findQuality() {\r
+    findQuality(0,maxLength-1);\r
+  }\r
+\r
+  int[][] cons2;\r
+\r
+  private void percentIdentity2() {\r
+    calcSeqNums(); // updates maxLength, too.\r
+    if (cons2==null || seqNumsChanged) {\r
+      cons2 = new int[maxLength][24];\r
+      // Initialize the array\r
+      for (int j=0;j<24;j++) {\r
+        for (int i=0; i < maxLength;i++) {\r
+          cons2[i][j] = 0;\r
+        }\r
+      }\r
+\r
+      int sqnum[];\r
+      int j = 0;\r
+      while(j < sequences.size()) {\r
+        sqnum=(int[])seqNums.elementAt(j);\r
+        for (int i = 1; i < sqnum.length; i++) {\r
+          cons2[i-1][sqnum[i]]++;\r
+        }\r
+        for (int i=sqnum.length-1; i<maxLength; i++) {\r
+          cons2[i][23]++; // gap count\r
+        }\r
+        j++;\r
+      }\r
+\r
+      // unnecessary ?\r
+      /* for (int i=start; i <= end; i++) {\r
+           int max = -1000;\r
+    int maxi = -1;\r
+    int maxj = -1;\r
+\r
+    for (int j=0;j<24;j++) {\r
+        if (cons2[i][j] > max) {\r
+        max = cons2[i][j];\r
+        maxi = i;\r
+        maxj = j;\r
+      }\r
+\r
+    }\r
+  } */\r
+    }\r
+\r
+}\r
+\r
+\r
+public void findQuality(int start, int end) {\r
+    quality = new Vector();\r
+    double max = -10000;\r
+    String s = "";\r
+    int[][] BLOSUM62 = jalview.schemes.ResidueProperties.getBLOSUM62();\r
+    //Loop over columns // JBPNote Profiling info\r
+    //    long ts = System.currentTimeMillis();\r
+    //long te = System.currentTimeMillis();\r
+    percentIdentity2();\r
+\r
+    int size = seqNums.size();\r
+    int[] lengths = new int[size];\r
+\r
+    for (int 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
+      // First Xr = depends on column only\r
+      double x[] = new double[24];\r
+\r
+      for (int ii=0; ii < 24; ii++) {\r
+       x[ii] = 0;\r
+       try {\r
+         for (int i2=0; i2 < 24; i2++) {\r
+           x[ii]  += (double)cons2[j][i2] * BLOSUM62[ii][i2]+4;\r
+         }\r
+       } catch (Exception e) {\r
+         System.out.println("Exception : "  + e);\r
+       }\r
+       //System.out.println("X " + ii + " " + x[ii]);\r
+       x[ii] /= (size);\r
+       //System.out.println("X " + ii + " " + x[ii]);\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 =\r
+            (j<lengths[k])\r
+            ? ((int[]) seqNums.elementAt(k))[j+1]\r
+            : 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
+          try {\r
+            sr = (double)BLOSUM62[i][seqNum]+4;\r
+          } catch (Exception e) {\r
+            System.out.println("Exception in sr: " + e);\r
+          }\r
+          //Calculate X with another loop over residues\r
+\r
+          //  System.out.println("Xi " + i + " " + x[i] + " " + sr);\r
+          xx[i] = x[i] - sr;\r
+\r
+          tot += xx[i]*xx[i];\r
+        }\r
+        bigtot += Math.sqrt(tot);\r
+      }\r
+      // This is the quality for one column\r
+      if (max < bigtot) {max = bigtot;}\r
+      //      bigtot  = bigtot * (size-cons2[j][23])/size;\r
+\r
+      quality.addElement(new Double(bigtot));\r
+\r
+      s += "-";\r
+\r
+      // Need to normalize by gaps\r
+    }\r
+    double newmax=-10000;\r
+    for (int j=start; j <= end; j++) {\r
+     double tmp =  ((Double)quality.elementAt(j)).doubleValue();\r
+     tmp = (max - tmp)*(size-cons2[j][23])/size;\r
+     //     System.out.println(tmp+ " " + j);\r
+     quality.setElementAt(new Double(tmp),j);\r
+     if (tmp>newmax)\r
+       newmax = tmp;\r
+    }\r
+    //    System.out.println("Quality " + s);\r
+    qualityRange[0] = new Double(0);\r
+    qualityRange[1] = new Double(newmax);\r
+  }\r
+\r
+\r
 }\r