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.Hashtable;
32 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);
68 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
69 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
70 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
71 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
75 * Returns those DBRefEntry objects whose source identifier (once converted to
76 * Jalview's canonical form) is in the list of sources to search for. Returns
77 * null if no matches found.
80 * DBRefEntry objects to search
82 * array of sources to select
85 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
88 if (dbrefs == null || sources == null)
92 HashSet<String> srcs = new HashSet<String>();
93 for (String src : sources)
98 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
99 for (DBRefEntry dbr : dbrefs)
101 String source = getCanonicalName(dbr.getSource());
102 if (srcs.contains(source))
110 DBRefEntry[] reply = new DBRefEntry[res.size()];
111 return res.toArray(reply);
117 * isDasCoordinateSystem
123 * @return boolean true if Source DBRefEntry is compatible with DAS
124 * CoordinateSystem name
127 public static boolean isDasCoordinateSystem(String string,
128 DBRefEntry dBRefEntry)
130 if (string == null || dBRefEntry == null)
134 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
135 return coordsys == null ? false : coordsys.equals(dBRefEntry
140 * look up source in an internal list of database reference sources and return
141 * the canonical jalview name for the source, or the original string if it has
145 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
148 public static String getCanonicalName(String source)
154 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
155 return canonical == null ? source : canonical;
159 * Returns a (possibly empty) list of those references that match the given
160 * entry. Currently uses a comparator which matches if
162 * <li>database sources are the same</li>
163 * <li>accession ids are the same</li>
164 * <li>both have no mapping, or the mappings are the same</li>
168 * Set of references to search
173 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
176 return searchRefs(ref, entry,
177 matchDbAndIdAndEitherMapOrEquivalentMapList);
181 * Returns a list of those references that match the given accession id
183 * <li>database sources are the same</li>
184 * <li>accession ids are the same</li>
185 * <li>both have no mapping, or the mappings are the same</li>
189 * Set of references to search
191 * accession id to match
194 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
196 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
200 * Returns a (possibly empty) list of those references that match the given
201 * entry, according to the given comparator.
204 * an array of database references to search
206 * an entry to compare against
210 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
211 DbRefComp comparator)
213 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
214 if (refs == null || entry == null)
218 for (int i = 0; i < refs.length; i++)
220 if (comparator.matches(entry, refs[i]))
230 public boolean matches(DBRefEntry refa, DBRefEntry refb);
234 * match on all non-null fields in refa
236 // TODO unused - remove?
237 public static DbRefComp matchNonNullonA = new DbRefComp()
240 public boolean matches(DBRefEntry refa, DBRefEntry refb)
242 if (refa.getSource() == null
243 || refb.getSource().equals(refa.getSource()))
245 if (refa.getVersion() == null
246 || refb.getVersion().equals(refa.getVersion()))
248 if (refa.getAccessionId() == null
249 || refb.getAccessionId().equals(refa.getAccessionId()))
251 if (refa.getMap() == null
252 || (refb.getMap() != null && refb.getMap().equals(
265 * either field is null or field matches for all of source, version, accession
268 // TODO unused - remove?
269 public static DbRefComp matchEitherNonNull = new DbRefComp()
272 public boolean matches(DBRefEntry refa, DBRefEntry refb)
274 if (nullOrEqual(refa.getSource(), refb.getSource())
275 && nullOrEqual(refa.getVersion(), refb.getVersion())
276 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
277 && nullOrEqual(refa.getMap(), refb.getMap()))
286 * accession ID and DB must be identical. Version is ignored. Map is either
287 * not defined or is a match (or is compatible?)
289 // TODO unused - remove?
290 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
293 public boolean matches(DBRefEntry refa, DBRefEntry refb)
295 if (refa.getSource() != null && refb.getSource() != null
296 && refb.getSource().equals(refa.getSource()))
298 // We dont care about version
299 if (refa.getAccessionId() != null && refb.getAccessionId() != null
300 // FIXME should be && not || here?
301 || refb.getAccessionId().equals(refa.getAccessionId()))
303 if ((refa.getMap() == null || refb.getMap() == null)
304 || (refa.getMap() != null && refb.getMap() != null && refb
305 .getMap().equals(refa.getMap())))
316 * accession ID and DB must be identical. Version is ignored. No map on either
317 * or map but no maplist on either or maplist of map on a is the complement of
318 * maplist of map on b.
320 // TODO unused - remove?
321 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
324 public boolean matches(DBRefEntry refa, DBRefEntry refb)
326 if (refa.getSource() != null && refb.getSource() != null
327 && refb.getSource().equals(refa.getSource()))
329 // We dont care about version
330 if (refa.getAccessionId() != null && refb.getAccessionId() != null
331 || refb.getAccessionId().equals(refa.getAccessionId()))
333 if ((refa.getMap() == null && refb.getMap() == null)
334 || (refa.getMap() != null && refb.getMap() != null))
336 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
337 || (refb.getMap().getMap() != null
338 && refa.getMap().getMap() != null && refb
339 .getMap().getMap().getInverse()
340 .equals(refa.getMap().getMap())))
352 * accession ID and DB must be identical. Version is ignored. No map on both
353 * or or map but no maplist on either or maplist of map on a is equivalent to
354 * the maplist of map on b.
356 // TODO unused - remove?
357 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
360 public boolean matches(DBRefEntry refa, DBRefEntry refb)
362 if (refa.getSource() != null && refb.getSource() != null
363 && refb.getSource().equals(refa.getSource()))
365 // We dont care about version
366 // if ((refa.getVersion()==null || refb.getVersion()==null)
367 // || refb.getVersion().equals(refa.getVersion()))
369 if (refa.getAccessionId() != null && refb.getAccessionId() != null
370 || refb.getAccessionId().equals(refa.getAccessionId()))
372 if (refa.getMap() == null && refb.getMap() == null)
376 if (refa.getMap() != null
377 && refb.getMap() != null
378 && ((refb.getMap().getMap() == null && refa.getMap()
379 .getMap() == null) || (refb.getMap().getMap() != null
380 && refa.getMap().getMap() != null && refb
381 .getMap().getMap().equals(refa.getMap().getMap()))))
392 * accession ID and DB must be identical, or null on a. Version is ignored. No
393 * map on either or map but no maplist on either or maplist of map on a is
394 * equivalent to the maplist of map on b.
396 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
399 public boolean matches(DBRefEntry refa, DBRefEntry refb)
401 if (refa.getSource() != null && refb.getSource() != null
402 && refb.getSource().equals(refa.getSource()))
404 // We dont care about version
406 if (refa.getAccessionId() == null
407 || refa.getAccessionId().equals(refb.getAccessionId()))
409 if (refa.getMap() == null || refb.getMap() == null)
413 if ((refa.getMap() != null && refb.getMap() != null)
414 && (refb.getMap().getMap() == null && refa.getMap()
416 || (refb.getMap().getMap() != null
417 && refa.getMap().getMap() != null && (refb
418 .getMap().getMap().equals(refa.getMap().getMap()))))
429 * accession ID only must be identical.
431 public static DbRefComp matchId = new DbRefComp()
434 public boolean matches(DBRefEntry refa, DBRefEntry refb)
436 if (refa.getAccessionId() != null && refb.getAccessionId() != null
437 && refb.getAccessionId().equals(refa.getAccessionId()))
446 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
449 * Used by file parsers to generate DBRefs from annotation within file (eg
456 * where to annotate with reference
457 * @return parsed version of entry that was added to seq (if any)
459 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
460 String version, String acn)
462 DBRefEntry ref = null;
465 String locsrc = DBRefUtils.getCanonicalName(dbname);
466 if (locsrc.equals(DBRefSource.PDB))
469 * Check for PFAM style stockhom PDB accession id citation e.g.
472 Regex r = new com.stevesoft.pat.Regex(
473 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
474 if (r.search(acn.trim()))
476 String pdbid = r.stringMatched(1);
477 String chaincode = r.stringMatched(2);
478 if (chaincode == null)
482 // String mapstart = r.stringMatched(3);
483 // String mapend = r.stringMatched(4);
484 if (chaincode.equals(" "))
488 // construct pdb ref.
489 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
490 PDBEntry pdbr = new PDBEntry();
492 pdbr.setType(PDBEntry.Type.PDB);
493 pdbr.setProperty(new Hashtable());
494 pdbr.setChainCode(chaincode);
495 // pdbr.getProperty().put("CHAIN", chaincode);
500 System.err.println("Malformed PDB DR line:" + acn);
506 ref = new DBRefEntry(locsrc, version, acn);
517 * Returns true if either object is null, or they are equal
523 public static boolean nullOrEqual(Object o1, Object o2)
525 if (o1 == null || o2 == null)
529 return (o1 == null ? o2.equals(o1) : o1.equals(o2));
533 * Selects just the DNA or protein references from a set of references
536 * if true, select references to 'standard' DNA databases, else to
537 * 'standard' peptide databases
539 * a set of references to select from
542 public static DBRefEntry[] selectDbRefs(boolean selectDna,
545 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
546 : DBRefSource.PROTEINDBS);
547 // could attempt to find other cross
548 // refs here - ie PDB xrefs
549 // (not dna, not protein seq)
553 * Returns the (possibly empty) list of those supplied dbrefs which have the
554 * specified source database, with a case-insensitive match of source name
560 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
563 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
564 if (dbRefs != null && source != null)
566 for (DBRefEntry dbref : dbRefs)
568 if (source.equalsIgnoreCase(dbref.getSource()))