JAL-891; structure logo is adapted to two char's; Missing: calculate
[jalview.git] / src / jalview / analysis / StructureFrequency.java
index 29c0a0b..b9c61c2 100644 (file)
@@ -181,6 +181,15 @@ public class StructureFrequency
     }
   }
   
+  public static int findPair(SequenceFeature[] pairs,int indice){
+         for(int i=0; i<pairs.length; i++){
+                 if(pairs[i].getBegin()==indice){
+                         return pairs[i].getEnd();
+                 }
+         }
+         return -1;
+  }
+  
   /**
    * Method to calculate a 'base pair consensus row', very similar 
    * to nucleotide consensus but takes into account a given structure
@@ -193,7 +202,7 @@ public class StructureFrequency
    */
   public static final void calculate(SequenceI[] sequences, int start,
                  int end, Hashtable[] result, boolean profile, AlignmentAnnotation rnaStruc){
-
+         //System.out.println("StructureFrequency.calculateNEW4");
          Hashtable residueHash;
          String maxResidue;
          char[] seq, struc=rnaStruc.getRNAStruc().toCharArray();
@@ -202,11 +211,11 @@ public class StructureFrequency
          int count,nonGap=0,i,bpEnd=-1,j,jSize = sequences.length;
          int[] values = new int[255];
          float percentage;
-         
+                 
          for (i = start; i < end; i++) //foreach column
          {
                  residueHash = new Hashtable();
-                 maxResidue="";
+                 maxResidue="-";
                  values = new int[255];
                  bpEnd=-1;
 
@@ -216,8 +225,10 @@ public class StructureFrequency
                          s = '-';
                  }
 
-                 if(s == '-'){
-                         values['-']++;
+                 if(s != '('){
+                         if(s == '-'){
+                                 values['-']++;
+                         }
                  }
                  else
                  {
@@ -245,35 +256,28 @@ public class StructureFrequency
                                                  values['-']++;
                                                  continue;
                                          }
-                                         if(s == '('){
-                                                 bpEnd=rna[(rna.length-1-nonGap)].getEnd();              
-                                                 cEnd=seq[bpEnd];
-                                                 if(checkBpType(c,cEnd)){
-                                                         values['H']++; //H means it's a helix (structured)
-                                                 }
+                                         bpEnd=findPair(rna,i);
+                                         cEnd=seq[bpEnd];
+                                         if(checkBpType(c,cEnd)){
+                                                 values['H']++; //H means it's a helix (structured)
                                          }
+                                         maxResidue="H";
                                  }
                          }
-                         nonGap++;
+//                       nonGap++;
                  }
-                 /*UPDATE this for new values
+                 //UPDATE this for new values
              if (profile)
              {
+                 //System.out.println("profile");
                residueHash.put(PROFILE, new int[][]
                { values, new int[]
-               { jSize, nongap } });
+               { jSize, values['H'] } });
              }
-                  */
+                  
 
                  count=values['H'];
-                 
-                 if (count == 0)
-                 {
-                         maxResidue = "-";
-                 }else{
-                         maxResidue="H";
-                 }
-                 
+                                 
                  residueHash.put(MAXCOUNT, new Integer(count));
              residueHash.put(MAXRESIDUE, maxResidue);
              
@@ -288,7 +292,6 @@ public class StructureFrequency
                  if(bpEnd>0){
                          result[bpEnd]=residueHash;
                  }
-
          }
   }
 
@@ -366,6 +369,7 @@ public class StructureFrequency
           boolean ignoreGapsInConsensusCalculation,
           boolean includeAllConsSymbols, char[] alphabet)
   {
+       System.out.println("StructureFrequency.completeConsensus "+includeAllConsSymbols);
     float tval, value;
     if (consensus == null || consensus.annotations == null
             || consensus.annotations.length < width)
@@ -403,8 +407,9 @@ public class StructureFrequency
         maxRes = "+";
       }
       int[][] profile = (int[][]) hconsensus[i].get(StructureFrequency.PROFILE);
-      if (profile != null && includeAllConsSymbols)
+      if (profile != null && includeAllConsSymbols) //Just responsible for the tooltip
       {
+       //System.out.println("StructureFrequency.includeAllConsSymbols");
         mouseOver = "";
         if (alphabet != null)
         {
@@ -420,6 +425,7 @@ public class StructureFrequency
         }
         else
         {
+          //System.out.println("StructureFrequency.NOTincludeAllConsSymbols");
           Object[] ca = new Object[profile[0].length];
           float[] vl = new float[profile[0].length];
           for (int c = 0; c < ca.length; c++)
@@ -463,38 +469,47 @@ public class StructureFrequency
    * @return
    */
   public static int[] extractProfile(Hashtable hconsensus,
-          boolean ignoreGapsInConsensusCalculation)
+                 boolean ignoreGapsInConsensusCalculation,
+                 int column)
   {
-    int[] rtnval = new int[64];
-    int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
-    if (profile == null)
-      return null;
-    Object[] ca = new Object[profile[0].length];
-    float[] vl = new float[profile[0].length];
-    for (int c = 0; c < ca.length; c++)
-    {
-      ca[c] = new char[]
-      { (char) c };
-      vl[c] = (float) profile[0][c];
-    }
-    ;
-    jalview.util.QuickSort.sort(vl, ca);
-    rtnval[0] = 1;
-    for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
-    {
-      if (((char[]) ca[c])[0] != '-')
-      {
-        rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
-        rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
-                : 0]);
-      }
-    }
-    return rtnval;
+          //TODO is there a more elegant way to acces the column number?
+          /*
+           * calculate the frequence of the 16 bp variations for this column
+           * 'somehow' transfer this via getProfile and let it correctly draw
+           */
+          int[] rtnval = new int[22];
+         int[][] profile = (int[][]) hconsensus.get(StructureFrequency.PROFILE);
+         if (profile == null)
+                 return null;
+         
+         Object[] ca = new Object[profile[0].length];
+         float[] vl = new float[profile[0].length];
+         for (int c = 0; c < ca.length; c++)
+         {
+                 ca[c] = new char[]
+                                  { (char) c };
+                 vl[c] = (float) profile[0][c];
+         }
+         ;
+         jalview.util.QuickSort.sort(vl, ca);
+         rtnval[0] = 1;
+         for (int c = ca.length - 1; profile[0][((char[]) ca[c])[0]] > 0; c--)
+         {
+                 if (((char[]) ca[c])[0] != '-')
+                 {
+                         rtnval[rtnval[0]++] = ((char[]) ca[c])[0];
+                         //System.out.println("rtnval-"+c+": "+((char[]) ca[c])[0]);
+                         rtnval[rtnval[0]++] = (int) (((float) profile[0][((char[]) ca[c])[0]]) * 100f / (float) profile[1][ignoreGapsInConsensusCalculation ? 1
+                                         : 0]);
+                 }
+         }
+         for(int i=0; i<rtnval.length;i++){
+         //  System.out.print(rtnval[i]+",");
+         }
+         System.out.print("\n");
+         return rtnval;
   }
 
-  enum base {A,T,g,C};
-  
   public static void main(String args[]){
          //Short test to see if checkBpType works
          ArrayList<String> test = new ArrayList<String>();