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.Hashtable;
31 import java.util.List;
34 import com.stevesoft.pat.Regex;
36 public class DBRefUtils
38 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
40 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
44 // TODO load these from a resource file?
45 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
47 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
48 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
50 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
51 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
52 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
53 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
57 * Utilities for handling DBRef objects and their collections.
62 * Vector of DBRef objects to search
64 * String[] array of source DBRef IDs to retrieve
67 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
78 Map<String, Integer> srcs = new HashMap<String, Integer>();
79 ArrayList<DBRefEntry> res = new ArrayList<DBRefEntry>();
81 for (int i = 0; i < sources.length; i++)
83 srcs.put(new String(sources[i]), new Integer(i));
85 for (int i = 0, j = dbrefs.length; i < j; i++)
87 if (srcs.containsKey(dbrefs[i].getSource()))
95 DBRefEntry[] reply = new DBRefEntry[res.size()];
96 return res.toArray(reply);
99 // there are probable memory leaks in the hashtable!
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 public static DbRefComp matchNonNullonA = new DbRefComp()
205 public boolean matches(DBRefEntry refa, DBRefEntry refb)
207 if (refa.getSource() == null
208 || refb.getSource().equals(refa.getSource()))
210 if (refa.getVersion() == null
211 || refb.getVersion().equals(refa.getVersion()))
213 if (refa.getAccessionId() == null
214 || refb.getAccessionId().equals(refa.getAccessionId()))
216 if (refa.getMap() == null
217 || (refb.getMap() != null && refb.getMap().equals(
230 * either field is null or field matches for all of source, version, accession
233 public static DbRefComp matchEitherNonNull = new DbRefComp()
235 public boolean matches(DBRefEntry refa, DBRefEntry refb)
237 if ((refa.getSource() == null || refb.getSource() == null)
238 || refb.getSource().equals(refa.getSource()))
240 if ((refa.getVersion() == null || refb.getVersion() == null)
241 || refb.getVersion().equals(refa.getVersion()))
243 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
244 || refb.getAccessionId().equals(refa.getAccessionId()))
246 if ((refa.getMap() == null || refb.getMap() == null)
247 || (refb.getMap() != null && refb.getMap().equals(
260 * accession ID and DB must be identical. Version is ignored. Map is either
261 * not defined or is a match (or is compatible?)
263 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
265 public boolean matches(DBRefEntry refa, DBRefEntry refb)
267 if (refa.getSource() != null && refb.getSource() != null
268 && refb.getSource().equals(refa.getSource()))
270 // We dont care about version
271 // if ((refa.getVersion()==null || refb.getVersion()==null)
272 // || refb.getVersion().equals(refa.getVersion()))
274 if (refa.getAccessionId() != null && refb.getAccessionId() != null
275 || refb.getAccessionId().equals(refa.getAccessionId()))
277 if ((refa.getMap() == null || refb.getMap() == null)
278 || (refa.getMap() != null && refb.getMap() != null && refb
279 .getMap().equals(refa.getMap())))
290 * accession ID and DB must be identical. Version is ignored. No map on either
291 * or map but no maplist on either or maplist of map on a is the complement of
292 * maplist of map on b.
294 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
296 public boolean matches(DBRefEntry refa, DBRefEntry refb)
298 if (refa.getSource() != null && refb.getSource() != null
299 && refb.getSource().equals(refa.getSource()))
301 // We dont care about version
302 // if ((refa.getVersion()==null || refb.getVersion()==null)
303 // || refb.getVersion().equals(refa.getVersion()))
305 if (refa.getAccessionId() != null && refb.getAccessionId() != null
306 || refb.getAccessionId().equals(refa.getAccessionId()))
308 if ((refa.getMap() == null && refb.getMap() == null)
309 || (refa.getMap() != null && refb.getMap() != null))
311 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
312 || (refb.getMap().getMap() != null
313 && refa.getMap().getMap() != null && refb
314 .getMap().getMap().getInverse()
315 .equals(refa.getMap().getMap())))
327 * accession ID and DB must be identical. Version is ignored. No map on both
328 * or or map but no maplist on either or maplist of map on a is equivalent to
329 * the maplist of map on b.
331 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
333 public boolean matches(DBRefEntry refa, DBRefEntry refb)
335 if (refa.getSource() != null && refb.getSource() != null
336 && refb.getSource().equals(refa.getSource()))
338 // We dont care about version
339 // if ((refa.getVersion()==null || refb.getVersion()==null)
340 // || refb.getVersion().equals(refa.getVersion()))
342 if (refa.getAccessionId() != null && refb.getAccessionId() != null
343 || refb.getAccessionId().equals(refa.getAccessionId()))
345 if (refa.getMap() == null && refb.getMap() == null)
349 if (refa.getMap() != null
350 && refb.getMap() != null
351 && ((refb.getMap().getMap() == null && refa.getMap()
352 .getMap() == null) || (refb.getMap().getMap() != null
353 && refa.getMap().getMap() != null && refb
354 .getMap().getMap().equals(refa.getMap().getMap()))))
365 * accession ID and DB must be identical. Version is ignored. No map on either
366 * or map but no maplist on either or maplist of map on a is equivalent to the
367 * maplist of map on b.
369 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
371 public boolean matches(DBRefEntry refa, DBRefEntry refb)
373 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
374 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
375 if (refa.getSource() != null && refb.getSource() != null
376 && refb.getSource().equals(refa.getSource()))
378 // We dont care about version
379 // if ((refa.getVersion()==null || refb.getVersion()==null)
380 // || refb.getVersion().equals(refa.getVersion()))
382 if (refa.getAccessionId() != null && refb.getAccessionId() != null
383 && refb.getAccessionId().equals(refa.getAccessionId()))
385 if (refa.getMap() == null || refb.getMap() == null)
389 if ((refa.getMap() != null && refb.getMap() != null)
390 && (refb.getMap().getMap() == null && refa.getMap()
392 || (refb.getMap().getMap() != null
393 && refa.getMap().getMap() != null && (refb
394 .getMap().getMap().equals(refa.getMap().getMap()))))
395 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
405 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
408 * Used by file parsers to generate DBRefs from annotation within file (eg
415 * where to annotate with reference
416 * @return parsed version of entry that was added to seq (if any)
418 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
419 String version, String acn)
421 DBRefEntry ref = null;
424 String locsrc = DBRefUtils.getCanonicalName(dbname);
425 if (locsrc.equals(DBRefSource.PDB))
428 * Check for PFAM style stockhom PDB accession id citation e.g.
431 Regex r = new com.stevesoft.pat.Regex(
432 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
433 if (r.search(acn.trim()))
435 String pdbid = r.stringMatched(1);
436 String chaincode = r.stringMatched(2);
437 if (chaincode == null)
441 // String mapstart = r.stringMatched(3);
442 // String mapend = r.stringMatched(4);
443 if (chaincode.equals(" "))
447 // construct pdb ref.
448 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
449 PDBEntry pdbr = new PDBEntry();
451 pdbr.setType(PDBEntry.Type.PDB);
452 pdbr.setProperty(new Hashtable());
453 pdbr.setChainCode(chaincode);
454 // pdbr.getProperty().put("CHAIN", chaincode);
459 System.err.println("Malformed PDB DR line:" + acn);
465 ref = new DBRefEntry(locsrc, version, acn);