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.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
35 import com.stevesoft.pat.Regex;
38 * Utilities for handling DBRef objects and their collections.
40 public class DBRefUtils
43 * lookup from lower-case form of a name to its canonical (standardised) form
45 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
47 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
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));
75 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
76 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
77 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
78 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
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.
87 * DBRefEntry objects to search
89 * array of sources to select
92 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
95 if (dbrefs == null || sources == null)
99 HashSet<String> srcs = new HashSet<String>();
100 for (String src : sources)
105 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
106 for (DBRefEntry dbr : dbrefs)
108 String source = getCanonicalName(dbr.getSource());
109 if (srcs.contains(source))
117 DBRefEntry[] reply = new DBRefEntry[res.size()];
118 return res.toArray(reply);
124 * isDasCoordinateSystem
130 * @return boolean true if Source DBRefEntry is compatible with DAS
131 * CoordinateSystem name
134 public static boolean isDasCoordinateSystem(String string,
135 DBRefEntry dBRefEntry)
137 if (string == null || dBRefEntry == null)
141 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
142 return coordsys == null ? false : coordsys.equals(dBRefEntry
147 * look up source in an internal list of database reference sources and return
148 * the canonical jalview name for the source, or the original string if it has
152 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
155 public static String getCanonicalName(String source)
161 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
162 return canonical == null ? source : canonical;
166 * Returns a (possibly empty) list of those references that match the given
167 * entry. Currently uses a comparator which matches if
169 * <li>database sources are the same</li>
170 * <li>accession ids are the same</li>
171 * <li>both have no mapping, or the mappings are the same</li>
175 * Set of references to search
180 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
183 return searchRefs(ref, entry,
184 matchDbAndIdAndEitherMapOrEquivalentMapList);
188 * Returns a list of those references that match the given accession id
190 * <li>database sources are the same</li>
191 * <li>accession ids are the same</li>
192 * <li>both have no mapping, or the mappings are the same</li>
196 * Set of references to search
198 * accession id to match
201 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
203 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
207 * Returns a (possibly empty) list of those references that match the given
208 * entry, according to the given comparator.
211 * an array of database references to search
213 * an entry to compare against
217 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
218 DbRefComp comparator)
220 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
221 if (refs == null || entry == null)
225 for (int i = 0; i < refs.length; i++)
227 if (comparator.matches(entry, refs[i]))
237 public boolean matches(DBRefEntry refa, DBRefEntry refb);
241 * match on all non-null fields in refa
243 // TODO unused - remove?
244 public static DbRefComp matchNonNullonA = new DbRefComp()
247 public boolean matches(DBRefEntry refa, DBRefEntry refb)
249 if (refa.getSource() == null
250 || DBRefUtils.getCanonicalName(refb.getSource()).equals(
251 DBRefUtils.getCanonicalName(refa.getSource())))
253 if (refa.getVersion() == null
254 || refb.getVersion().equals(refa.getVersion()))
256 if (refa.getAccessionId() == null
257 || refb.getAccessionId().equals(refa.getAccessionId()))
259 if (refa.getMap() == null
260 || (refb.getMap() != null && refb.getMap().equals(
273 * either field is null or field matches for all of source, version, accession
276 // TODO unused - remove?
277 public static DbRefComp matchEitherNonNull = new DbRefComp()
280 public boolean matches(DBRefEntry refa, DBRefEntry refb)
282 if (nullOrEqualSource(refa.getSource(), refb.getSource())
283 && nullOrEqual(refa.getVersion(), refb.getVersion())
284 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
285 && nullOrEqual(refa.getMap(), refb.getMap()))
294 * accession ID and DB must be identical. Version is ignored. Map is either
295 * not defined or is a match (or is compatible?)
297 // TODO unused - remove?
298 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
301 public boolean matches(DBRefEntry refa, DBRefEntry refb)
303 if (refa.getSource() != null && refb.getSource() != null
304 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
305 DBRefUtils.getCanonicalName(refa.getSource())))
307 // We dont care about version
308 if (refa.getAccessionId() != null && refb.getAccessionId() != null
309 // FIXME should be && not || here?
310 || refb.getAccessionId().equals(refa.getAccessionId()))
312 if ((refa.getMap() == null || refb.getMap() == null)
313 || (refa.getMap() != null && refb.getMap() != null && refb
314 .getMap().equals(refa.getMap())))
325 * accession ID and DB must be identical. Version is ignored. No map on either
326 * or map but no maplist on either or maplist of map on a is the complement of
327 * maplist of map on b.
329 // TODO unused - remove?
330 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
333 public boolean matches(DBRefEntry refa, DBRefEntry refb)
335 if (refa.getSource() != null && refb.getSource() != null
336 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
337 DBRefUtils.getCanonicalName(refa.getSource())))
339 // We dont care about version
340 if (refa.getAccessionId() != null && refb.getAccessionId() != null
341 || refb.getAccessionId().equals(refa.getAccessionId()))
343 if ((refa.getMap() == null && refb.getMap() == null)
344 || (refa.getMap() != null && refb.getMap() != null))
346 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
347 || (refb.getMap().getMap() != null
348 && refa.getMap().getMap() != null && refb
349 .getMap().getMap().getInverse()
350 .equals(refa.getMap().getMap())))
362 * accession ID and DB must be identical. Version is ignored. No map on both
363 * or or map but no maplist on either or maplist of map on a is equivalent to
364 * the maplist of map on b.
366 // TODO unused - remove?
367 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
370 public boolean matches(DBRefEntry refa, DBRefEntry refb)
372 if (refa.getSource() != null && refb.getSource() != null
373 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
374 DBRefUtils.getCanonicalName(refa.getSource())))
376 // We dont care about version
377 // if ((refa.getVersion()==null || refb.getVersion()==null)
378 // || refb.getVersion().equals(refa.getVersion()))
380 if (refa.getAccessionId() != null && refb.getAccessionId() != null
381 || refb.getAccessionId().equals(refa.getAccessionId()))
383 if (refa.getMap() == null && refb.getMap() == null)
387 if (refa.getMap() != null
388 && refb.getMap() != null
389 && ((refb.getMap().getMap() == null && refa.getMap()
390 .getMap() == null) || (refb.getMap().getMap() != null
391 && refa.getMap().getMap() != null && refb
392 .getMap().getMap().equals(refa.getMap().getMap()))))
403 * accession ID and DB must be identical, or null on a. Version is ignored. No
404 * map on either or map but no maplist on either or maplist of map on a is
405 * equivalent to the maplist of map on b.
407 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
410 public boolean matches(DBRefEntry refa, DBRefEntry refb)
412 if (refa.getSource() != null && refb.getSource() != null
413 && DBRefUtils.getCanonicalName(refb.getSource()).equals(
414 DBRefUtils.getCanonicalName(refa.getSource())))
416 // We dont care about version
418 if (refa.getAccessionId() == null
419 || refa.getAccessionId().equals(refb.getAccessionId()))
421 if (refa.getMap() == null || refb.getMap() == null)
425 if ((refa.getMap() != null && refb.getMap() != null)
426 && (refb.getMap().getMap() == null && refa.getMap()
428 || (refb.getMap().getMap() != null
429 && refa.getMap().getMap() != null && (refb
430 .getMap().getMap().equals(refa.getMap().getMap()))))
441 * accession ID only must be identical.
443 public static DbRefComp matchId = new DbRefComp()
446 public boolean matches(DBRefEntry refa, DBRefEntry refb)
448 if (refa.getAccessionId() != null && refb.getAccessionId() != null
449 && refb.getAccessionId().equals(refa.getAccessionId()))
458 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
461 * Used by file parsers to generate DBRefs from annotation within file (eg
468 * where to annotate with reference
469 * @return parsed version of entry that was added to seq (if any)
471 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
472 String version, String acn)
474 DBRefEntry ref = null;
477 String locsrc = DBRefUtils.getCanonicalName(dbname);
478 if (locsrc.equals(DBRefSource.PDB))
481 * Check for PFAM style stockhom PDB accession id citation e.g.
484 Regex r = new com.stevesoft.pat.Regex(
485 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
486 if (r.search(acn.trim()))
488 String pdbid = r.stringMatched(1);
489 String chaincode = r.stringMatched(2);
490 if (chaincode == null)
494 // String mapstart = r.stringMatched(3);
495 // String mapend = r.stringMatched(4);
496 if (chaincode.equals(" "))
500 // construct pdb ref.
501 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
502 PDBEntry pdbr = new PDBEntry();
504 pdbr.setType(PDBEntry.Type.PDB);
505 pdbr.setChainCode(chaincode);
510 System.err.println("Malformed PDB DR line:" + acn);
516 ref = new DBRefEntry(locsrc, version, acn);
527 * Returns true if either object is null, or they are equal
533 public static boolean nullOrEqual(Object o1, Object o2)
535 if (o1 == null || o2 == null)
539 return o1.equals(o2);
543 * canonicalise source string before comparing. null is always wildcard
546 * - null or source string to compare
548 * - null or source string to compare
549 * @return true if either o1 or o2 are null, or o1 equals o2 under
550 * DBRefUtils.getCanonicalName
551 * (o1).equals(DBRefUtils.getCanonicalName(o2))
553 public static boolean nullOrEqualSource(String o1, String o2)
555 if (o1 == null || o2 == null)
559 return DBRefUtils.getCanonicalName(o1).equals(
560 DBRefUtils.getCanonicalName(o2));
564 * Selects just the DNA or protein references from a set of references
567 * if true, select references to 'standard' DNA databases, else to
568 * 'standard' peptide databases
570 * a set of references to select from
573 public static DBRefEntry[] selectDbRefs(boolean selectDna,
576 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
577 : DBRefSource.PROTEINDBS);
578 // could attempt to find other cross
579 // refs here - ie PDB xrefs
580 // (not dna, not protein seq)
584 * Returns the (possibly empty) list of those supplied dbrefs which have the
585 * specified source database, with a case-insensitive match of source name
591 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
594 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
595 if (dbRefs != null && source != null)
597 for (DBRefEntry dbref : dbRefs)
599 if (source.equalsIgnoreCase(dbref.getSource()))