From 204617b73ee07ba28dcec3ffdf6b8e8eb2062011 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Thu, 19 Feb 2015 15:00:03 +0000 Subject: [PATCH] JAL-1115 JAL-653 partial introduction of List interface for sequence matcher --- src/jalview/analysis/SequenceIdMatcher.java | 79 ++++++++++++++++++--------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/jalview/analysis/SequenceIdMatcher.java b/src/jalview/analysis/SequenceIdMatcher.java index e6a4853..5b812dd 100755 --- a/src/jalview/analysis/SequenceIdMatcher.java +++ b/src/jalview/analysis/SequenceIdMatcher.java @@ -24,7 +24,9 @@ import jalview.datamodel.DBRefEntry; import jalview.datamodel.SequenceI; import java.util.ArrayList; -import java.util.Hashtable; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; import java.util.Vector; /** @@ -35,17 +37,27 @@ import java.util.Vector; */ public class SequenceIdMatcher { - private Hashtable names; + private HashMap names; - public SequenceIdMatcher(SequenceI[] seqs) + public SequenceIdMatcher(List seqs) { - names = new Hashtable(); - for (int i = 0; i < seqs.length; i++) + names = new HashMap(); + addAll(seqs); + } + + /** + * add more sequences to this matcher - also used by the constructor + * + * @param seqs + */ + public void addAll(List seqs) + { + for (SequenceI seq : seqs) { // TODO: deal with ID collisions - SequenceI should be appended to list // associated with this key. - names.put(new SeqIdName(seqs[i].getDisplayId(true)), seqs[i]); - SequenceI dbseq = seqs[i]; + names.put(new SeqIdName(seq.getDisplayId(true)), seq); + SequenceI dbseq = seq; while (dbseq.getDatasetSequence()!=null) { dbseq = dbseq.getDatasetSequence(); @@ -58,9 +70,9 @@ public class SequenceIdMatcher for (int r = 0; r < dbr.length; r++) { sid = new SeqIdName(dbr[r].getAccessionId()); - if (!names.contains(sid)) + if (!names.containsKey(sid)) { - names.put(sid, seqs[i]); + names.put(sid, seq); } } } @@ -68,19 +80,30 @@ public class SequenceIdMatcher } /** + * convenience method to make a matcher from concrete array + * + * @param sequences + */ + public SequenceIdMatcher(SequenceI[] sequences) + { + this(Arrays.asList(sequences)); + } + + /** * 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 + * List of SequenceI objects * @return SequenceI closest SequenceI to SeqIdName */ - private SequenceI pickbestMatch(SeqIdName candName, Vector matches) + private SequenceI pickbestMatch(SeqIdName candName, + List matches) { - SequenceI[] st = pickbestMatches(candName, matches); - return st == null || st.length == 0 ? null : st[0]; + List st = pickbestMatches(candName, matches); + return st == null || st.size() == 0 ? null : st.get(0); } /** @@ -94,16 +117,15 @@ public class SequenceIdMatcher * @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[] * ties } */ - private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches) + private List pickbestMatches(SeqIdName candName, + List matches) { - ArrayList best = new ArrayList(); - SequenceI match = null; + ArrayList best = new ArrayList(); if (candName == null || matches == null || matches.size() == 0) { return null; } - match = (SequenceI) matches.elementAt(0); - matches.removeElementAt(0); + SequenceI match = matches.remove(0); best.add(match); names.put(new SeqIdName(match.getName()), match); int matchlen = match.getName().length(); @@ -111,8 +133,7 @@ public class SequenceIdMatcher while (matches.size() > 0) { // look through for a better one. - SequenceI cand = (SequenceI) matches.elementAt(0); - matches.remove(0); + SequenceI cand = matches.remove(0); names.put(new SeqIdName(cand.getName()), cand); int q, w, candlen = cand.getName().length(); // keep the one with an id 'closer' to the given seqnam string @@ -136,7 +157,7 @@ public class SequenceIdMatcher return null; } ; - return (SequenceI[]) best.toArray(new SequenceI[0]); + return best; } /** @@ -163,12 +184,18 @@ public class SequenceIdMatcher * * @param seqnam * string to query Matcher with. + * @return a new array or (possibly) null */ public SequenceI[] findAllIdMatches(String seqnam) { SeqIdName nam = new SeqIdName(seqnam); - return findAllIdMatches(nam); + List m = findAllIdMatches(nam); + if (m!=null) + { + return m.toArray(new SequenceI[m.size()]); + } + return null; } /** @@ -233,15 +260,15 @@ public class SequenceIdMatcher * SeqIdName * @return SequenceI[] */ - private SequenceI[] findAllIdMatches( + private List findAllIdMatches( jalview.analysis.SequenceIdMatcher.SeqIdName nam) { - Vector matches = new Vector(); + ArrayList matches = new ArrayList(); while (names.containsKey(nam)) { - matches.addElement(names.remove(nam)); + matches.add(names.remove(nam)); } - SequenceI[] r = pickbestMatches(nam, matches); + List r = pickbestMatches(nam, matches); return r; } -- 1.7.10.2