JAL-4386 - Implementation of substitution matrix and test cases.
[jalview.git] / src / jalview / analysis / AAFrequency.java
index 796625a..ea02228 100755 (executable)
@@ -31,9 +31,11 @@ import jalview.datamodel.ProfilesI;
 import jalview.datamodel.ResidueCount;
 import jalview.datamodel.ResidueCount.SymbolCounts;
 import jalview.datamodel.SecondaryStructureCount;
+import jalview.datamodel.SeqCigar;
 import jalview.datamodel.SequenceI;
 import jalview.ext.android.SparseIntArray;
 import jalview.util.Comparison;
+import jalview.util.Constants;
 import jalview.util.Format;
 import jalview.util.MappingUtils;
 import jalview.util.QuickSort;
@@ -55,8 +57,6 @@ import java.util.List;
 public class AAFrequency
 {
   public static final String PROFILE = "P";
-  private static final String SS_ANNOTATION_LABEL = "Secondary Structure";
-  private static final char COIL = 'C';
 
   /*
    * Quick look-up of String value of char 'A' to 'Z'
@@ -231,23 +231,13 @@ public class AAFrequency
   public static final ProfilesI calculateSS(final SequenceI[] sequences,
           int width, int start, int end, boolean saveFullProfile)
   {
-    // long now = System.currentTimeMillis();
+
     int seqCount = sequences.length;
     
     ProfileI[] result = new ProfileI[width];
 
     for (int column = start; column < end; column++)
     {
-      /*
-       * Apply a heuristic to detect nucleotide data (which can
-       * be counted in more compact arrays); here we test for
-       * more than 90% nucleotide; recheck every 10 columns in case
-       * of misleading data e.g. highly conserved Alanine in peptide!
-       * Mistakenly guessing nucleotide has a small performance cost,
-       * as it will result in counting in sparse arrays.
-       * Mistakenly guessing peptide has a small space cost, 
-       * as it will use a larger than necessary array to hold counts. 
-       */
       
       int ssCount = 0;
     
@@ -263,39 +253,27 @@ public class AAFrequency
         }
         
         char c = sequences[row].getCharAt(column);
+        AlignmentAnnotation[] aa = sequences[row].getAnnotation(Constants.SS_ANNOTATION_LABEL);
+        if(aa == null) {
+          aa = sequences[row].getAnnotation(Constants.SS_ANNOTATION_FROM_JPRED_LABEL);
+        }
+        if(aa!=null) {
+          ssCount++;
+        }
         
-        if (sequences[row].getLength() > column && !Comparison.isGap(c))
+        if (sequences[row].getLength() > column && !Comparison.isGap(c) && aa !=null)
         {
           
-          AlignmentAnnotation[] aa = sequences[row].getAnnotation(SS_ANNOTATION_LABEL);
-          if(aa == null) {
-            continue;
-          }
           int seqPosition = sequences[row].findPosition(column);
-          char ss;
-          if (aa[0].getAnnotationForPosition(seqPosition) != null) {
-            ss = aa[0].getAnnotationForPosition(seqPosition).secondaryStructure;            
-            
-            //There is no representation for coil and it can be either ' ' or null. 
-            if (ss == ' ') {
-              ss = COIL; 
-            }
-          }
-          else {
-            ss = COIL;
-          }
-          
-          //secondaryStructures[row][column] = ss;
-          
-          ssCounts.add(ss);
-          ssCount++;
           
+          char ss = AlignmentUtils.findSSAnnotationForGivenSeqposition(
+                  aa, seqPosition); 
+          if(ss == '*') {
+            continue;
+          }        
+          ssCounts.add(ss);                    
         }
-        else
-        {
-          /*
-           * count a gap if the sequence doesn't reach this column
-           */
+        else if(Comparison.isGap(c) && aa!=null) {
           ssCounts.addGap();
         }
       }
@@ -314,8 +292,6 @@ public class AAFrequency
       result[column] = profile;
     }
     return new Profiles(result);
-    // long elapsed = System.currentTimeMillis() - now;
-    // jalview.bin.Console.outPrintln(elapsed);
   }
 
   /**