From: amwaterhouse Date: Wed, 15 Dec 2004 17:11:34 +0000 (+0000) Subject: isGap method added to utils X-Git-Tag: Release_2_0~820 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8116c10ff719ac0b542b33dbabfc9051634bf5ea;p=jalview.git isGap method added to utils --- diff --git a/src/jalview/analysis/Conservation.java b/src/jalview/analysis/Conservation.java index f99661e..eed6d0a 100755 --- a/src/jalview/analysis/Conservation.java +++ b/src/jalview/analysis/Conservation.java @@ -119,17 +119,19 @@ public class Conservation { } } - public int countGaps(int j) { + public int countGaps(int j) + { int count = 0; - for (int i = 0; i < sequences.size();i++) { + for (int i = 0; i < sequences.size();i++) + { if( j+1 > ((Sequence)sequences.elementAt(i)).getSequence().length()) { count++; continue;} - String tmp = ((Sequence)sequences.elementAt(i)).getSequence().substring(j,j+1); - if (tmp.equals(" ") || tmp.equals(".") || tmp.equals("-")) { + char c = ((Sequence)sequences.elementAt(i)).getSequence().charAt(j); + if (jalview.util.Comparison.isGap((c))) count++; - } + } return count; } diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index cc5d760..d35cbe8 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -144,19 +144,18 @@ public class Sequence implements SequenceI while (i< sequence.length() && j <= end && j <= pos) { - String s = sequence.substring(i,i+1); + char c = sequence.charAt(i); - if (!(s.equals(".") || s.equals("-") || s.equals(" "))) { + if (!jalview.util.Comparison.isGap((c))) j++; - } + i++; } - if (j == end && j < pos) { + if (j == end && j < pos) return end+1; - } else { - + else return i; - } + } public int findPosition(int i) { @@ -164,12 +163,13 @@ public class Sequence implements SequenceI int j = 0; int pos = start; - while (j < i) { - String s = sequence.substring(j,j+1); + while (j < i) + { + char c = sequence.charAt(j); - if (!(s.equals(".") || s.equals("-") || s.equals(" "))) { + if (!jalview.util.Comparison.isGap((c))) pos++; - } + j++; } return pos; diff --git a/src/jalview/datamodel/SequenceFeature.java b/src/jalview/datamodel/SequenceFeature.java index 2512978..cba91df 100755 --- a/src/jalview/datamodel/SequenceFeature.java +++ b/src/jalview/datamodel/SequenceFeature.java @@ -161,12 +161,12 @@ public class SequenceFeature { } for (int i = fstart; i <= fend; i++) { - String s = sequence.sequence.substring(i,i+1); - if (!(s.equals(".") || s.equals("-") || s.equals(" "))) { + char c = sequence.sequence.charAt(i); + if (!jalview.util.Comparison.isGap((c))) g.fillRect(x1+(i-fstart)*width,y1,width,height); - } else { + else g.drawString("-",x1+(i-fstart)*width,y1+height); - } + } } diff --git a/src/jalview/util/Comparison.java b/src/jalview/util/Comparison.java index 72fb1d1..d8e599b 100755 --- a/src/jalview/util/Comparison.java +++ b/src/jalview/util/Comparison.java @@ -16,27 +16,22 @@ public class Comparison { int ilen = end-start+1; int jlen = end-start+1; - if ( si.substring(start + ilen).equals("-") || - si.substring(start + ilen).equals(".") || - si.substring(start + ilen).equals(" ")) { - + if ( jalview.util.Comparison.isGap( si.charAt(start+ilen)) ) + { ilen--; - while (si.substring(start + ilen,start + ilen+1).equals("-") || - si.substring(start + ilen,start + ilen+1).equals(".") || - si.substring(start + ilen,start + ilen+1).equals(" ")) { + while (jalview.util.Comparison.isGap( si.charAt(start+ilen))) + { ilen--; } } - if ( sj.substring(start + jlen).equals("-") || - sj.substring(start + jlen).equals(".") || - sj.substring(start + jlen).equals(" ")) { + if ( jalview.util.Comparison.isGap( sj.charAt(start+jlen)) ) + { jlen--; - while (sj.substring(start + jlen,start + jlen+1).equals("-") || - sj.substring(start + jlen,start + jlen+1).equals(".") || - sj.substring(start + jlen,start + jlen+1).equals(" ")) { + while (jalview.util.Comparison.isGap( sj.charAt(start+jlen))) + { jlen--; } } @@ -68,47 +63,47 @@ public class Comparison { } /** */ - public static float PID(Sequence s1 , Sequence s2) { - int res = 0; + public static float PID(Sequence s1 , Sequence s2) + { int len; - if (s1.getSequence().length() > s2.getSequence().length()) { + if (s1.getSequence().length() > s2.getSequence().length()) len = s1.getSequence().length(); - } else { + else len = s2.getSequence().length(); - } + int bad = 0; - for (int i = 0; i < len; i++) { - String str1 = ""; - String str2 = ""; + for (int i = 0; i < len; i++) + { + char chr1; + char chr2; - if (i < s1.getSequence().length()) { - str1 = s1.getSequence().substring(i,i+1); - } else { - str1 = "."; - } + if (i < s1.getSequence().length()) + chr1 = s1.getSequence().charAt(i); + else + chr1 = '.'; - if (i < s2.getSequence().length()) { - str2 = s2.getSequence().substring(i,i+1); - } else { - str2 = "."; - } - if (!(str1.equals(".") || - str1.equals("-") || - str1.equals(" ")) && - !(str2.equals(".") || - str2.equals("-") || - str2.equals(" "))) { + if (i < s2.getSequence().length()) + chr2 = s2.getSequence().charAt(i); + else + chr2 = '.'; + - if (!str1.equals(str2)) { + if (!(jalview.util.Comparison.isGap( chr1 )) && !(jalview.util.Comparison.isGap( chr2 ))) + { + if (chr1!=chr2) bad++; - } } } return (float)100*(len-bad)/len; } + + public static boolean isGap(char c) + { + return (c != '.' && c != '-' && c != ' ') ? false : true; + } }