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);
55 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
56 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
58 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
59 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
60 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
61 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
67 * array of DBRef objects to search
69 * String[] array of source DBRef IDs to retrieve
72 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
75 if (dbrefs == null || sources == null)
79 HashSet<String> srcs = new HashSet<String>();
80 for (String src : sources)
85 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
86 for (DBRefEntry dbr : dbrefs)
88 String source = getCanonicalName(dbr.getSource());
89 if (srcs.contains(source))
97 DBRefEntry[] reply = new DBRefEntry[res.size()];
98 return res.toArray(reply);
104 * isDasCoordinateSystem
110 * @return boolean true if Source DBRefEntry is compatible with DAS
111 * CoordinateSystem name
114 public static boolean isDasCoordinateSystem(String string,
115 DBRefEntry dBRefEntry)
117 if (string == null || dBRefEntry == null)
121 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
122 return coordsys == null ? false : coordsys.equals(dBRefEntry
127 * look up source in an internal list of database reference sources and return
128 * the canonical jalview name for the source, or the original string if it has
132 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
135 public static String getCanonicalName(String source)
141 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
142 return canonical == null ? source : canonical;
146 * Returns an array of those references that match the given entry, or null if
147 * no matches. Currently uses a comparator which matches if
149 * <li>database sources are the same</li>
150 * <li>accession ids are the same</li>
151 * <li>both have no mapping, or the mappings are the same</li>
155 * Set of references to search
160 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
162 return searchRefs(ref, entry,
163 matchDbAndIdAndEitherMapOrEquivalentMapList);
167 * Returns an array of those references that match the given entry, according
168 * to the given comparator. Returns null if no matches.
171 * an array of database references to search
173 * an entry to compare against
177 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
178 DbRefComp comparator)
180 if (refs == null || entry == null)
184 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
185 for (int i = 0; i < refs.length; i++)
187 if (comparator.matches(entry, refs[i]))
192 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
197 public boolean matches(DBRefEntry refa, DBRefEntry refb);
201 * match on all non-null fields in refa
203 // TODO unused - remove?
204 public static DbRefComp matchNonNullonA = new DbRefComp()
207 public boolean matches(DBRefEntry refa, DBRefEntry refb)
209 if (refa.getSource() == null
210 || refb.getSource().equals(refa.getSource()))
212 if (refa.getVersion() == null
213 || refb.getVersion().equals(refa.getVersion()))
215 if (refa.getAccessionId() == null
216 || refb.getAccessionId().equals(refa.getAccessionId()))
218 if (refa.getMap() == null
219 || (refb.getMap() != null && refb.getMap().equals(
232 * either field is null or field matches for all of source, version, accession
235 // TODO unused - remove?
236 public static DbRefComp matchEitherNonNull = new DbRefComp()
239 public boolean matches(DBRefEntry refa, DBRefEntry refb)
241 if (nullOrEqual(refa.getSource(), refb.getSource())
242 && nullOrEqual(refa.getVersion(), refb.getVersion())
243 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
244 && nullOrEqual(refa.getMap(), refb.getMap()))
253 * accession ID and DB must be identical. Version is ignored. Map is either
254 * not defined or is a match (or is compatible?)
256 // TODO unused - remove?
257 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
260 public boolean matches(DBRefEntry refa, DBRefEntry refb)
262 if (refa.getSource() != null && refb.getSource() != null
263 && refb.getSource().equals(refa.getSource()))
265 // We dont care about version
266 if (refa.getAccessionId() != null && refb.getAccessionId() != null
267 // FIXME should be && not || here?
268 || refb.getAccessionId().equals(refa.getAccessionId()))
270 if ((refa.getMap() == null || refb.getMap() == null)
271 || (refa.getMap() != null && refb.getMap() != null && refb
272 .getMap().equals(refa.getMap())))
283 * accession ID and DB must be identical. Version is ignored. No map on either
284 * or map but no maplist on either or maplist of map on a is the complement of
285 * maplist of map on b.
287 // TODO unused - remove?
288 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
291 public boolean matches(DBRefEntry refa, DBRefEntry refb)
293 if (refa.getSource() != null && refb.getSource() != null
294 && refb.getSource().equals(refa.getSource()))
296 // We dont care about version
297 if (refa.getAccessionId() != null && refb.getAccessionId() != null
298 || refb.getAccessionId().equals(refa.getAccessionId()))
300 if ((refa.getMap() == null && refb.getMap() == null)
301 || (refa.getMap() != null && refb.getMap() != null))
303 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
304 || (refb.getMap().getMap() != null
305 && refa.getMap().getMap() != null && refb
306 .getMap().getMap().getInverse()
307 .equals(refa.getMap().getMap())))
319 * accession ID and DB must be identical. Version is ignored. No map on both
320 * or or map but no maplist on either or maplist of map on a is equivalent to
321 * the maplist of map on b.
323 // TODO unused - remove?
324 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
327 public boolean matches(DBRefEntry refa, DBRefEntry refb)
329 if (refa.getSource() != null && refb.getSource() != null
330 && refb.getSource().equals(refa.getSource()))
332 // We dont care about version
333 // if ((refa.getVersion()==null || refb.getVersion()==null)
334 // || refb.getVersion().equals(refa.getVersion()))
336 if (refa.getAccessionId() != null && refb.getAccessionId() != null
337 || refb.getAccessionId().equals(refa.getAccessionId()))
339 if (refa.getMap() == null && refb.getMap() == null)
343 if (refa.getMap() != null
344 && refb.getMap() != null
345 && ((refb.getMap().getMap() == null && refa.getMap()
346 .getMap() == null) || (refb.getMap().getMap() != null
347 && refa.getMap().getMap() != null && refb
348 .getMap().getMap().equals(refa.getMap().getMap()))))
359 * accession ID and DB must be identical. Version is ignored. No map on either
360 * or map but no maplist on either or maplist of map on a is equivalent to the
361 * maplist of map on b.
363 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
366 public boolean matches(DBRefEntry refa, DBRefEntry refb)
368 if (refa.getSource() != null && refb.getSource() != null
369 && refb.getSource().equals(refa.getSource()))
371 // We dont care about version
372 if (refa.getAccessionId() != null && refb.getAccessionId() != null
373 && refb.getAccessionId().equals(refa.getAccessionId()))
375 if (refa.getMap() == null || refb.getMap() == null)
379 if ((refa.getMap() != null && refb.getMap() != null)
380 && (refb.getMap().getMap() == null && refa.getMap()
382 || (refb.getMap().getMap() != null
383 && refa.getMap().getMap() != null && (refb
384 .getMap().getMap().equals(refa.getMap().getMap()))))
385 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
395 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
398 * Used by file parsers to generate DBRefs from annotation within file (eg
405 * where to annotate with reference
406 * @return parsed version of entry that was added to seq (if any)
408 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
409 String version, String acn)
411 DBRefEntry ref = null;
414 String locsrc = DBRefUtils.getCanonicalName(dbname);
415 if (locsrc.equals(DBRefSource.PDB))
418 * Check for PFAM style stockhom PDB accession id citation e.g.
421 Regex r = new com.stevesoft.pat.Regex(
422 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
423 if (r.search(acn.trim()))
425 String pdbid = r.stringMatched(1);
426 String chaincode = r.stringMatched(2);
427 if (chaincode == null)
431 // String mapstart = r.stringMatched(3);
432 // String mapend = r.stringMatched(4);
433 if (chaincode.equals(" "))
437 // construct pdb ref.
438 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
439 PDBEntry pdbr = new PDBEntry();
441 pdbr.setType(PDBEntry.Type.PDB);
442 pdbr.setProperty(new Hashtable());
443 pdbr.setChainCode(chaincode);
444 // pdbr.getProperty().put("CHAIN", chaincode);
449 System.err.println("Malformed PDB DR line:" + acn);
455 ref = new DBRefEntry(locsrc, version, acn);
466 * Returns true if either object is null, or they are equal
472 public static boolean nullOrEqual(Object o1, Object o2)
474 if (o1 == null || o2 == null)
478 return (o1 == null ? o2.equals(o1) : o1.equals(o2));