2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import jalview.datamodel.*;
24 public class DBRefUtils
27 * Utilities for handling DBRef objects and their collections.
32 * Vector of DBRef objects to search
34 * String[] array of source DBRef IDs to retrieve
37 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
48 Hashtable srcs = new Hashtable();
49 Vector res = new Vector();
51 for (int i = 0; i < sources.length; i++)
53 srcs.put(new String(sources[i]), new Integer(i));
55 for (int i = 0, j = dbrefs.length; i < j; i++)
57 if (srcs.containsKey(dbrefs[i].getSource()))
59 res.addElement(dbrefs[i]);
65 DBRefEntry[] reply = new DBRefEntry[res.size()];
66 for (int i = 0; i < res.size(); i++)
68 reply[i] = (DBRefEntry) res.elementAt(i);
73 // there are probable memory leaks in the hashtable!
78 * isDasCoordinateSystem
84 * @return boolean true if Source DBRefEntry is compatible with DAS
85 * CoordinateSystem name
87 public static Hashtable DasCoordinateSystemsLookup = null;
89 public static boolean isDasCoordinateSystem(String string,
90 DBRefEntry dBRefEntry)
92 if (DasCoordinateSystemsLookup == null)
94 // TODO: Make a DasCoordinateSystemsLookup properties resource
96 DasCoordinateSystemsLookup = new Hashtable();
97 DasCoordinateSystemsLookup.put("pdbresnum",
98 jalview.datamodel.DBRefSource.PDB);
99 DasCoordinateSystemsLookup.put("uniprot",
100 jalview.datamodel.DBRefSource.UNIPROT);
101 DasCoordinateSystemsLookup.put("EMBL",
102 jalview.datamodel.DBRefSource.EMBL);
103 // DasCoordinateSystemsLookup.put("EMBL",
104 // jalview.datamodel.DBRefSource.EMBLCDS);
107 String coordsys = (String) DasCoordinateSystemsLookup.get(string
109 if (coordsys != null)
111 return coordsys.equals(dBRefEntry.getSource());
116 public static Hashtable CanonicalSourceNameLookup = null;
119 * look up source in an internal list of database reference sources and return
120 * the canonical jalview name for the source, or the original string if it has
124 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
127 public static String getCanonicalName(String source)
129 if (CanonicalSourceNameLookup == null)
131 CanonicalSourceNameLookup = new Hashtable();
132 CanonicalSourceNameLookup.put("uniprotkb/swiss-prot",
133 jalview.datamodel.DBRefSource.UNIPROT);
134 CanonicalSourceNameLookup.put("uniprotkb/trembl",
135 jalview.datamodel.DBRefSource.UNIPROT);
136 CanonicalSourceNameLookup.put("pdb",
137 jalview.datamodel.DBRefSource.PDB);
139 String canonical = (String) CanonicalSourceNameLookup.get(source
141 if (canonical == null)
149 * find RefEntry corresponding to a particular pattern the equals method of
150 * each entry is used, from String attributes right down to Mapping
154 * Set of references to search
156 * pattern to collect - null any entry for wildcard match
159 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
161 return searchRefs(ref, entry,
162 matchDbAndIdAndEitherMapOrEquivalentMapList);
165 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry,
166 DbRefComp comparator)
168 if (ref == null || entry == null)
170 Vector rfs = new Vector();
171 for (int i = 0; i < ref.length; i++)
173 if (comparator.matches(entry, ref[i]))
175 rfs.addElement(ref[i]);
178 // TODO Auto-generated method stub
181 DBRefEntry[] rf = new DBRefEntry[rfs.size()];
188 public interface DbRefComp
190 public boolean matches(DBRefEntry refa, DBRefEntry refb);
194 * match on all non-null fields in refa
196 public static DbRefComp matchNonNullonA = new DbRefComp()
198 public boolean matches(DBRefEntry refa, DBRefEntry refb)
200 if (refa.getSource() == null
201 || refb.getSource().equals(refa.getSource()))
203 if (refa.getVersion() == null
204 || refb.getVersion().equals(refa.getVersion()))
206 if (refa.getAccessionId() == null
207 || refb.getAccessionId().equals(refa.getAccessionId()))
209 if (refa.getMap() == null
210 || (refb.getMap() != null && refb.getMap().equals(
223 * either field is null or field matches for all of source, version, accession
226 public static DbRefComp matchEitherNonNull = new DbRefComp()
228 public boolean matches(DBRefEntry refa, DBRefEntry refb)
230 if ((refa.getSource() == null || refb.getSource() == null)
231 || refb.getSource().equals(refa.getSource()))
233 if ((refa.getVersion() == null || refb.getVersion() == null)
234 || refb.getVersion().equals(refa.getVersion()))
236 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
237 || refb.getAccessionId().equals(refa.getAccessionId()))
239 if ((refa.getMap() == null || refb.getMap() == null)
240 || (refb.getMap() != null && refb.getMap().equals(
253 * accession ID and DB must be identical. Version is ignored. Map is either
254 * not defined or is a match (or is compatible?)
256 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
258 public boolean matches(DBRefEntry refa, DBRefEntry refb)
260 if (refa.getSource() != null && refb.getSource() != null
261 && refb.getSource().equals(refa.getSource()))
263 // We dont care about version
264 // if ((refa.getVersion()==null || refb.getVersion()==null)
265 // || refb.getVersion().equals(refa.getVersion()))
267 if (refa.getAccessionId() != null && refb.getAccessionId() != null
268 || refb.getAccessionId().equals(refa.getAccessionId()))
270 if ((refa.getMap() == null || refb.getMap() == null)
271 || (refa.getMap() != null && refb.getMap() != null && refb
272 .getMap().equals(refa.getMap())))
283 * accession ID and DB must be identical. Version is ignored. No map on either
284 * or map but no maplist on either or maplist of map on a is the complement of
285 * maplist of map on b.
287 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
289 public boolean matches(DBRefEntry refa, DBRefEntry refb)
291 if (refa.getSource() != null && refb.getSource() != null
292 && refb.getSource().equals(refa.getSource()))
294 // We dont care about version
295 // if ((refa.getVersion()==null || refb.getVersion()==null)
296 // || refb.getVersion().equals(refa.getVersion()))
298 if (refa.getAccessionId() != null && refb.getAccessionId() != null
299 || refb.getAccessionId().equals(refa.getAccessionId()))
301 if ((refa.getMap() == null && refb.getMap() == null)
302 || (refa.getMap() != null && refb.getMap() != null))
303 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
304 || (refb.getMap().getMap() != null
305 && refa.getMap().getMap() != null && refb
306 .getMap().getMap().getInverse()
307 .equals(refa.getMap().getMap())))
318 * accession ID and DB must be identical. Version is ignored. No map on both
319 * or or map but no maplist on either or maplist of map on a is equivalent to
320 * the maplist of map on b.
322 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
324 public boolean matches(DBRefEntry refa, DBRefEntry refb)
326 if (refa.getSource() != null && refb.getSource() != null
327 && refb.getSource().equals(refa.getSource()))
329 // We dont care about version
330 // if ((refa.getVersion()==null || refb.getVersion()==null)
331 // || refb.getVersion().equals(refa.getVersion()))
333 if (refa.getAccessionId() != null && refb.getAccessionId() != null
334 || refb.getAccessionId().equals(refa.getAccessionId()))
336 if (refa.getMap() == null && refb.getMap() == null)
340 if (refa.getMap() != null
341 && refb.getMap() != null
342 && ((refb.getMap().getMap() == null && refa.getMap()
343 .getMap() == null) || (refb.getMap().getMap() != null
344 && refa.getMap().getMap() != null && refb
345 .getMap().getMap().equals(refa.getMap().getMap()))))
356 * accession ID and DB must be identical. Version is ignored. No map on either
357 * or map but no maplist on either or maplist of map on a is equivalent to the
358 * maplist of map on b.
360 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
362 public boolean matches(DBRefEntry refa, DBRefEntry refb)
364 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
365 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
366 if (refa.getSource() != null && refb.getSource() != null
367 && refb.getSource().equals(refa.getSource()))
369 // We dont care about version
370 // if ((refa.getVersion()==null || refb.getVersion()==null)
371 // || refb.getVersion().equals(refa.getVersion()))
373 if (refa.getAccessionId() != null && refb.getAccessionId() != null
374 && refb.getAccessionId().equals(refa.getAccessionId()))
376 if (refa.getMap() == null || refb.getMap() == null)
380 if ((refa.getMap() != null && refb.getMap() != null)
381 && (refb.getMap().getMap() == null && refa.getMap()
383 || (refb.getMap().getMap() != null
384 && refa.getMap().getMap() != null && (refb
385 .getMap().getMap().equals(refa.getMap().getMap()))))
386 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
396 * used by file parsers to generate DBRefs from annotation within file (eg
403 * where to anotate with reference
404 * @return parsed version of entry that was added to seq (if any)
406 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
407 String version, String acn)
409 DBRefEntry ref = null;
412 String locsrc = jalview.util.DBRefUtils.getCanonicalName(dbname);
413 if (locsrc.equals(jalview.datamodel.DBRefSource.PDB))
415 // check for chaincode and mapping
416 // PFAM style stockhom PDB citation
417 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
418 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;([0-9]+)-([0-9]+)");
419 if (r.search(acn.trim()))
421 String pdbid = r.stringMatched(1);
422 String chaincode = r.stringMatched(2);
423 String mapstart = r.stringMatched(3);
424 String mapend = r.stringMatched(4);
425 if (chaincode.equals(" "))
429 // construct pdb ref.
430 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
431 PDBEntry pdbr = new PDBEntry();
439 ref = new DBRefEntry(locsrc, version, acn);