2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import jalview.datamodel.*;
25 public class DBRefUtils
28 * Utilities for handling DBRef objects and their collections.
33 * Vector of DBRef objects to search
35 * String[] array of source DBRef IDs to retrieve
38 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
49 Hashtable srcs = new Hashtable();
50 Vector res = new Vector();
52 for (int i = 0; i < sources.length; i++)
54 srcs.put(new String(sources[i]), new Integer(i));
56 for (int i = 0, j = dbrefs.length; i < j; i++)
58 if (srcs.containsKey(dbrefs[i].getSource()))
60 res.addElement(dbrefs[i]);
66 DBRefEntry[] reply = new DBRefEntry[res.size()];
67 for (int i = 0; i < res.size(); i++)
69 reply[i] = (DBRefEntry) res.elementAt(i);
74 // there are probable memory leaks in the hashtable!
79 * isDasCoordinateSystem
85 * @return boolean true if Source DBRefEntry is compatible with DAS
86 * CoordinateSystem name
88 public static Hashtable DasCoordinateSystemsLookup = null;
90 public static boolean isDasCoordinateSystem(String string,
91 DBRefEntry dBRefEntry)
93 if (DasCoordinateSystemsLookup == null)
95 // TODO: Make a DasCoordinateSystemsLookup properties resource
97 DasCoordinateSystemsLookup = new Hashtable();
98 DasCoordinateSystemsLookup.put("pdbresnum",
99 jalview.datamodel.DBRefSource.PDB);
100 DasCoordinateSystemsLookup.put("uniprot",
101 jalview.datamodel.DBRefSource.UNIPROT);
102 DasCoordinateSystemsLookup.put("EMBL",
103 jalview.datamodel.DBRefSource.EMBL);
104 // DasCoordinateSystemsLookup.put("EMBL",
105 // jalview.datamodel.DBRefSource.EMBLCDS);
108 String coordsys = (String) DasCoordinateSystemsLookup.get(string
110 if (coordsys != null)
112 return coordsys.equals(dBRefEntry.getSource());
117 public static Hashtable CanonicalSourceNameLookup = null;
120 * look up source in an internal list of database reference sources and return
121 * the canonical jalview name for the source, or the original string if it has
125 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
128 public static String getCanonicalName(String source)
130 if (CanonicalSourceNameLookup == null)
132 CanonicalSourceNameLookup = new Hashtable();
133 CanonicalSourceNameLookup.put("uniprotkb/swiss-prot",
134 jalview.datamodel.DBRefSource.UNIPROT);
135 CanonicalSourceNameLookup.put("uniprotkb/trembl",
136 jalview.datamodel.DBRefSource.UNIPROT);
137 CanonicalSourceNameLookup.put("pdb",
138 jalview.datamodel.DBRefSource.PDB);
140 String canonical = (String) CanonicalSourceNameLookup.get(source
142 if (canonical == null)
150 * find RefEntry corresponding to a particular pattern the equals method of
151 * each entry is used, from String attributes right down to Mapping
155 * Set of references to search
157 * pattern to collect - null any entry for wildcard match
160 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
162 return searchRefs(ref, entry,
163 matchDbAndIdAndEitherMapOrEquivalentMapList);
166 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry,
167 DbRefComp comparator)
169 if (ref == null || entry == null)
171 Vector rfs = new Vector();
172 for (int i = 0; i < ref.length; i++)
174 if (comparator.matches(entry, ref[i]))
176 rfs.addElement(ref[i]);
179 // TODO Auto-generated method stub
182 DBRefEntry[] rf = new DBRefEntry[rfs.size()];
189 public interface DbRefComp
191 public boolean matches(DBRefEntry refa, DBRefEntry refb);
195 * match on all non-null fields in refa
197 public static DbRefComp matchNonNullonA = new DbRefComp()
199 public boolean matches(DBRefEntry refa, DBRefEntry refb)
201 if (refa.getSource() == null
202 || refb.getSource().equals(refa.getSource()))
204 if (refa.getVersion() == null
205 || refb.getVersion().equals(refa.getVersion()))
207 if (refa.getAccessionId() == null
208 || refb.getAccessionId().equals(refa.getAccessionId()))
210 if (refa.getMap() == null
211 || (refb.getMap() != null && refb.getMap().equals(
224 * either field is null or field matches for all of source, version, accession
227 public static DbRefComp matchEitherNonNull = new DbRefComp()
229 public boolean matches(DBRefEntry refa, DBRefEntry refb)
231 if ((refa.getSource() == null || refb.getSource() == null)
232 || refb.getSource().equals(refa.getSource()))
234 if ((refa.getVersion() == null || refb.getVersion() == null)
235 || refb.getVersion().equals(refa.getVersion()))
237 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
238 || refb.getAccessionId().equals(refa.getAccessionId()))
240 if ((refa.getMap() == null || refb.getMap() == null)
241 || (refb.getMap() != null && refb.getMap().equals(
254 * accession ID and DB must be identical. Version is ignored. Map is either
255 * not defined or is a match (or is compatible?)
257 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
259 public boolean matches(DBRefEntry refa, DBRefEntry refb)
261 if (refa.getSource() != null && refb.getSource() != null
262 && refb.getSource().equals(refa.getSource()))
264 // We dont care about version
265 // if ((refa.getVersion()==null || refb.getVersion()==null)
266 // || refb.getVersion().equals(refa.getVersion()))
268 if (refa.getAccessionId() != null && refb.getAccessionId() != null
269 || refb.getAccessionId().equals(refa.getAccessionId()))
271 if ((refa.getMap() == null || refb.getMap() == null)
272 || (refa.getMap() != null && refb.getMap() != null && refb
273 .getMap().equals(refa.getMap())))
284 * accession ID and DB must be identical. Version is ignored. No map on either
285 * or map but no maplist on either or maplist of map on a is the complement of
286 * maplist of map on b.
288 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
290 public boolean matches(DBRefEntry refa, DBRefEntry refb)
292 if (refa.getSource() != null && refb.getSource() != null
293 && refb.getSource().equals(refa.getSource()))
295 // We dont care about version
296 // if ((refa.getVersion()==null || refb.getVersion()==null)
297 // || refb.getVersion().equals(refa.getVersion()))
299 if (refa.getAccessionId() != null && refb.getAccessionId() != null
300 || refb.getAccessionId().equals(refa.getAccessionId()))
302 if ((refa.getMap() == null && refb.getMap() == null)
303 || (refa.getMap() != null && refb.getMap() != null))
304 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
305 || (refb.getMap().getMap() != null
306 && refa.getMap().getMap() != null && refb
307 .getMap().getMap().getInverse().equals(
308 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 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
325 public boolean matches(DBRefEntry refa, DBRefEntry refb)
327 if (refa.getSource() != null && refb.getSource() != null
328 && refb.getSource().equals(refa.getSource()))
330 // We dont care about version
331 // if ((refa.getVersion()==null || refb.getVersion()==null)
332 // || refb.getVersion().equals(refa.getVersion()))
334 if (refa.getAccessionId() != null && refb.getAccessionId() != null
335 || refb.getAccessionId().equals(refa.getAccessionId()))
337 if (refa.getMap() == null && refb.getMap() == null)
341 if (refa.getMap() != null
342 && refb.getMap() != null
343 && ((refb.getMap().getMap() == null && refa.getMap()
344 .getMap() == null) || (refb.getMap().getMap() != null
345 && refa.getMap().getMap() != null && refb
346 .getMap().getMap().equals(refa.getMap().getMap()))))
357 * accession ID and DB must be identical. Version is ignored. No map on either
358 * or map but no maplist on either or maplist of map on a is equivalent to the
359 * maplist of map on b.
361 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
363 public boolean matches(DBRefEntry refa, DBRefEntry refb)
365 if (refa.getSource() != null && refb.getSource() != null
366 && refb.getSource().equals(refa.getSource()))
368 // We dont care about version
369 // if ((refa.getVersion()==null || refb.getVersion()==null)
370 // || refb.getVersion().equals(refa.getVersion()))
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())))
395 * used by file parsers to generate DBRefs from annotation within file (eg
402 * where to anotate with reference
403 * @return parsed version of entry that was added to seq (if any)
405 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
406 String version, String acn)
408 DBRefEntry ref = null;
411 String locsrc = jalview.util.DBRefUtils.getCanonicalName(dbname);
412 if (locsrc.equals(jalview.datamodel.DBRefSource.PDB))
414 // check for chaincode and mapping
415 // PFAM style stockhom PDB citation
416 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
417 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;([0-9]+)-([0-9]+)");
418 if (r.search(acn.trim()))
420 String pdbid = r.stringMatched(1);
421 String chaincode = r.stringMatched(2);
422 String mapstart = r.stringMatched(3);
423 String mapend = r.stringMatched(4);
424 if (chaincode.equals(" "))
428 // construct pdb ref.
429 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
430 PDBEntry pdbr = new PDBEntry();
438 ref = new DBRefEntry(locsrc, version, acn);