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;
27 import jalview.ws.seqfetcher.DbSourceProxy;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Hashtable;
32 import java.util.List;
35 import com.stevesoft.pat.Regex;
37 public class DBRefUtils
39 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
41 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
45 // TODO load these from a resource file?
46 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
48 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
49 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
51 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
52 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
53 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
54 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
58 * Utilities for handling DBRef objects and their collections.
63 * Vector of DBRef objects to search
65 * String[] array of source DBRef IDs to retrieve
68 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
79 Map<String, Integer> srcs = new HashMap<String, Integer>();
80 ArrayList<DBRefEntry> res = new ArrayList<DBRefEntry>();
82 for (int i = 0; i < sources.length; i++)
84 srcs.put(new String(sources[i]), new Integer(i));
86 for (int i = 0, j = dbrefs.length; i < j; i++)
88 if (srcs.containsKey(dbrefs[i].getSource()))
96 DBRefEntry[] reply = new DBRefEntry[res.size()];
97 return res.toArray(reply);
100 // there are probable memory leaks in the hashtable!
105 * isDasCoordinateSystem
111 * @return boolean true if Source DBRefEntry is compatible with DAS
112 * CoordinateSystem name
115 public static boolean isDasCoordinateSystem(String string,
116 DBRefEntry dBRefEntry)
118 if (string == null || dBRefEntry == null)
122 String coordsys = dasCoordinateSystemsLookup.get(string
124 return coordsys == null ? false : coordsys.equals(dBRefEntry
129 * look up source in an internal list of database reference sources and return
130 * the canonical jalview name for the source, or the original string if it has
134 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
137 public static String getCanonicalName(String source)
143 String canonical = canonicalSourceNameLookup.get(source
145 return canonical == null ? source : canonical;
149 * Returns an array of those references that match the given entry, or null if
150 * no matches. Currently uses a comparator which matches if
152 * <li>database sources are the same</li>
153 * <li>accession ids are the same</li>
154 * <li>both have no mapping, or the mappings are the same</li>
158 * Set of references to search
163 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
165 return searchRefs(ref, entry,
166 matchDbAndIdAndEitherMapOrEquivalentMapList);
170 * Returns an array of those references that match the given entry, according
171 * to the given comparator. Returns null if no matches.
174 * an array of database references to search
176 * an entry to compare against
180 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
181 DbRefComp comparator)
183 if (refs == null || entry == null)
187 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
188 for (int i = 0; i < refs.length; i++)
190 if (comparator.matches(entry, refs[i]))
195 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
200 public boolean matches(DBRefEntry refa, DBRefEntry refb);
204 * match on all non-null fields in refa
206 public static DbRefComp matchNonNullonA = new DbRefComp()
208 public boolean matches(DBRefEntry refa, DBRefEntry refb)
210 if (refa.getSource() == null
211 || refb.getSource().equals(refa.getSource()))
213 if (refa.getVersion() == null
214 || refb.getVersion().equals(refa.getVersion()))
216 if (refa.getAccessionId() == null
217 || refb.getAccessionId().equals(refa.getAccessionId()))
219 if (refa.getMap() == null
220 || (refb.getMap() != null && refb.getMap().equals(
233 * either field is null or field matches for all of source, version, accession
236 public static DbRefComp matchEitherNonNull = new DbRefComp()
238 public boolean matches(DBRefEntry refa, DBRefEntry refb)
240 if ((refa.getSource() == null || refb.getSource() == null)
241 || refb.getSource().equals(refa.getSource()))
243 if ((refa.getVersion() == null || refb.getVersion() == null)
244 || refb.getVersion().equals(refa.getVersion()))
246 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
247 || refb.getAccessionId().equals(refa.getAccessionId()))
249 if ((refa.getMap() == null || refb.getMap() == null)
250 || (refb.getMap() != null && refb.getMap().equals(
263 * accession ID and DB must be identical. Version is ignored. Map is either
264 * not defined or is a match (or is compatible?)
266 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
268 public boolean matches(DBRefEntry refa, DBRefEntry refb)
270 if (refa.getSource() != null && refb.getSource() != null
271 && refb.getSource().equals(refa.getSource()))
273 // We dont care about version
274 // if ((refa.getVersion()==null || refb.getVersion()==null)
275 // || refb.getVersion().equals(refa.getVersion()))
277 if (refa.getAccessionId() != null && refb.getAccessionId() != null
278 || refb.getAccessionId().equals(refa.getAccessionId()))
280 if ((refa.getMap() == null || refb.getMap() == null)
281 || (refa.getMap() != null && refb.getMap() != null && refb
282 .getMap().equals(refa.getMap())))
293 * accession ID and DB must be identical. Version is ignored. No map on either
294 * or map but no maplist on either or maplist of map on a is the complement of
295 * maplist of map on b.
297 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
299 public boolean matches(DBRefEntry refa, DBRefEntry refb)
301 if (refa.getSource() != null && refb.getSource() != null
302 && refb.getSource().equals(refa.getSource()))
304 // We dont care about version
305 // if ((refa.getVersion()==null || refb.getVersion()==null)
306 // || refb.getVersion().equals(refa.getVersion()))
308 if (refa.getAccessionId() != null && refb.getAccessionId() != null
309 || refb.getAccessionId().equals(refa.getAccessionId()))
311 if ((refa.getMap() == null && refb.getMap() == null)
312 || (refa.getMap() != null && refb.getMap() != null))
314 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
315 || (refb.getMap().getMap() != null
316 && refa.getMap().getMap() != null && refb
317 .getMap().getMap().getInverse()
318 .equals(refa.getMap().getMap())))
330 * accession ID and DB must be identical. Version is ignored. No map on both
331 * or or map but no maplist on either or maplist of map on a is equivalent to
332 * the maplist of map on b.
334 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
336 public boolean matches(DBRefEntry refa, DBRefEntry refb)
338 if (refa.getSource() != null && refb.getSource() != null
339 && refb.getSource().equals(refa.getSource()))
341 // We dont care about version
342 // if ((refa.getVersion()==null || refb.getVersion()==null)
343 // || refb.getVersion().equals(refa.getVersion()))
345 if (refa.getAccessionId() != null && refb.getAccessionId() != null
346 || refb.getAccessionId().equals(refa.getAccessionId()))
348 if (refa.getMap() == null && refb.getMap() == null)
352 if (refa.getMap() != null
353 && refb.getMap() != null
354 && ((refb.getMap().getMap() == null && refa.getMap()
355 .getMap() == null) || (refb.getMap().getMap() != null
356 && refa.getMap().getMap() != null && refb
357 .getMap().getMap().equals(refa.getMap().getMap()))))
368 * accession ID and DB must be identical. Version is ignored. No map on either
369 * or map but no maplist on either or maplist of map on a is equivalent to the
370 * maplist of map on b.
372 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
374 public boolean matches(DBRefEntry refa, DBRefEntry refb)
376 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
377 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
378 if (refa.getSource() != null && refb.getSource() != null
379 && refb.getSource().equals(refa.getSource()))
381 // We dont care about version
382 // if ((refa.getVersion()==null || refb.getVersion()==null)
383 // || refb.getVersion().equals(refa.getVersion()))
385 if (refa.getAccessionId() != null && refb.getAccessionId() != null
386 && refb.getAccessionId().equals(refa.getAccessionId()))
388 if (refa.getMap() == null || refb.getMap() == null)
392 if ((refa.getMap() != null && refb.getMap() != null)
393 && (refb.getMap().getMap() == null && refa.getMap()
395 || (refb.getMap().getMap() != null
396 && refa.getMap().getMap() != null && (refb
397 .getMap().getMap().equals(refa.getMap().getMap()))))
398 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
408 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
411 * Used by file parsers to generate DBRefs from annotation within file (eg
418 * where to annotate with reference
419 * @return parsed version of entry that was added to seq (if any)
421 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
422 String version, String acn)
424 DBRefEntry ref = null;
427 String locsrc = DBRefUtils.getCanonicalName(dbname);
428 if (locsrc.equals(DBRefSource.PDB))
431 * Check for PFAM style stockhom PDB accession id citation e.g.
434 Regex r = new com.stevesoft.pat.Regex(
435 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
436 if (r.search(acn.trim()))
438 String pdbid = r.stringMatched(1);
439 String chaincode = r.stringMatched(2);
444 // String mapstart = r.stringMatched(3);
445 // String mapend = r.stringMatched(4);
446 if (chaincode.equals(" "))
450 // construct pdb ref.
451 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
452 PDBEntry pdbr = new PDBEntry();
454 pdbr.setType(PDBEntry.Type.PDB);
455 pdbr.setProperty(new Hashtable());
456 pdbr.setChainCode(chaincode);
457 // pdbr.getProperty().put("CHAIN", chaincode);
460 System.err.println("Malformed PDB DR line:"+acn);
466 ref = new DBRefEntry(locsrc, version, acn);
477 * Extract valid accession strings from a query string. Used by the
478 * SequenceFetcher and DBRefFetcher to create valid accession strings from an
479 * ID string for database sources with a Regex validation field.
483 * @return q if proxy.getAccessionValidator()==null, otherwise the matched
484 * region or the first subgroup match from the matched region
486 public static String processQueryToAccessionFor(DbSourceProxy proxy,
489 if (proxy.getAccessionValidator() != null)
491 Regex vgr = proxy.getAccessionValidator();
493 if (vgr.numSubs() > 0)
495 return (vgr.stringMatched(1));
499 return (vgr.stringMatched());