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.
25 import jalview.datamodel.*;
27 public class DBRefUtils
30 * Utilities for handling DBRef objects and their collections.
35 * Vector of DBRef objects to search
37 * String[] array of source DBRef IDs to retrieve
40 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
51 Hashtable srcs = new Hashtable();
52 Vector res = new Vector();
54 for (int i = 0; i < sources.length; i++)
56 srcs.put(new String(sources[i]), new Integer(i));
58 for (int i = 0, j = dbrefs.length; i < j; i++)
60 if (srcs.containsKey(dbrefs[i].getSource()))
62 res.addElement(dbrefs[i]);
68 DBRefEntry[] reply = new DBRefEntry[res.size()];
69 for (int i = 0; i < res.size(); i++)
71 reply[i] = (DBRefEntry) res.elementAt(i);
76 // there are probable memory leaks in the hashtable!
81 * isDasCoordinateSystem
87 * @return boolean true if Source DBRefEntry is compatible with DAS
88 * CoordinateSystem name
90 public static Hashtable DasCoordinateSystemsLookup = null;
92 public static boolean isDasCoordinateSystem(String string,
93 DBRefEntry dBRefEntry)
95 if (DasCoordinateSystemsLookup == null)
97 // TODO: Make a DasCoordinateSystemsLookup properties resource
99 DasCoordinateSystemsLookup = new Hashtable();
100 DasCoordinateSystemsLookup.put("pdbresnum",
101 jalview.datamodel.DBRefSource.PDB);
102 DasCoordinateSystemsLookup.put("uniprot",
103 jalview.datamodel.DBRefSource.UNIPROT);
104 DasCoordinateSystemsLookup.put("EMBL",
105 jalview.datamodel.DBRefSource.EMBL);
106 // DasCoordinateSystemsLookup.put("EMBL",
107 // jalview.datamodel.DBRefSource.EMBLCDS);
110 String coordsys = (String) DasCoordinateSystemsLookup.get(string
112 if (coordsys != null)
114 return coordsys.equals(dBRefEntry.getSource());
119 public static Hashtable CanonicalSourceNameLookup = null;
122 * look up source in an internal list of database reference sources and return
123 * the canonical jalview name for the source, or the original string if it has
127 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
130 public static String getCanonicalName(String source)
132 if (CanonicalSourceNameLookup == null)
134 CanonicalSourceNameLookup = new Hashtable();
135 CanonicalSourceNameLookup.put("uniprotkb/swiss-prot",
136 jalview.datamodel.DBRefSource.UNIPROT);
137 CanonicalSourceNameLookup.put("uniprotkb/trembl",
138 jalview.datamodel.DBRefSource.UNIPROT);
139 CanonicalSourceNameLookup.put("pdb",
140 jalview.datamodel.DBRefSource.PDB);
142 String canonical = (String) CanonicalSourceNameLookup.get(source
144 if (canonical == null)
152 * find RefEntry corresponding to a particular pattern the equals method of
153 * each entry is used, from String attributes right down to Mapping
157 * Set of references to search
159 * pattern to collect - null any entry for wildcard match
162 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
164 return searchRefs(ref, entry,
165 matchDbAndIdAndEitherMapOrEquivalentMapList);
168 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry,
169 DbRefComp comparator)
171 if (ref == null || entry == null)
173 Vector rfs = new Vector();
174 for (int i = 0; i < ref.length; i++)
176 if (comparator.matches(entry, ref[i]))
178 rfs.addElement(ref[i]);
181 // TODO Auto-generated method stub
184 DBRefEntry[] rf = new DBRefEntry[rfs.size()];
191 public interface DbRefComp
193 public boolean matches(DBRefEntry refa, DBRefEntry refb);
197 * match on all non-null fields in refa
199 public static DbRefComp matchNonNullonA = new DbRefComp()
201 public boolean matches(DBRefEntry refa, DBRefEntry refb)
203 if (refa.getSource() == null
204 || refb.getSource().equals(refa.getSource()))
206 if (refa.getVersion() == null
207 || refb.getVersion().equals(refa.getVersion()))
209 if (refa.getAccessionId() == null
210 || refb.getAccessionId().equals(refa.getAccessionId()))
212 if (refa.getMap() == null
213 || (refb.getMap() != null && refb.getMap().equals(
226 * either field is null or field matches for all of source, version, accession
229 public static DbRefComp matchEitherNonNull = new DbRefComp()
231 public boolean matches(DBRefEntry refa, DBRefEntry refb)
233 if ((refa.getSource() == null || refb.getSource() == null)
234 || refb.getSource().equals(refa.getSource()))
236 if ((refa.getVersion() == null || refb.getVersion() == null)
237 || refb.getVersion().equals(refa.getVersion()))
239 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
240 || refb.getAccessionId().equals(refa.getAccessionId()))
242 if ((refa.getMap() == null || refb.getMap() == null)
243 || (refb.getMap() != null && refb.getMap().equals(
256 * accession ID and DB must be identical. Version is ignored. Map is either
257 * not defined or is a match (or is compatible?)
259 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
261 public boolean matches(DBRefEntry refa, DBRefEntry refb)
263 if (refa.getSource() != null && refb.getSource() != null
264 && refb.getSource().equals(refa.getSource()))
266 // We dont care about version
267 // if ((refa.getVersion()==null || refb.getVersion()==null)
268 // || refb.getVersion().equals(refa.getVersion()))
270 if (refa.getAccessionId() != null && refb.getAccessionId() != null
271 || refb.getAccessionId().equals(refa.getAccessionId()))
273 if ((refa.getMap() == null || refb.getMap() == null)
274 || (refa.getMap() != null && refb.getMap() != null && refb
275 .getMap().equals(refa.getMap())))
286 * accession ID and DB must be identical. Version is ignored. No map on either
287 * or map but no maplist on either or maplist of map on a is the complement of
288 * maplist of map on b.
290 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
292 public boolean matches(DBRefEntry refa, DBRefEntry refb)
294 if (refa.getSource() != null && refb.getSource() != null
295 && refb.getSource().equals(refa.getSource()))
297 // We dont care about version
298 // if ((refa.getVersion()==null || refb.getVersion()==null)
299 // || refb.getVersion().equals(refa.getVersion()))
301 if (refa.getAccessionId() != null && refb.getAccessionId() != null
302 || refb.getAccessionId().equals(refa.getAccessionId()))
304 if ((refa.getMap() == null && refb.getMap() == null)
305 || (refa.getMap() != null && refb.getMap() != null))
306 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
307 || (refb.getMap().getMap() != null
308 && refa.getMap().getMap() != null && refb
309 .getMap().getMap().getInverse()
310 .equals(refa.getMap().getMap())))
321 * accession ID and DB must be identical. Version is ignored. No map on both
322 * or or map but no maplist on either or maplist of map on a is equivalent to
323 * the maplist of map on b.
325 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()
365 public boolean matches(DBRefEntry refa, DBRefEntry refb)
367 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
368 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
369 if (refa.getSource() != null && refb.getSource() != null
370 && refb.getSource().equals(refa.getSource()))
372 // We dont care about version
373 // if ((refa.getVersion()==null || refb.getVersion()==null)
374 // || refb.getVersion().equals(refa.getVersion()))
376 if (refa.getAccessionId() != null && refb.getAccessionId() != null
377 && refb.getAccessionId().equals(refa.getAccessionId()))
379 if (refa.getMap() == null || refb.getMap() == null)
383 if ((refa.getMap() != null && refb.getMap() != null)
384 && (refb.getMap().getMap() == null && refa.getMap()
386 || (refb.getMap().getMap() != null
387 && refa.getMap().getMap() != null && (refb
388 .getMap().getMap().equals(refa.getMap().getMap()))))
389 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
399 * used by file parsers to generate DBRefs from annotation within file (eg
406 * where to anotate with reference
407 * @return parsed version of entry that was added to seq (if any)
409 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
410 String version, String acn)
412 DBRefEntry ref = null;
415 String locsrc = jalview.util.DBRefUtils.getCanonicalName(dbname);
416 if (locsrc.equals(jalview.datamodel.DBRefSource.PDB))
418 // check for chaincode and mapping
419 // PFAM style stockhom PDB citation
420 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
421 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
422 if (r.search(acn.trim()))
424 String pdbid = r.stringMatched(1);
425 String chaincode = r.stringMatched(2);
430 String mapstart = r.stringMatched(3);
431 String mapend = r.stringMatched(4);
432 if (chaincode.equals(" "))
436 // construct pdb ref.
437 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
438 PDBEntry pdbr = new PDBEntry();
440 pdbr.setProperty(new Hashtable());
441 pdbr.getProperty().put("CHAIN", chaincode);
444 System.err.println("Malformed PDB DR line:"+acn);
450 ref = new DBRefEntry(locsrc, version, acn);