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.jsdev.RegExp;
28 import jalview.jsdev.api.RegExpInterface;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Hashtable;
33 import java.util.List;
36 //import com.stevesoft.pat.Regex;
38 public class DBRefUtils
40 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
42 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
46 // TODO load these from a resource file?
47 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
49 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
50 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
52 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
53 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
54 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
55 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
59 * Utilities for handling DBRef objects and their collections.
64 * Vector of DBRef objects to search
66 * String[] array of source DBRef IDs to retrieve
69 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
80 Map<String, Integer> srcs = new HashMap<String, Integer>();
81 ArrayList<DBRefEntry> res = new ArrayList<DBRefEntry>();
83 for (int i = 0; i < sources.length; i++)
85 srcs.put(new String(sources[i]), new Integer(i));
87 for (int i = 0, j = dbrefs.length; i < j; i++)
89 if (srcs.containsKey(dbrefs[i].getSource()))
97 DBRefEntry[] reply = new DBRefEntry[res.size()];
98 return res.toArray(reply);
101 // there are probable memory leaks in the hashtable!
106 * isDasCoordinateSystem
112 * @return boolean true if Source DBRefEntry is compatible with DAS
113 * CoordinateSystem name
116 public static boolean isDasCoordinateSystem(String string,
117 DBRefEntry dBRefEntry)
119 if (string == null || dBRefEntry == null)
123 String coordsys = dasCoordinateSystemsLookup.get(string
125 return coordsys == null ? false : coordsys.equals(dBRefEntry
130 * look up source in an internal list of database reference sources and return
131 * the canonical jalview name for the source, or the original string if it has
135 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
138 public static String getCanonicalName(String source)
144 String canonical = canonicalSourceNameLookup.get(source
146 return canonical == null ? source : canonical;
150 * Returns an array of those references that match the given entry, or null if
151 * no matches. Currently uses a comparator which matches if
153 * <li>database sources are the same</li>
154 * <li>accession ids are the same</li>
155 * <li>both have no mapping, or the mappings are the same</li>
159 * Set of references to search
164 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
166 return searchRefs(ref, entry,
167 matchDbAndIdAndEitherMapOrEquivalentMapList);
171 * Returns an array of those references that match the given entry, according
172 * to the given comparator. Returns null if no matches.
175 * an array of database references to search
177 * an entry to compare against
181 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
182 DbRefComp comparator)
184 if (refs == null || entry == null)
188 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
189 for (int i = 0; i < refs.length; i++)
191 if (comparator.matches(entry, refs[i]))
196 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
201 public boolean matches(DBRefEntry refa, DBRefEntry refb);
205 * match on all non-null fields in refa
207 public static DbRefComp matchNonNullonA = new DbRefComp()
209 public boolean matches(DBRefEntry refa, DBRefEntry refb)
211 if (refa.getSource() == null
212 || refb.getSource().equals(refa.getSource()))
214 if (refa.getVersion() == null
215 || refb.getVersion().equals(refa.getVersion()))
217 if (refa.getAccessionId() == null
218 || refb.getAccessionId().equals(refa.getAccessionId()))
220 if (refa.getMap() == null
221 || (refb.getMap() != null && refb.getMap().equals(
234 * either field is null or field matches for all of source, version, accession
237 public static DbRefComp matchEitherNonNull = new DbRefComp()
239 public boolean matches(DBRefEntry refa, DBRefEntry refb)
241 if ((refa.getSource() == null || refb.getSource() == null)
242 || refb.getSource().equals(refa.getSource()))
244 if ((refa.getVersion() == null || refb.getVersion() == null)
245 || refb.getVersion().equals(refa.getVersion()))
247 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
248 || refb.getAccessionId().equals(refa.getAccessionId()))
250 if ((refa.getMap() == null || refb.getMap() == null)
251 || (refb.getMap() != null && refb.getMap().equals(
264 * accession ID and DB must be identical. Version is ignored. Map is either
265 * not defined or is a match (or is compatible?)
267 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
269 public boolean matches(DBRefEntry refa, DBRefEntry refb)
271 if (refa.getSource() != null && refb.getSource() != null
272 && refb.getSource().equals(refa.getSource()))
274 // We dont care about version
275 // if ((refa.getVersion()==null || refb.getVersion()==null)
276 // || refb.getVersion().equals(refa.getVersion()))
278 if (refa.getAccessionId() != null && refb.getAccessionId() != null
279 || refb.getAccessionId().equals(refa.getAccessionId()))
281 if ((refa.getMap() == null || refb.getMap() == null)
282 || (refa.getMap() != null && refb.getMap() != null && refb
283 .getMap().equals(refa.getMap())))
294 * accession ID and DB must be identical. Version is ignored. No map on either
295 * or map but no maplist on either or maplist of map on a is the complement of
296 * maplist of map on b.
298 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
300 public boolean matches(DBRefEntry refa, DBRefEntry refb)
302 if (refa.getSource() != null && refb.getSource() != null
303 && refb.getSource().equals(refa.getSource()))
305 // We dont care about version
306 // if ((refa.getVersion()==null || refb.getVersion()==null)
307 // || refb.getVersion().equals(refa.getVersion()))
309 if (refa.getAccessionId() != null && refb.getAccessionId() != null
310 || refb.getAccessionId().equals(refa.getAccessionId()))
312 if ((refa.getMap() == null && refb.getMap() == null)
313 || (refa.getMap() != null && refb.getMap() != null))
315 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
316 || (refb.getMap().getMap() != null
317 && refa.getMap().getMap() != null && refb
318 .getMap().getMap().getInverse()
319 .equals(refa.getMap().getMap())))
331 * accession ID and DB must be identical. Version is ignored. No map on both
332 * or or map but no maplist on either or maplist of map on a is equivalent to
333 * the maplist of map on b.
335 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
337 public boolean matches(DBRefEntry refa, DBRefEntry refb)
339 if (refa.getSource() != null && refb.getSource() != null
340 && refb.getSource().equals(refa.getSource()))
342 // We dont care about version
343 // if ((refa.getVersion()==null || refb.getVersion()==null)
344 // || refb.getVersion().equals(refa.getVersion()))
346 if (refa.getAccessionId() != null && refb.getAccessionId() != null
347 || refb.getAccessionId().equals(refa.getAccessionId()))
349 if (refa.getMap() == null && refb.getMap() == null)
353 if (refa.getMap() != null
354 && refb.getMap() != null
355 && ((refb.getMap().getMap() == null && refa.getMap()
356 .getMap() == null) || (refb.getMap().getMap() != null
357 && refa.getMap().getMap() != null && refb
358 .getMap().getMap().equals(refa.getMap().getMap()))))
369 * accession ID and DB must be identical. Version is ignored. No map on either
370 * or map but no maplist on either or maplist of map on a is equivalent to the
371 * maplist of map on b.
373 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
375 public boolean matches(DBRefEntry refa, DBRefEntry refb)
377 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
378 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
379 if (refa.getSource() != null && refb.getSource() != null
380 && refb.getSource().equals(refa.getSource()))
382 // We dont care about version
383 // if ((refa.getVersion()==null || refb.getVersion()==null)
384 // || refb.getVersion().equals(refa.getVersion()))
386 if (refa.getAccessionId() != null && refb.getAccessionId() != null
387 && refb.getAccessionId().equals(refa.getAccessionId()))
389 if (refa.getMap() == null || refb.getMap() == null)
393 if ((refa.getMap() != null && refb.getMap() != null)
394 && (refb.getMap().getMap() == null && refa.getMap()
396 || (refb.getMap().getMap() != null
397 && refa.getMap().getMap() != null && (refb
398 .getMap().getMap().equals(refa.getMap().getMap()))))
399 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
409 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
412 * Used by file parsers to generate DBRefs from annotation within file (eg
419 * where to annotate with reference
420 * @return parsed version of entry that was added to seq (if any)
422 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
423 String version, String acn)
425 DBRefEntry ref = null;
428 String locsrc = DBRefUtils.getCanonicalName(dbname);
429 if (locsrc.equals(DBRefSource.PDB))
432 * Check for PFAM style stockhom PDB accession id citation e.g.
435 RegExpInterface r = RegExp.newRegex(
436 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
437 if (r.search(acn.trim()))
439 String pdbid = r.stringMatchedI(1);
440 String chaincode = r.stringMatchedI(2);
445 // String mapstart = r.stringMatched(3);
446 // String mapend = r.stringMatched(4);
447 if (chaincode.equals(" "))
451 // construct pdb ref.
452 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
453 PDBEntry pdbr = new PDBEntry();
455 pdbr.setType(PDBEntry.Type.PDB);
456 pdbr.setProperty(new Hashtable());
457 pdbr.setChainCode(chaincode);
458 // pdbr.getProperty().put("CHAIN", chaincode);
461 System.err.println("Malformed PDB DR line:"+acn);
467 ref = new DBRefEntry(locsrc, version, acn);