X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FDBRefUtils.java;h=fb54bbabaad9e6585e5cee0eb3e20f8a91e8c3a2;hb=a3f13508626aa8190ecc10eed9d7ebf6a177c2a4;hp=485d98cb57914113cfa1f3a898f79d8cc8f45d0f;hpb=beff7ac7ab7be2c24ccb179be16b8816d2c18610;p=jalview.git diff --git a/src/jalview/util/DBRefUtils.java b/src/jalview/util/DBRefUtils.java index 485d98c..fb54bba 100755 --- a/src/jalview/util/DBRefUtils.java +++ b/src/jalview/util/DBRefUtils.java @@ -32,13 +32,19 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import com.stevesoft.pat.Regex; /** * Utilities for handling DBRef objects and their collections. */ -public class DBRefUtils { +public class DBRefUtils +{ + /* + * lookup from lower-case form of a name to its canonical (standardised) form + */ + private static Map canonicalSourceNameLookup = new HashMap<>(); public final static int DB_SOURCE = 1; public final static int DB_VERSION = 2; @@ -48,14 +54,8 @@ public class DBRefUtils { public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID; public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID | DB_MAP; - /* - * lookup from lower-case form of a name to its canonical (standardised) form - */ - private static Map canonicalSourceNameLookup = new HashMap(); - - private static Map dasCoordinateSystemsLookup = new HashMap(); - - static { + static + { // TODO load these from a resource file? canonicalSourceNameLookup.put("uniprotkb/swiss-prot", DBRefSource.UNIPROT); canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT); @@ -71,87 +71,101 @@ public class DBRefUtils { canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL); canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL); - // Make sure we have lowercase entries for all canonical string lookups -// BH 2019.01.25 unnecessary -- they are all lower case already - // Set keys = canonicalSourceNameLookup.keySet(); -// for (String k : keys) -// { -// canonicalSourceNameLookup.put(k.toLowerCase(), -// canonicalSourceNameLookup.get(k)); -// } - - dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB); - dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT); - dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL); - // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS); + // guarantee we always have lowercase entries for canonical string lookups + for (String k : canonicalSourceNameLookup.keySet()) + { + canonicalSourceNameLookup.put(k.toLowerCase(), + canonicalSourceNameLookup.get(k)); + } + } + + /** + * Returns those DBRefEntry objects whose source identifier (once converted to + * Jalview's canonical form) is in the list of sources to search for. Returns + * null if no matches found. + * + * @param dbrefs DBRefEntry objects to search + * @param sources array of sources to select + * @return + */ + public static List selectRefs(List dbrefs, String[] sources) + { + if (dbrefs == null || sources == null) + { + return dbrefs; } - /** - * Returns those DBRefEntry objects whose source identifier (once converted to - * Jalview's canonical form) is in the list of sources to search for. Returns - * null if no matches found. - * - * @param dbrefs DBRefEntry objects to search - * @param sources array of sources to select - * @return - */ - public static List selectRefs(List dbrefs, String[] sources) { - if (dbrefs == null || sources == null) { - return dbrefs; - } - - // BH TODO - HashSet srcs = new HashSet(); - for (String src : sources) { - srcs.add(src.toUpperCase()); - } - - int nrefs = dbrefs.size(); - List res = new ArrayList(); - for (int ib = 0; ib < nrefs; ib++) { - DBRefEntry dbr = dbrefs.get(ib); - String source = getCanonicalName(dbr.getSource()); - if (srcs.contains(source.toUpperCase())) { - res.add(dbr); - } - } - - if (res.size() > 0) { - // List reply = new DBRefEntry[res.size()]; - return res;// .toArray(reply); - } - return null; + // BH TODO (what?) + HashSet srcs = new HashSet(); + for (String src : sources) + { + srcs.add(src.toUpperCase()); } - private static boolean selectRefsBS(List dbrefs, int sourceKeys, BitSet bsSelect) { - if (dbrefs == null || sourceKeys == 0) { - return false; - } - for (int i = 0, n = dbrefs.size(); i < n; i++) { - DBRefEntry dbr = dbrefs.get(i); - if ((dbr.getSourceKey() & sourceKeys) != 0) { - bsSelect.clear(i); - } - } - return !bsSelect.isEmpty(); + int nrefs = dbrefs.size(); + List res = new ArrayList(); + for (int ib = 0; ib < nrefs; ib++) + { + DBRefEntry dbr = dbrefs.get(ib); + String source = getCanonicalName(dbr.getSource()); + if (srcs.contains(source.toUpperCase())) + { + res.add(dbr); + } } - - /** - * isDasCoordinateSystem - * - * @param string String - * @param dBRefEntry DBRefEntry - * @return boolean true if Source DBRefEntry is compatible with DAS - * CoordinateSystem name - */ - - public static boolean isDasCoordinateSystem(String string, DBRefEntry dBRefEntry) { - if (string == null || dBRefEntry == null) { - return false; - } - String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase()); - return coordsys == null ? false : coordsys.equals(dBRefEntry.getSource()); + if (res.size() > 0) + { + // List reply = new DBRefEntry[res.size()]; + return res;// .toArray(reply); } + return null; + } + + private static boolean selectRefsBS(List dbrefs, int sourceKeys, BitSet bsSelect) + { + if (dbrefs == null || sourceKeys == 0) + { + return false; + } + for (int i = 0, n = dbrefs.size(); i < n; i++) + { + DBRefEntry dbr = dbrefs.get(i); + if ((dbr.getSourceKey() & sourceKeys) != 0) + { + bsSelect.clear(i); + } + } + return !bsSelect.isEmpty(); + } + + /** + * Returns a (possibly empty) list of those references that match the given + * entry, according to the given comparator. + * + * @param refs + * an array of database references to search + * @param entry + * an entry to compare against + * @param comparator + * @return + */ + static List searchRefs(DBRefEntry[] refs, DBRefEntry entry, + DbRefComp comparator) + { + List rfs = new ArrayList<>(); + if (refs == null || entry == null) + { + return rfs; + } + for (int i = 0; i < refs.length; i++) + { + if (comparator.matches(entry, refs[i])) + { + rfs.add(refs[i]); + } + } + return rfs; + } /** * look up source in an internal list of database reference sources and return @@ -162,12 +176,14 @@ public class DBRefUtils { * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or * original source */ - public static String getCanonicalName(String source) { - if (source == null) { - return null; - } - String canonical = canonicalSourceNameLookup.get(source.toLowerCase()); - return canonical == null ? source : canonical; + public static String getCanonicalName(String source) + { + if (source == null) + { + return null; + } + String canonical = canonicalSourceNameLookup.get(source.toLowerCase()); + return canonical == null ? source : canonical; } /** @@ -381,21 +397,26 @@ public class DBRefUtils { * map on either or map but no maplist on either or maplist of map on a is * equivalent to the maplist of map on b. */ - public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp() { + public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp() + { @Override - public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) { + public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) + { if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource()) - .equals(DBRefUtils.getCanonicalName(refa.getSource()))) { + .equals(DBRefUtils.getCanonicalName(refa.getSource()))) + { // We dont care about version - - if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId())) { - if (refa.getMap() == null || refb.getMap() == null) { + if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId())) + { + if (refa.getMap() == null || refb.getMap() == null) + { return true; } if ((refa.getMap() != null && refb.getMap() != null) && (refb.getMap().getMap() == null && refa.getMap().getMap() == null) || (refb.getMap().getMap() != null && refa.getMap().getMap() != null - && (refb.getMap().getMap().equals(refa.getMap().getMap())))) { + && (refb.getMap().getMap().equals(refa.getMap().getMap())))) + { return true; } } @@ -405,18 +426,29 @@ public class DBRefUtils { }; /** - * accession ID only must be identical. - */ - public static DbRefComp matchId = new DbRefComp() { - @Override - public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) { - if (refa.getAccessionId() != null && refb.getAccessionId() != null - && refb.getAccessionId().equals(refa.getAccessionId())) { - return true; - } - return false; - } - }; + * Returns the (possibly empty) list of those supplied dbrefs which have the + * specified source database, with a case-insensitive match of source name + * + * @param dbRefs + * @param source + * @return + */ + public static List searchRefsForSource(DBRefEntry[] dbRefs, + String source) + { + List matches = new ArrayList<>(); + if (dbRefs != null && source != null) + { + for (DBRefEntry dbref : dbRefs) + { + if (source.equalsIgnoreCase(dbref.getSource())) + { + matches.add(dbref); + } + } + } + return matches; + } /** * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the