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.Locale;
25 import java.util.ArrayList;
26 import java.util.BitSet;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import com.stevesoft.pat.Regex;
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 // TODO keep ? (in phyloviewer branch only)
82 canonicalSourceNameLookup.put("pfam", DBRefSource.PFAM);
84 // guarantee we always have lowercase entries for canonical string lookups
85 for (String k : canonicalSourceNameLookup.keySet())
87 canonicalSourceNameLookup.put(k.toLowerCase(Locale.ROOT),
88 canonicalSourceNameLookup.get(k));
93 * Returns those DBRefEntry objects whose source identifier (once converted to
94 * Jalview's canonical form) is in the list of sources to search for. Returns
95 * null if no matches found.
98 * DBRefEntry objects to search
100 * array of sources to select
103 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs,
106 if (dbrefs == null || sources == null)
112 HashSet<String> srcs = new HashSet<String>();
113 for (String src : sources)
115 srcs.add(src.toUpperCase(Locale.ROOT));
118 int nrefs = dbrefs.size();
119 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
120 for (int ib = 0; ib < nrefs; ib++)
122 DBRefEntry dbr = dbrefs.get(ib);
123 String source = getCanonicalName(dbr.getSource());
124 if (srcs.contains(source.toUpperCase(Locale.ROOT)))
131 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
132 return res;// .toArray(reply);
137 private static boolean selectRefsBS(List<DBRefEntry> dbrefs,
138 int sourceKeys, BitSet bsSelect)
140 if (dbrefs == null || sourceKeys == 0)
144 for (int i = 0, n = dbrefs.size(); i < n; i++)
146 DBRefEntry dbr = dbrefs.get(i);
147 if ((dbr.getSourceKey() & sourceKeys) != 0)
152 return !bsSelect.isEmpty();
156 * Returns a (possibly empty) list of those references that match the given
157 * entry, according to the given comparator.
160 * an array of database references to search
162 * an entry to compare against
166 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
167 DbRefComp comparator)
169 List<DBRefEntry> rfs = new ArrayList<>();
170 if (refs == null || entry == null)
174 for (int i = 0; i < refs.length; i++)
176 if (comparator.matches(entry, refs[i]))
185 * look up source in an internal list of database reference sources and return
186 * the canonical jalview name for the source, or the original string if it has
190 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
193 public static String getCanonicalName(String source)
199 String canonical = canonicalSourceNameLookup
200 .get(source.toLowerCase(Locale.ROOT));
201 return canonical == null ? source : canonical;
205 * Returns a (possibly empty) list of those references that match the given
206 * entry. Currently uses a comparator which matches if
208 * <li>database sources are the same</li>
209 * <li>accession ids are the same</li>
210 * <li>both have no mapping, or the mappings are the same</li>
214 * Set of references to search
218 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
221 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref,
222 DBRefEntry entry, int mode)
224 return searchRefs(ref, entry,
225 matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
229 * Returns a list of those references that match the given accession id
231 * <li>database sources are the same</li>
232 * <li>accession ids are the same</li>
233 * <li>both have no mapping, or the mappings are the same</li>
237 * Set of references to search
239 * accession id to match
242 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
245 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
246 if (refs == null || accId == null)
250 for (int i = 0, n = refs.size(); i < n; i++)
252 DBRefEntry e = refs.get(i);
253 if (accId.equals(e.getAccessionId()))
259 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId,
260 // SEARCH_MODE_FULL);
264 * Returns a (possibly empty) list of those references that match the given
265 * entry, according to the given comparator.
268 * an array of database references to search
270 * an entry to compare against
273 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
276 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs,
277 DBRefEntry entry, DbRefComp comparator, int mode)
279 List<DBRefEntry> rfs = new ArrayList<>();
280 if (refs == null || entry == null)
284 for (int i = 0, n = refs.size(); i < n; i++)
286 DBRefEntry e = refs.get(i);
287 if (comparator.matches(entry, e, SEARCH_MODE_FULL))
297 default public boolean matches(DBRefEntry refa, DBRefEntry refb)
299 return matches(refa, refb, SEARCH_MODE_FULL);
302 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
306 * match on all non-null fields in refa
308 // TODO unused - remove? would be broken by equating "" with null
309 public static DbRefComp matchNonNullonA = new DbRefComp()
312 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
314 if ((mode & DB_SOURCE) != 0 && (refa.getSource() == null
315 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
316 DBRefUtils.getCanonicalName(refa.getSource()))))
318 if ((mode & DB_VERSION) != 0 && (refa.getVersion() == null
319 || refb.getVersion().equals(refa.getVersion())))
321 if ((mode & DB_ID) != 0 && (refa.getAccessionId() == null
322 || refb.getAccessionId().equals(refa.getAccessionId())))
324 if ((mode & DB_MAP) != 0
325 && (refa.getMap() == null || (refb.getMap() != null
326 && refb.getMap().equals(refa.getMap()))))
338 * either field is null or field matches for all of source, version, accession
341 // TODO unused - remove?
342 public static DbRefComp matchEitherNonNull = new DbRefComp()
345 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
347 if (nullOrEqualSource(refa.getSource(), refb.getSource())
348 && nullOrEqual(refa.getVersion(), refb.getVersion())
349 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
350 && nullOrEqual(refa.getMap(), refb.getMap()))
360 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
363 * Used by file parsers to generate DBRefs from annotation within file (eg
370 * where to annotate with reference
371 * @return parsed version of entry that was added to seq (if any)
373 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
374 String version, String acn)
376 DBRefEntry ref = null;
379 String locsrc = DBRefUtils.getCanonicalName(dbname);
380 if (locsrc.equals(DBRefSource.PDB))
383 * Check for PFAM style stockhom PDB accession id citation e.g.
386 Regex r = new com.stevesoft.pat.Regex(
387 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
388 if (r.search(acn.trim()))
390 String pdbid = r.stringMatched(1);
391 String chaincode = r.stringMatched(2);
392 if (chaincode == null)
396 // String mapstart = r.stringMatched(3);
397 // String mapend = r.stringMatched(4);
398 if (chaincode.equals(" "))
402 // construct pdb ref.
403 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
404 PDBEntry pdbr = new PDBEntry();
406 pdbr.setType(PDBEntry.Type.PDB);
407 pdbr.setChainCode(chaincode);
412 System.err.println("Malformed PDB DR line:" + acn);
418 ref = new DBRefEntry(locsrc, version, acn.trim());
429 * accession ID and DB must be identical. Version is ignored. Map is either
430 * not defined or is a match (or is compatible?)
432 // TODO unused - remove?
433 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
436 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
438 if (refa.getSource() != null && refb.getSource() != null
439 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
440 DBRefUtils.getCanonicalName(refa.getSource())))
442 // We dont care about version
443 if (refa.getAccessionId() != null && refb.getAccessionId() != null
444 // FIXME should be && not || here?
445 || refb.getAccessionId().equals(refa.getAccessionId()))
447 if ((refa.getMap() == null || refb.getMap() == null)
448 || (refa.getMap() != null && refb.getMap() != null
449 && refb.getMap().equals(refa.getMap())))
460 * accession ID and DB must be identical. Version is ignored. No map on either
461 * or map but no maplist on either or maplist of map on a is the complement of
462 * maplist of map on b.
464 // TODO unused - remove?
465 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
468 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
470 if (refa.getSource() != null && refb.getSource() != null
471 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
472 DBRefUtils.getCanonicalName(refa.getSource())))
474 // We dont care about version
475 if (refa.getAccessionId() != null && refb.getAccessionId() != null
476 || refb.getAccessionId().equals(refa.getAccessionId()))
478 if ((refa.getMap() == null && refb.getMap() == null)
479 || (refa.getMap() != null && refb.getMap() != null))
481 if ((refb.getMap().getMap() == null
482 && refa.getMap().getMap() == null)
483 || (refb.getMap().getMap() != null
484 && refa.getMap().getMap() != null
485 && refb.getMap().getMap().getInverse()
486 .equals(refa.getMap().getMap())))
498 * accession ID and DB must be identical. Version is ignored. No map on both
499 * or or map but no maplist on either or maplist of map on a is equivalent to
500 * the maplist of map on b.
502 // TODO unused - remove?
503 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
506 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
508 if (refa.getSource() != null && refb.getSource() != null
509 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
510 DBRefUtils.getCanonicalName(refa.getSource())))
512 // We dont care about version
513 // if ((refa.getVersion()==null || refb.getVersion()==null)
514 // || refb.getVersion().equals(refa.getVersion()))
516 if (refa.getAccessionId() != null && refb.getAccessionId() != null
517 || refb.getAccessionId().equals(refa.getAccessionId()))
519 if (refa.getMap() == null && refb.getMap() == null)
523 if (refa.getMap() != null && refb.getMap() != null
524 && ((refb.getMap().getMap() == null
525 && refa.getMap().getMap() == null)
526 || (refb.getMap().getMap() != null
527 && refa.getMap().getMap() != null
528 && refb.getMap().getMap()
529 .equals(refa.getMap().getMap()))))
540 * accession ID and DB must be identical, or null on a. Version is ignored. No
541 * map on either or map but no maplist on either or maplist of map on a is
542 * equivalent to the maplist of map on b.
544 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
547 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
549 if (refa.getSource() != null && refb.getSource() != null
550 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
551 DBRefUtils.getCanonicalName(refa.getSource())))
553 // We dont care about version
554 if (refa.getAccessionId() == null
555 || refa.getAccessionId().equals(refb.getAccessionId()))
557 if (refa.getMap() == null || refb.getMap() == null)
561 if ((refa.getMap() != null && refb.getMap() != null)
562 && (refb.getMap().getMap() == null
563 && refa.getMap().getMap() == null)
564 || (refb.getMap().getMap() != null
565 && refa.getMap().getMap() != null
566 && (refb.getMap().getMap()
567 .equals(refa.getMap().getMap()))))
578 * Returns the (possibly empty) list of those supplied dbrefs which have the
579 * specified source database, with a case-insensitive match of source name
585 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
588 List<DBRefEntry> matches = new ArrayList<>();
589 if (dbRefs != null && source != null)
591 for (DBRefEntry dbref : dbRefs)
593 if (source.equalsIgnoreCase(dbref.getSource()))
603 * Returns true if either object is null, or they are equal
609 public static boolean nullOrEqual(Object o1, Object o2)
611 if (o1 == null || o2 == null)
615 return o1.equals(o2);
619 * canonicalise source string before comparing. null is always wildcard
622 * - null or source string to compare
624 * - null or source string to compare
625 * @return true if either o1 or o2 are null, or o1 equals o2 under
626 * DBRefUtils.getCanonicalName
627 * (o1).equals(DBRefUtils.getCanonicalName(o2))
629 public static boolean nullOrEqualSource(String o1, String o2)
631 if (o1 == null || o2 == null)
635 return DBRefUtils.getCanonicalName(o1)
636 .equals(DBRefUtils.getCanonicalName(o2));
640 * Selects just the DNA or protein references from a set of references
643 * if true, select references to 'standard' DNA databases, else to
644 * 'standard' peptide databases
646 * a set of references to select from
649 public static List<DBRefEntry> selectDbRefs(boolean selectDna,
650 List<DBRefEntry> refs)
652 return selectRefs(refs,
653 selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
654 // could attempt to find other cross
655 // refs here - ie PDB xrefs
656 // (not dna, not protein seq)
660 * Returns the (possibly empty) list of those supplied dbrefs which have the
661 * specified source database, with a case-insensitive match of source name
667 public static List<DBRefEntry> searchRefsForSource(
668 List<DBRefEntry> dbRefs, String source)
670 List<DBRefEntry> matches = new ArrayList<>();
671 if (dbRefs != null && source != null)
673 for (DBRefEntry dbref : dbRefs)
675 if (source.equalsIgnoreCase(dbref.getSource()))
685 * promote direct database references to primary for nucleotide or protein
686 * sequences if they have an appropriate primary ref
690 * <th>Primary DB</th>
691 * <th>Direct which will be promoted</th>
712 public static void ensurePrimaries(SequenceI sequence,
720 int sstart = sequence.getStart();
721 int send = sequence.getEnd();
722 boolean isProtein = sequence.isProtein();
723 BitSet bsSelect = new BitSet();
725 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
728 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
729 // if (selfs == null || selfs.size() == 0)
735 List<DBRefEntry> dbrefs = sequence.getDBRefs();
736 bsSelect.set(0, dbrefs.size());
738 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK
739 : DBRefSource.DNA_CODING_MASK, bsSelect))
742 // selfs.addAll(selfArray);
745 // filter non-primary refs
746 for (int ip = pr.size(); --ip >= 0;)
748 DBRefEntry p = pr.get(ip);
749 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect
752 if (dbrefs.get(i) == p)
755 // while (selfs.contains(p))
760 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
762 for (int ip = pr.size(), keys = 0; --ip >= 0
763 && keys != DBRefSource.PRIMARY_MASK;)
765 DBRefEntry p = pr.get(ip);
768 switch (getCanonicalName(p.getSource()))
770 case DBRefSource.UNIPROT:
771 keys |= DBRefSource.UNIPROT_MASK;
773 case DBRefSource.ENSEMBL:
774 keys |= DBRefSource.ENSEMBL_MASK;
780 // TODO: promote transcript refs ??
782 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
784 // if (candidates != null)
786 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect
788 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
790 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
793 Mapping map = cand.getMap();
794 SequenceI cto = map.getTo();
795 if (cto != null && cto != sequence)
797 // can't promote refs with mappings to other sequences
800 MapList mlist = map.getMap();
801 if (mlist.getFromLowest() != sstart
802 && mlist.getFromHighest() != send)
804 // can't promote refs with mappings from a region of this sequence
809 // and promote - not that version must be non-null here,
810 // as p must have passed isPrimaryCandidate()
811 cand.setVersion(p.getVersion() + " (promoted)");
813 // selfs.remove(cand);
814 // toPromote.add(cand);
815 if (!cand.isPrimaryCandidate())
818 "Warning: Couldn't promote dbref " + cand.toString()
819 + " for sequence " + sequence.toString());