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;
36 import com.stevesoft.pat.Regex;
39 * Utilities for handling DBRef objects and their collections.
41 public class DBRefUtils {
43 public final static int DB_SOURCE = 1;
44 public final static int DB_VERSION = 2;
45 public final static int DB_ID = 4;
46 public final static int DB_MAP = 8;
48 public final static int SEARCH_MODE_NO_MAP_NO_VERSION = DB_SOURCE | DB_ID;
49 public final static int SEARCH_MODE_FULL = DB_SOURCE | DB_VERSION | DB_ID | DB_MAP;
52 * lookup from lower-case form of a name to its canonical (standardised) form
54 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
56 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
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 // Make sure we have lowercase entries for all canonical string lookups
75 // BH 2019.01.25 unnecessary -- they are all lower case already
76 // Set<String> keys = canonicalSourceNameLookup.keySet();
77 // for (String k : keys)
79 // canonicalSourceNameLookup.put(k.toLowerCase(),
80 // canonicalSourceNameLookup.get(k));
83 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
84 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
85 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
86 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
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.
94 * @param dbrefs DBRefEntry objects to search
95 * @param sources array of sources to select
98 public static List<DBRefEntry> selectRefs(List<DBRefEntry> dbrefs, String[] sources) {
99 if (dbrefs == null || sources == null) {
104 HashSet<String> srcs = new HashSet<String>();
105 for (String src : sources) {
106 srcs.add(src.toUpperCase());
109 int nrefs = dbrefs.size();
110 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
111 for (int ib = 0; ib < nrefs; ib++) {
112 DBRefEntry dbr = dbrefs.get(ib);
113 String source = getCanonicalName(dbr.getSource());
114 if (srcs.contains(source.toUpperCase())) {
119 if (res.size() > 0) {
120 // List<DBRefEntry> reply = new DBRefEntry[res.size()];
121 return res;// .toArray(reply);
126 private static boolean selectRefsBS(List<DBRefEntry> dbrefs, int sourceKeys, BitSet bsSelect) {
127 if (dbrefs == null || sourceKeys == 0) {
130 for (int i = 0, n = dbrefs.size(); i < n; i++) {
131 DBRefEntry dbr = dbrefs.get(i);
132 if ((dbr.getSourceKey() & sourceKeys) != 0) {
136 return !bsSelect.isEmpty();
140 * isDasCoordinateSystem
142 * @param string String
143 * @param dBRefEntry DBRefEntry
144 * @return boolean true if Source DBRefEntry is compatible with DAS
145 * CoordinateSystem name
148 public static boolean isDasCoordinateSystem(String string, DBRefEntry dBRefEntry) {
149 if (string == null || dBRefEntry == null) {
152 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
153 return coordsys == null ? false : coordsys.equals(dBRefEntry.getSource());
157 * look up source in an internal list of database reference sources and return
158 * the canonical jalview name for the source, or the original string if it has
162 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*) or
165 public static String getCanonicalName(String source) {
166 if (source == null) {
169 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
170 return canonical == null ? source : canonical;
174 * Returns a (possibly empty) list of those references that match the given
175 * entry. Currently uses a comparator which matches if
177 * <li>database sources are the same</li>
178 * <li>accession ids are the same</li>
179 * <li>both have no mapping, or the mappings are the same</li>
182 * @param ref Set of references to search
183 * @param entry pattern to match
184 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
187 public static List<DBRefEntry> searchRefs(List<DBRefEntry> ref, DBRefEntry entry, int mode) {
188 return searchRefs(ref, entry, matchDbAndIdAndEitherMapOrEquivalentMapList, mode);
192 * Returns a list of those references that match the given accession id
194 * <li>database sources are the same</li>
195 * <li>accession ids are the same</li>
196 * <li>both have no mapping, or the mappings are the same</li>
199 * @param refs Set of references to search
200 * @param accId accession id to match
203 public static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, String accId) {
204 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
205 if (refs == null || accId == null) {
208 for (int i = 0, n = refs.size(); i < n; i++) {
209 DBRefEntry e = refs.get(i);
210 if (accId.equals(e.getAccessionId())) {
215 // return searchRefs(refs, new DBRefEntry("", "", accId), matchId, SEARCH_MODE_FULL);
219 * Returns a (possibly empty) list of those references that match the given
220 * entry, according to the given comparator.
222 * @param refs an array of database references to search
223 * @param entry an entry to compare against
225 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION
229 static List<DBRefEntry> searchRefs(List<DBRefEntry> refs, DBRefEntry entry, DbRefComp comparator, int mode) {
230 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
231 if (refs == null || entry == null) {
234 for (int i = 0, n = refs.size(); i < n; i++) {
235 DBRefEntry e = refs.get(i);
236 if (comparator.matches(entry, e, SEARCH_MODE_FULL)) {
243 interface DbRefComp {
244 default public boolean matches(DBRefEntry refa, DBRefEntry refb) {
245 return matches(refa, refb, SEARCH_MODE_FULL);
248 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode);
252 * match on all non-null fields in refa
254 // TODO unused - remove? would be broken by equating "" with null
255 public static DbRefComp matchNonNullonA = new DbRefComp() {
257 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
258 if ((mode & DB_SOURCE) != 0 &&
259 (refa.getSource() == null || DBRefUtils.getCanonicalName(refb.getSource())
260 .equals(DBRefUtils.getCanonicalName(refa.getSource())))) {
261 if ((mode & DB_VERSION) != 0 &&
262 (refa.getVersion() == null || refb.getVersion().equals(refa.getVersion()))) {
263 if ((mode & DB_ID) != 0 &&
264 (refa.getAccessionId() == null || refb.getAccessionId().equals(refa.getAccessionId()))) {
265 if ((mode & DB_MAP) != 0 &&
266 (refa.getMap() == null || (refb.getMap() != null && refb.getMap().equals(refa.getMap())))) {
277 * either field is null or field matches for all of source, version, accession
280 // TODO unused - remove?
281 public static DbRefComp matchEitherNonNull = new DbRefComp() {
283 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
284 if (nullOrEqualSource(refa.getSource(), refb.getSource())
285 && nullOrEqual(refa.getVersion(), refb.getVersion())
286 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
287 && nullOrEqual(refa.getMap(), refb.getMap())) {
296 * accession ID and DB must be identical. Version is ignored. Map is either not
297 * defined or is a match (or is compatible?)
299 // TODO unused - remove?
300 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp() {
302 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
303 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
304 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
305 // We dont care about version
306 if (refa.getAccessionId() != null && refb.getAccessionId() != null
307 // FIXME should be && not || here?
308 || refb.getAccessionId().equals(refa.getAccessionId())) {
309 if ((refa.getMap() == null || refb.getMap() == null) || (refa.getMap() != null
310 && refb.getMap() != null && refb.getMap().equals(refa.getMap()))) {
320 * accession ID and DB must be identical. Version is ignored. No map on either
321 * or map but no maplist on either or maplist of map on a is the complement of
322 * maplist of map on b.
324 // TODO unused - remove?
325 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp() {
327 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
328 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
329 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
330 // We dont care about version
331 if (refa.getAccessionId() != null && refb.getAccessionId() != null
332 || refb.getAccessionId().equals(refa.getAccessionId())) {
333 if ((refa.getMap() == null && refb.getMap() == null)
334 || (refa.getMap() != null && refb.getMap() != null)) {
335 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
336 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
337 && refb.getMap().getMap().getInverse().equals(refa.getMap().getMap()))) {
348 * accession ID and DB must be identical. Version is ignored. No map on both or
349 * or map but no maplist on either or maplist of map on a is equivalent to the
350 * maplist of map on b.
352 // TODO unused - remove?
353 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp() {
355 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
356 if (refa.getSource() != null && refb.getSource() != null && DBRefUtils.getCanonicalName(refb.getSource())
357 .equals(DBRefUtils.getCanonicalName(refa.getSource()))) {
358 // We dont care about version
359 // if ((refa.getVersion()==null || refb.getVersion()==null)
360 // || refb.getVersion().equals(refa.getVersion()))
362 if (refa.getAccessionId() != null && refb.getAccessionId() != null
363 || refb.getAccessionId().equals(refa.getAccessionId())) {
364 if (refa.getMap() == null && refb.getMap() == null) {
367 if (refa.getMap() != null && refb.getMap() != null
368 && ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
369 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
370 && refb.getMap().getMap().equals(refa.getMap().getMap())))) {
380 * accession ID and DB must be identical, or null on a. Version is ignored. No
381 * map on either or map but no maplist on either or maplist of map on a is
382 * equivalent to the maplist of map on b.
384 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = 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
391 if (refa.getAccessionId() == null || refa.getAccessionId().equals(refb.getAccessionId())) {
392 if (refa.getMap() == null || refb.getMap() == null) {
395 if ((refa.getMap() != null && refb.getMap() != null)
396 && (refb.getMap().getMap() == null && refa.getMap().getMap() == null)
397 || (refb.getMap().getMap() != null && refa.getMap().getMap() != null
398 && (refb.getMap().getMap().equals(refa.getMap().getMap())))) {
408 * accession ID only must be identical.
410 public static DbRefComp matchId = new DbRefComp() {
412 public boolean matches(DBRefEntry refa, DBRefEntry refb, int mode) {
413 if (refa.getAccessionId() != null && refb.getAccessionId() != null
414 && refb.getAccessionId().equals(refa.getAccessionId())) {
422 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
425 * Used by file parsers to generate DBRefs from annotation within file (eg
431 * @param seq where to annotate with reference
432 * @return parsed version of entry that was added to seq (if any)
434 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname, String version, String acn) {
435 DBRefEntry ref = null;
436 if (dbname != null) {
437 String locsrc = DBRefUtils.getCanonicalName(dbname);
438 if (locsrc.equals(DBRefSource.PDB)) {
440 * Check for PFAM style stockhom PDB accession id citation e.g. "1WRI A; 7-80;"
442 Regex r = new com.stevesoft.pat.Regex("([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
443 if (r.search(acn.trim())) {
444 String pdbid = r.stringMatched(1);
445 String chaincode = r.stringMatched(2);
446 if (chaincode == null) {
449 // String mapstart = r.stringMatched(3);
450 // String mapend = r.stringMatched(4);
451 if (chaincode.equals(" ")) {
454 // construct pdb ref.
455 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
456 PDBEntry pdbr = new PDBEntry();
458 pdbr.setType(PDBEntry.Type.PDB);
459 pdbr.setChainCode(chaincode);
462 System.err.println("Malformed PDB DR line:" + acn);
466 ref = new DBRefEntry(locsrc, version, acn);
476 * Returns true if either object is null, or they are equal
482 public static boolean nullOrEqual(Object o1, Object o2) {
483 if (o1 == null || o2 == null) {
486 return o1.equals(o2);
490 * canonicalise source string before comparing. null is always wildcard
492 * @param o1 - null or source string to compare
493 * @param o2 - null or source string to compare
494 * @return true if either o1 or o2 are null, or o1 equals o2 under
495 * DBRefUtils.getCanonicalName
496 * (o1).equals(DBRefUtils.getCanonicalName(o2))
498 public static boolean nullOrEqualSource(String o1, String o2) {
499 if (o1 == null || o2 == null) {
502 return DBRefUtils.getCanonicalName(o1).equals(DBRefUtils.getCanonicalName(o2));
506 * Selects just the DNA or protein references from a set of references
508 * @param selectDna if true, select references to 'standard' DNA databases, else
509 * to 'standard' peptide databases
510 * @param refs a set of references to select from
513 public static List<DBRefEntry> selectDbRefs(boolean selectDna, List<DBRefEntry> refs) {
514 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
515 // could attempt to find other cross
516 // refs here - ie PDB xrefs
517 // (not dna, not protein seq)
521 * Returns the (possibly empty) list of those supplied dbrefs which have the
522 * specified source database, with a case-insensitive match of source name
528 public static List<DBRefEntry> searchRefsForSource(List<DBRefEntry> dbRefs, String source) {
529 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
530 if (dbRefs != null && source != null) {
531 for (DBRefEntry dbref : dbRefs) {
532 if (source.equalsIgnoreCase(dbref.getSource())) {
541 * promote direct database references to primary for nucleotide or protein
542 * sequences if they have an appropriate primary ref
546 * <th>Primary DB</th>
547 * <th>Direct which will be promoted</th>
568 public static void ensurePrimaries(SequenceI sequence, List<DBRefEntry> pr) {
569 if (pr.size() == 0) {
573 int sstart = sequence.getStart();
574 int send = sequence.getEnd();
575 boolean isProtein = sequence.isProtein();
576 BitSet bsSelect = new BitSet();
578 // List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
581 // List<DBRefEntry> selddfs = selectDbRefs(!isprot, sequence.getDBRefs());
582 // if (selfs == null || selfs.size() == 0)
588 List<DBRefEntry> dbrefs = sequence.getDBRefs();
589 bsSelect.set(0, dbrefs.size());
591 if (!selectRefsBS(dbrefs, isProtein ? DBRefSource.PROTEIN_MASK : DBRefSource.DNA_CODING_MASK, bsSelect))
594 // selfs.addAll(selfArray);
597 // filter non-primary refs
598 for (int ip = pr.size(); --ip >= 0;) {
599 DBRefEntry p = pr.get(ip);
600 for (int i = bsSelect.nextSetBit(0); i >= 0; i = bsSelect.nextSetBit(i + 1)) {
601 if (dbrefs.get(i) == p)
604 // while (selfs.contains(p))
609 // List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
611 for (int ip = pr.size(), keys = 0; --ip >= 0 && keys != DBRefSource.PRIMARY_MASK;) {
612 DBRefEntry p = pr.get(ip);
614 switch (getCanonicalName(p.getSource())) {
615 case DBRefSource.UNIPROT:
616 keys |= DBRefSource.UNIPROT_MASK;
618 case DBRefSource.ENSEMBL:
619 keys |= DBRefSource.ENSEMBL_MASK;
623 // TODO: promote transcript refs ??
625 if (keys == 0 || !selectRefsBS(dbrefs, keys, bsSelect))
627 // if (candidates != null)
629 for (int ic = bsSelect.nextSetBit(0); ic >= 0; ic = bsSelect.nextSetBit(ic + 1))
630 // for (int ic = 0, n = candidates.size(); ic < n; ic++)
632 DBRefEntry cand = dbrefs.get(ic);// candidates.get(ic);
634 Mapping map = cand.getMap();
635 SequenceI cto = map.getTo();
636 if (cto != null && cto != sequence) {
637 // can't promote refs with mappings to other sequences
640 MapList mlist = map.getMap();
641 if (mlist.getFromLowest() != sstart && mlist.getFromHighest() != send) {
642 // can't promote refs with mappings from a region of this sequence
647 // and promote - not that version must be non-null here,
648 // as p must have passed isPrimaryCandidate()
649 cand.setVersion(p.getVersion() + " (promoted)");
651 // selfs.remove(cand);
652 // toPromote.add(cand);
653 if (!cand.isPrimaryCandidate()) {
654 System.out.println("Warning: Couldn't promote dbref " + cand.toString() + " for sequence "
655 + sequence.toString());