2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.ArrayList;
24 import java.util.BitSet;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Locale;
31 import com.stevesoft.pat.Regex;
33 import jalview.datamodel.DBRefEntry;
34 import jalview.datamodel.DBRefSource;
35 import jalview.datamodel.Mapping;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.SequenceI;
40 * Utilities for handling DBRef objects and their collections.
42 public class DBRefUtils
45 * lookup from lower-case form of a name to its canonical (standardised) form
47 private static Map<String, String> canonicalSourceNameLookup = new HashMap<>();
49 public final static int DB_SOURCE = 1;
51 public final static int DB_VERSION = 2;
53 public final static int DB_ID = 4;
55 public final static int DB_MAP = 8;
57 public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
59 public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID
64 // TODO load these from a resource file?
65 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
67 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
69 // Ensembl values for dbname in xref REST service:
70 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
71 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
73 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
74 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
75 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
77 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
78 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
80 // guarantee we always have lowercase entries for canonical string lookups
81 for (String k : canonicalSourceNameLookup.keySet())
83 canonicalSourceNameLookup.put(k.toLowerCase(Locale.ROOT),
84 canonicalSourceNameLookup.get(k));
89 * Returns those DBRefEntry objects whose source identifier (once converted to
90 * Jalview's canonical form) is in the list of sources to search for. Returns
91 * null if no matches found.
94 * DBRefEntry objects to search
96 * array of sources to select
99 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs,
102 if (dbrefs == null || sources == null)
108 HashSet<String> srcs = new HashSet<String>();
109 for (String src : sources)
111 srcs.add(src.toUpperCase(Locale.ROOT));
114 int nrefs = dbrefs.size();
115 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
116 for (int ib = 0; ib < nrefs; ib++)
118 DBRefEntry dbr = dbrefs.get(ib);
119 String source = getCanonicalName(dbr.getSource());
120 if (srcs.contains(source.toUpperCase(Locale.ROOT)))
127 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
128 return res;// .toArray(reply);
133 private static boolean selectRefsBS(List<DBRefEntry> dbrefs,
134 int sourceKeys, BitSet bsSelect)
136 if (dbrefs == null || sourceKeys == 0)
140 for (int i = 0, n = dbrefs.size(); i < n; i++)
142 DBRefEntry dbr = dbrefs.get(i);
143 if ((dbr.getSourceKey() & sourceKeys) != 0)
148 return !bsSelect.isEmpty();
152 * Returns a (possibly empty) list of those references that match the given
153 * entry, according to the given comparator.
156 * an array of database references to search
158 * an entry to compare against
162 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
163 DbRefComp comparator)
165 List<DBRefEntry> rfs = new ArrayList<>();
166 if (refs == null || entry == null)
170 for (int i = 0; i < refs.length; i++)
172 if (comparator.matches(entry, refs[i]))
181 * look up source in an internal list of database reference sources and return
182 * the canonical jalview name for the source, or the original string if it has
186 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
189 public static String getCanonicalName(String source)
195 String canonical = canonicalSourceNameLookup
196 .get(source.toLowerCase(Locale.ROOT));
197 return canonical == null ? source : canonical;
201 * Returns a (possibly empty) list of those references that match the given
202 * entry. Currently uses a comparator which matches if
204 * <li>database sources are the same</li>
205 * <li>accession ids are the same</li>
206 * <li>both have no mapping, or the mappings are the same</li>
210 * Set of references to search
214 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
217 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref,
218 DBRefEntry entry, int mode)
220 return searchRefs(ref, entry,
221 matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
225 * Returns a list of those references that match the given accession id
227 * <li>database sources are the same</li>
228 * <li>accession ids are the same</li>
229 * <li>both have no mapping, or the mappings are the same</li>
233 * Set of references to search
235 * accession id to match
238 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
241 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
242 if (refs == null || accId == null)
246 for (int i = 0, n = refs.size(); i < n; i++)
248 DBRefEntry e = refs.get(i);
249 if (accId.equals(e.getAccessionId()))
255 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId,
256 // SEARCH_MODE_FULL);
260 * Returns a (possibly empty) list of those references that match the given
261 * entry, according to the given comparator.
264 * an array of database references to search
266 * an entry to compare against
269 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
272 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
273 DBRefEntry entry, DbRefComp comparator, int mode)
275 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
276 if (refs == null || entry == null)
280 for (int i = 0, n = refs.size(); i < n; i++)
282 DBRefEntry e = refs.get(i);
283 if (comparator.matches(entry, e, SEARCH_MODE_FULL))
293 default public boolean matches(DBRefEntry refa, DBRefEntry refb)
295 return matches(refa, refb, SEARCH_MODE_FULL);
298 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
302 * match on all non-null fields in refa
304 // TODO unused - remove? would be broken by equating "" with null
305 public static DbRefComp matchNonNullonA = new DbRefComp()
308 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
310 if ((mode & DB_SOURCE) != 0 && (refa.getSource() == null
311 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
312 DBRefUtils.getCanonicalName(refa.getSource()))))
314 if ((mode & DB_VERSION) != 0 && (refa.getVersion() == null
315 || refb.getVersion().equals(refa.getVersion())))
317 if ((mode & DB_ID) != 0 && (refa.getAccessionId() == null
318 || refb.getAccessionId().equals(refa.getAccessionId())))
320 if ((mode & DB_MAP) != 0
321 && (refa.getMap() == null || (refb.getMap() != null
322 && refb.getMap().equals(refa.getMap()))))
334 * either field is null or field matches for all of source, version, accession
337 // TODO unused - remove?
338 public static DbRefComp matchEitherNonNull = new DbRefComp()
341 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
343 if (nullOrEqualSource(refa.getSource(), refb.getSource())
344 && nullOrEqual(refa.getVersion(), refb.getVersion())
345 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
346 && nullOrEqual(refa.getMap(), refb.getMap()))
356 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
359 * Used by file parsers to generate DBRefs from annotation within file (eg
366 * where to annotate with reference
367 * @return parsed version of entry that was added to seq (if any)
369 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
370 String version, String acn)
372 DBRefEntry ref = null;
375 String locsrc = DBRefUtils.getCanonicalName(dbname);
376 if (locsrc.equals(DBRefSource.PDB))
379 * Check for PFAM style stockhom PDB accession id citation e.g.
382 Regex r = new com.stevesoft.pat.Regex(
383 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
384 if (r.search(acn.trim()))
386 String pdbid = r.stringMatched(1);
387 String chaincode = r.stringMatched(2);
388 if (chaincode == null)
392 // String mapstart = r.stringMatched(3);
393 // String mapend = r.stringMatched(4);
394 if (chaincode.equals(" "))
398 // construct pdb ref.
399 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
400 PDBEntry pdbr = new PDBEntry();
402 pdbr.setType(PDBEntry.Type.PDB);
403 pdbr.setChainCode(chaincode);
408 System.err.println("Malformed PDB DR line:" + acn);
414 ref = new DBRefEntry(locsrc, version, acn.trim());
425 * accession ID and DB must be identical. Version is ignored. Map is either
426 * not defined or is a match (or is compatible?)
428 // TODO unused - remove?
429 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
432 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
434 if (refa.getSource() != null && refb.getSource() != null
435 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
436 DBRefUtils.getCanonicalName(refa.getSource())))
438 // We dont care about version
439 if (refa.getAccessionId() != null && refb.getAccessionId() != null
440 // FIXME should be && not || here?
441 || refb.getAccessionId().equals(refa.getAccessionId()))
443 if ((refa.getMap() == null || refb.getMap() == null)
444 || (refa.getMap() != null && refb.getMap() != null
445 && refb.getMap().equals(refa.getMap())))
456 * accession ID and DB must be identical. Version is ignored. No map on either
457 * or map but no maplist on either or maplist of map on a is the complement of
458 * maplist of map on b.
460 // TODO unused - remove?
461 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
464 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
466 if (refa.getSource() != null && refb.getSource() != null
467 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
468 DBRefUtils.getCanonicalName(refa.getSource())))
470 // We dont care about version
471 if (refa.getAccessionId() != null && refb.getAccessionId() != null
472 || refb.getAccessionId().equals(refa.getAccessionId()))
474 if ((refa.getMap() == null && refb.getMap() == null)
475 || (refa.getMap() != null && refb.getMap() != null))
477 if ((refb.getMap().getMap() == null
478 && refa.getMap().getMap() == null)
479 || (refb.getMap().getMap() != null
480 && refa.getMap().getMap() != null
481 && refb.getMap().getMap().getInverse()
482 .equals(refa.getMap().getMap())))
494 * accession ID and DB must be identical. Version is ignored. No map on both
495 * or or map but no maplist on either or maplist of map on a is equivalent to
496 * the maplist of map on b.
498 // TODO unused - remove?
499 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
502 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
504 if (refa.getSource() != null && refb.getSource() != null
505 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
506 DBRefUtils.getCanonicalName(refa.getSource())))
508 // We dont care about version
509 // if ((refa.getVersion()==null || refb.getVersion()==null)
510 // || refb.getVersion().equals(refa.getVersion()))
512 if (refa.getAccessionId() != null && refb.getAccessionId() != null
513 || refb.getAccessionId().equals(refa.getAccessionId()))
515 if (refa.getMap() == null && refb.getMap() == null)
519 if (refa.getMap() != null && refb.getMap() != null
520 && ((refb.getMap().getMap() == null
521 && refa.getMap().getMap() == null)
522 || (refb.getMap().getMap() != null
523 && refa.getMap().getMap() != null
524 && refb.getMap().getMap()
525 .equals(refa.getMap().getMap()))))
536 * accession ID and DB must be identical, or null on a. Version is ignored. No
537 * map on either or map but no maplist on either or maplist of map on a is
538 * equivalent to the maplist of map on b.
540 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
543 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
545 if (refa.getSource() != null && refb.getSource() != null
546 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
547 DBRefUtils.getCanonicalName(refa.getSource())))
549 // We dont care about version
550 if (refa.getAccessionId() == null
551 || refa.getAccessionId().equals(refb.getAccessionId()))
553 if (refa.getMap() == null || refb.getMap() == null)
557 if ((refa.getMap() != null && refb.getMap() != null)
558 && (refb.getMap().getMap() == null
559 && refa.getMap().getMap() == null)
560 || (refb.getMap().getMap() != null
561 && refa.getMap().getMap() != null
562 && (refb.getMap().getMap()
563 .equals(refa.getMap().getMap()))))
574 * Returns the (possibly empty) list of those supplied dbrefs which have the
575 * specified source database, with a case-insensitive match of source name
581 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
584 List<DBRefEntry> matches = new ArrayList<>();
585 if (dbRefs != null && source != null)
587 for (DBRefEntry dbref : dbRefs)
589 if (source.equalsIgnoreCase(dbref.getSource()))
599 * Returns true if either object is null, or they are equal
605 public static boolean nullOrEqual(Object o1, Object o2)
607 if (o1 == null || o2 == null)
611 return o1.equals(o2);
615 * canonicalise source string before comparing. null is always wildcard
618 * - null or source string to compare
620 * - null or source string to compare
621 * @return true if either o1 or o2 are null, or o1 equals o2 under
622 * DBRefUtils.getCanonicalName
623 * (o1).equals(DBRefUtils.getCanonicalName(o2))
625 public static boolean nullOrEqualSource(String o1, String o2)
627 if (o1 == null || o2 == null)
631 return DBRefUtils.getCanonicalName(o1)
632 .equals(DBRefUtils.getCanonicalName(o2));
636 * Selects just the DNA or protein references from a set of references
639 * if true, select references to 'standard' DNA databases, else to
640 * 'standard' peptide databases
642 * a set of references to select from
645 public static List<DBRefEntry> selectDbRefs(boolean selectDna,
646 List<DBRefEntry> refs)
648 return selectRefs(refs,
649 selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
650 // could attempt to find other cross
651 // refs here - ie PDB xrefs
652 // (not dna, not protein seq)
656 * Returns the (possibly empty) list of those supplied dbrefs which have the
657 * specified source database, with a case-insensitive match of source name
663 public static List<DBRefEntry> searchRefsForSource(
664 List<DBRefEntry> dbRefs, String source)
666 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
667 if (dbRefs != null && source != null)
669 for (DBRefEntry dbref : dbRefs)
671 if (source.equalsIgnoreCase(dbref.getSource()))
681 * promote direct database references to primary for nucleotide or protein
682 * sequences if they have an appropriate primary ref
686 * <th>Primary DB</th>
687 * <th>Direct which will be promoted</th>
708 public static void ensurePrimaries(SequenceI sequence,
716 int sstart = sequence.getStart();
717 int send = sequence.getEnd();
718 boolean isProtein = sequence.isProtein();
719 BitSet bsSelect = new BitSet();
721 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
724 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
725 // if (selfs == null || selfs.size() == 0)
731 List<DBRefEntry> dbrefs = sequence.getDBRefs();
732 bsSelect.set(0, dbrefs.size());
734 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK
735 : DBRefSource.DNA_CODING_MASK, bsSelect))
738 // selfs.addAll(selfArray);
741 // filter non-primary refs
742 for (int ip = pr.size(); --ip >= 0;)
744 DBRefEntry p = pr.get(ip);
745 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect
748 if (dbrefs.get(i) == p)
751 // while (selfs.contains(p))
756 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
758 for (int ip = pr.size(), keys = 0; --ip >= 0
759 && keys != DBRefSource.PRIMARY_MASK;)
761 DBRefEntry p = pr.get(ip);
764 switch (getCanonicalName(p.getSource()))
766 case DBRefSource.UNIPROT:
767 keys |= DBRefSource.UNIPROT_MASK;
769 case DBRefSource.ENSEMBL:
770 keys |= DBRefSource.ENSEMBL_MASK;
776 // TODO: promote transcript refs ??
778 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
780 // if (candidates != null)
782 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect
784 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
786 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
789 Mapping map = cand.getMap();
790 SequenceI cto = map.getTo();
791 if (cto != null && cto != sequence)
793 // can't promote refs with mappings to other sequences
796 MapList mlist = map.getMap();
797 if (mlist.getFromLowest() != sstart
798 && mlist.getFromHighest() != send)
800 // can't promote refs with mappings from a region of this sequence
805 // and promote - not that version must be non-null here,
806 // as p must have passed isPrimaryCandidate()
807 cand.setVersion(cand.getVersion() + " (promoted)");
809 // selfs.remove(cand);
810 // toPromote.add(cand);
811 if (!cand.isPrimaryCandidate())
814 "Warning: Couldn't promote dbref " + cand.toString()
815 + " for sequence " + sequence.toString());