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()))
355 private static Regex PARSE_REGEX;
357 private static Regex getParseRegex()
359 return (PARSE_REGEX == null ? PARSE_REGEX = Platform.newRegex(
360 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)")
365 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
368 * Used by file parsers to generate DBRefs from annotation within file (eg
375 * where to annotate with reference
376 * @return parsed version of entry that was added to seq (if any)
378 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
379 String version, String acn)
381 DBRefEntry ref = null;
384 String locsrc = DBRefUtils.getCanonicalName(dbname);
385 if (locsrc.equals(DBRefSource.PDB))
388 * Check for PFAM style stockhom PDB accession id citation e.g.
391 Regex r = getParseRegex();
392 if (r.search(acn.trim()))
394 String pdbid = r.stringMatched(1);
395 String chaincode = r.stringMatched(2);
396 if (chaincode == null)
400 // String mapstart = r.stringMatched(3);
401 // String mapend = r.stringMatched(4);
402 if (chaincode.equals(" "))
406 // construct pdb ref.
407 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
408 PDBEntry pdbr = new PDBEntry();
410 pdbr.setType(PDBEntry.Type.PDB);
411 pdbr.setChainCode(chaincode);
416 System.err.println("Malformed PDB DR line:" + acn);
422 ref = new DBRefEntry(locsrc, version, acn.trim());
433 * accession ID and DB must be identical. Version is ignored. Map is either
434 * not defined or is a match (or is compatible?)
436 // TODO unused - remove?
437 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
440 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
442 if (refa.getSource() != null && refb.getSource() != null
443 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
444 DBRefUtils.getCanonicalName(refa.getSource())))
446 // We dont care about version
447 if (refa.getAccessionId() != null && refb.getAccessionId() != null
448 // FIXME should be && not || here?
449 || refb.getAccessionId().equals(refa.getAccessionId()))
451 if ((refa.getMap() == null || refb.getMap() == null)
452 || (refa.getMap() != null && refb.getMap() != null
453 && refb.getMap().equals(refa.getMap())))
464 * accession ID and DB must be identical. Version is ignored. No map on either
465 * or map but no maplist on either or maplist of map on a is the complement of
466 * maplist of map on b.
468 // TODO unused - remove?
469 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
472 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
474 if (refa.getSource() != null && refb.getSource() != null
475 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
476 DBRefUtils.getCanonicalName(refa.getSource())))
478 // We dont care about version
479 if (refa.getAccessionId() != null && refb.getAccessionId() != null
480 || refb.getAccessionId().equals(refa.getAccessionId()))
482 if ((refa.getMap() == null && refb.getMap() == null)
483 || (refa.getMap() != null && refb.getMap() != null))
485 if ((refb.getMap().getMap() == null
486 && refa.getMap().getMap() == null)
487 || (refb.getMap().getMap() != null
488 && refa.getMap().getMap() != null
489 && refb.getMap().getMap().getInverse()
490 .equals(refa.getMap().getMap())))
502 * accession ID and DB must be identical. Version is ignored. No map on both
503 * or or map but no maplist on either or maplist of map on a is equivalent to
504 * the maplist of map on b.
506 // TODO unused - remove?
507 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
510 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
512 if (refa.getSource() != null && refb.getSource() != null
513 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
514 DBRefUtils.getCanonicalName(refa.getSource())))
516 // We dont care about version
517 // if ((refa.getVersion()==null || refb.getVersion()==null)
518 // || refb.getVersion().equals(refa.getVersion()))
520 if (refa.getAccessionId() != null && refb.getAccessionId() != null
521 || refb.getAccessionId().equals(refa.getAccessionId()))
523 if (refa.getMap() == null && refb.getMap() == null)
527 if (refa.getMap() != null && refb.getMap() != null
528 && ((refb.getMap().getMap() == null
529 && refa.getMap().getMap() == null)
530 || (refb.getMap().getMap() != null
531 && refa.getMap().getMap() != null
532 && refb.getMap().getMap()
533 .equals(refa.getMap().getMap()))))
544 * accession ID and DB must be identical, or null on a. Version is ignored. No
545 * map on either or map but no maplist on either or maplist of map on a is
546 * equivalent to the maplist of map on b.
548 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
551 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
553 if (refa.getSource() != null && refb.getSource() != null
554 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
555 DBRefUtils.getCanonicalName(refa.getSource())))
557 // We dont care about version
558 if (refa.getAccessionId() == null
559 || refa.getAccessionId().equals(refb.getAccessionId()))
561 if (refa.getMap() == null || refb.getMap() == null)
565 if ((refa.getMap() != null && refb.getMap() != null)
566 && (refb.getMap().getMap() == null
567 && refa.getMap().getMap() == null)
568 || (refb.getMap().getMap() != null
569 && refa.getMap().getMap() != null
570 && (refb.getMap().getMap()
571 .equals(refa.getMap().getMap()))))
582 * Returns the (possibly empty) list of those supplied dbrefs which have the
583 * specified source database, with a case-insensitive match of source name
589 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
592 List<DBRefEntry> matches = new ArrayList<>();
593 if (dbRefs != null && source != null)
595 for (DBRefEntry dbref : dbRefs)
597 if (source.equalsIgnoreCase(dbref.getSource()))
607 * Returns true if either object is null, or they are equal
613 public static boolean nullOrEqual(Object o1, Object o2)
615 if (o1 == null || o2 == null)
619 return o1.equals(o2);
623 * canonicalise source string before comparing. null is always wildcard
626 * - null or source string to compare
628 * - null or source string to compare
629 * @return true if either o1 or o2 are null, or o1 equals o2 under
630 * DBRefUtils.getCanonicalName
631 * (o1).equals(DBRefUtils.getCanonicalName(o2))
633 public static boolean nullOrEqualSource(String o1, String o2)
635 if (o1 == null || o2 == null)
639 return DBRefUtils.getCanonicalName(o1)
640 .equals(DBRefUtils.getCanonicalName(o2));
644 * Selects just the DNA or protein references from a set of references
647 * if true, select references to 'standard' DNA databases, else to
648 * 'standard' peptide databases
650 * a set of references to select from
653 public static List<DBRefEntry> selectDbRefs(boolean selectDna,
654 List<DBRefEntry> refs)
656 return selectRefs(refs,
657 selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
658 // could attempt to find other cross
659 // refs here - ie PDB xrefs
660 // (not dna, not protein seq)
664 * Returns the (possibly empty) list of those supplied dbrefs which have the
665 * specified source database, with a case-insensitive match of source name
671 public static List<DBRefEntry> searchRefsForSource(
672 List<DBRefEntry> dbRefs, String source)
674 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
675 if (dbRefs != null && source != null)
677 for (DBRefEntry dbref : dbRefs)
679 if (source.equalsIgnoreCase(dbref.getSource()))
689 * promote direct database references to primary for nucleotide or protein
690 * sequences if they have an appropriate primary ref
694 * <th>Primary DB</th>
695 * <th>Direct which will be promoted</th>
716 public static void ensurePrimaries(SequenceI sequence,
724 int sstart = sequence.getStart();
725 int send = sequence.getEnd();
726 boolean isProtein = sequence.isProtein();
727 BitSet bsSelect = new BitSet();
729 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
732 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
733 // if (selfs == null || selfs.size() == 0)
739 List<DBRefEntry> dbrefs = sequence.getDBRefs();
740 bsSelect.set(0, dbrefs.size());
742 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK
743 : DBRefSource.DNA_CODING_MASK, bsSelect))
746 // selfs.addAll(selfArray);
749 // filter non-primary refs
750 for (int ip = pr.size(); --ip >= 0;)
752 DBRefEntry p = pr.get(ip);
753 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect
756 if (dbrefs.get(i) == p)
759 // while (selfs.contains(p))
764 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
766 for (int ip = pr.size(), keys = 0; --ip >= 0
767 && keys != DBRefSource.PRIMARY_MASK;)
769 DBRefEntry p = pr.get(ip);
772 switch (getCanonicalName(p.getSource()))
774 case DBRefSource.UNIPROT:
775 keys |= DBRefSource.UNIPROT_MASK;
777 case DBRefSource.ENSEMBL:
778 keys |= DBRefSource.ENSEMBL_MASK;
784 // TODO: promote transcript refs ??
786 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
788 // if (candidates != null)
790 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect
792 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
794 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
797 Mapping map = cand.getMap();
798 SequenceI cto = map.getTo();
799 if (cto != null && cto != sequence)
801 // can't promote refs with mappings to other sequences
804 MapList mlist = map.getMap();
805 if (mlist.getFromLowest() != sstart
806 && mlist.getFromHighest() != send)
808 // can't promote refs with mappings from a region of this sequence
813 // and promote - not that version must be non-null here,
814 // as p must have passed isPrimaryCandidate()
815 cand.setVersion(cand.getVersion() + " (promoted)");
817 // selfs.remove(cand);
818 // toPromote.add(cand);
819 if (!cand.isPrimaryCandidate())
822 "Warning: Couldn't promote dbref " + cand.toString()
823 + " for sequence " + sequence.toString());