Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[jalview.git] / src / jalview / util / Comparison.java
index 4afa12d..6a7e655 100644 (file)
@@ -261,14 +261,50 @@ public class Comparison
 
   /**
    * Overloaded method signature to test whether a single sequence is nucleotide
-   * (that is, more than 85% CGTA)
+   * (that is, more than 85% CGTAUNX)
    * 
    * @param seq
    * @return
    */
   public static final boolean isNucleotide(SequenceI seq)
   {
-    return isNucleotide(new SequenceI[] { seq });
+    if (seq==null)
+    {
+      return false;
+    }
+    long ntCount = 0;
+    long aaCount = 0;
+    long nCount = 0;
+
+    int len = seq.getLength();
+    for (int i = 0; i < len; i++)
+    {
+      char c = seq.getCharAt(i);
+      if (isNucleotide(c) || isX(c))
+      {
+        ntCount++;
+      }
+      else if (!isGap(c))
+      {
+        aaCount++;
+        if (isN(c))
+        {
+          nCount++;
+        }
+      }
+    }
+    /*
+     * Check for nucleotide count > 85% of total count (in a form that evades
+     * int / float conversion or divide by zero).
+     */
+    if ((ntCount+nCount) * 100 > EIGHTY_FIVE * (ntCount + aaCount))
+    {
+      return ntCount>0; // all N is considered protein. Could use a threshold here too
+    }
+    else
+    {
+      return false;
+    }
   }
 
   /**
@@ -285,45 +321,24 @@ public class Comparison
     {
       return false;
     }
-
-    int ntCount = 0;
-    int aaCount = 0;
+    // true if we have seen a nucleotide sequence
+    boolean na = false;
     for (SequenceI seq : seqs)
     {
       if (seq == null)
       {
         continue;
       }
+      na = true;
       // TODO could possibly make an informed guess just from the first sequence
       // to save a lengthy calculation
-      int len = seq.getLength();
-      for (int i = 0; i < len; i++)
+      if (seq.isProtein())
       {
-        char c = seq.getCharAt(i);
-        if (isNucleotide(c))
-        {
-          ntCount++;
-        }
-        else if (!isGap(c))
-        {
-          aaCount++;
-        }
+        // if even one looks like protein, the alignment is protein
+        return false;
       }
     }
-
-    /*
-     * Check for nucleotide count > 85% of total count (in a form that evades
-     * int / float conversion or divide by zero).
-     */
-    if (ntCount * 100 > EIGHTY_FIVE * (ntCount + aaCount))
-    {
-      return true;
-    }
-    else
-    {
-      return false;
-    }
-
+    return na;
   }
 
   /**
@@ -338,7 +353,6 @@ public class Comparison
     {
       c -= TO_UPPER_CASE;
     }
-
     switch (c)
     {
     case 'A':
@@ -351,6 +365,28 @@ public class Comparison
     return false;
   }
 
+  public static boolean isN(char c)
+  {
+    switch (c)
+    {
+    case 'N':
+    case 'n':
+      return true;
+    }
+    return false;
+  }
+
+  public static boolean isX(char c)
+  {
+    switch (c)
+    {
+    case 'X':
+    case 'x':
+      return true;
+    }
+    return false;
+  }
+
   /**
    * Answers true if every character in the string is one of aAcCgGtTuU, or
    * (optionally) a gap character (dot, dash, space), else false