X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FSequenceIdMatcher.java;h=98886472f1de8e750b46438ddb1680a81fa206c8;hb=63cead20b09743e899a22cb89f1c09e4d41cc8c0;hp=a51f2f21e5f74d3fb4e67ebc7b66e5bc73a50a14;hpb=838e4f91d4a53dd315640dbc9ff6ef7a815ee576;p=jalview.git diff --git a/src/jalview/analysis/SequenceIdMatcher.java b/src/jalview/analysis/SequenceIdMatcher.java index a51f2f2..9888647 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. * @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; -import java.util.Vector; +import java.util.Map; /** * Routines for approximate Sequence Id resolution by name using string @@ -37,16 +37,19 @@ import java.util.Vector; */ public class SequenceIdMatcher { - private HashMap names; + private HashMap> names; + + private Map> excludes; public SequenceIdMatcher(List seqs) { - names = new HashMap(); + names = new HashMap<>(); + excludes = new HashMap<>(); addAll(seqs); } /** - * add more sequences to this matcher - also used by the constructor + * Adds sequences to this matcher * * @param seqs */ @@ -54,31 +57,67 @@ 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) + { + SeqIdName key = new SeqIdName(seq.getDisplayId(true)); + addMatchCandidate(key, seq); + SequenceI dbseq = seq; + while (dbseq.getDatasetSequence() != null) + { + dbseq = dbseq.getDatasetSequence(); + } + // add in any interesting identifiers + if (dbseq.getDBRefs() != null) + { + DBRefEntry dbr[] = dbseq.getDBRefs(); + for (int r = 0; r < dbr.length; r++) { - DBRefEntry dbr[] = dbseq.getDBRef(); - SeqIdName sid = null; - for (int r = 0; r < dbr.length; r++) + DBRefEntry dbref = dbr[r]; + SeqIdName sid = new SeqIdName(dbref.getAccessionId()); + if (dbref.getMap() != null + && dbref.getMap().getMap().isTripletMap()) { - sid = new SeqIdName(dbr[r].getAccessionId()); - if (!names.containsKey(sid)) + /* + * dbref with 3:1 or 1:3 mapping (e.g. CDS/protein); + * mark as not a valid match for this id + */ + List excluded = excludes.get(sid); + if (excluded == null) { - names.put(sid, seq); + excludes.put(sid, excluded = new ArrayList<>()); } + excluded.add(seq); + System.out.println("Excluding " + sid + "->" + seq); + continue; } + addMatchCandidate(sid, seq); } } } + void addMatchCandidate(SeqIdName key, SequenceI seq) + { + List namesList = names.get(key); + if (namesList == null) + { + names.put(key, namesList = new ArrayList<>()); + } + if (!namesList.contains(seq)) + { + namesList.add(seq); + System.out.println("Adding " + key + "->" + seq); + } + } + /** * convenience method to make a matcher from concrete array * @@ -120,26 +159,25 @@ public class SequenceIdMatcher private List pickbestMatches(SeqIdName candName, List matches) { - ArrayList best = new ArrayList(); + List best = new ArrayList<>(); if (candName == null || matches == null || matches.size() == 0) { return null; } SequenceI match = matches.remove(0); best.add(match); - names.put(new SeqIdName(match.getName()), match); + addMatchCandidate(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 = matches.remove(0); - names.put(new SeqIdName(cand.getName()), cand); + addMatchCandidate(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; @@ -242,13 +280,22 @@ public class SequenceIdMatcher * SeqIdName * @return SequenceI */ - private SequenceI findIdMatch( - jalview.analysis.SequenceIdMatcher.SeqIdName nam) + private SequenceI findIdMatch(SeqIdName nam) { - Vector matches = new Vector(); + List matches = new ArrayList<>(); while (names.containsKey(nam)) { - matches.addElement(names.remove(nam)); + List candidates = names.remove(nam); + List except = excludes.get(nam); + int j = candidates.size(); + for (int i = 0; i < j; i++) + { + SequenceI candidate = candidates.get(i); + if (!except.contains(candidate)) + { + matches.add(candidate); + } + } } return pickbestMatch(nam, matches); } @@ -263,16 +310,16 @@ public class SequenceIdMatcher private List findAllIdMatches( jalview.analysis.SequenceIdMatcher.SeqIdName nam) { - ArrayList matches = new ArrayList(); + List matches = new ArrayList<>(); while (names.containsKey(nam)) { - matches.add(names.remove(nam)); + matches.addAll(names.remove(nam)); } List r = pickbestMatches(nam, matches); return r; } - private class SeqIdName + class SeqIdName { String id; @@ -280,7 +327,7 @@ public class SequenceIdMatcher { if (s != null) { - id = new String(s); + id = s.toLowerCase(); } else { @@ -291,8 +338,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 +351,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 +379,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; } } }