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);
63 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
64 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
65 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
66 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
70 * Returns those DBRefEntry objects whose source identifier (once converted to
71 * Jalview's canonical form) is in the list of sources to search for. Returns
72 * null if no matches found.
75 * DBRefEntry objects to search
77 * array of sources to select
80 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
83 if (dbrefs == null || sources == null)
87 HashSet<String> srcs = new HashSet<String>();
88 for (String src : sources)
93 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
94 for (DBRefEntry dbr : dbrefs)
96 String source = getCanonicalName(dbr.getSource());
97 if (srcs.contains(source))
105 DBRefEntry[] reply = new DBRefEntry[res.size()];
106 return res.toArray(reply);
112 * isDasCoordinateSystem
118 * @return boolean true if Source DBRefEntry is compatible with DAS
119 * CoordinateSystem name
122 public static boolean isDasCoordinateSystem(String string,
123 DBRefEntry dBRefEntry)
125 if (string == null || dBRefEntry == null)
129 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
130 return coordsys == null ? false : coordsys.equals(dBRefEntry
135 * look up source in an internal list of database reference sources and return
136 * the canonical jalview name for the source, or the original string if it has
140 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
143 public static String getCanonicalName(String source)
149 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
150 return canonical == null ? source : canonical;
154 * Returns a (possibly empty) list of those references that match the given
155 * entry. Currently uses a comparator which matches if
157 * <li>database sources are the same</li>
158 * <li>accession ids are the same</li>
159 * <li>both have no mapping, or the mappings are the same</li>
163 * Set of references to search
168 public static List<DBRefEntry> searchRefs(DBRefEntry[] ref,
171 return searchRefs(ref, entry,
172 matchDbAndIdAndEitherMapOrEquivalentMapList);
176 * Returns a list of those references that match the given accession id
178 * <li>database sources are the same</li>
179 * <li>accession ids are the same</li>
180 * <li>both have no mapping, or the mappings are the same</li>
184 * Set of references to search
186 * accession id to match
189 public static List<DBRefEntry> searchRefs(DBRefEntry[] refs, String accId)
191 return searchRefs(refs, new DBRefEntry("", "", accId), matchId);
195 * Returns a (possibly empty) list of those references that match the given
196 * entry, according to the given comparator.
199 * an array of database references to search
201 * an entry to compare against
205 static List<DBRefEntry> searchRefs(DBRefEntry[] refs, DBRefEntry entry,
206 DbRefComp comparator)
208 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
209 if (refs == null || entry == null)
213 for (int i = 0; i < refs.length; i++)
215 if (comparator.matches(entry, refs[i]))
225 public boolean matches(DBRefEntry refa, DBRefEntry refb);
229 * match on all non-null fields in refa
231 // TODO unused - remove?
232 public static DbRefComp matchNonNullonA = new DbRefComp()
235 public boolean matches(DBRefEntry refa, DBRefEntry refb)
237 if (refa.getSource() == null
238 || refb.getSource().equals(refa.getSource()))
240 if (refa.getVersion() == null
241 || refb.getVersion().equals(refa.getVersion()))
243 if (refa.getAccessionId() == null
244 || refb.getAccessionId().equals(refa.getAccessionId()))
246 if (refa.getMap() == null
247 || (refb.getMap() != null && refb.getMap().equals(
260 * either field is null or field matches for all of source, version, accession
263 // TODO unused - remove?
264 public static DbRefComp matchEitherNonNull = new DbRefComp()
267 public boolean matches(DBRefEntry refa, DBRefEntry refb)
269 if (nullOrEqual(refa.getSource(), refb.getSource())
270 && nullOrEqual(refa.getVersion(), refb.getVersion())
271 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
272 && nullOrEqual(refa.getMap(), refb.getMap()))
281 * accession ID and DB must be identical. Version is ignored. Map is either
282 * not defined or is a match (or is compatible?)
284 // TODO unused - remove?
285 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
288 public boolean matches(DBRefEntry refa, DBRefEntry refb)
290 if (refa.getSource() != null && refb.getSource() != null
291 && refb.getSource().equals(refa.getSource()))
293 // We dont care about version
294 if (refa.getAccessionId() != null && refb.getAccessionId() != null
295 // FIXME should be && not || here?
296 || refb.getAccessionId().equals(refa.getAccessionId()))
298 if ((refa.getMap() == null || refb.getMap() == null)
299 || (refa.getMap() != null && refb.getMap() != null && refb
300 .getMap().equals(refa.getMap())))
311 * accession ID and DB must be identical. Version is ignored. No map on either
312 * or map but no maplist on either or maplist of map on a is the complement of
313 * maplist of map on b.
315 // TODO unused - remove?
316 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
319 public boolean matches(DBRefEntry refa, DBRefEntry refb)
321 if (refa.getSource() != null && refb.getSource() != null
322 && refb.getSource().equals(refa.getSource()))
324 // We dont care about version
325 if (refa.getAccessionId() != null && refb.getAccessionId() != null
326 || refb.getAccessionId().equals(refa.getAccessionId()))
328 if ((refa.getMap() == null && refb.getMap() == null)
329 || (refa.getMap() != null && refb.getMap() != null))
331 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
332 || (refb.getMap().getMap() != null
333 && refa.getMap().getMap() != null && refb
334 .getMap().getMap().getInverse()
335 .equals(refa.getMap().getMap())))
347 * accession ID and DB must be identical. Version is ignored. No map on both
348 * or or map but no maplist on either or maplist of map on a is equivalent to
349 * the maplist of map on b.
351 // TODO unused - remove?
352 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
355 public boolean matches(DBRefEntry refa, DBRefEntry refb)
357 if (refa.getSource() != null && refb.getSource() != null
358 && refb.getSource().equals(refa.getSource()))
360 // We dont care about version
361 // if ((refa.getVersion()==null || refb.getVersion()==null)
362 // || refb.getVersion().equals(refa.getVersion()))
364 if (refa.getAccessionId() != null && refb.getAccessionId() != null
365 || refb.getAccessionId().equals(refa.getAccessionId()))
367 if (refa.getMap() == null && refb.getMap() == null)
371 if (refa.getMap() != null
372 && refb.getMap() != null
373 && ((refb.getMap().getMap() == null && refa.getMap()
374 .getMap() == null) || (refb.getMap().getMap() != null
375 && refa.getMap().getMap() != null && refb
376 .getMap().getMap().equals(refa.getMap().getMap()))))
387 * accession ID and DB must be identical, or null on a. Version is ignored. No
388 * map on either or map but no maplist on either or maplist of map on a is
389 * equivalent to the maplist of map on b.
391 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
394 public boolean matches(DBRefEntry refa, DBRefEntry refb)
396 if (refa.getSource() != null && refb.getSource() != null
397 && refb.getSource().equals(refa.getSource()))
399 // We dont care about version
401 if (refa.getAccessionId() == null
402 || refa.getAccessionId().equals(refb.getAccessionId()))
404 if (refa.getMap() == null || refb.getMap() == null)
408 if ((refa.getMap() != null && refb.getMap() != null)
409 && (refb.getMap().getMap() == null && refa.getMap()
411 || (refb.getMap().getMap() != null
412 && refa.getMap().getMap() != null && (refb
413 .getMap().getMap().equals(refa.getMap().getMap()))))
424 * accession ID only must be identical.
426 public static DbRefComp matchId = new DbRefComp()
429 public boolean matches(DBRefEntry refa, DBRefEntry refb)
431 if (refa.getAccessionId() != null && refb.getAccessionId() != null
432 && refb.getAccessionId().equals(refa.getAccessionId()))
441 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
444 * Used by file parsers to generate DBRefs from annotation within file (eg
451 * where to annotate with reference
452 * @return parsed version of entry that was added to seq (if any)
454 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
455 String version, String acn)
457 DBRefEntry ref = null;
460 String locsrc = DBRefUtils.getCanonicalName(dbname);
461 if (locsrc.equals(DBRefSource.PDB))
464 * Check for PFAM style stockhom PDB accession id citation e.g.
467 Regex r = new com.stevesoft.pat.Regex(
468 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
469 if (r.search(acn.trim()))
471 String pdbid = r.stringMatched(1);
472 String chaincode = r.stringMatched(2);
473 if (chaincode == null)
477 // String mapstart = r.stringMatched(3);
478 // String mapend = r.stringMatched(4);
479 if (chaincode.equals(" "))
483 // construct pdb ref.
484 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
485 PDBEntry pdbr = new PDBEntry();
487 pdbr.setType(PDBEntry.Type.PDB);
488 pdbr.setProperty(new Hashtable());
489 pdbr.setChainCode(chaincode);
490 // pdbr.getProperty().put("CHAIN", chaincode);
495 System.err.println("Malformed PDB DR line:" + acn);
501 ref = new DBRefEntry(locsrc, version, acn);
512 * Returns true if either object is null, or they are equal
518 public static boolean nullOrEqual(Object o1, Object o2)
520 if (o1 == null || o2 == null)
524 return (o1 == null ? o2.equals(o1) : o1.equals(o2));
528 * Selects just the DNA or protein references from a set of references
531 * if true, select references to 'standard' DNA databases, else to
532 * 'standard' peptide databases
534 * a set of references to select from
537 public static DBRefEntry[] selectDbRefs(boolean selectDna,
540 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
541 : DBRefSource.PROTEINDBS);
542 // could attempt to find other cross
543 // refs here - ie PDB xrefs
544 // (not dna, not protein seq)
548 * Returns the (possibly empty) list of those supplied dbrefs which have the
549 * specified source database, with a case-insensitive match of source name
555 public static List<DBRefEntry> searchRefsForSource(DBRefEntry[] dbRefs,
558 List<DBRefEntry> matches = new ArrayList<DBRefEntry>();
559 if (dbRefs != null && source != null)
561 for (DBRefEntry dbref : dbRefs)
563 if (source.equalsIgnoreCase(dbref.getSource()))