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;
30 import com.stevesoft.pat.Regex;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.DBRefSource;
34 import jalview.datamodel.Mapping;
35 import jalview.datamodel.PDBEntry;
36 import jalview.datamodel.SequenceI;
39 * Utilities for handling DBRef objects and their collections.
41 public class DBRefUtils
44 * lookup from lower-case form of a name to its canonical (standardised) form
46 private static Map<String, String> canonicalSourceNameLookup = new HashMap<>();
48 public final static int DB_SOURCE = 1;
49 public final static int DB_VERSION = 2;
50 public final static int DB_ID = 4;
51 public final static int DB_MAP = 8;
53 public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
54 public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID | DB_MAP;
58 // TODO load these from a resource file?
59 canonicalSourceNameLookup.put("uniprotkb/swiss-prot", DBRefSource.UNIPROT);
60 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
62 // Ensembl values for dbname in xref REST service:
63 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
64 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
66 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
67 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
68 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
70 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
71 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
73 // guarantee we always have lowercase entries for canonical string lookups
74 for (String k : canonicalSourceNameLookup.keySet())
76 canonicalSourceNameLookup.put(k.toLowerCase(),
77 canonicalSourceNameLookup.get(k));
82 * Returns those DBRefEntry objects whose source identifier (once converted to
83 * Jalview's canonical form) is in the list of sources to search for. Returns
84 * null if no matches found.
86 * @param dbrefs DBRefEntry objects to search
87 * @param sources array of sources to select
90 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs, String[] sources)
92 if (dbrefs == null || sources == null)
98 HashSet<String> srcs = new HashSet<String>();
99 for (String src : sources)
101 srcs.add(src.toUpperCase());
104 int nrefs = dbrefs.size();
105 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
106 for (int ib = 0; ib < nrefs; ib++)
108 DBRefEntry dbr = dbrefs.get(ib);
109 String source = getCanonicalName(dbr.getSource());
110 if (srcs.contains(source.toUpperCase()))
117 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
118 return res;// .toArray(reply);
123 private static boolean selectRefsBS(List<DBRefEntry> dbrefs, int sourceKeys, BitSet bsSelect)
125 if (dbrefs == null || sourceKeys == 0)
129 for (int i = 0, n = dbrefs.size(); i < n; i++)
131 DBRefEntry dbr = dbrefs.get(i);
132 if ((dbr.getSourceKey() & sourceKeys) != 0)
137 return !bsSelect.isEmpty();
141 * Returns a (possibly empty) list of those references that match the given
142 * entry, according to the given comparator.
145 * an array of database references to search
147 * an entry to compare against
151 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
152 DbRefComp comparator)
154 List<DBRefEntry> rfs = new ArrayList<>();
155 if (refs == null || entry == null)
159 for (int i = 0; i < refs.length; i++)
161 if (comparator.matches(entry, refs[i]))
170 * look up source in an internal list of database reference sources and return
171 * the canonical jalview name for the source, or the original string if it has
175 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or
178 public static String getCanonicalName(String source)
184 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
185 return canonical == null ? source : canonical;
189 * Returns a (possibly empty) list of those references that match the given
190 * entry. Currently uses a comparator which matches if
192 * <li>database sources are the same</li>
193 * <li>accession ids are the same</li>
194 * <li>both have no mapping, or the mappings are the same</li>
197 * @param ref Set of references to search
198 * @param entry pattern to match
199 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
202 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref, DBRefEntry entry, int mode) {
203 return searchRefs(ref, entry, matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
207 * Returns a list of those references that match the given accession id
209 * <li>database sources are the same</li>
210 * <li>accession ids are the same</li>
211 * <li>both have no mapping, or the mappings are the same</li>
214 * @param refs Set of references to search
215 * @param accId accession id to match
218 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, String accId) {
219 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
220 if (refs == null || accId == null) {
223 for (int i = 0, n = refs.size(); i < n; i++) {
224 DBRefEntry e = refs.get(i);
225 if (accId.equals(e.getAccessionId())) {
230 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId, SEARCH_MODE_FULL);
234 * Returns a (possibly empty) list of those references that match the given
235 * entry, according to the given comparator.
237 * @param refs an array of database references to search
238 * @param entry an entry to compare against
240 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION
244 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, DBRefEntry entry, DbRefComp comparator, int mode) {
245 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
246 if (refs == null || entry == null) {
249 for (int i = 0, n = refs.size(); i < n; i++) {
250 DBRefEntry e = refs.get(i);
251 if (comparator.matches(entry, e, SEARCH_MODE_FULL)) {
258 interface DbRefComp {
259 default public boolean matches(DBRefEntry refa, DBRefEntry refb) {
260 return matches(refa, refb, SEARCH_MODE_FULL);
263 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
267 * match on all non-null fields in refa
269 // TODO unused - remove? would be broken by equating "" with null
270 public static DbRefComp matchNonNullonA = new DbRefComp() {
272 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
273 if ((mode & DB_SOURCE) != 0 &&
274 (refa.getSource() == null || DBRefUtils.getCanonicalName(refb.getSource())
275 .equals(DBRefUtils.getCanonicalName(refa.getSource())))) {
276 if ((mode & DB_VERSION) != 0 &&
277 (refa.getVersion() == null || refb.getVersion().equals(refa.getVersion()))) {
278 if ((mode & DB_ID) != 0 &&
279 (refa.getAccessionId() == null || refb.getAccessionId().equals(refa.getAccessionId()))) {
280 if ((mode & DB_MAP) != 0 &&
281 (refa.getMap() == null || (refb.getMap() != null && refb.getMap().equals(refa.getMap())))) {
292 * either field is null or field matches for all of source, version, accession
295 // TODO unused - remove?
296 public static DbRefComp matchEitherNonNull = new DbRefComp() {
298 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
299 if (nullOrEqualSource(refa.getSource(), refb.getSource())
300 && nullOrEqual(refa.getVersion(), refb.getVersion())
301 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
302 && nullOrEqual(refa.getMap(), refb.getMap())) {
311 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
314 * Used by file parsers to generate DBRefs from annotation within file (eg
321 * where to annotate with reference
322 * @return parsed version of entry that was added to seq (if any)
324 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
325 String version, String acn)
327 DBRefEntry ref = null;
330 String locsrc = DBRefUtils.getCanonicalName(dbname);
331 if (locsrc.equals(DBRefSource.PDB))
334 * Check for PFAM style stockhom PDB accession id citation e.g.
337 Regex r = new com.stevesoft.pat.Regex(
338 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
339 if (r.search(acn.trim()))
341 String pdbid = r.stringMatched(1);
342 String chaincode = r.stringMatched(2);
343 if (chaincode == null)
347 // String mapstart = r.stringMatched(3);
348 // String mapend = r.stringMatched(4);
349 if (chaincode.equals(" "))
353 // construct pdb ref.
354 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
355 PDBEntry pdbr = new PDBEntry();
357 pdbr.setType(PDBEntry.Type.PDB);
358 pdbr.setChainCode(chaincode);
363 System.err.println("Malformed PDB DR line:" + acn);
369 ref = new DBRefEntry(locsrc, version, acn.trim());
380 * accession ID and DB must be identical. Version is ignored. Map is either not
381 * defined or is a match (or is compatible?)
383 // TODO unused - remove?
384 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp() {
386 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
387 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
388 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
389 // We dont care about version
390 if (refa.getAccessionId() != null && refb.getAccessionId() != null
391 // FIXME should be && not || here?
392 || refb.getAccessionId().equals(refa.getAccessionId())) {
393 if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
394 && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
404 * accession ID and DB must be identical. Version is ignored. No map on either
405 * or map but no maplist on either or maplist of map on a is the complement of
406 * maplist of map on b.
408 // TODO unused - remove?
409 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp() {
411 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
412 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
413 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
414 // We dont care about version
415 if (refa.getAccessionId() != null && refb.getAccessionId() != null
416 || refb.getAccessionId().equals(refa.getAccessionId())) {
417 if ((refa.getMap() == null && refb.getMap() == null)
418 || (refa.getMap() != null && refb.getMap() != null)) {
419 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
420 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
421 && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap()))) {
432 * accession ID and DB must be identical. Version is ignored. No map on both or
433 * or map but no maplist on either or maplist of map on a is equivalent to the
434 * maplist of map on b.
436 // TODO unused - remove?
437 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp() {
439 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
440 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
441 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
442 // We dont care about version
443 // if ((refa.getVersion()==null || refb.getVersion()==null)
444 // || refb.getVersion().equals(refa.getVersion()))
446 if (refa.getAccessionId() != null && refb.getAccessionId() != null
447 || refb.getAccessionId().equals(refa.getAccessionId())) {
448 if (refa.getMap() == null && refb.getMap() == null) {
451 if (refa.getMap() != null && refb.getMap() != null
452 && ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
453 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
454 && refb.getMap().getMap().equals(refa.getMap().getMap())))) {
464 * accession ID and DB must be identical, or null on a. Version is ignored. No
465 * map on either or map but no maplist on either or maplist of map on a is
466 * equivalent to the maplist of map on b.
468 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
471 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode)
473 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
474 .equals(DBRefUtils.getCanonicalName(refa.getSource())))
476 // We dont care about version
477 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId()))
479 if (refa.getMap() == null || refb.getMap() == null)
483 if ((refa.getMap() != null && refb.getMap() != null)
484 && (refb.getMap().getMap() == null && refa.getMap().getMap() == null)
485 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
486 && (refb.getMap().getMap().equals(refa.getMap().getMap()))))
497 * Returns the (possibly empty) list of those supplied dbrefs which have the
498 * specified source database, with a case-insensitive match of source name
504 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
507 List<DBRefEntry> matches = new ArrayList<>();
508 if (dbRefs != null && source != null)
510 for (DBRefEntry dbref : dbRefs)
512 if (source.equalsIgnoreCase(dbref.getSource()))
522 * Returns true if either object is null, or they are equal
528 public static boolean nullOrEqual(Object o1, Object o2) {
529 if (o1 == null || o2 == null) {
532 return o1.equals(o2);
536 * canonicalise source string before comparing. null is always wildcard
538 * @param o1 - null or source string to compare
539 * @param o2 - null or source string to compare
540 * @return true if either o1 or o2 are null, or o1 equals o2 under
541 * DBRefUtils.getCanonicalName
542 * (o1).equals(DBRefUtils.getCanonicalName(o2))
544 public static boolean nullOrEqualSource(String o1, String o2) {
545 if (o1 == null || o2 == null) {
548 return DBRefUtils.getCanonicalName(o1).equals(DBRefUtils.getCanonicalName(o2));
552 * Selects just the DNA or protein references from a set of references
554 * @param selectDna if true, select references to 'standard' DNA databases, else
555 * to 'standard' peptide databases
556 * @param refs a set of references to select from
559 public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
560 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
561 // could attempt to find other cross
562 // refs here - ie PDB xrefs
563 // (not dna, not protein seq)
567 * Returns the (possibly empty) list of those supplied dbrefs which have the
568 * specified source database, with a case-insensitive match of source name
574 public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
575 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
576 if (dbRefs != null && source != null) {
577 for (DBRefEntry dbref : dbRefs) {
578 if (source.equalsIgnoreCase(dbref.getSource())) {
587 * promote direct database references to primary for nucleotide or protein
588 * sequences if they have an appropriate primary ref
592 * <th>Primary DB</th>
593 * <th>Direct which will be promoted</th>
614 public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
615 if (pr.size() == 0) {
619 int sstart = sequence.getStart();
620 int send = sequence.getEnd();
621 boolean isProtein = sequence.isProtein();
622 BitSet bsSelect = new BitSet();
624 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
627 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
628 // if (selfs == null || selfs.size() == 0)
634 List<DBRefEntry> dbrefs = sequence.getDBRefs();
635 bsSelect.set(0, dbrefs.size());
637 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
640 // selfs.addAll(selfArray);
643 // filter non-primary refs
644 for (int ip = pr.size(); --ip >= 0;) {
645 DBRefEntry p = pr.get(ip);
646 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
647 if (dbrefs.get(i) == p)
650 // while (selfs.contains(p))
655 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
657 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
658 DBRefEntry p = pr.get(ip);
660 switch (getCanonicalName(p.getSource())) {
661 case DBRefSource.UNIPROT:
662 keys |= DBRefSource.UNIPROT_MASK;
664 case DBRefSource.ENSEMBL:
665 keys |= DBRefSource.ENSEMBL_MASK;
669 // TODO: promote transcript refs ??
671 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
673 // if (candidates != null)
675 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect.nextSetBit(ic + 1))
676 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
678 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
680 Mapping map = cand.getMap();
681 SequenceI cto = map.getTo();
682 if (cto != null && cto != sequence) {
683 // can't promote refs with mappings to other sequences
686 MapList mlist = map.getMap();
687 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
688 // can't promote refs with mappings from a region of this sequence
693 // and promote - not that version must be non-null here,
694 // as p must have passed isPrimaryCandidate()
695 cand.setVersion(p.getVersion() + " (promoted)");
697 // selfs.remove(cand);
698 // toPromote.add(cand);
699 if (!cand.isPrimaryCandidate()) {
700 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
701 + sequence.toString());