X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FSequenceIdMatcher.java;h=3d4cbe7963fb04cb003e543de34943c0441fa923;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=a51f2f21e5f74d3fb4e67ebc7b66e5bc73a50a14;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/src/jalview/analysis/SequenceIdMatcher.java b/src/jalview/analysis/SequenceIdMatcher.java index a51f2f2..3d4cbe7 100755 --- a/src/jalview/analysis/SequenceIdMatcher.java +++ b/src/jalview/analysis/SequenceIdMatcher.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1) - * Copyright (C) 2015 The Jalview Authors + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * @@ -46,7 +46,7 @@ public class SequenceIdMatcher } /** - * add more sequences to this matcher - also used by the constructor + * Adds sequences to this matcher * * @param seqs */ @@ -54,26 +54,36 @@ public class SequenceIdMatcher { for (SequenceI seq : seqs) { - // TODO: deal with ID collisions - SequenceI should be appended to list - // associated with this key. - names.put(new SeqIdName(seq.getDisplayId(true)), seq); - SequenceI dbseq = seq; - while (dbseq.getDatasetSequence() != null) - { - dbseq = dbseq.getDatasetSequence(); - } - // add in any interesting identifiers - if (dbseq.getDBRef() != null) + add(seq); + } + } + + /** + * Adds one sequence to this matcher + * + * @param seq + */ + public void add(SequenceI seq) + { + // TODO: deal with ID collisions - SequenceI should be appended to list + // associated with this key. + names.put(new SeqIdName(seq.getDisplayId(true)), seq); + SequenceI dbseq = seq; + while (dbseq.getDatasetSequence() != null) + { + dbseq = dbseq.getDatasetSequence(); + } + // add in any interesting identifiers + if (dbseq.getDBRefs() != null) + { + DBRefEntry dbr[] = dbseq.getDBRefs(); + SeqIdName sid = null; + for (int r = 0; r < dbr.length; r++) { - DBRefEntry dbr[] = dbseq.getDBRef(); - SeqIdName sid = null; - for (int r = 0; r < dbr.length; r++) + sid = new SeqIdName(dbr[r].getAccessionId()); + if (!names.containsKey(sid)) { - sid = new SeqIdName(dbr[r].getAccessionId()); - if (!names.containsKey(sid)) - { - names.put(sid, seq); - } + names.put(sid, seq); } } } @@ -137,9 +147,8 @@ public class SequenceIdMatcher 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 - if ((q = Math.abs(matchlen - namlen)) > (w = Math.abs(candlen - - namlen)) - && candlen > matchlen) + if ((q = Math.abs(matchlen - namlen)) > (w = Math + .abs(candlen - namlen)) && candlen > matchlen) { best.clear(); match = cand; @@ -272,7 +281,7 @@ public class SequenceIdMatcher return r; } - private class SeqIdName + class SeqIdName { String id; @@ -280,7 +289,7 @@ public class SequenceIdMatcher { if (s != null) { - id = new String(s); + id = s.toLowerCase(); } else { @@ -291,8 +300,8 @@ public class SequenceIdMatcher @Override public int hashCode() { - return ((id.length() >= 4) ? id.substring(0, 4).hashCode() : id - .hashCode()); + return ((id.length() >= 4) ? id.substring(0, 4).hashCode() + : id.hashCode()); } @Override @@ -304,13 +313,13 @@ public class SequenceIdMatcher } if (s instanceof SeqIdName) { - return this.equals((SeqIdName) s); + return this.stringequals(((SeqIdName) s).id); } else { if (s instanceof String) { - return this.equals((String) s); + return this.stringequals(((String) s).toLowerCase()); } } @@ -332,37 +341,33 @@ public class SequenceIdMatcher * todo: (JBPNote) Set separator characters appropriately * * @param s - * SeqIdName * @return boolean */ - public boolean equals(SeqIdName s) + private boolean stringequals(String s) { - // TODO: JAL-732 patch for cases when name includes a list of IDs, and the - // match contains one ID flanked - if (id.length() > s.id.length()) + if (id.length() > s.length()) { - return id.startsWith(s.id) ? (WORD_SEP.indexOf(id.charAt(s.id - .length())) > -1) : false; + return id.startsWith(s) + ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1) + : false; } else { - return s.id.startsWith(id) ? (s.id.equals(id) ? true : (WORD_SEP - .indexOf(s.id.charAt(id.length())) > -1)) : false; + return s.startsWith(id) + ? (s.equals(id) ? true + : (WORD_SEP.indexOf(s.charAt(id.length())) > -1)) + : false; } } - public boolean equals(String s) + /** + * toString method returns the wrapped sequence id. For debugging purposes + * only, behaviour not guaranteed not to change. + */ + @Override + public String toString() { - if (id.length() > s.length()) - { - return id.startsWith(s) ? (WORD_SEP.indexOf(id.charAt(s.length())) > -1) - : false; - } - else - { - return s.startsWith(id) ? (s.equals(id) ? true : (WORD_SEP - .indexOf(s.charAt(id.length())) > -1)) : false; - } + return id; } } }