JAL-4392 Merging consensus secondary structure
[jalview.git] / src / jalview / analysis / AAFrequency.java
index 6967885..796625a 100755 (executable)
@@ -30,6 +30,7 @@ import jalview.datamodel.Profiles;
 import jalview.datamodel.ProfilesI;
 import jalview.datamodel.ResidueCount;
 import jalview.datamodel.ResidueCount.SymbolCounts;
+import jalview.datamodel.SecondaryStructureCount;
 import jalview.datamodel.SequenceI;
 import jalview.ext.android.SparseIntArray;
 import jalview.util.Comparison;
@@ -54,6 +55,8 @@ 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'
@@ -191,6 +194,130 @@ public class AAFrequency
     // jalview.bin.Console.outPrintln(elapsed);
   }
 
+  
+  public static final ProfilesI calculateSS(List<SequenceI> list, int start,
+          int end)
+  {
+    return calculateSS(list, start, end, false);
+  }
+  
+  public static final ProfilesI calculateSS(List<SequenceI> sequences,
+          int start, int end, boolean profile)
+  {
+    SequenceI[] seqs = new SequenceI[sequences.size()];
+    int width = 0;
+    synchronized (sequences)
+    {
+      for (int i = 0; i < sequences.size(); i++)
+      {
+        seqs[i] = sequences.get(i);
+        int length = seqs[i].getLength();
+        if (length > width)
+        {
+          width = length;
+        }
+      }
+
+      if (end >= width)
+      {
+        end = width;
+      }
+
+      ProfilesI reply = calculateSS(seqs, width, start, end, profile);
+      return reply;
+    }
+  }
+  
+  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;
+    
+      SecondaryStructureCount ssCounts = new SecondaryStructureCount();
+
+      for (int row = 0; row < seqCount; row++)
+      {
+        if (sequences[row] == null)
+        {
+          jalview.bin.Console.errPrintln(
+                  "WARNING: Consensus skipping null sequence - possible race condition.");
+          continue;
+        }
+        
+        char c = sequences[row].getCharAt(column);
+        
+        if (sequences[row].getLength() > column && !Comparison.isGap(c))
+        {
+          
+          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++;
+          
+        }
+        else
+        {
+          /*
+           * count a gap if the sequence doesn't reach this column
+           */
+          ssCounts.addGap();
+        }
+      }
+
+      int maxSSCount = ssCounts.getModalCount();
+      String maxSS = ssCounts.getSSForCount(maxSSCount);
+      int gapCount = ssCounts.getGapCount();
+      ProfileI profile = new Profile(maxSS, ssCount, gapCount, 
+              maxSSCount);
+
+      if (saveFullProfile)
+      {
+        profile.setSSCounts(ssCounts);
+      }
+
+      result[column] = profile;
+    }
+    return new Profiles(result);
+    // long elapsed = System.currentTimeMillis() - now;
+    // jalview.bin.Console.outPrintln(elapsed);
+  }
+
   /**
    * Make an estimate of the profile size we are going to compute i.e. how many
    * different characters may be present in it. Overestimating has a cost of
@@ -287,6 +414,58 @@ public class AAFrequency
     // long elapsed = System.currentTimeMillis() - now;
     // jalview.bin.Console.outPrintln(-elapsed);
   }
+  
+ public static void completeSSConsensus(AlignmentAnnotation ssConsensus,
+         ProfilesI profiles, int startCol, int endCol, boolean ignoreGaps,
+         boolean showSequenceLogo, long nseq)
+ {
+   // long now = System.currentTimeMillis();
+   if (ssConsensus == null || ssConsensus.annotations == null
+           || ssConsensus.annotations.length < endCol)
+   {
+     /*
+      * called with a bad alignment annotation row 
+      * wait for it to be initialised properly
+      */
+     return;
+   }
+
+   for (int i = startCol; i < endCol; i++)
+   {
+     ProfileI profile = profiles.get(i);
+     if (profile == null)
+     {
+       /*
+        * happens if sequences calculated over were 
+        * shorter than alignment width
+        */
+       ssConsensus.annotations[i] = null;
+       return;
+     }
+
+     final int dp = getPercentageDp(nseq);
+
+     float value = profile.getSSPercentageIdentity(ignoreGaps);
+
+     String description = getSSTooltip(profile, value, showSequenceLogo,
+             ignoreGaps, dp);
+
+     String modalSS = profile.getModalSS();
+     if ("".equals(modalSS))
+     {
+       modalSS = "-";
+     }
+     else if (modalSS.length() > 1)
+     {
+       modalSS = "+";
+     }
+     ssConsensus.annotations[i] = new Annotation(modalSS, description,
+             ' ', value);
+   }
+   // long elapsed = System.currentTimeMillis() - now;
+   // jalview.bin.Console.outPrintln(-elapsed);
+ }
 
   /**
    * Derive the gap count annotation row.
@@ -392,6 +571,41 @@ public class AAFrequency
     }
     return description;
   }
+  
+  static String getSSTooltip(ProfileI profile, float pid,
+          boolean showSequenceLogo, boolean ignoreGaps, int dp)
+  {
+    SecondaryStructureCount counts = profile.getSSCounts();
+
+    String description = null;
+    if (counts != null && showSequenceLogo)
+    {
+      int normaliseBy = ignoreGaps ? profile.getNonGapped()
+              : profile.getHeight();
+      description = counts.getTooltip(normaliseBy, dp);
+    }
+    else
+    {
+      StringBuilder sb = new StringBuilder(64);
+      String maxSS = profile.getModalSS();
+      if (maxSS.length() > 1)
+      {
+        sb.append("[").append(maxSS).append("]");
+      }
+      else
+      {
+        sb.append(maxSS);
+      }
+      if (maxSS.length() > 0)
+      {
+        sb.append(" ");
+        Format.appendPercentage(sb, pid, dp);
+        sb.append("%");
+      }
+      description = sb.toString();
+    }
+    return description;
+  }
 
   /**
    * Returns the sorted profile for the given consensus data. The returned array
@@ -411,15 +625,30 @@ public class AAFrequency
    */
   public static int[] extractProfile(ProfileI profile, boolean ignoreGaps)
   {
-    ResidueCount counts = profile.getCounts();
-    if (counts == null)
+    char[] symbols;
+    int[] values;
+    
+    if (profile.getCounts() != null)
     {
+      ResidueCount counts = profile.getCounts();
+      SymbolCounts symbolCounts = counts.getSymbolCounts();
+      symbols = symbolCounts.symbols;
+      values = symbolCounts.values;
+      
+    }
+    else if(profile.getSSCounts() != null) 
+    {
+      SecondaryStructureCount counts = profile.getSSCounts();
+      // to do
+      SecondaryStructureCount.SymbolCounts symbolCounts = counts.getSymbolCounts();
+      symbols = symbolCounts.symbols;
+      values = symbolCounts.values;
+    }
+    else {
       return null;
     }
+    
 
-    SymbolCounts symbolCounts = counts.getSymbolCounts();
-    char[] symbols = symbolCounts.symbols;
-    int[] values = symbolCounts.values;
     QuickSort.sort(values, symbols);
     int totalPercentage = 0;
     final int divisor = ignoreGaps ? profile.getNonGapped()