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.bin.Console;
34 import jalview.datamodel.DBRefEntry;
35 import jalview.datamodel.DBRefSource;
36 import jalview.datamodel.Mapping;
37 import jalview.datamodel.PDBEntry;
38 import jalview.datamodel.SequenceI;
41 * Utilities for handling DBRef objects and their collections.
43 public class DBRefUtils
46 * lookup from lower-case form of a name to its canonical (standardised) form
48 private static Map<String, String> canonicalSourceNameLookup = new HashMap<>();
50 public final static int DB_SOURCE = 1;
52 public final static int DB_VERSION = 2;
54 public final static int DB_ID = 4;
56 public final static int DB_MAP = 8;
58 public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
60 public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID
65 // TODO load these from a resource file?
66 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
68 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
70 // Ensembl values for dbname in xref REST service:
71 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
72 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
74 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
75 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
76 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
78 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
79 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
81 // guarantee we always have lowercase entries for canonical string lookups
82 for (String k : canonicalSourceNameLookup.keySet())
84 canonicalSourceNameLookup.put(k.toLowerCase(Locale.ROOT),
85 canonicalSourceNameLookup.get(k));
90 * Returns those DBRefEntry objects whose source identifier (once converted to
91 * Jalview's canonical form) is in the list of sources to search for. Returns
92 * null if no matches found.
95 * DBRefEntry objects to search
97 * array of sources to select
100 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs,
103 if (dbrefs == null || sources == null)
109 HashSet<String> srcs = new HashSet<String>();
110 for (String src : sources)
112 srcs.add(src.toUpperCase(Locale.ROOT));
115 int nrefs = dbrefs.size();
116 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
117 for (int ib = 0; ib < nrefs; ib++)
119 DBRefEntry dbr = dbrefs.get(ib);
120 String source = getCanonicalName(dbr.getSource());
121 if (srcs.contains(source.toUpperCase(Locale.ROOT)))
128 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
129 return res;// .toArray(reply);
134 private static boolean selectRefsBS(List<DBRefEntry> dbrefs,
135 int sourceKeys, BitSet bsSelect)
137 if (dbrefs == null || sourceKeys == 0)
141 for (int i = 0, n = dbrefs.size(); i < n; i++)
143 DBRefEntry dbr = dbrefs.get(i);
144 if ((dbr.getSourceKey() & sourceKeys) != 0)
149 return !bsSelect.isEmpty();
153 * Returns a (possibly empty) list of those references that match the given
154 * entry, according to the given comparator.
157 * an array of database references to search
159 * an entry to compare against
163 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
164 DbRefComp comparator)
166 List<DBRefEntry> rfs = new ArrayList<>();
167 if (refs == null || entry == null)
171 for (int i = 0; i < refs.length; i++)
173 if (comparator.matches(entry, refs[i]))
182 * look up source in an internal list of database reference sources and return
183 * the canonical jalview name for the source, or the original string if it has
187 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
190 public static String getCanonicalName(String source)
196 String canonical = canonicalSourceNameLookup
197 .get(source.toLowerCase(Locale.ROOT));
198 return canonical == null ? source : canonical;
202 * Returns a (possibly empty) list of those references that match the given
203 * entry. Currently uses a comparator which matches if
205 * <li>database sources are the same</li>
206 * <li>accession ids are the same</li>
207 * <li>both have no mapping, or the mappings are the same</li>
211 * Set of references to search
215 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
218 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref,
219 DBRefEntry entry, int mode)
221 return searchRefs(ref, entry,
222 matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
226 * Returns a list of those references that match the given accession id
228 * <li>database sources are the same</li>
229 * <li>accession ids are the same</li>
230 * <li>both have no mapping, or the mappings are the same</li>
234 * Set of references to search
236 * accession id to match
239 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
242 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
243 if (refs == null || accId == null)
247 for (int i = 0, n = refs.size(); i < n; i++)
249 DBRefEntry e = refs.get(i);
250 if (accId.equals(e.getAccessionId()))
256 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId,
257 // SEARCH_MODE_FULL);
261 * Returns a (possibly empty) list of those references that match the given
262 * entry, according to the given comparator.
265 * an array of database references to search
267 * an entry to compare against
270 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
273 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
274 DBRefEntry entry, DbRefComp comparator, int mode)
276 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
277 if (refs == null || entry == null)
281 for (int i = 0, n = refs.size(); i < n; i++)
283 DBRefEntry e = refs.get(i);
284 if (comparator.matches(entry, e, SEARCH_MODE_FULL))
294 default public boolean matches(DBRefEntry refa, DBRefEntry refb)
296 return matches(refa, refb, SEARCH_MODE_FULL);
299 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
303 * match on all non-null fields in refa
305 // TODO unused - remove? would be broken by equating "" with null
306 public static DbRefComp matchNonNullonA = new DbRefComp()
309 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
311 if ((mode & DB_SOURCE) != 0 && (refa.getSource() == null
312 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
313 DBRefUtils.getCanonicalName(refa.getSource()))))
315 if ((mode & DB_VERSION) != 0 && (refa.getVersion() == null
316 || refb.getVersion().equals(refa.getVersion())))
318 if ((mode & DB_ID) != 0 && (refa.getAccessionId() == null
319 || refb.getAccessionId().equals(refa.getAccessionId())))
321 if ((mode & DB_MAP) != 0
322 && (refa.getMap() == null || (refb.getMap() != null
323 && refb.getMap().equals(refa.getMap()))))
335 * either field is null or field matches for all of source, version, accession
338 // TODO unused - remove?
339 public static DbRefComp matchEitherNonNull = new DbRefComp()
342 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
344 if (nullOrEqualSource(refa.getSource(), refb.getSource())
345 && nullOrEqual(refa.getVersion(), refb.getVersion())
346 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
347 && nullOrEqual(refa.getMap(), refb.getMap()))
357 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
360 * Used by file parsers to generate DBRefs from annotation within file (eg
367 * where to annotate with reference
368 * @return parsed version of entry that was added to seq (if any)
370 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
371 String version, String acn)
373 DBRefEntry ref = null;
376 String locsrc = DBRefUtils.getCanonicalName(dbname);
377 if (locsrc.equals(DBRefSource.PDB))
380 * Check for PFAM style stockhom PDB accession id citation e.g.
383 Regex r = new com.stevesoft.pat.Regex(
384 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
385 if (r.search(acn.trim()))
387 String pdbid = r.stringMatched(1);
388 String chaincode = r.stringMatched(2);
389 if (chaincode == null)
393 // String mapstart = r.stringMatched(3);
394 // String mapend = r.stringMatched(4);
395 if (chaincode.equals(" "))
399 // construct pdb ref.
400 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
401 PDBEntry pdbr = new PDBEntry();
403 pdbr.setType(PDBEntry.Type.PDB);
404 pdbr.setChainCode(chaincode);
409 System.err.println("Malformed PDB DR line:" + acn);
415 ref = new DBRefEntry(locsrc, version, acn.trim());
426 * accession ID and DB must be identical. Version is ignored. Map is either
427 * not defined or is a match (or is compatible?)
429 // TODO unused - remove?
430 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
433 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
435 if (refa.getSource() != null && refb.getSource() != null
436 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
437 DBRefUtils.getCanonicalName(refa.getSource())))
439 // We dont care about version
440 if (refa.getAccessionId() != null && refb.getAccessionId() != null
441 // FIXME should be && not || here?
442 || refb.getAccessionId().equals(refa.getAccessionId()))
444 if ((refa.getMap() == null || refb.getMap() == null)
445 || (refa.getMap() != null && refb.getMap() != null
446 && refb.getMap().equals(refa.getMap())))
457 * accession ID and DB must be identical. Version is ignored. No map on either
458 * or map but no maplist on either or maplist of map on a is the complement of
459 * maplist of map on b.
461 // TODO unused - remove?
462 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
465 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
467 if (refa.getSource() != null && refb.getSource() != null
468 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
469 DBRefUtils.getCanonicalName(refa.getSource())))
471 // We dont care about version
472 if (refa.getAccessionId() != null && refb.getAccessionId() != null
473 || refb.getAccessionId().equals(refa.getAccessionId()))
475 if ((refa.getMap() == null && refb.getMap() == null)
476 || (refa.getMap() != null && refb.getMap() != null))
478 if ((refb.getMap().getMap() == null
479 && refa.getMap().getMap() == null)
480 || (refb.getMap().getMap() != null
481 && refa.getMap().getMap() != null
482 && refb.getMap().getMap().getInverse()
483 .equals(refa.getMap().getMap())))
495 * accession ID and DB must be identical. Version is ignored. No map on both
496 * or or map but no maplist on either or maplist of map on a is equivalent to
497 * the maplist of map on b.
499 // TODO unused - remove?
500 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
503 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
505 if (refa.getSource() != null && refb.getSource() != null
506 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
507 DBRefUtils.getCanonicalName(refa.getSource())))
509 // We dont care about version
510 // if ((refa.getVersion()==null || refb.getVersion()==null)
511 // || refb.getVersion().equals(refa.getVersion()))
513 if (refa.getAccessionId() != null && refb.getAccessionId() != null
514 || refb.getAccessionId().equals(refa.getAccessionId()))
516 if (refa.getMap() == null && refb.getMap() == null)
520 if (refa.getMap() != null && refb.getMap() != null
521 && ((refb.getMap().getMap() == null
522 && refa.getMap().getMap() == null)
523 || (refb.getMap().getMap() != null
524 && refa.getMap().getMap() != null
525 && refb.getMap().getMap()
526 .equals(refa.getMap().getMap()))))
537 * accession ID and DB must be identical, or null on a. Version is ignored. No
538 * map on either or map but no maplist on either or maplist of map on a is
539 * equivalent to the maplist of map on b.
541 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
544 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
546 if (refa.getSource() != null && refb.getSource() != null
547 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
548 DBRefUtils.getCanonicalName(refa.getSource())))
550 // We dont care about version
551 if (refa.getAccessionId() == null
552 || refa.getAccessionId().equals(refb.getAccessionId()))
554 if (refa.getMap() == null || refb.getMap() == null)
558 if ((refa.getMap() != null && refb.getMap() != null)
559 && (refb.getMap().getMap() == null
560 && refa.getMap().getMap() == null)
561 || (refb.getMap().getMap() != null
562 && refa.getMap().getMap() != null
563 && (refb.getMap().getMap()
564 .equals(refa.getMap().getMap()))))
575 * Returns the (possibly empty) list of those supplied dbrefs which have the
576 * specified source database, with a case-insensitive match of source name
582 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
585 List<DBRefEntry> matches = new ArrayList<>();
586 if (dbRefs != null && source != null)
588 for (DBRefEntry dbref : dbRefs)
590 if (source.equalsIgnoreCase(dbref.getSource()))
600 * Returns true if either object is null, or they are equal
606 public static boolean nullOrEqual(Object o1, Object o2)
608 if (o1 == null || o2 == null)
612 return o1.equals(o2);
616 * canonicalise source string before comparing. null is always wildcard
619 * - null or source string to compare
621 * - null or source string to compare
622 * @return true if either o1 or o2 are null, or o1 equals o2 under
623 * DBRefUtils.getCanonicalName
624 * (o1).equals(DBRefUtils.getCanonicalName(o2))
626 public static boolean nullOrEqualSource(String o1, String o2)
628 if (o1 == null || o2 == null)
632 return DBRefUtils.getCanonicalName(o1)
633 .equals(DBRefUtils.getCanonicalName(o2));
637 * Selects just the DNA or protein references from a set of references
640 * if true, select references to 'standard' DNA databases, else to
641 * 'standard' peptide databases
643 * a set of references to select from
646 public static List<DBRefEntry> selectDbRefs(boolean selectDna,
647 List<DBRefEntry> refs)
649 return selectRefs(refs,
650 selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
651 // could attempt to find other cross
652 // refs here - ie PDB xrefs
653 // (not dna, not protein seq)
657 * Returns the (possibly empty) list of those supplied dbrefs which have the
658 * specified source database, with a case-insensitive match of source name
664 public static List<DBRefEntry> searchRefsForSource(
665 List<DBRefEntry> dbRefs, String source)
667 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
668 if (dbRefs != null && source != null)
670 for (DBRefEntry dbref : dbRefs)
672 if (source.equalsIgnoreCase(dbref.getSource()))
682 * promote direct database references to primary for nucleotide or protein
683 * sequences if they have an appropriate primary ref
687 * <th>Primary DB</th>
688 * <th>Direct which will be promoted</th>
709 public static void ensurePrimaries(SequenceI sequence,
717 int sstart = sequence.getStart();
718 int send = sequence.getEnd();
719 boolean isProtein = sequence.isProtein();
720 BitSet bsSelect = new BitSet();
722 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
725 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
726 // if (selfs == null || selfs.size() == 0)
732 List<DBRefEntry> dbrefs = sequence.getDBRefs();
733 bsSelect.set(0, dbrefs.size());
735 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK
736 : DBRefSource.DNA_CODING_MASK, bsSelect))
739 // selfs.addAll(selfArray);
742 // filter non-primary refs
743 for (int ip = pr.size(); --ip >= 0;)
745 DBRefEntry p = pr.get(ip);
746 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect
749 if (dbrefs.get(i) == p)
752 // while (selfs.contains(p))
757 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
759 for (int ip = pr.size(), keys = 0; --ip >= 0
760 && keys != DBRefSource.PRIMARY_MASK;)
762 DBRefEntry p = pr.get(ip);
765 switch (getCanonicalName(p.getSource()))
767 case DBRefSource.UNIPROT:
768 keys |= DBRefSource.UNIPROT_MASK;
770 case DBRefSource.ENSEMBL:
771 keys |= DBRefSource.ENSEMBL_MASK;
777 // TODO: promote transcript refs ??
779 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
781 // if (candidates != null)
783 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect
785 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
787 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
790 Mapping map = cand.getMap();
791 SequenceI cto = map.getTo();
792 if (cto != null && cto != sequence)
794 // can't promote refs with mappings to other sequences
797 MapList mlist = map.getMap();
798 if (mlist.getFromLowest() != sstart
799 && mlist.getFromHighest() != send)
801 // can't promote refs with mappings from a region of this sequence
806 // and promote - not that version must be non-null here,
807 // as p must have passed isPrimaryCandidate()
808 cand.setVersion(cand.getVersion() + " (promoted)");
810 // selfs.remove(cand);
811 // toPromote.add(cand);
812 if (!cand.isPrimaryCandidate())
814 if (Console.isDebugEnabled())
817 "Warning: Couldn't promote dbref " + cand.toString()
818 + " for sequence " + sequence.toString());