X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FComparison.java;h=67a668a9c66b29d3e4ca6d36a530b8d4900fd901;hb=77e9dc41516acc93470931bcc5f344c67e819e70;hp=e48c0cac61ade1262c60ca13e062d3615977d9be;hpb=55e2e9b22b133db8b9ff0979b0338a33081fc8fd;p=jalview.git diff --git a/src/jalview/util/Comparison.java b/src/jalview/util/Comparison.java index e48c0ca..67a668a 100755 --- a/src/jalview/util/Comparison.java +++ b/src/jalview/util/Comparison.java @@ -1,26 +1,25 @@ /* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -*/ + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ package jalview.util; import jalview.datamodel.*; - /** * DOCUMENT ME! * @@ -30,7 +29,7 @@ import jalview.datamodel.*; public class Comparison { /** DOCUMENT ME!! */ - public static String GapChars = " .-"; + public static final String GapChars = " .-"; /** * DOCUMENT ME! @@ -40,7 +39,7 @@ public class Comparison * * @return DOCUMENT ME! */ - public static float compare(SequenceI ii, SequenceI jj) + public static final float compare(SequenceI ii, SequenceI jj) { return Comparison.compare(ii, jj, 0, ii.getLength() - 1); } @@ -55,8 +54,8 @@ public class Comparison */ public static float compare(SequenceI ii, SequenceI jj, int start, int end) { - String si = ii.getSequence(); - String sj = jj.getSequence(); + String si = ii.getSequenceAsString(); + String sj = jj.getSequenceAsString(); int ilen = si.length() - 1; int jlen = sj.length() - 1; @@ -116,70 +115,21 @@ public class Comparison * @param s2 SequenceI * @return float */ - public static float PID(SequenceI s1, SequenceI s2) + public final static float PID(String seq1, String seq2) { - int len; - - if (s1.getSequence().length() > s2.getSequence().length()) - { - len = s1.getSequence().length(); - } - else - { - len = s2.getSequence().length(); - } - - int bad = 0; - - for (int i = 0; i < len; i++) - { - char chr1; - char chr2; - - if (i < s1.getSequence().length()) - { - chr1 = Character.toUpperCase(s1.getSequence().charAt(i)); - } - else - { - chr1 = '.'; - } - - if (i < s2.getSequence().length()) - { - chr2 = Character.toUpperCase(s2.getSequence().charAt(i)); - } - else - { - chr2 = '.'; - } - - if (! (jalview.util.Comparison.isGap(chr1)) && - ! (jalview.util.Comparison.isGap(chr2))) - { - if (chr1 != chr2) - { - bad++; - } - } - } - - return ( (float) 100 * (len - bad)) / len; + return PID(seq1, seq2, 0, seq1.length()); } + static final int caseShift = 'a' - 'A'; + // Another pid with region specification - public static float PID(SequenceI s1, SequenceI s2, int start, int end) + public final static float PID(String seq1, String seq2, int start, int end) { - int len; - if (s1.getSequence().length() > s2.getSequence().length()) - { - len = s1.getSequence().length(); - } - else - { - len = s2.getSequence().length(); - } + int s1len = seq1.length(); + int s2len = seq2.length(); + + int len = Math.min(s1len, s2len); if (end < len) { @@ -192,37 +142,31 @@ public class Comparison } int bad = 0; + char chr1; + char chr2; for (int i = start; i < len; i++) { - char chr1; - char chr2; + chr1 = seq1.charAt(i); - if (i < s1.getSequence().length()) - { - chr1 = Character.toUpperCase(s1.getSequence().charAt(i)); - } - else - { - chr1 = '.'; - } + chr2 = seq2.charAt(i); - if (i < s2.getSequence().length()) + if ('a' <= chr1 && chr1 <= 'z') { - chr2 = Character.toUpperCase(s2.getSequence().charAt(i)); + // TO UPPERCASE !!! + //Faster than toUpperCase + chr1 -= caseShift; } - else + if ('a' <= chr2 && chr2 <= 'z') { - chr2 = '.'; + // TO UPPERCASE !!! + //Faster than toUpperCase + chr2 -= caseShift; } - if (! (jalview.util.Comparison.isGap(chr1)) && - ! (jalview.util.Comparison.isGap(chr2))) + if (chr1 != chr2 && !isGap(chr1) && !isGap(chr2)) { - if (chr1 != chr2) - { - bad++; - } + bad++; } } @@ -236,12 +180,12 @@ public class Comparison * * @return DOCUMENT ME! */ - public static boolean isGap(char c) + public static final boolean isGap(char c) { return (c == '-' || c == '.' || c == ' ') ? true : false; } - public static boolean isNucleotide(SequenceI [] seqs) + public static final boolean isNucleotide(SequenceI[] seqs) { int i = 0, iSize = seqs.length, j, jSize; float nt = 0, aa = 0; @@ -253,11 +197,15 @@ public class Comparison { c = seqs[i].getCharAt(j); if ('a' <= c && c <= 'z') + { c -= ('a' - 'A'); + } if (c == 'A' || c == 'G' || c == 'C' || c == 'T' || c == 'U') + { nt++; - else if (!jalview.util.Comparison.isGap( seqs[i].getCharAt(j))) + } + else if (!jalview.util.Comparison.isGap(seqs[i].getCharAt(j))) { aa++; } @@ -266,9 +214,13 @@ public class Comparison } if ( (nt / (nt + aa)) > 0.85f) + { return true; + } else + { return false; + } } }