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.PDBEntry;
26 import jalview.datamodel.SequenceI;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
36 import com.stevesoft.pat.Regex;
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<>();
51 // TODO load these from a resource file?
52 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
54 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
56 // Ensembl values for dbname in xref REST service:
57 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
58 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
60 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
61 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
62 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
64 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
65 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
67 // Make sure we have lowercase entries for all canonical string lookups
68 Set<String> keys = canonicalSourceNameLookup.keySet();
71 canonicalSourceNameLookup.put(k.toLowerCase(),
72 canonicalSourceNameLookup.get(k));
78 * Returns those DBRefEntry objects whose source identifier (once converted to
79 * Jalview's canonical form) is in the list of sources to search for. Returns
80 * null if no matches found.
83 * DBRefEntry objects to search
85 * array of sources to select
88 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
91 if (dbrefs == null || sources == null)
95 HashSet<String> srcs = new HashSet<>();
96 for (String src : sources)
98 srcs.add(src.toUpperCase());
101 List<DBRefEntry> res = new ArrayList<>();
102 for (DBRefEntry dbr : dbrefs)
104 String source = getCanonicalName(dbr.getSource());
105 if (srcs.contains(source.toUpperCase()))
113 DBRefEntry[] reply = new DBRefEntry[res.size()];
114 return res.toArray(reply);
120 * look up source in an internal list of database reference sources and return
121 * the canonical jalview name for the source, or the original string if it has
125 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
128 public static String getCanonicalName(String source)
134 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
135 return canonical == null ? source : canonical;
139 * Returns a (possibly empty) list of those references that match the given
140 * entry. Currently uses a comparator which matches if
142 * <li>database sources are the same</li>
143 * <li>accession ids are the same</li>
144 * <li>both have no mapping, or the mappings are the same</li>
148 * Set of references to search
153 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
156 return searchRefs(ref, entry,
157 matchDbAndIdAndEitherMapOrEquivalentMapList);
161 * Returns a list of those references that match the given accession id
163 * <li>database sources are the same</li>
164 * <li>accession ids are the same</li>
165 * <li>both have no mapping, or the mappings are the same</li>
169 * Set of references to search
171 * accession id to match
174 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
176 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
180 * Returns a (possibly empty) list of those references that match the given
181 * entry, according to the given comparator.
184 * an array of database references to search
186 * an entry to compare against
190 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
191 DbRefComp comparator)
193 List<DBRefEntry> rfs = new ArrayList<>();
194 if (refs == null || entry == null)
198 for (int i = 0; i < refs.length; i++)
200 if (comparator.matches(entry, refs[i]))
210 public boolean matches(DBRefEntry refa, DBRefEntry refb);
214 * match on all non-null fields in refa
216 // TODO unused - remove?
217 public static DbRefComp matchNonNullonA = new DbRefComp()
220 public boolean matches(DBRefEntry refa, DBRefEntry refb)
222 if (refa.getSource() == null
223 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
224 DBRefUtils.getCanonicalName(refa.getSource())))
226 if (refa.getVersion() == null
227 || refb.getVersion().equals(refa.getVersion()))
229 if (refa.getAccessionId() == null
230 || refb.getAccessionId().equals(refa.getAccessionId()))
232 if (refa.getMap() == null || (refb.getMap() != null
233 && refb.getMap().equals(refa.getMap())))
245 * either field is null or field matches for all of source, version, accession
248 // TODO unused - remove?
249 public static DbRefComp matchEitherNonNull = new DbRefComp()
252 public boolean matches(DBRefEntry refa, DBRefEntry refb)
254 if (nullOrEqualSource(refa.getSource(), refb.getSource())
255 && nullOrEqual(refa.getVersion(), refb.getVersion())
256 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
257 && nullOrEqual(refa.getMap(), refb.getMap()))
266 * accession ID and DB must be identical. Version is ignored. Map is either
267 * not defined or is a match (or is compatible?)
269 // TODO unused - remove?
270 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
273 public boolean matches(DBRefEntry refa, DBRefEntry refb)
275 if (refa.getSource() != null && refb.getSource() != null
276 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
277 DBRefUtils.getCanonicalName(refa.getSource())))
279 // We dont care about version
280 if (refa.getAccessionId() != null && refb.getAccessionId() != null
281 // FIXME should be && not || here?
282 || refb.getAccessionId().equals(refa.getAccessionId()))
284 if ((refa.getMap() == null || refb.getMap() == null)
285 || (refa.getMap() != null && refb.getMap() != null
286 && refb.getMap().equals(refa.getMap())))
297 * accession ID and DB must be identical. Version is ignored. No map on either
298 * or map but no maplist on either or maplist of map on a is the complement of
299 * maplist of map on b.
301 // TODO unused - remove?
302 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
305 public boolean matches(DBRefEntry refa, DBRefEntry refb)
307 if (refa.getSource() != null && refb.getSource() != null
308 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
309 DBRefUtils.getCanonicalName(refa.getSource())))
311 // We dont care about version
312 if (refa.getAccessionId() != null && refb.getAccessionId() != null
313 || refb.getAccessionId().equals(refa.getAccessionId()))
315 if ((refa.getMap() == null && refb.getMap() == null)
316 || (refa.getMap() != null && refb.getMap() != null))
318 if ((refb.getMap().getMap() == null
319 && refa.getMap().getMap() == null)
320 || (refb.getMap().getMap() != null
321 && refa.getMap().getMap() != null
322 && refb.getMap().getMap().getInverse()
323 .equals(refa.getMap().getMap())))
335 * accession ID and DB must be identical. Version is ignored. No map on both
336 * or or map but no maplist on either or maplist of map on a is equivalent to
337 * the maplist of map on b.
339 // TODO unused - remove?
340 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
343 public boolean matches(DBRefEntry refa, DBRefEntry refb)
345 if (refa.getSource() != null && refb.getSource() != null
346 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
347 DBRefUtils.getCanonicalName(refa.getSource())))
349 // We dont care about version
350 // if ((refa.getVersion()==null || refb.getVersion()==null)
351 // || refb.getVersion().equals(refa.getVersion()))
353 if (refa.getAccessionId() != null && refb.getAccessionId() != null
354 || refb.getAccessionId().equals(refa.getAccessionId()))
356 if (refa.getMap() == null && refb.getMap() == null)
360 if (refa.getMap() != null && refb.getMap() != null
361 && ((refb.getMap().getMap() == null
362 && refa.getMap().getMap() == null)
363 || (refb.getMap().getMap() != null
364 && refa.getMap().getMap() != null
365 && refb.getMap().getMap()
366 .equals(refa.getMap().getMap()))))
377 * accession ID and DB must be identical, or null on a. Version is ignored. No
378 * map on either or map but no maplist on either or maplist of map on a is
379 * equivalent to the maplist of map on b.
381 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
384 public boolean matches(DBRefEntry refa, DBRefEntry refb)
386 if (refa.getSource() != null && refb.getSource() != null
387 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
388 DBRefUtils.getCanonicalName(refa.getSource())))
390 // We dont care about version
392 if (refa.getAccessionId() == null
393 || refa.getAccessionId().equals(refb.getAccessionId()))
395 if (refa.getMap() == null || refb.getMap() == null)
399 if ((refa.getMap() != null && refb.getMap() != null)
400 && (refb.getMap().getMap() == null
401 && refa.getMap().getMap() == null)
402 || (refb.getMap().getMap() != null
403 && refa.getMap().getMap() != null
404 && (refb.getMap().getMap()
405 .equals(refa.getMap().getMap()))))
416 * accession ID only must be identical.
418 public static DbRefComp matchId = new DbRefComp()
421 public boolean matches(DBRefEntry refa, DBRefEntry refb)
423 if (refa.getAccessionId() != null && refb.getAccessionId() != null
424 && refb.getAccessionId().equals(refa.getAccessionId()))
433 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
436 * Used by file parsers to generate DBRefs from annotation within file (eg
443 * where to annotate with reference
444 * @return parsed version of entry that was added to seq (if any)
446 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
447 String version, String acn)
449 DBRefEntry ref = null;
452 String locsrc = DBRefUtils.getCanonicalName(dbname);
453 if (locsrc.equals(DBRefSource.PDB))
456 * Check for PFAM style stockhom PDB accession id citation e.g.
459 Regex r = new com.stevesoft.pat.Regex(
460 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
461 if (r.search(acn.trim()))
463 String pdbid = r.stringMatched(1);
464 String chaincode = r.stringMatched(2);
465 if (chaincode == null)
469 // String mapstart = r.stringMatched(3);
470 // String mapend = r.stringMatched(4);
471 if (chaincode.equals(" "))
475 // construct pdb ref.
476 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
477 PDBEntry pdbr = new PDBEntry();
479 pdbr.setType(PDBEntry.Type.PDB);
480 pdbr.setChainCode(chaincode);
485 System.err.println("Malformed PDB DR line:" + acn);
491 ref = new DBRefEntry(locsrc, version, acn);
502 * Returns true if either object is null, or they are equal
508 public static boolean nullOrEqual(Object o1, Object o2)
510 if (o1 == null || o2 == null)
514 return o1.equals(o2);
518 * canonicalise source string before comparing. null is always wildcard
521 * - null or source string to compare
523 * - null or source string to compare
524 * @return true if either o1 or o2 are null, or o1 equals o2 under
525 * DBRefUtils.getCanonicalName
526 * (o1).equals(DBRefUtils.getCanonicalName(o2))
528 public static boolean nullOrEqualSource(String o1, String o2)
530 if (o1 == null || o2 == null)
534 return DBRefUtils.getCanonicalName(o1)
535 .equals(DBRefUtils.getCanonicalName(o2));
539 * Selects just the DNA or protein references from a set of references
542 * if true, select references to 'standard' DNA databases, else to
543 * 'standard' peptide databases
545 * a set of references to select from
548 public static DBRefEntry[] selectDbRefs(boolean selectDna,
551 return selectRefs(refs,
552 selectDna ? DBRefSource.DNACODINGDBS : DBRefSource.PROTEINDBS);
553 // could attempt to find other cross
554 // refs here - ie PDB xrefs
555 // (not dna, not protein seq)
559 * Returns the (possibly empty) list of those supplied dbrefs which have the
560 * specified source database, with a case-insensitive match of source name
566 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
569 List<DBRefEntry> matches = new ArrayList<>();
570 if (dbRefs != null && source != null)
572 for (DBRefEntry dbref : dbRefs)
574 if (source.equalsIgnoreCase(dbref.getSource()))
584 * promote direct database references to primary for nucleotide or protein
585 * sequences if they have an appropriate primary ref
589 * <th>Primary DB</th>
590 * <th>Direct which will be promoted</th>
611 public static void ensurePrimaries(SequenceI sequence)
613 List<DBRefEntry> pr = sequence.getPrimaryDBRefs();
619 List<DBRefEntry> selfs = new ArrayList<>();
621 DBRefEntry[] selfArray = selectDbRefs(!sequence.isProtein(),
622 sequence.getDBRefs());
623 if (selfArray == null || selfArray.length == 0)
628 selfs.addAll(Arrays.asList(selfArray));
631 // filter non-primary refs
632 for (DBRefEntry p : pr)
634 while (selfs.contains(p))
639 List<DBRefEntry> toPromote = new ArrayList<>();
641 for (DBRefEntry p : pr)
643 List<String> promType = new ArrayList<>();
644 if (sequence.isProtein())
646 switch (getCanonicalName(p.getSource()))
648 case DBRefSource.UNIPROT:
649 // case DBRefSource.UNIPROTKB:
650 // case DBRefSource.UP_NAME:
651 // search for and promote ensembl
652 promType.add(DBRefSource.ENSEMBL);
654 case DBRefSource.ENSEMBL:
655 // search for and promote Uniprot
656 promType.add(DBRefSource.UNIPROT);
662 // TODO: promote transcript refs
665 // collate candidates and promote them
666 DBRefEntry[] candidates = selectRefs(selfs.toArray(new DBRefEntry[0]),
667 promType.toArray(new String[0]));
668 if (candidates != null)
670 for (DBRefEntry cand : candidates)
674 if (cand.getMap().getTo() != null
675 && cand.getMap().getTo() != sequence)
677 // can't promote refs with mappings to other sequences
680 if (cand.getMap().getMap().getFromLowest() != sequence
682 && cand.getMap().getMap().getFromHighest() != sequence
685 // can't promote refs with mappings from a region of this sequence
691 cand.setVersion(p.getVersion() + " (promoted)");
694 if (!cand.isPrimaryCandidate())
697 "Warning: Couldn't promote dbref " + cand.toString()
698 + " for sequence " + sequence.toString());