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;
/**
*/
public class SequenceIdMatcher
{
- private Hashtable names;
+ private HashMap<SeqIdName, SequenceI> names;
- public SequenceIdMatcher(SequenceI[] seqs)
+ public SequenceIdMatcher(List<SequenceI> seqs)
{
- names = new Hashtable();
- for (int i = 0; i < seqs.length; i++)
+ names = new HashMap<SeqIdName, SequenceI>();
+ addAll(seqs);
+ }
+
+ /**
+ * add more sequences to this matcher - also used by the constructor
+ *
+ * @param seqs
+ */
+ public void addAll(List<SequenceI> 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();
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);
}
}
}
}
/**
+ * 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<SequenceI> matches)
{
- SequenceI[] st = pickbestMatches(candName, matches);
- return st == null || st.length == 0 ? null : st[0];
+ List<SequenceI> st = pickbestMatches(candName, matches);
+ return st == null || st.size() == 0 ? null : st.get(0);
}
/**
* @return Object[] { SequenceI closest SequenceI to SeqIdName, SequenceI[]
* ties }
*/
- private SequenceI[] pickbestMatches(SeqIdName candName, Vector matches)
+ private List<SequenceI> pickbestMatches(SeqIdName candName,
+ List<SequenceI> matches)
{
- ArrayList best = new ArrayList();
- SequenceI match = null;
+ ArrayList<SequenceI> best = new ArrayList<SequenceI>();
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();
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
return null;
}
;
- return (SequenceI[]) best.toArray(new SequenceI[0]);
+ return best;
}
/**
*
* @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<SequenceI> m = findAllIdMatches(nam);
+ if (m!=null)
+ {
+ return m.toArray(new SequenceI[m.size()]);
+ }
+ return null;
}
/**
* SeqIdName
* @return SequenceI[]
*/
- private SequenceI[] findAllIdMatches(
+ private List<SequenceI> findAllIdMatches(
jalview.analysis.SequenceIdMatcher.SeqIdName nam)
{
- Vector matches = new Vector();
+ ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
while (names.containsKey(nam))
{
- matches.addElement(names.remove(nam));
+ matches.add(names.remove(nam));
}
- SequenceI[] r = pickbestMatches(nam, matches);
+ List<SequenceI> r = pickbestMatches(nam, matches);
return r;
}