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.Hashtable;
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<String, String>();
49 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
53 // TODO load these from a resource file?
54 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
56 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
58 // Ensembl values for dbname in xref REST service:
59 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
60 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
62 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
63 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
64 // Ensembl Gn and Tr are for Ensembl genomic and transcript IDs as served
66 canonicalSourceNameLookup.put("ensembl-tr", DBRefSource.ENSEMBL);
67 canonicalSourceNameLookup.put("ensembl-gn", DBRefSource.ENSEMBL);
69 // Make sure we have lowercase entries for all canonical string lookups
70 Set<String> keys = canonicalSourceNameLookup.keySet();
73 canonicalSourceNameLookup.put(k.toLowerCase(),
74 canonicalSourceNameLookup.get(k));
77 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
78 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
79 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
80 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
84 * Returns those DBRefEntry objects whose source identifier (once converted to
85 * Jalview's canonical form) is in the list of sources to search for. Returns
86 * null if no matches found.
89 * DBRefEntry objects to search
91 * array of sources to select
94 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
97 if (dbrefs == null || sources == null)
101 HashSet<String> srcs = new HashSet<String>();
102 for (String src : sources)
107 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
108 for (DBRefEntry dbr : dbrefs)
110 String source = getCanonicalName(dbr.getSource());
111 if (srcs.contains(source))
119 DBRefEntry[] reply = new DBRefEntry[res.size()];
120 return res.toArray(reply);
126 * isDasCoordinateSystem
132 * @return boolean true if Source DBRefEntry is compatible with DAS
133 * CoordinateSystem name
136 public static boolean isDasCoordinateSystem(String string,
137 DBRefEntry dBRefEntry)
139 if (string == null || dBRefEntry == null)
143 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
144 return coordsys == null ? false : coordsys.equals(dBRefEntry
149 * look up source in an internal list of database reference sources and return
150 * the canonical jalview name for the source, or the original string if it has
154 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
157 public static String getCanonicalName(String source)
163 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
164 return canonical == null ? source : canonical;
168 * Returns a (possibly empty) list of those references that match the given
169 * entry. Currently uses a comparator which matches if
171 * <li>database sources are the same</li>
172 * <li>accession ids are the same</li>
173 * <li>both have no mapping, or the mappings are the same</li>
177 * Set of references to search
182 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
185 return searchRefs(ref, entry,
186 matchDbAndIdAndEitherMapOrEquivalentMapList);
190 * Returns a list of those references that match the given accession id
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>
198 * Set of references to search
200 * accession id to match
203 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
205 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
209 * Returns a (possibly empty) list of those references that match the given
210 * entry, according to the given comparator.
213 * an array of database references to search
215 * an entry to compare against
219 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
220 DbRefComp comparator)
222 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
223 if (refs == null || entry == null)
227 for (int i = 0; i < refs.length; i++)
229 if (comparator.matches(entry, refs[i]))
239 public boolean matches(DBRefEntry refa, DBRefEntry refb);
243 * match on all non-null fields in refa
245 // TODO unused - remove?
246 public static DbRefComp matchNonNullonA = new DbRefComp()
249 public boolean matches(DBRefEntry refa, DBRefEntry refb)
251 if (refa.getSource() == null
252 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
253 DBRefUtils.getCanonicalName(refa.getSource())))
255 if (refa.getVersion() == null
256 || refb.getVersion().equals(refa.getVersion()))
258 if (refa.getAccessionId() == null
259 || refb.getAccessionId().equals(refa.getAccessionId()))
261 if (refa.getMap() == null
262 || (refb.getMap() != null && refb.getMap().equals(
275 * either field is null or field matches for all of source, version, accession
278 // TODO unused - remove?
279 public static DbRefComp matchEitherNonNull = new DbRefComp()
282 public boolean matches(DBRefEntry refa, DBRefEntry refb)
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
297 * not defined or is a match (or is compatible?)
299 // TODO unused - remove?
300 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
303 public boolean matches(DBRefEntry refa, DBRefEntry refb)
305 if (refa.getSource() != null
306 && refb.getSource() != null
307 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
308 DBRefUtils.getCanonicalName(refa.getSource())))
310 // We dont care about version
311 if (refa.getAccessionId() != null && refb.getAccessionId() != null
312 // FIXME should be && not || here?
313 || refb.getAccessionId().equals(refa.getAccessionId()))
315 if ((refa.getMap() == null || refb.getMap() == null)
316 || (refa.getMap() != null && refb.getMap() != null && refb
317 .getMap().equals(refa.getMap())))
328 * accession ID and DB must be identical. Version is ignored. No map on either
329 * or map but no maplist on either or maplist of map on a is the complement of
330 * maplist of map on b.
332 // TODO unused - remove?
333 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
336 public boolean matches(DBRefEntry refa, DBRefEntry refb)
338 if (refa.getSource() != null
339 && refb.getSource() != null
340 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
341 DBRefUtils.getCanonicalName(refa.getSource())))
343 // We dont care about version
344 if (refa.getAccessionId() != null && refb.getAccessionId() != null
345 || refb.getAccessionId().equals(refa.getAccessionId()))
347 if ((refa.getMap() == null && refb.getMap() == null)
348 || (refa.getMap() != null && refb.getMap() != null))
350 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
351 || (refb.getMap().getMap() != null
352 && refa.getMap().getMap() != null && refb
353 .getMap().getMap().getInverse()
354 .equals(refa.getMap().getMap())))
366 * accession ID and DB must be identical. Version is ignored. No map on both
367 * or or map but no maplist on either or maplist of map on a is equivalent to
368 * the maplist of map on b.
370 // TODO unused - remove?
371 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
374 public boolean matches(DBRefEntry refa, DBRefEntry refb)
376 if (refa.getSource() != null
377 && refb.getSource() != null
378 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
379 DBRefUtils.getCanonicalName(refa.getSource())))
381 // We dont care about version
382 // if ((refa.getVersion()==null || refb.getVersion()==null)
383 // || refb.getVersion().equals(refa.getVersion()))
385 if (refa.getAccessionId() != null && refb.getAccessionId() != null
386 || refb.getAccessionId().equals(refa.getAccessionId()))
388 if (refa.getMap() == null && refb.getMap() == null)
392 if (refa.getMap() != null
393 && refb.getMap() != null
394 && ((refb.getMap().getMap() == null && refa.getMap()
395 .getMap() == null) || (refb.getMap().getMap() != null
396 && refa.getMap().getMap() != null && refb
397 .getMap().getMap().equals(refa.getMap().getMap()))))
408 * accession ID and DB must be identical, or null on a. Version is ignored. No
409 * map on either or map but no maplist on either or maplist of map on a is
410 * equivalent to the maplist of map on b.
412 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
415 public boolean matches(DBRefEntry refa, DBRefEntry refb)
417 if (refa.getSource() != null
418 && refb.getSource() != null
419 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
420 DBRefUtils.getCanonicalName(refa.getSource())))
422 // We dont care about version
424 if (refa.getAccessionId() == null
425 || refa.getAccessionId().equals(refb.getAccessionId()))
427 if (refa.getMap() == null || refb.getMap() == null)
431 if ((refa.getMap() != null && refb.getMap() != null)
432 && (refb.getMap().getMap() == null && refa.getMap()
434 || (refb.getMap().getMap() != null
435 && refa.getMap().getMap() != null && (refb
436 .getMap().getMap().equals(refa.getMap().getMap()))))
447 * accession ID only must be identical.
449 public static DbRefComp matchId = new DbRefComp()
452 public boolean matches(DBRefEntry refa, DBRefEntry refb)
454 if (refa.getAccessionId() != null && refb.getAccessionId() != null
455 && refb.getAccessionId().equals(refa.getAccessionId()))
464 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
467 * Used by file parsers to generate DBRefs from annotation within file (eg
474 * where to annotate with reference
475 * @return parsed version of entry that was added to seq (if any)
477 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
478 String version, String acn)
480 DBRefEntry ref = null;
483 String locsrc = DBRefUtils.getCanonicalName(dbname);
484 if (locsrc.equals(DBRefSource.PDB))
487 * Check for PFAM style stockhom PDB accession id citation e.g.
490 Regex r = new com.stevesoft.pat.Regex(
491 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
492 if (r.search(acn.trim()))
494 String pdbid = r.stringMatched(1);
495 String chaincode = r.stringMatched(2);
496 if (chaincode == null)
500 // String mapstart = r.stringMatched(3);
501 // String mapend = r.stringMatched(4);
502 if (chaincode.equals(" "))
506 // construct pdb ref.
507 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
508 PDBEntry pdbr = new PDBEntry();
510 pdbr.setType(PDBEntry.Type.PDB);
511 pdbr.setProperty(new Hashtable());
512 pdbr.setChainCode(chaincode);
513 // pdbr.getProperty().put("CHAIN", chaincode);
518 System.err.println("Malformed PDB DR line:" + acn);
524 ref = new DBRefEntry(locsrc, version, acn);
535 * Returns true if either object is null, or they are equal
541 public static boolean nullOrEqual(Object o1, Object o2)
543 if (o1 == null || o2 == null)
547 return o1.equals(o2);
551 * canonicalise source string before comparing. null is always wildcard
554 * - null or source string to compare
556 * - null or source string to compare
557 * @return true if either o1 or o2 are null, or o1 equals o2 under
558 * DBRefUtils.getCanonicalName
559 * (o1).equals(DBRefUtils.getCanonicalName(o2))
561 public static boolean nullOrEqualSource(String o1, String o2)
563 if (o1 == null || o2 == null)
567 return DBRefUtils.getCanonicalName(o1).equals(
568 DBRefUtils.getCanonicalName(o2));
572 * Selects just the DNA or protein references from a set of references
575 * if true, select references to 'standard' DNA databases, else to
576 * 'standard' peptide databases
578 * a set of references to select from
581 public static DBRefEntry[] selectDbRefs(boolean selectDna,
584 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
585 : DBRefSource.PROTEINDBS);
586 // could attempt to find other cross
587 // refs here - ie PDB xrefs
588 // (not dna, not protein seq)
592 * Returns the (possibly empty) list of those supplied dbrefs which have the
593 * specified source database, with a case-insensitive match of source name
599 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
602 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
603 if (dbRefs != null && source != null)
605 for (DBRefEntry dbref : dbRefs)
607 if (source.equalsIgnoreCase(dbref.getSource()))
617 * promote direct database references to primary for nucleotide or protein
618 * sequences if they have an appropriate primary ref
622 * <th>Primary DB</th>
623 * <th>Direct which will be promoted</th>
644 public static void ensurePrimaries(SequenceI sequence)
646 List<DBRefEntry> pr = sequence.getPrimaryDBRefs();
652 List<DBRefEntry> selfs = new ArrayList<DBRefEntry>();
654 DBRefEntry[] selfArray = selectDbRefs(!sequence.isProtein(),
655 sequence.getDBRefs());
656 if (selfArray == null || selfArray.length == 0)
661 selfs.addAll(Arrays.asList(selfArray));
664 // filter non-primary refs
665 for (DBRefEntry p : pr)
667 while (selfs.contains(p))
672 List<DBRefEntry> toPromote = new ArrayList<DBRefEntry>();
674 for (DBRefEntry p : pr)
676 List<String> promType = new ArrayList<String>();
677 if (sequence.isProtein())
679 switch (getCanonicalName(p.getSource()))
681 case DBRefSource.UNIPROT:
682 // case DBRefSource.UNIPROTKB:
683 // case DBRefSource.UP_NAME:
684 // search for and promote ensembl
685 promType.add(DBRefSource.ENSEMBL);
687 case DBRefSource.ENSEMBL:
688 // search for and promote Uniprot
689 promType.add(DBRefSource.UNIPROT);
695 // TODO: promote transcript refs
698 // collate candidates and promote them
699 DBRefEntry[] candidates = selectRefs(
700 selfs.toArray(new DBRefEntry[0]),
701 promType.toArray(new String[0]));
702 if (candidates != null)
704 for (DBRefEntry cand : candidates)
708 if (cand.getMap().getTo() != null
709 && cand.getMap().getTo() != sequence)
711 // can't promote refs with mappings to other sequences
714 if (cand.getMap().getMap().getFromLowest() != sequence
716 && cand.getMap().getMap().getFromHighest() != sequence
719 // can't promote refs with mappings from a region of this sequence
725 cand.setVersion(p.getVersion() + " (promoted)");
728 if (!cand.isPrimaryCandidate())
730 System.out.println("Warning: Couldn't promote dbref "
731 + cand.toString() + " for sequence "
732 + sequence.toString());