X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FComparison.java;h=0d945ac22f85bb69c1e18ce0b4d93840c5864b8b;hb=a1984b1c8c273ed33c7ce9283039f4027dcae2de;hp=17d3a7013b7b3f7de10ff6ff17268ede7f957bd0;hpb=3d0101179759ef157b088ea135423cd909512d9f;p=jalview.git diff --git a/src/jalview/util/Comparison.java b/src/jalview/util/Comparison.java index 17d3a70..0d945ac 100644 --- a/src/jalview/util/Comparison.java +++ b/src/jalview/util/Comparison.java @@ -261,44 +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 }); - } - - /** - * Answers true if more than 85% of the sequence residues (ignoring gaps) are - * A, G, C, T or U, else false. This is just a heuristic guess and may give a - * wrong answer (as AGCT are also amino acid codes). - * - * @param seqs - * @return - */ - public static final boolean isNucleotide(SequenceI[] seqs) - { - if (seqs == null) + if (seq==null) { return false; } - char[][] letters = new char[seqs.length][]; - for (int i = 0; i < seqs.length; i++) + long ntCount = 0; + long aaCount = 0; + long nCount = 0; + + int len = seq.getLength(); + for (int i = 0; i < len; i++) { - if (seqs[i] != null) + char c = seq.getCharAt(i); + if (isNucleotide(c) || isX(c)) + { + ntCount++; + } + else if (!isGap(c)) { - char[] sequence = seqs[i].getSequence(); - if (sequence != null) + aaCount++; + if (isN(c)) { - letters[i] = sequence; + nCount++; } } } - - return areNucleotide(letters); + /* + * 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; + } } /** @@ -306,47 +312,33 @@ public class Comparison * A, G, C, T or U, else false. This is just a heuristic guess and may give a * wrong answer (as AGCT are also amino acid codes). * - * @param letters + * @param seqs * @return */ - static final boolean areNucleotide(char[][] letters) + public static final boolean isNucleotide(SequenceI[] seqs) { - int ntCount = 0; - int aaCount = 0; - for (char[] seq : letters) + if (seqs == null) + { + return false; + } + // 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 - for (char c : seq) + if (seq.isProtein()) { - 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; } /** @@ -361,7 +353,6 @@ public class Comparison { c -= TO_UPPER_CASE; } - switch (c) { case 'A': @@ -374,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