From fbec1b33d0fc169d72be059a5d2cf12b248270e1 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 17 Aug 2005 15:20:10 +0000 Subject: [PATCH] Fixed basic name to sequence ID matching (bug http://dragonfly.compbio.dundee.ac.uk/mantis/view.php?id=9227) and improved code robustness when dealing with badly formed IDs (null strings). --- src/jalview/analysis/SequenceIdMatcher.java | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/jalview/analysis/SequenceIdMatcher.java b/src/jalview/analysis/SequenceIdMatcher.java index b88a03f..f4f305b 100755 --- a/src/jalview/analysis/SequenceIdMatcher.java +++ b/src/jalview/analysis/SequenceIdMatcher.java @@ -115,12 +115,15 @@ public class SequenceIdMatcher SeqIdName(String s) { - id = new String(s); + if (s!=null) + id = new String(s); + else + id = ""; } public int hashCode() { - return (id.substring(0, 4).hashCode()); + return ((id.length()>=4) ? id.substring(0, 4).hashCode() : id.hashCode()); } public boolean equals(Object s) @@ -141,20 +144,28 @@ public class SequenceIdMatcher } /** - * matches if one ID properly contains another at a whitespace boundary. - * TODO: (JBPNote) These are not efficient. should use char[] for speed - * @param s SeqIdName - * @return boolean + * Characters that define the end of a unique sequence ID at + * the beginning of an arbitrary ID string */ + private String WORD_SEP="~. |#\\/<>!\"£$%^*)}[@',?"; + + /** + * matches if one ID properly contains another at a whitespace boundary. + * TODO: (JBPNote) These are not efficient. should use char[] for speed + * todo: (JBPNote) Set separator characters appropriately + * @param s SeqIdName + * @return boolean + */ public boolean equals(SeqIdName s) { if (id.length()>s.id.length()) { return id.startsWith(s.id) ? - (id.equals(s.id) ? true : id.startsWith(s.id+" ")) + (WORD_SEP.indexOf(id.charAt(s.id.length()))>-1) : false; } else return s.id.startsWith(id) ? - (s.id.equals(id) ? true : s.id.startsWith(id+" ")) + (s.id.equals(id) ? true : + (WORD_SEP.indexOf(s.id.charAt(id.length()))>-1)) : false; } @@ -162,11 +173,12 @@ public class SequenceIdMatcher { if (id.length()>s.length()) { return id.startsWith(s) ? - (id.equals(s) ? true : id.startsWith(s+" ")) + (WORD_SEP.indexOf(id.charAt(s.length()))>-1) : false; } else return s.startsWith(id) ? - (s.equals(id) ? true : s.startsWith(id+" ")) + (s.equals(id) ? true : + (WORD_SEP.indexOf(s.charAt(id.length()))>-1)) : false; } } -- 1.7.10.2