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
123 return coordsys == null ? false : coordsys.equals(dBRefEntry
128 * look up source in an internal list of database reference sources and return
129 * the canonical jalview name for the source, or the original string if it has
133 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
136 public static String getCanonicalName(String source)
142 String canonical = canonicalSourceNameLookup.get(source
144 return canonical == null ? source : canonical;
148 * Returns an array of those references that match the given entry, or null if
149 * no matches. Currently uses a comparator which matches if
151 * <li>database sources are the same</li>
152 * <li>accession ids are the same</li>
153 * <li>both have no mapping, or the mappings are the same</li>
157 * Set of references to search
162 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
164 return searchRefs(ref, entry,
165 matchDbAndIdAndEitherMapOrEquivalentMapList);
169 * Returns an array of those references that match the given entry, according
170 * to the given comparator. Returns null if no matches.
173 * an array of database references to search
175 * an entry to compare against
179 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
180 DbRefComp comparator)
182 if (refs == null || entry == null)
186 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
187 for (int i = 0; i < refs.length; i++)
189 if (comparator.matches(entry, refs[i]))
194 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
199 public boolean matches(DBRefEntry refa, DBRefEntry refb);
203 * match on all non-null fields in refa
205 public static DbRefComp matchNonNullonA = new DbRefComp()
207 public boolean matches(DBRefEntry refa, DBRefEntry refb)
209 if (refa.getSource() == null
210 || refb.getSource().equals(refa.getSource()))
212 if (refa.getVersion() == null
213 || refb.getVersion().equals(refa.getVersion()))
215 if (refa.getAccessionId() == null
216 || refb.getAccessionId().equals(refa.getAccessionId()))
218 if (refa.getMap() == null
219 || (refb.getMap() != null && refb.getMap().equals(
232 * either field is null or field matches for all of source, version, accession
235 public static DbRefComp matchEitherNonNull = new DbRefComp()
237 public boolean matches(DBRefEntry refa, DBRefEntry refb)
239 if ((refa.getSource() == null || refb.getSource() == null)
240 || refb.getSource().equals(refa.getSource()))
242 if ((refa.getVersion() == null || refb.getVersion() == null)
243 || refb.getVersion().equals(refa.getVersion()))
245 if ((refa.getAccessionId() == null || refb.getAccessionId() == null)
246 || refb.getAccessionId().equals(refa.getAccessionId()))
248 if ((refa.getMap() == null || refb.getMap() == null)
249 || (refb.getMap() != null && refb.getMap().equals(
262 * accession ID and DB must be identical. Version is ignored. Map is either
263 * not defined or is a match (or is compatible?)
265 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
267 public boolean matches(DBRefEntry refa, DBRefEntry refb)
269 if (refa.getSource() != null && refb.getSource() != null
270 && refb.getSource().equals(refa.getSource()))
272 // We dont care about version
273 // if ((refa.getVersion()==null || refb.getVersion()==null)
274 // || refb.getVersion().equals(refa.getVersion()))
276 if (refa.getAccessionId() != null && refb.getAccessionId() != null
277 || refb.getAccessionId().equals(refa.getAccessionId()))
279 if ((refa.getMap() == null || refb.getMap() == null)
280 || (refa.getMap() != null && refb.getMap() != null && refb
281 .getMap().equals(refa.getMap())))
292 * accession ID and DB must be identical. Version is ignored. No map on either
293 * or map but no maplist on either or maplist of map on a is the complement of
294 * maplist of map on b.
296 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
298 public boolean matches(DBRefEntry refa, DBRefEntry refb)
300 if (refa.getSource() != null && refb.getSource() != null
301 && refb.getSource().equals(refa.getSource()))
303 // We dont care about version
304 // if ((refa.getVersion()==null || refb.getVersion()==null)
305 // || refb.getVersion().equals(refa.getVersion()))
307 if (refa.getAccessionId() != null && refb.getAccessionId() != null
308 || refb.getAccessionId().equals(refa.getAccessionId()))
310 if ((refa.getMap() == null && refb.getMap() == null)
311 || (refa.getMap() != null && refb.getMap() != null))
313 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
314 || (refb.getMap().getMap() != null
315 && refa.getMap().getMap() != null && refb
316 .getMap().getMap().getInverse()
317 .equals(refa.getMap().getMap())))
329 * accession ID and DB must be identical. Version is ignored. No map on both
330 * or or map but no maplist on either or maplist of map on a is equivalent to
331 * the maplist of map on b.
333 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
335 public boolean matches(DBRefEntry refa, DBRefEntry refb)
337 if (refa.getSource() != null && refb.getSource() != null
338 && refb.getSource().equals(refa.getSource()))
340 // We dont care about version
341 // if ((refa.getVersion()==null || refb.getVersion()==null)
342 // || refb.getVersion().equals(refa.getVersion()))
344 if (refa.getAccessionId() != null && refb.getAccessionId() != null
345 || refb.getAccessionId().equals(refa.getAccessionId()))
347 if (refa.getMap() == null && refb.getMap() == null)
351 if (refa.getMap() != null
352 && refb.getMap() != null
353 && ((refb.getMap().getMap() == null && refa.getMap()
354 .getMap() == null) || (refb.getMap().getMap() != null
355 && refa.getMap().getMap() != null && refb
356 .getMap().getMap().equals(refa.getMap().getMap()))))
367 * accession ID and DB must be identical. Version is ignored. No map on either
368 * or map but no maplist on either or maplist of map on a is equivalent to the
369 * maplist of map on b.
371 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
373 public boolean matches(DBRefEntry refa, DBRefEntry refb)
375 // System.err.println("Comparing A: "+refa.getSrcAccString()+(refa.hasMap()?" has map.":"."));
376 // System.err.println("Comparing B: "+refb.getSrcAccString()+(refb.hasMap()?" has map.":"."));
377 if (refa.getSource() != null && refb.getSource() != null
378 && refb.getSource().equals(refa.getSource()))
380 // We dont care about version
381 // if ((refa.getVersion()==null || refb.getVersion()==null)
382 // || refb.getVersion().equals(refa.getVersion()))
384 if (refa.getAccessionId() != null && refb.getAccessionId() != null
385 && refb.getAccessionId().equals(refa.getAccessionId()))
387 if (refa.getMap() == null || refb.getMap() == null)
391 if ((refa.getMap() != null && refb.getMap() != null)
392 && (refb.getMap().getMap() == null && refa.getMap()
394 || (refb.getMap().getMap() != null
395 && refa.getMap().getMap() != null && (refb
396 .getMap().getMap().equals(refa.getMap().getMap()))))
397 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
407 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
410 * Used by file parsers to generate DBRefs from annotation within file (eg
417 * where to annotate with reference
418 * @return parsed version of entry that was added to seq (if any)
420 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
421 String version, String acn)
423 DBRefEntry ref = null;
426 String locsrc = DBRefUtils.getCanonicalName(dbname);
427 if (locsrc.equals(DBRefSource.PDB))
430 * Check for PFAM style stockhom PDB accession id citation e.g.
433 Regex r = new com.stevesoft.pat.Regex(
434 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
435 if (r.search(acn.trim()))
437 String pdbid = r.stringMatched(1);
438 String chaincode = r.stringMatched(2);
443 // String mapstart = r.stringMatched(3);
444 // String mapend = r.stringMatched(4);
445 if (chaincode.equals(" "))
449 // construct pdb ref.
450 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
451 PDBEntry pdbr = new PDBEntry();
453 pdbr.setType(PDBEntry.Type.PDB);
454 pdbr.setProperty(new Hashtable());
455 pdbr.setChainCode(chaincode);
456 // pdbr.getProperty().put("CHAIN", chaincode);
459 System.err.println("Malformed PDB DR line:"+acn);
465 ref = new DBRefEntry(locsrc, version, acn);