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.HashSet;
31 import java.util.Hashtable;
32 import java.util.List;
35 import com.stevesoft.pat.Regex;
38 * Utilities for handling DBRef objects and their collections.
40 public class DBRefUtils
43 * lookup from lower-case form of a name to its canonical (standardised) form
45 private static Map<String, String> canonicalSourceNameLookup = new HashMap<String, String>();
47 private static Map<String, String> dasCoordinateSystemsLookup = new HashMap<String, String>();
51 // TODO load these from a resource file?
52 canonicalSourceNameLookup.put("uniprotkb/swiss-prot",
54 canonicalSourceNameLookup.put("uniprotkb/trembl", DBRefSource.UNIPROT);
56 // Ensembl values for dbname in xref REST service:
57 canonicalSourceNameLookup.put("uniprot/sptrembl", DBRefSource.UNIPROT);
58 canonicalSourceNameLookup.put("uniprot/swissprot", DBRefSource.UNIPROT);
60 canonicalSourceNameLookup.put("pdb", DBRefSource.PDB);
61 canonicalSourceNameLookup.put("ensembl", DBRefSource.ENSEMBL);
63 dasCoordinateSystemsLookup.put("pdbresnum", DBRefSource.PDB);
64 dasCoordinateSystemsLookup.put("uniprot", DBRefSource.UNIPROT);
65 dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBL);
66 // dasCoordinateSystemsLookup.put("embl", DBRefSource.EMBLCDS);
72 * array of DBRef objects to search
74 * String[] array of source DBRef IDs to retrieve
77 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
80 if (dbrefs == null || sources == null)
84 HashSet<String> srcs = new HashSet<String>();
85 for (String src : sources)
90 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
91 for (DBRefEntry dbr : dbrefs)
93 String source = getCanonicalName(dbr.getSource());
94 if (srcs.contains(source))
102 DBRefEntry[] reply = new DBRefEntry[res.size()];
103 return res.toArray(reply);
109 * isDasCoordinateSystem
115 * @return boolean true if Source DBRefEntry is compatible with DAS
116 * CoordinateSystem name
119 public static boolean isDasCoordinateSystem(String string,
120 DBRefEntry dBRefEntry)
122 if (string == null || dBRefEntry == null)
126 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
127 return coordsys == null ? false : coordsys.equals(dBRefEntry
132 * look up source in an internal list of database reference sources and return
133 * the canonical jalview name for the source, or the original string if it has
137 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
140 public static String getCanonicalName(String source)
146 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
147 return canonical == null ? source : canonical;
151 * Returns an array of those references that match the given entry, or null if
152 * no matches. Currently uses a comparator which matches if
154 * <li>database sources are the same</li>
155 * <li>accession ids are the same</li>
156 * <li>both have no mapping, or the mappings are the same</li>
160 * Set of references to search
165 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
167 return searchRefs(ref, entry,
168 matchDbAndIdAndEitherMapOrEquivalentMapList);
172 * Returns an array of those references that match the given entry, according
173 * to the given comparator. Returns null if no matches.
176 * an array of database references to search
178 * an entry to compare against
182 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
183 DbRefComp comparator)
185 if (refs == null || entry == null)
189 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
190 for (int i = 0; i < refs.length; i++)
192 if (comparator.matches(entry, refs[i]))
197 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
202 public boolean matches(DBRefEntry refa, DBRefEntry refb);
206 * match on all non-null fields in refa
208 // TODO unused - remove?
209 public static DbRefComp matchNonNullonA = new DbRefComp()
212 public boolean matches(DBRefEntry refa, DBRefEntry refb)
214 if (refa.getSource() == null
215 || refb.getSource().equals(refa.getSource()))
217 if (refa.getVersion() == null
218 || refb.getVersion().equals(refa.getVersion()))
220 if (refa.getAccessionId() == null
221 || refb.getAccessionId().equals(refa.getAccessionId()))
223 if (refa.getMap() == null
224 || (refb.getMap() != null && refb.getMap().equals(
237 * either field is null or field matches for all of source, version, accession
240 // TODO unused - remove?
241 public static DbRefComp matchEitherNonNull = new DbRefComp()
244 public boolean matches(DBRefEntry refa, DBRefEntry refb)
246 if (nullOrEqual(refa.getSource(), refb.getSource())
247 && nullOrEqual(refa.getVersion(), refb.getVersion())
248 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
249 && nullOrEqual(refa.getMap(), refb.getMap()))
258 * accession ID and DB must be identical. Version is ignored. Map is either
259 * not defined or is a match (or is compatible?)
261 // TODO unused - remove?
262 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
265 public boolean matches(DBRefEntry refa, DBRefEntry refb)
267 if (refa.getSource() != null && refb.getSource() != null
268 && refb.getSource().equals(refa.getSource()))
270 // We dont care about version
271 if (refa.getAccessionId() != null && refb.getAccessionId() != null
272 // FIXME should be && not || here?
273 || refb.getAccessionId().equals(refa.getAccessionId()))
275 if ((refa.getMap() == null || refb.getMap() == null)
276 || (refa.getMap() != null && refb.getMap() != null && refb
277 .getMap().equals(refa.getMap())))
288 * accession ID and DB must be identical. Version is ignored. No map on either
289 * or map but no maplist on either or maplist of map on a is the complement of
290 * maplist of map on b.
292 // TODO unused - remove?
293 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
296 public boolean matches(DBRefEntry refa, DBRefEntry refb)
298 if (refa.getSource() != null && refb.getSource() != null
299 && refb.getSource().equals(refa.getSource()))
301 // We dont care about version
302 if (refa.getAccessionId() != null && refb.getAccessionId() != null
303 || refb.getAccessionId().equals(refa.getAccessionId()))
305 if ((refa.getMap() == null && refb.getMap() == null)
306 || (refa.getMap() != null && refb.getMap() != null))
308 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
309 || (refb.getMap().getMap() != null
310 && refa.getMap().getMap() != null && refb
311 .getMap().getMap().getInverse()
312 .equals(refa.getMap().getMap())))
324 * accession ID and DB must be identical. Version is ignored. No map on both
325 * or or map but no maplist on either or maplist of map on a is equivalent to
326 * the maplist of map on b.
328 // TODO unused - remove?
329 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
332 public boolean matches(DBRefEntry refa, DBRefEntry refb)
334 if (refa.getSource() != null && refb.getSource() != null
335 && refb.getSource().equals(refa.getSource()))
337 // We dont care about version
338 // if ((refa.getVersion()==null || refb.getVersion()==null)
339 // || refb.getVersion().equals(refa.getVersion()))
341 if (refa.getAccessionId() != null && refb.getAccessionId() != null
342 || refb.getAccessionId().equals(refa.getAccessionId()))
344 if (refa.getMap() == null && refb.getMap() == null)
348 if (refa.getMap() != null
349 && refb.getMap() != null
350 && ((refb.getMap().getMap() == null && refa.getMap()
351 .getMap() == null) || (refb.getMap().getMap() != null
352 && refa.getMap().getMap() != null && refb
353 .getMap().getMap().equals(refa.getMap().getMap()))))
364 * accession ID and DB must be identical. Version is ignored. No map on either
365 * or map but no maplist on either or maplist of map on a is equivalent to the
366 * maplist of map on b.
368 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
371 public boolean matches(DBRefEntry refa, DBRefEntry refb)
373 if (refa.getSource() != null && refb.getSource() != null
374 && refb.getSource().equals(refa.getSource()))
376 // We dont care about version
377 if (refa.getAccessionId() != null && refb.getAccessionId() != null
378 && refb.getAccessionId().equals(refa.getAccessionId()))
380 if (refa.getMap() == null || refb.getMap() == null)
384 if ((refa.getMap() != null && refb.getMap() != null)
385 && (refb.getMap().getMap() == null && refa.getMap()
387 || (refb.getMap().getMap() != null
388 && refa.getMap().getMap() != null && (refb
389 .getMap().getMap().equals(refa.getMap().getMap()))))
390 { // getMap().getMap().containsEither(false,refa.getMap().getMap())
400 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
403 * Used by file parsers to generate DBRefs from annotation within file (eg
410 * where to annotate with reference
411 * @return parsed version of entry that was added to seq (if any)
413 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
414 String version, String acn)
416 DBRefEntry ref = null;
419 String locsrc = DBRefUtils.getCanonicalName(dbname);
420 if (locsrc.equals(DBRefSource.PDB))
423 * Check for PFAM style stockhom PDB accession id citation e.g.
426 Regex r = new com.stevesoft.pat.Regex(
427 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
428 if (r.search(acn.trim()))
430 String pdbid = r.stringMatched(1);
431 String chaincode = r.stringMatched(2);
432 if (chaincode == null)
436 // String mapstart = r.stringMatched(3);
437 // String mapend = r.stringMatched(4);
438 if (chaincode.equals(" "))
442 // construct pdb ref.
443 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
444 PDBEntry pdbr = new PDBEntry();
446 pdbr.setType(PDBEntry.Type.PDB);
447 pdbr.setProperty(new Hashtable());
448 pdbr.setChainCode(chaincode);
449 // pdbr.getProperty().put("CHAIN", chaincode);
454 System.err.println("Malformed PDB DR line:" + acn);
460 ref = new DBRefEntry(locsrc, version, acn);
471 * Returns true if either object is null, or they are equal
477 public static boolean nullOrEqual(Object o1, Object o2)
479 if (o1 == null || o2 == null)
483 return (o1 == null ? o2.equals(o1) : o1.equals(o2));