X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FComparison.java;h=0beb45bcefca1ad9319236dac9d93bd18af68636;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=95f298b82bec574b3a36b8426a94741eead7ecb0;hpb=4d7f98a6dd54d9863ba449ec79dcd95d25ed863d;p=jalview.git diff --git a/src/jalview/util/Comparison.java b/src/jalview/util/Comparison.java index 95f298b..0beb45b 100644 --- a/src/jalview/util/Comparison.java +++ b/src/jalview/util/Comparison.java @@ -249,6 +249,18 @@ public class Comparison } /** + * Overloaded method signature to test whether a single sequence is nucleotide + * (that is, more than 85% CGTA) + * + * @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). @@ -262,9 +274,35 @@ public class Comparison { return false; } + char[][] letters = new char[seqs.length][]; + for (int i = 0; i < seqs.length; i++) + { + if (seqs[i] != null) + { + char[] sequence = seqs[i].getSequence(); + if (sequence != null) + { + letters[i] = sequence; + } + } + } + + return areNucleotide(letters); + } + + /** + * 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 letters + * @return + */ + static final boolean areNucleotide(char[][] letters) + { int ntCount = 0; int aaCount = 0; - for (SequenceI seq : seqs) + for (char[] seq : letters) { if (seq == null) { @@ -272,18 +310,13 @@ public class Comparison } // TODO could possibly make an informed guess just from the first sequence // to save a lengthy calculation - for (char c : seq.getSequence()) + for (char c : seq) { - if ('a' <= c && c <= 'z') - { - c -= TO_UPPER_CASE; - } - - if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U') + if (isNucleotide(c)) { ntCount++; } - else if (!Comparison.isGap(c)) + else if (!isGap(c)) { aaCount++; } @@ -306,6 +339,59 @@ public class Comparison } /** + * Answers true if the character is one of aAcCgGtTuU + * + * @param c + * @return + */ + public static boolean isNucleotide(char c) + { + if ('a' <= c && c <= 'z') + { + c -= TO_UPPER_CASE; + } + + switch (c) + { + case 'A': + case 'C': + case 'G': + case 'T': + case 'U': + 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 + * + * @param s + * @param allowGaps + * @return + */ + public static boolean isNucleotideSequence(String s, boolean allowGaps) + { + if (s == null) + { + return false; + } + for (int i = 0; i < s.length(); i++) + { + char c = s.charAt(i); + if (!isNucleotide(c)) + { + if (!allowGaps || !isGap(c)) + { + return false; + } + } + } + return true; + } + + /** * Convenience overload of isNucleotide * * @param seqs