JAL-885; Multi-helix bug is solved; One should think about a more
[jalview.git] / src / jalview / analysis / StructureFrequency.java
index a666bc0..a5dd030 100644 (file)
@@ -102,6 +102,7 @@ public class StructureFrequency
       maxResidue = "";
       nongap = 0;
       values = new int[255];
+      
 
       for (j = 0; j < jSize; j++)
       {
@@ -180,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
@@ -191,83 +201,106 @@ public class StructureFrequency
    * @param rnaStruc
    */
   public static final void calculate(SequenceI[] sequences, int start,
-          int end, Hashtable[] result, boolean profile, AlignmentAnnotation rnaStruc){
-         //TODO Consider to use AlignmentAnnotation instead of structure string
-
+                 int end, Hashtable[] result, boolean profile, AlignmentAnnotation rnaStruc){
+         System.out.println("StructureFrequency.calculate");
          Hashtable residueHash;
-
+         String maxResidue;
          char[] seq, struc=rnaStruc.getRNAStruc().toCharArray();
          SequenceFeature[] rna =rnaStruc._rnasecstr;
          char c,s,cEnd;
-         int count,nonGap,i,j,jSize = sequences.length;
+         int count,nonGap=0,i,bpEnd=-1,j,jSize = sequences.length;
          int[] values = new int[255];
          float percentage;
-
-
+         
+         /*for(int x=(rna.length-1); x>=0; x--){
+                 System.out.println("BP-"+((rna.length-1)-x)+" "+rna[x].getBegin()+","+rna[x].getEnd()+" "+rna[x].getFeatureGroup());
+         }*/
+         
+         
          for (i = start; i < end; i++) //foreach column
-           {
-             residueHash = new Hashtable();
-             for (j = 0; j < jSize; j++) //foreach row
-             {
-               if (sequences[j]==null)
-               {
-                 System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
-                 continue;
-               }
-               seq = sequences[j].getSequence();
-               if (seq.length > i)
-               {
-                       c = seq[i];
-                       s = struc[i];
-                       nonGap=0;
-                       
-                       //standard representation for gaps in sequence and structure
-                       if (c == '.' || c == ' ')
-                   {
-                     c = '-';
-                   }
-                       if (s == '.' || s == ' ')
-                   {
-                     s = '-';
-                   }
-                       
-                       if (c == '-')
-                   {
-                     values['-']++;
-                     continue;
-                   }
-                       if(s == '-'){
-                               values['-']++;
-                               continue;
-                       }
-                       nonGap++;
-                       cEnd=seq[rna[i].getEnd()];
-                       if(checkBpType(c,cEnd)){
-                               values['H']++; //H means it's a helix (structured)
-                       }
-               }
-             }
-             /*UPDATE this for new values
-             if (profile)
-             {
-               residueHash.put(PROFILE, new int[][]
-               { values, new int[]
-               { jSize, nongap } });
-             }
-             */
-             
-             count=values['H'];
-
-             percentage = ((float) count * 100) / (float) jSize;
-             residueHash.put(PID_GAPS, new Float(percentage));
+         {
+                 residueHash = new Hashtable();
+                 maxResidue="-";
+                 values = new int[255];
+                 bpEnd=-1;
+
+                 s = struc[i];
+                 if (s == '.' || s == ' ')
+                 {
+                         s = '-';
+                 }
 
-             //percentage = ((float) count * 100) / (float) nongap;
-             //residueHash.put(PID_NOGAPS, new Float(percentage));
-             result[i] = residueHash;
+                 if(s != '('){
+                         values['-']++;
+                 }
+                 else
+                 {
+                         for (j = 0; j < jSize; j++) //foreach row
+                         {
+                                 if (sequences[j]==null)
+                                 {
+                                         System.err.println("WARNING: Consensus skipping null sequence - possible race condition.");
+                                         continue;
+                                 }
+                                 seq = sequences[j].getSequence();
+                                 
+                                 if (seq.length > i)
+                                 {
+                                         c = seq[i];
+
+                                         //standard representation for gaps in sequence and structure
+                                         if (c == '.' || c == ' ')
+                                         {
+                                                 c = '-';
+                                         }
+
+                                         if (c == '-')
+                                         {
+                                                 values['-']++;
+                                                 continue;
+                                         }
+                                         //if(s == '('){
+                                                 //bpEnd=rna[(rna.length-1-nonGap)].getEnd();
+                                                 bpEnd=findPair(rna,i);
+                                                 cEnd=seq[bpEnd];
+                                                 //System.out.println(i+","+bpEnd);
+                                                 if(checkBpType(c,cEnd)){
+                                                         values['H']++; //H means it's a helix (structured)
+                                                 }
+                                                 maxResidue="H";
+                                 //}
+                                 }
+                         }
+                         nonGap++;
+                 }
+                 //UPDATE this for new values
+             //if (profile)
+             //{
+            //   residueHash.put(PROFILE, new int[][]
+             //  { values, new int[]
+            //   { jSize, nongap } });
+            // }
+                  
+
+                 count=values['H'];
+                                 
+                 residueHash.put(MAXCOUNT, new Integer(count));
+             residueHash.put(MAXRESIDUE, maxResidue);
              
-           }
+                 percentage = ((float) count * 100) / (float) jSize;
+                 residueHash.put(PID_GAPS, new Float(percentage));
+
+                 //percentage = ((float) count * 100) / (float) nongap;
+                 //residueHash.put(PID_NOGAPS, new Float(percentage));
+                 if(result[i]==null){
+                         result[i] = residueHash;
+                 }
+                 if(bpEnd>0){
+                         result[bpEnd]=residueHash;
+                 }
+         }
   }
-                       
+
         
   /**
    * Method to check if a base-pair is a canonical or a wobble bp 
@@ -370,7 +403,7 @@ public class StructureFrequency
         value = ((Float) hconsensus[i].get(StructureFrequency.PID_GAPS))
                 .floatValue();
       }
-
+      
       String maxRes = hconsensus[i].get(StructureFrequency.MAXRESIDUE).toString();
       String mouseOver = hconsensus[i].get(StructureFrequency.MAXRESIDUE) + " ";
       if (maxRes.length() > 1)