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.toLowerCase());
123 return coordsys == null ? false : coordsys.equals(dBRefEntry
128 * look up source in an internal list of database reference sources and return
129 * the canonical jalview name for the source, or the original string if it has
133 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
136 public static String getCanonicalName(String source)
142 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
143 return canonical == null ? source : canonical;
147 * Returns an array of those references that match the given entry, or null if
148 * no matches. Currently uses a comparator which matches if
150 * <li>database sources are the same</li>
151 * <li>accession ids are the same</li>
152 * <li>both have no mapping, or the mappings are the same</li>
156 * Set of references to search
161 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
163 return searchRefs(ref, entry,
164 matchDbAndIdAndEitherMapOrEquivalentMapList);
168 * Returns an array of those references that match the given entry, according
169 * to the given comparator. Returns null if no matches.
172 * an array of database references to search
174 * an entry to compare against
178 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
179 DbRefComp comparator)
181 if (refs == null || entry == null)
185 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
186 for (int i = 0; i < refs.length; i++)
188 if (comparator.matches(entry, refs[i]))
193 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
198 public boolean matches(DBRefEntry refa, DBRefEntry refb);
202 * match on all non-null fields in refa
204 public static DbRefComp matchNonNullonA = new DbRefComp()
206 public boolean matches(DBRefEntry refa, DBRefEntry refb)
208 if (refa.getSource() == null
209 || refb.getSource().equals(refa.getSource()))
211 if (refa.getVersion() == null
212 || refb.getVersion().equals(refa.getVersion()))
214 if (refa.getAccessionId() == null
215 || refb.getAccessionId().equals(refa.getAccessionId()))
217 if (refa.getMap() == null
218 || (refb.getMap() != null && refb.getMap().equals(
231 * either field is null or field matches for all of source, version, accession
234 public static DbRefComp matchEitherNonNull = new DbRefComp()
236 public boolean matches(DBRefEntry refa, DBRefEntry refb)
238 if ((refa.getSource() == null || refb.getSource() == null)
239 || refb.getSource().equals(refa.getSource()))
241 if ((refa.getVersion() == null || refb.getVersion() == null)
242 || refb.getVersion().equals(refa.getVersion()))
244 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
245 || refb.getAccessionId().equals(refa.getAccessionId()))
247 if ((refa.getMap() == null || refb.getMap() == null)
248 || (refb.getMap() != null && refb.getMap().equals(
261 * accession ID and DB must be identical. Version is ignored. Map is either
262 * not defined or is a match (or is compatible?)
264 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
266 public boolean matches(DBRefEntry refa, DBRefEntry refb)
268 if (refa.getSource() != null && refb.getSource() != null
269 && refb.getSource().equals(refa.getSource()))
271 // We dont care about version
272 // if ((refa.getVersion()==null || refb.getVersion()==null)
273 // || refb.getVersion().equals(refa.getVersion()))
275 if (refa.getAccessionId() != null && refb.getAccessionId() != null
276 || refb.getAccessionId().equals(refa.getAccessionId()))
278 if ((refa.getMap() == null || refb.getMap() == null)
279 || (refa.getMap() != null && refb.getMap() != null && refb
280 .getMap().equals(refa.getMap())))
291 * accession ID and DB must be identical. Version is ignored. No map on either
292 * or map but no maplist on either or maplist of map on a is the complement of
293 * maplist of map on b.
295 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
297 public boolean matches(DBRefEntry refa, DBRefEntry refb)
299 if (refa.getSource() != null && refb.getSource() != null
300 && refb.getSource().equals(refa.getSource()))
302 // We dont care about version
303 // if ((refa.getVersion()==null || refb.getVersion()==null)
304 // || refb.getVersion().equals(refa.getVersion()))
306 if (refa.getAccessionId() != null && refb.getAccessionId() != null
307 || refb.getAccessionId().equals(refa.getAccessionId()))
309 if ((refa.getMap() == null && refb.getMap() == null)
310 || (refa.getMap() != null && refb.getMap() != null))
312 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
313 || (refb.getMap().getMap() != null
314 && refa.getMap().getMap() != null && refb
315 .getMap().getMap().getInverse()
316 .equals(refa.getMap().getMap())))
328 * accession ID and DB must be identical. Version is ignored. No map on both
329 * or or map but no maplist on either or maplist of map on a is equivalent to
330 * the maplist of map on b.
332 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
334 public boolean matches(DBRefEntry refa, DBRefEntry refb)
336 if (refa.getSource() != null && refb.getSource() != null
337 && refb.getSource().equals(refa.getSource()))
339 // We dont care about version
340 // if ((refa.getVersion()==null || refb.getVersion()==null)
341 // || refb.getVersion().equals(refa.getVersion()))
343 if (refa.getAccessionId() != null && refb.getAccessionId() != null
344 || refb.getAccessionId().equals(refa.getAccessionId()))
346 if (refa.getMap() == null && refb.getMap() == null)
350 if (refa.getMap() != null
351 && refb.getMap() != null
352 && ((refb.getMap().getMap() == null && refa.getMap()
353 .getMap() == null) || (refb.getMap().getMap() != null
354 && refa.getMap().getMap() != null && refb
355 .getMap().getMap().equals(refa.getMap().getMap()))))
366 * accession ID and DB must be identical. Version is ignored. No map on either
367 * or map but no maplist on either or maplist of map on a is equivalent to the
368 * maplist of map on b.
370 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
372 public boolean matches(DBRefEntry refa, DBRefEntry refb)
374 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
375 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
376 if (refa.getSource() != null && refb.getSource() != null
377 && refb.getSource().equals(refa.getSource()))
379 // We dont care about version
380 // if ((refa.getVersion()==null || refb.getVersion()==null)
381 // || refb.getVersion().equals(refa.getVersion()))
383 if (refa.getAccessionId() != null && refb.getAccessionId() != null
384 && refb.getAccessionId().equals(refa.getAccessionId()))
386 if (refa.getMap() == null || refb.getMap() == null)
390 if ((refa.getMap() != null && refb.getMap() != null)
391 && (refb.getMap().getMap() == null && refa.getMap()
393 || (refb.getMap().getMap() != null
394 && refa.getMap().getMap() != null && (refb
395 .getMap().getMap().equals(refa.getMap().getMap()))))
396 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
406 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
409 * Used by file parsers to generate DBRefs from annotation within file (eg
416 * where to annotate with reference
417 * @return parsed version of entry that was added to seq (if any)
419 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
420 String version, String acn)
422 DBRefEntry ref = null;
425 String locsrc = DBRefUtils.getCanonicalName(dbname);
426 if (locsrc.equals(DBRefSource.PDB))
429 * Check for PFAM style stockhom PDB accession id citation e.g.
432 Regex r = new com.stevesoft.pat.Regex(
433 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
434 if (r.search(acn.trim()))
436 String pdbid = r.stringMatched(1);
437 String chaincode = r.stringMatched(2);
438 if (chaincode == null)
442 // String mapstart = r.stringMatched(3);
443 // String mapend = r.stringMatched(4);
444 if (chaincode.equals(" "))
448 // construct pdb ref.
449 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
450 PDBEntry pdbr = new PDBEntry();
452 pdbr.setType(PDBEntry.Type.PDB);
453 pdbr.setProperty(new Hashtable());
454 pdbr.setChainCode(chaincode);
455 // pdbr.getProperty().put("CHAIN", chaincode);
460 System.err.println("Malformed PDB DR line:" + acn);
466 ref = new DBRefEntry(locsrc, version, acn);