From: jprocter Date: Tue, 10 Oct 2006 16:01:58 +0000 (+0000) Subject: refined id matching to pick the closest match (by length) to the query and added... X-Git-Tag: Release_2_2~286 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7ae574fd34bd7ffd1ea23be7602d6b3dc57aa49f;p=jalview.git refined id matching to pick the closest match (by length) to the query and added '_' to the list of word separators --- diff --git a/src/jalview/analysis/SequenceIdMatcher.java b/src/jalview/analysis/SequenceIdMatcher.java index ed90422..7c974e2 100755 --- a/src/jalview/analysis/SequenceIdMatcher.java +++ b/src/jalview/analysis/SequenceIdMatcher.java @@ -27,7 +27,9 @@ import jalview.datamodel.*; * SequenceIdMatcher *

Description:

* Routine which does approximate Sequence Id resolution by name using - * string containment (on word boundaries) rather than equivalence + * string containment (on word boundaries) rather than equivalence. It also + * attempts to resolve ties where no exact match is available by picking the + * the id closest to the query. *

Copyright: Copyright (c) 2004

* *

Company: Dundee University

@@ -47,31 +49,52 @@ public class SequenceIdMatcher names.put(new SeqIdName(seqs[i].getName()), seqs[i]); } } + /** + * returns the closest SequenceI in matches to SeqIdName and returns all the matches + * to the names hash. + * @param candName SeqIdName + * @param matches Vector of SequenceI objects + * @return SequenceI closest SequenceI to SeqIdName + */ + private SequenceI pickbestMatch(SeqIdName candName, Vector matches) { + SequenceI match=null; + if (candName==null || matches==null || matches.size()==0) + return null; + match=(SequenceI) matches.elementAt(0); + matches.removeElementAt(0); + names.put(new SeqIdName(match.getName()), match); + int matchlen=match.getName().length(); + int namlen=candName.id.length(); + while (matches.size()>0) { + // look through for a better one. + SequenceI cand=(SequenceI) matches.elementAt(0); + names.put(new SeqIdName(cand.getName()), cand); + int candlen = cand.getName().length(); + // keep the one with an id 'closer' to the given seqnam string + if (Math.abs(matchlen-namlen)>Math.abs(candlen-namlen) && candlen>matchlen) { + match = cand; + matchlen = candlen; + } + } + return match; + } + /** + * get SequenceI with closest SequenceI.getName() to seq.getName() + * @param seq SequenceI + * @return SequenceI + */ SequenceI findIdMatch(SequenceI seq) { SeqIdName nam = new SeqIdName(seq.getName()); - - if (names.containsKey(nam)) - { - return (SequenceI) names.get(nam); - } - - return null; + return findIdMatch(nam); } SequenceI findIdMatch(String seqnam) - { - SeqIdName nam = new SeqIdName(seqnam); - - if (names.containsKey(nam)) { - return (SequenceI) names.get(nam); - } - - return null; + SeqIdName nam = new SeqIdName(seqnam); + return findIdMatch(nam); } - /** * findIdMatch * @@ -96,7 +119,7 @@ public class SequenceIdMatcher if (names.containsKey(nam)) { - namedseqs[i] = (SequenceI) names.get(nam); + namedseqs[i] = findIdMatch(nam); } else { @@ -109,6 +132,22 @@ public class SequenceIdMatcher return namedseqs; } + /** + * core findIdMatch search method + * @param nam SeqIdName + * @return SequenceI + */ + private SequenceI findIdMatch(jalview.analysis.SequenceIdMatcher.SeqIdName + nam) + { + Vector matches=new Vector(); + while (names.containsKey(nam)) + { + matches.addElement(names.remove(nam)); + } + return pickbestMatch(nam, matches); + } + private class SeqIdName { String id; @@ -149,7 +188,7 @@ public class SequenceIdMatcher * JBPNote: This is a heuristic that will fail for arbritrarily extended sequence id's * (like portions of an aligned set of repeats from one sequence) */ - private String WORD_SEP="~. |#\\/<>!\"£$%^*)}[@',?"; + private String WORD_SEP="~. |#\\/<>!\"£$%^*)}[@',?_"; /** * matches if one ID properly contains another at a whitespace boundary.