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<String, String>();
48 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
52 // TODO load these from a resource file?
53 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
55 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
57 // Ensembl values for dbname in xref REST service:
58 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
59 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
61 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
62 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
63 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
65 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
66 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
68 // Make sure we have lowercase entries for all canonical string lookups
69 Set<String> keys = canonicalSourceNameLookup.keySet();
72 canonicalSourceNameLookup.put(k.toLowerCase(),
73 canonicalSourceNameLookup.get(k));
76 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
77 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
78 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
79 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
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.
88 * DBRefEntry objects to search
90 * array of sources to select
93 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
96 if (dbrefs == null || sources == null)
100 HashSet<String> srcs = new HashSet<String>();
101 for (String src : sources)
106 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
107 for (DBRefEntry dbr : dbrefs)
109 String source = getCanonicalName(dbr.getSource());
110 if (srcs.contains(source))
118 DBRefEntry[] reply = new DBRefEntry[res.size()];
119 return res.toArray(reply);
125 * isDasCoordinateSystem
131 * @return boolean true if Source DBRefEntry is compatible with DAS
132 * CoordinateSystem name
135 public static boolean isDasCoordinateSystem(String string,
136 DBRefEntry dBRefEntry)
138 if (string == null || dBRefEntry == null)
142 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
143 return coordsys == null ? false : coordsys.equals(dBRefEntry
148 * look up source in an internal list of database reference sources and return
149 * the canonical jalview name for the source, or the original string if it has
153 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
156 public static String getCanonicalName(String source)
162 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
163 return canonical == null ? source : canonical;
167 * Returns a (possibly empty) list of those references that match the given
168 * entry. Currently uses a comparator which matches if
170 * <li>database sources are the same</li>
171 * <li>accession ids are the same</li>
172 * <li>both have no mapping, or the mappings are the same</li>
176 * Set of references to search
181 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
184 return searchRefs(ref, entry,
185 matchDbAndIdAndEitherMapOrEquivalentMapList);
189 * Returns a list of those references that match the given accession id
191 * <li>database sources are the same</li>
192 * <li>accession ids are the same</li>
193 * <li>both have no mapping, or the mappings are the same</li>
197 * Set of references to search
199 * accession id to match
202 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
204 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
208 * Returns a (possibly empty) list of those references that match the given
209 * entry, according to the given comparator.
212 * an array of database references to search
214 * an entry to compare against
218 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
219 DbRefComp comparator)
221 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
222 if (refs == null || entry == null)
226 for (int i = 0; i < refs.length; i++)
228 if (comparator.matches(entry, refs[i]))
238 public boolean matches(DBRefEntry refa, DBRefEntry refb);
242 * match on all non-null fields in refa
244 // TODO unused - remove?
245 public static DbRefComp matchNonNullonA = new DbRefComp()
248 public boolean matches(DBRefEntry refa, DBRefEntry refb)
250 if (refa.getSource() == null
251 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
252 DBRefUtils.getCanonicalName(refa.getSource())))
254 if (refa.getVersion() == null
255 || refb.getVersion().equals(refa.getVersion()))
257 if (refa.getAccessionId() == null
258 || refb.getAccessionId().equals(refa.getAccessionId()))
260 if (refa.getMap() == null
261 || (refb.getMap() != null && refb.getMap().equals(
274 * either field is null or field matches for all of source, version, accession
277 // TODO unused - remove?
278 public static DbRefComp matchEitherNonNull = new DbRefComp()
281 public boolean matches(DBRefEntry refa, DBRefEntry refb)
283 if (nullOrEqualSource(refa.getSource(), refb.getSource())
284 && nullOrEqual(refa.getVersion(), refb.getVersion())
285 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
286 && nullOrEqual(refa.getMap(), refb.getMap()))
295 * accession ID and DB must be identical. Version is ignored. Map is either
296 * not defined or is a match (or is compatible?)
298 // TODO unused - remove?
299 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
302 public boolean matches(DBRefEntry refa, DBRefEntry refb)
304 if (refa.getSource() != null
305 && refb.getSource() != null
306 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
307 DBRefUtils.getCanonicalName(refa.getSource())))
309 // We dont care about version
310 if (refa.getAccessionId() != null && refb.getAccessionId() != null
311 // FIXME should be && not || here?
312 || refb.getAccessionId().equals(refa.getAccessionId()))
314 if ((refa.getMap() == null || refb.getMap() == null)
315 || (refa.getMap() != null && refb.getMap() != null && refb
316 .getMap().equals(refa.getMap())))
327 * accession ID and DB must be identical. Version is ignored. No map on either
328 * or map but no maplist on either or maplist of map on a is the complement of
329 * maplist of map on b.
331 // TODO unused - remove?
332 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
335 public boolean matches(DBRefEntry refa, DBRefEntry refb)
337 if (refa.getSource() != null
338 && refb.getSource() != null
339 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
340 DBRefUtils.getCanonicalName(refa.getSource())))
342 // We dont care about version
343 if (refa.getAccessionId() != null && refb.getAccessionId() != null
344 || refb.getAccessionId().equals(refa.getAccessionId()))
346 if ((refa.getMap() == null && refb.getMap() == null)
347 || (refa.getMap() != null && refb.getMap() != null))
349 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
350 || (refb.getMap().getMap() != null
351 && refa.getMap().getMap() != null && refb
352 .getMap().getMap().getInverse()
353 .equals(refa.getMap().getMap())))
365 * accession ID and DB must be identical. Version is ignored. No map on both
366 * or or map but no maplist on either or maplist of map on a is equivalent to
367 * the maplist of map on b.
369 // TODO unused - remove?
370 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
373 public boolean matches(DBRefEntry refa, DBRefEntry refb)
375 if (refa.getSource() != null
376 && refb.getSource() != null
377 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
378 DBRefUtils.getCanonicalName(refa.getSource())))
380 // We dont care about version
381 // if ((refa.getVersion()==null || refb.getVersion()==null)
382 // || refb.getVersion().equals(refa.getVersion()))
384 if (refa.getAccessionId() != null && refb.getAccessionId() != null
385 || refb.getAccessionId().equals(refa.getAccessionId()))
387 if (refa.getMap() == null && refb.getMap() == null)
391 if (refa.getMap() != null
392 && refb.getMap() != null
393 && ((refb.getMap().getMap() == null && refa.getMap()
394 .getMap() == null) || (refb.getMap().getMap() != null
395 && refa.getMap().getMap() != null && refb
396 .getMap().getMap().equals(refa.getMap().getMap()))))
407 * accession ID and DB must be identical, or null on a. Version is ignored. No
408 * map on either or map but no maplist on either or maplist of map on a is
409 * equivalent to the maplist of map on b.
411 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
414 public boolean matches(DBRefEntry refa, DBRefEntry refb)
416 if (refa.getSource() != null
417 && refb.getSource() != null
418 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
419 DBRefUtils.getCanonicalName(refa.getSource())))
421 // We dont care about version
423 if (refa.getAccessionId() == null
424 || refa.getAccessionId().equals(refb.getAccessionId()))
426 if (refa.getMap() == null || refb.getMap() == null)
430 if ((refa.getMap() != null && refb.getMap() != null)
431 && (refb.getMap().getMap() == null && refa.getMap()
433 || (refb.getMap().getMap() != null
434 && refa.getMap().getMap() != null && (refb
435 .getMap().getMap().equals(refa.getMap().getMap()))))
446 * accession ID only must be identical.
448 public static DbRefComp matchId = new DbRefComp()
451 public boolean matches(DBRefEntry refa, DBRefEntry refb)
453 if (refa.getAccessionId() != null && refb.getAccessionId() != null
454 && refb.getAccessionId().equals(refa.getAccessionId()))
463 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
466 * Used by file parsers to generate DBRefs from annotation within file (eg
473 * where to annotate with reference
474 * @return parsed version of entry that was added to seq (if any)
476 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
477 String version, String acn)
479 DBRefEntry ref = null;
482 String locsrc = DBRefUtils.getCanonicalName(dbname);
483 if (locsrc.equals(DBRefSource.PDB))
486 * Check for PFAM style stockhom PDB accession id citation e.g.
489 Regex r = new com.stevesoft.pat.Regex(
490 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
491 if (r.search(acn.trim()))
493 String pdbid = r.stringMatched(1);
494 String chaincode = r.stringMatched(2);
495 if (chaincode == null)
499 // String mapstart = r.stringMatched(3);
500 // String mapend = r.stringMatched(4);
501 if (chaincode.equals(" "))
505 // construct pdb ref.
506 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
507 PDBEntry pdbr = new PDBEntry();
509 pdbr.setType(PDBEntry.Type.PDB);
510 pdbr.setChainCode(chaincode);
515 System.err.println("Malformed PDB DR line:" + acn);
521 ref = new DBRefEntry(locsrc, version, acn);
532 * Returns true if either object is null, or they are equal
538 public static boolean nullOrEqual(Object o1, Object o2)
540 if (o1 == null || o2 == null)
544 return o1.equals(o2);
548 * canonicalise source string before comparing. null is always wildcard
551 * - null or source string to compare
553 * - null or source string to compare
554 * @return true if either o1 or o2 are null, or o1 equals o2 under
555 * DBRefUtils.getCanonicalName
556 * (o1).equals(DBRefUtils.getCanonicalName(o2))
558 public static boolean nullOrEqualSource(String o1, String o2)
560 if (o1 == null || o2 == null)
564 return DBRefUtils.getCanonicalName(o1).equals(
565 DBRefUtils.getCanonicalName(o2));
569 * Selects just the DNA or protein references from a set of references
572 * if true, select references to 'standard' DNA databases, else to
573 * 'standard' peptide databases
575 * a set of references to select from
578 public static DBRefEntry[] selectDbRefs(boolean selectDna,
581 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
582 : DBRefSource.PROTEINDBS);
583 // could attempt to find other cross
584 // refs here - ie PDB xrefs
585 // (not dna, not protein seq)
589 * Returns the (possibly empty) list of those supplied dbrefs which have the
590 * specified source database, with a case-insensitive match of source name
596 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
599 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
600 if (dbRefs != null && source != null)
602 for (DBRefEntry dbref : dbRefs)
604 if (source.equalsIgnoreCase(dbref.getSource()))
614 * promote direct database references to primary for nucleotide or protein
615 * sequences if they have an appropriate primary ref
619 * <th>Primary DB</th>
620 * <th>Direct which will be promoted</th>
641 public static void ensurePrimaries(SequenceI sequence)
643 List<DBRefEntry> pr = sequence.getPrimaryDBRefs();
649 List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
651 DBRefEntry[] selfArray = selectDbRefs(!sequence.isProtein(),
652 sequence.getDBRefs());
653 if (selfArray == null || selfArray.length == 0)
658 selfs.addAll(Arrays.asList(selfArray));
661 // filter non-primary refs
662 for (DBRefEntry p : pr)
664 while (selfs.contains(p))
669 List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
671 for (DBRefEntry p : pr)
673 List<String> promType = new ArrayList<String>();
674 if (sequence.isProtein())
676 switch (getCanonicalName(p.getSource()))
678 case DBRefSource.UNIPROT:
679 // case DBRefSource.UNIPROTKB:
680 // case DBRefSource.UP_NAME:
681 // search for and promote ensembl
682 promType.add(DBRefSource.ENSEMBL);
684 case DBRefSource.ENSEMBL:
685 // search for and promote Uniprot
686 promType.add(DBRefSource.UNIPROT);
692 // TODO: promote transcript refs
695 // collate candidates and promote them
696 DBRefEntry[] candidates = selectRefs(
697 selfs.toArray(new DBRefEntry[0]),
698 promType.toArray(new String[0]));
699 if (candidates != null)
701 for (DBRefEntry cand : candidates)
705 if (cand.getMap().getTo() != null
706 && cand.getMap().getTo() != sequence)
708 // can't promote refs with mappings to other sequences
711 if (cand.getMap().getMap().getFromLowest() != sequence
713 && cand.getMap().getMap().getFromHighest() != sequence
716 // can't promote refs with mappings from a region of this sequence
722 cand.setVersion(p.getVersion() + " (promoted)");
725 if (!cand.isPrimaryCandidate())
727 System.out.println("Warning: Couldn't promote dbref "
728 + cand.toString() + " for sequence "
729 + sequence.toString());