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 jalview.datamodel.DBRefEntry;
24 import jalview.datamodel.DBRefSource;
25 import jalview.datamodel.Mapping;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
29 import java.util.ArrayList;
30 import java.util.BitSet;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
37 import com.stevesoft.pat.Regex;
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;
50 public final static int DB_VERSION = 2;
51 public final static int DB_ID = 4;
52 public final static int DB_MAP = 8;
54 public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
55 public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID | DB_MAP;
59 // TODO load these from a resource file?
60 canonicalSourceNameLookup.put("uniprotkb/swiss-prot", DBRefSource.UNIPROT);
61 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
63 // Ensembl values for dbname in xref REST service:
64 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
65 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
67 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
68 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
69 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
71 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
72 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
74 // guarantee we always have lowercase entries for canonical string lookups
75 for (String k : canonicalSourceNameLookup.keySet())
77 canonicalSourceNameLookup.put(k.toLowerCase(),
78 canonicalSourceNameLookup.get(k));
83 * Returns those DBRefEntry objects whose source identifier (once converted to
84 * Jalview's canonical form) is in the list of sources to search for. Returns
85 * null if no matches found.
87 * @param dbrefs DBRefEntry objects to search
88 * @param sources array of sources to select
91 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs, String[] sources)
93 if (dbrefs == null || sources == null)
99 HashSet<String> srcs = new HashSet<String>();
100 for (String src : sources)
102 srcs.add(src.toUpperCase());
105 int nrefs = dbrefs.size();
106 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
107 for (int ib = 0; ib < nrefs; ib++)
109 DBRefEntry dbr = dbrefs.get(ib);
110 String source = getCanonicalName(dbr.getSource());
111 if (srcs.contains(source.toUpperCase()))
118 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
119 return res;// .toArray(reply);
124 private static boolean selectRefsBS(List<DBRefEntry> dbrefs, int sourceKeys, BitSet bsSelect)
126 if (dbrefs == null || sourceKeys == 0)
130 for (int i = 0, n = dbrefs.size(); i < n; i++)
132 DBRefEntry dbr = dbrefs.get(i);
133 if ((dbr.getSourceKey() & sourceKeys) != 0)
138 return !bsSelect.isEmpty();
142 * Returns a (possibly empty) list of those references that match the given
143 * entry, according to the given comparator.
146 * an array of database references to search
148 * an entry to compare against
152 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
153 DbRefComp comparator)
155 List<DBRefEntry> rfs = new ArrayList<>();
156 if (refs == null || entry == null)
160 for (int i = 0; i < refs.length; i++)
162 if (comparator.matches(entry, refs[i]))
171 * look up source in an internal list of database reference sources and return
172 * the canonical jalview name for the source, or the original string if it has
176 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or
179 public static String getCanonicalName(String source)
185 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
186 return canonical == null ? source : canonical;
190 * Returns a (possibly empty) list of those references that match the given
191 * entry. Currently uses a comparator which matches if
193 * <li>database sources are the same</li>
194 * <li>accession ids are the same</li>
195 * <li>both have no mapping, or the mappings are the same</li>
198 * @param ref Set of references to search
199 * @param entry pattern to match
200 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
203 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref, DBRefEntry entry, int mode) {
204 return searchRefs(ref, entry, matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
208 * Returns a list of those references that match the given accession id
210 * <li>database sources are the same</li>
211 * <li>accession ids are the same</li>
212 * <li>both have no mapping, or the mappings are the same</li>
215 * @param refs Set of references to search
216 * @param accId accession id to match
219 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, String accId) {
220 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
221 if (refs == null || accId == null) {
224 for (int i = 0, n = refs.size(); i < n; i++) {
225 DBRefEntry e = refs.get(i);
226 if (accId.equals(e.getAccessionId())) {
231 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId, SEARCH_MODE_FULL);
235 * Returns a (possibly empty) list of those references that match the given
236 * entry, according to the given comparator.
238 * @param refs an array of database references to search
239 * @param entry an entry to compare against
241 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION
245 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, DBRefEntry entry, DbRefComp comparator, int mode) {
246 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
247 if (refs == null || entry == null) {
250 for (int i = 0, n = refs.size(); i < n; i++) {
251 DBRefEntry e = refs.get(i);
252 if (comparator.matches(entry, e, SEARCH_MODE_FULL)) {
259 interface DbRefComp {
260 default public boolean matches(DBRefEntry refa, DBRefEntry refb) {
261 return matches(refa, refb, SEARCH_MODE_FULL);
264 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
268 * match on all non-null fields in refa
270 // TODO unused - remove? would be broken by equating "" with null
271 public static DbRefComp matchNonNullonA = new DbRefComp() {
273 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
274 if ((mode & DB_SOURCE) != 0 &&
275 (refa.getSource() == null || DBRefUtils.getCanonicalName(refb.getSource())
276 .equals(DBRefUtils.getCanonicalName(refa.getSource())))) {
277 if ((mode & DB_VERSION) != 0 &&
278 (refa.getVersion() == null || refb.getVersion().equals(refa.getVersion()))) {
279 if ((mode & DB_ID) != 0 &&
280 (refa.getAccessionId() == null || refb.getAccessionId().equals(refa.getAccessionId()))) {
281 if ((mode & DB_MAP) != 0 &&
282 (refa.getMap() == null || (refb.getMap() != null && refb.getMap().equals(refa.getMap())))) {
293 * either field is null or field matches for all of source, version, accession
296 // TODO unused - remove?
297 public static DbRefComp matchEitherNonNull = new DbRefComp() {
299 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
300 if (nullOrEqualSource(refa.getSource(), refb.getSource())
301 && nullOrEqual(refa.getVersion(), refb.getVersion())
302 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
303 && nullOrEqual(refa.getMap(), refb.getMap())) {
312 * accession ID and DB must be identical. Version is ignored. Map is either not
313 * defined or is a match (or is compatible?)
315 // TODO unused - remove?
316 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp() {
318 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
319 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
320 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
321 // We dont care about version
322 if (refa.getAccessionId() != null && refb.getAccessionId() != null
323 // FIXME should be && not || here?
324 || refb.getAccessionId().equals(refa.getAccessionId())) {
325 if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
326 && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
336 * accession ID and DB must be identical. Version is ignored. No map on either
337 * or map but no maplist on either or maplist of map on a is the complement of
338 * maplist of map on b.
340 // TODO unused - remove?
341 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp() {
343 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
344 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
345 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
346 // We dont care about version
347 if (refa.getAccessionId() != null && refb.getAccessionId() != null
348 || refb.getAccessionId().equals(refa.getAccessionId())) {
349 if ((refa.getMap() == null && refb.getMap() == null)
350 || (refa.getMap() != null && refb.getMap() != null)) {
351 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
352 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
353 && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap()))) {
364 * accession ID and DB must be identical. Version is ignored. No map on both or
365 * or map but no maplist on either or maplist of map on a is equivalent to the
366 * maplist of map on b.
368 // TODO unused - remove?
369 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp() {
371 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
372 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
373 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
374 // We dont care about version
375 // if ((refa.getVersion()==null || refb.getVersion()==null)
376 // || refb.getVersion().equals(refa.getVersion()))
378 if (refa.getAccessionId() != null && refb.getAccessionId() != null
379 || refb.getAccessionId().equals(refa.getAccessionId())) {
380 if (refa.getMap() == null && refb.getMap() == null) {
383 if (refa.getMap() != null && refb.getMap() != null
384 && ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
385 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
386 && refb.getMap().getMap().equals(refa.getMap().getMap())))) {
396 * accession ID and DB must be identical, or null on a. Version is ignored. No
397 * map on either or map but no maplist on either or maplist of map on a is
398 * equivalent to the maplist of map on b.
400 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
403 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
405 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
406 .equals(DBRefUtils.getCanonicalName(refa.getSource())))
408 // We dont care about version
409 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId()))
411 if (refa.getMap() == null || refb.getMap() == null)
415 if ((refa.getMap() != null && refb.getMap() != null)
416 && (refb.getMap().getMap() == null && refa.getMap().getMap() == null)
417 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
418 && (refb.getMap().getMap().equals(refa.getMap().getMap()))))
429 * Returns the (possibly empty) list of those supplied dbrefs which have the
430 * specified source database, with a case-insensitive match of source name
436 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
439 List<DBRefEntry> matches = new ArrayList<>();
440 if (dbRefs != null && source != null)
442 for (DBRefEntry dbref : dbRefs)
444 if (source.equalsIgnoreCase(dbref.getSource()))
454 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
457 * Used by file parsers to generate DBRefs from annotation within file (eg
463 * @param seq where to annotate with reference
464 * @return parsed version of entry that was added to seq (if any)
466 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname, String version, String acn) {
467 DBRefEntry ref = null;
468 if (dbname != null) {
469 String locsrc = DBRefUtils.getCanonicalName(dbname);
470 if (locsrc.equals(DBRefSource.PDB)) {
472 * Check for PFAM style stockhom PDB accession id citation e.g. "1WRI A; 7-80;"
474 Regex r = new com.stevesoft.pat.Regex("([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
475 if (r.search(acn.trim())) {
476 String pdbid = r.stringMatched(1);
477 String chaincode = r.stringMatched(2);
478 if (chaincode == null) {
481 // String mapstart = r.stringMatched(3);
482 // String mapend = r.stringMatched(4);
483 if (chaincode.equals(" ")) {
486 // construct pdb ref.
487 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
488 PDBEntry pdbr = new PDBEntry();
490 pdbr.setType(PDBEntry.Type.PDB);
491 pdbr.setChainCode(chaincode);
494 System.err.println("Malformed PDB DR line:" + acn);
498 ref = new DBRefEntry(locsrc, version, acn);
508 * Returns true if either object is null, or they are equal
514 public static boolean nullOrEqual(Object o1, Object o2) {
515 if (o1 == null || o2 == null) {
518 return o1.equals(o2);
522 * canonicalise source string before comparing. null is always wildcard
524 * @param o1 - null or source string to compare
525 * @param o2 - null or source string to compare
526 * @return true if either o1 or o2 are null, or o1 equals o2 under
527 * DBRefUtils.getCanonicalName
528 * (o1).equals(DBRefUtils.getCanonicalName(o2))
530 public static boolean nullOrEqualSource(String o1, String o2) {
531 if (o1 == null || o2 == null) {
534 return DBRefUtils.getCanonicalName(o1).equals(DBRefUtils.getCanonicalName(o2));
538 * Selects just the DNA or protein references from a set of references
540 * @param selectDna if true, select references to 'standard' DNA databases, else
541 * to 'standard' peptide databases
542 * @param refs a set of references to select from
545 public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
546 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
547 // could attempt to find other cross
548 // refs here - ie PDB xrefs
549 // (not dna, not protein seq)
553 * Returns the (possibly empty) list of those supplied dbrefs which have the
554 * specified source database, with a case-insensitive match of source name
560 public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
561 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
562 if (dbRefs != null && source != null) {
563 for (DBRefEntry dbref : dbRefs) {
564 if (source.equalsIgnoreCase(dbref.getSource())) {
573 * promote direct database references to primary for nucleotide or protein
574 * sequences if they have an appropriate primary ref
578 * <th>Primary DB</th>
579 * <th>Direct which will be promoted</th>
600 public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
601 if (pr.size() == 0) {
605 int sstart = sequence.getStart();
606 int send = sequence.getEnd();
607 boolean isProtein = sequence.isProtein();
608 BitSet bsSelect = new BitSet();
610 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
613 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
614 // if (selfs == null || selfs.size() == 0)
620 List<DBRefEntry> dbrefs = sequence.getDBRefs();
621 bsSelect.set(0, dbrefs.size());
623 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
626 // selfs.addAll(selfArray);
629 // filter non-primary refs
630 for (int ip = pr.size(); --ip >= 0;) {
631 DBRefEntry p = pr.get(ip);
632 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
633 if (dbrefs.get(i) == p)
636 // while (selfs.contains(p))
641 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
643 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
644 DBRefEntry p = pr.get(ip);
646 switch (getCanonicalName(p.getSource())) {
647 case DBRefSource.UNIPROT:
648 keys |= DBRefSource.UNIPROT_MASK;
650 case DBRefSource.ENSEMBL:
651 keys |= DBRefSource.ENSEMBL_MASK;
655 // TODO: promote transcript refs ??
657 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
659 // if (candidates != null)
661 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect.nextSetBit(ic + 1))
662 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
664 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
666 Mapping map = cand.getMap();
667 SequenceI cto = map.getTo();
668 if (cto != null && cto != sequence) {
669 // can't promote refs with mappings to other sequences
672 MapList mlist = map.getMap();
673 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
674 // can't promote refs with mappings from a region of this sequence
679 // and promote - not that version must be non-null here,
680 // as p must have passed isPrimaryCandidate()
681 cand.setVersion(p.getVersion() + " (promoted)");
683 // selfs.remove(cand);
684 // toPromote.add(cand);
685 if (!cand.isPrimaryCandidate()) {
686 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
687 + sequence.toString());