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);
72 * array of DBRef objects to search
74 * String[] array of source DBRef IDs to retrieve
77 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
80 if (dbrefs == null || sources == null)
84 HashSet<String> srcs = new HashSet<String>();
85 for (String src : sources)
90 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
91 for (DBRefEntry dbr : dbrefs)
93 String source = getCanonicalName(dbr.getSource());
94 if (srcs.contains(source))
102 DBRefEntry[] reply = new DBRefEntry[res.size()];
103 return res.toArray(reply);
109 * isDasCoordinateSystem
115 * @return boolean true if Source DBRefEntry is compatible with DAS
116 * CoordinateSystem name
119 public static boolean isDasCoordinateSystem(String string,
120 DBRefEntry dBRefEntry)
122 if (string == null || dBRefEntry == null)
126 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
127 return coordsys == null ? false : coordsys.equals(dBRefEntry
132 * look up source in an internal list of database reference sources and return
133 * the canonical jalview name for the source, or the original string if it has
137 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
140 public static String getCanonicalName(String source)
146 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
147 return canonical == null ? source : canonical;
151 * Returns an array of those references that match the given entry, or null if
152 * no matches. Currently uses a comparator which matches if
154 * <li>database sources are the same</li>
155 * <li>accession ids are the same</li>
156 * <li>both have no mapping, or the mappings are the same</li>
160 * Set of references to search
165 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
167 return searchRefs(ref, entry,
168 matchDbAndIdAndEitherMapOrEquivalentMapList);
172 * Returns an array of those references that match the given accession id
174 * <li>database sources are the same</li>
175 * <li>accession ids are the same</li>
176 * <li>both have no mapping, or the mappings are the same</li>
180 * Set of references to search
185 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, String accId)
187 return searchRefs(ref, new DBRefEntry("", "", accId), matchId);
191 * Returns an array of those references that match the given entry, according
192 * to the given comparator. Returns null if no matches.
195 * an array of database references to search
197 * an entry to compare against
201 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
202 DbRefComp comparator)
204 if (refs == null || entry == null)
208 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
209 for (int i = 0; i < refs.length; i++)
211 if (comparator.matches(entry, refs[i]))
216 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
221 public boolean matches(DBRefEntry refa, DBRefEntry refb);
225 * match on all non-null fields in refa
227 // TODO unused - remove?
228 public static DbRefComp matchNonNullonA = new DbRefComp()
231 public boolean matches(DBRefEntry refa, DBRefEntry refb)
233 if (refa.getSource() == null
234 || refb.getSource().equals(refa.getSource()))
236 if (refa.getVersion() == null
237 || refb.getVersion().equals(refa.getVersion()))
239 if (refa.getAccessionId() == null
240 || refb.getAccessionId().equals(refa.getAccessionId()))
242 if (refa.getMap() == null
243 || (refb.getMap() != null && refb.getMap().equals(
256 * either field is null or field matches for all of source, version, accession
259 // TODO unused - remove?
260 public static DbRefComp matchEitherNonNull = new DbRefComp()
263 public boolean matches(DBRefEntry refa, DBRefEntry refb)
265 if (nullOrEqual(refa.getSource(), refb.getSource())
266 && nullOrEqual(refa.getVersion(), refb.getVersion())
267 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
268 && nullOrEqual(refa.getMap(), refb.getMap()))
277 * accession ID and DB must be identical. Version is ignored. Map is either
278 * not defined or is a match (or is compatible?)
280 // TODO unused - remove?
281 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
284 public boolean matches(DBRefEntry refa, DBRefEntry refb)
286 if (refa.getSource() != null && refb.getSource() != null
287 && refb.getSource().equals(refa.getSource()))
289 // We dont care about version
290 if (refa.getAccessionId() != null && refb.getAccessionId() != null
291 // FIXME should be && not || here?
292 || refb.getAccessionId().equals(refa.getAccessionId()))
294 if ((refa.getMap() == null || refb.getMap() == null)
295 || (refa.getMap() != null && refb.getMap() != null && refb
296 .getMap().equals(refa.getMap())))
307 * accession ID and DB must be identical. Version is ignored. No map on either
308 * or map but no maplist on either or maplist of map on a is the complement of
309 * maplist of map on b.
311 // TODO unused - remove?
312 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
315 public boolean matches(DBRefEntry refa, DBRefEntry refb)
317 if (refa.getSource() != null && refb.getSource() != null
318 && refb.getSource().equals(refa.getSource()))
320 // We dont care about version
321 if (refa.getAccessionId() != null && refb.getAccessionId() != null
322 || refb.getAccessionId().equals(refa.getAccessionId()))
324 if ((refa.getMap() == null && refb.getMap() == null)
325 || (refa.getMap() != null && refb.getMap() != null))
327 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
328 || (refb.getMap().getMap() != null
329 && refa.getMap().getMap() != null && refb
330 .getMap().getMap().getInverse()
331 .equals(refa.getMap().getMap())))
343 * accession ID and DB must be identical. Version is ignored. No map on both
344 * or or map but no maplist on either or maplist of map on a is equivalent to
345 * the maplist of map on b.
347 // TODO unused - remove?
348 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
351 public boolean matches(DBRefEntry refa, DBRefEntry refb)
353 if (refa.getSource() != null && refb.getSource() != null
354 && refb.getSource().equals(refa.getSource()))
356 // We dont care about version
357 // if ((refa.getVersion()==null || refb.getVersion()==null)
358 // || refb.getVersion().equals(refa.getVersion()))
360 if (refa.getAccessionId() != null && refb.getAccessionId() != null
361 || refb.getAccessionId().equals(refa.getAccessionId()))
363 if (refa.getMap() == null && refb.getMap() == null)
367 if (refa.getMap() != null
368 && refb.getMap() != null
369 && ((refb.getMap().getMap() == null && refa.getMap()
370 .getMap() == null) || (refb.getMap().getMap() != null
371 && refa.getMap().getMap() != null && refb
372 .getMap().getMap().equals(refa.getMap().getMap()))))
383 * accession ID and DB must be identical. Version is ignored. No map on either
384 * or map but no maplist on either or maplist of map on a is equivalent to the
385 * maplist of map on b.
387 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
390 public boolean matches(DBRefEntry refa, DBRefEntry refb)
392 if (refa.getSource() != null && refb.getSource() != null
393 && refb.getSource().equals(refa.getSource()))
395 // We dont care about version
396 if (refa.getAccessionId() != null && refb.getAccessionId() != null
397 && refb.getAccessionId().equals(refa.getAccessionId()))
399 if (refa.getMap() == null || refb.getMap() == null)
403 if ((refa.getMap() != null && refb.getMap() != null)
404 && (refb.getMap().getMap() == null && refa.getMap()
406 || (refb.getMap().getMap() != null
407 && refa.getMap().getMap() != null && (refb
408 .getMap().getMap().equals(refa.getMap().getMap()))))
409 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
419 * accession ID only must be identical.
421 public static DbRefComp matchId = new DbRefComp()
424 public boolean matches(DBRefEntry refa, DBRefEntry refb)
426 if (refa.getAccessionId() != null && refb.getAccessionId() != null
427 && refb.getAccessionId().equals(refa.getAccessionId()))
436 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
439 * Used by file parsers to generate DBRefs from annotation within file (eg
446 * where to annotate with reference
447 * @return parsed version of entry that was added to seq (if any)
449 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
450 String version, String acn)
452 DBRefEntry ref = null;
455 String locsrc = DBRefUtils.getCanonicalName(dbname);
456 if (locsrc.equals(DBRefSource.PDB))
459 * Check for PFAM style stockhom PDB accession id citation e.g.
462 Regex r = new com.stevesoft.pat.Regex(
463 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
464 if (r.search(acn.trim()))
466 String pdbid = r.stringMatched(1);
467 String chaincode = r.stringMatched(2);
468 if (chaincode == null)
472 // String mapstart = r.stringMatched(3);
473 // String mapend = r.stringMatched(4);
474 if (chaincode.equals(" "))
478 // construct pdb ref.
479 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
480 PDBEntry pdbr = new PDBEntry();
482 pdbr.setType(PDBEntry.Type.PDB);
483 pdbr.setProperty(new Hashtable());
484 pdbr.setChainCode(chaincode);
485 // pdbr.getProperty().put("CHAIN", chaincode);
490 System.err.println("Malformed PDB DR line:" + acn);
496 ref = new DBRefEntry(locsrc, version, acn);
507 * Returns true if either object is null, or they are equal
513 public static boolean nullOrEqual(Object o1, Object o2)
515 if (o1 == null || o2 == null)
519 return (o1 == null ? o2.equals(o1) : o1.equals(o2));