X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FComparison.java;h=fabfbff6d15915951891ca72b8a91f5740752724;hb=3cc6cd558fa548219f4e12b3abbb6b88d0d20c23;hp=94d630066a9a15458e16b31c2153a825d07e4bb0;hpb=ff450fad8709ae81919af7a15ea382af7292794c;p=jalview.git diff --git a/src/jalview/util/Comparison.java b/src/jalview/util/Comparison.java index 94d6300..fabfbff 100644 --- a/src/jalview/util/Comparison.java +++ b/src/jalview/util/Comparison.java @@ -40,8 +40,9 @@ public class Comparison public static final char GAP_DASH = '-'; - public static final String GapChars = new String(new char[] { GAP_SPACE, - GAP_DOT, GAP_DASH }); + public static final String GapChars = new String( + new char[] + { GAP_SPACE, GAP_DOT, GAP_DASH }); /** * DOCUMENT ME! @@ -71,7 +72,8 @@ public class Comparison * int * @return float */ - public static float compare(SequenceI ii, SequenceI jj, int start, int end) + public static float compare(SequenceI ii, SequenceI jj, int start, + int end) { String si = ii.getSequenceAsString(); String sj = jj.getSequenceAsString(); @@ -97,8 +99,8 @@ public class Comparison { for (int j = 0; j < jlen; j++) { - if (si.substring(start + j, start + j + 1).equals( - sj.substring(start + j, start + j + 1))) + if (si.substring(start + j, start + j + 1) + .equals(sj.substring(start + j, start + j + 1))) { match++; } @@ -112,8 +114,8 @@ public class Comparison { for (int j = 0; j < jlen; j++) { - if (si.substring(start + j, start + j + 1).equals( - sj.substring(start + j, start + j + 1))) + if (si.substring(start + j, start + j + 1) + .equals(sj.substring(start + j, start + j + 1))) { match++; } @@ -150,7 +152,8 @@ public class Comparison * @deprecated use PIDModel.computePID() */ @Deprecated - public final static float PID(String seq1, String seq2, int start, int end) + public final static float PID(String seq1, String seq2, int start, + int end) { return PID(seq1, seq2, start, end, true, false); } @@ -253,19 +256,56 @@ public class Comparison */ public static final boolean isGap(char c) { - return (c == GAP_DASH || c == GAP_DOT || c == GAP_SPACE) ? true : false; + return (c == GAP_DASH || c == GAP_DOT || c == GAP_SPACE); } /** * 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; + } } /** @@ -282,45 +322,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; } /** @@ -335,7 +354,6 @@ public class Comparison { c -= TO_UPPER_CASE; } - switch (c) { case 'A': @@ -348,6 +366,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 @@ -396,8 +436,8 @@ public class Comparison flattened.add(s); } } - final SequenceI[] oneDArray = flattened.toArray(new SequenceI[flattened - .size()]); + final SequenceI[] oneDArray = flattened + .toArray(new SequenceI[flattened.size()]); return isNucleotide(oneDArray); }