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);
70 * Returns those DBRefEntry objects whose source identifier (once converted to
71 * Jalview's canonical form) is in the list of sources to search for. Returns
72 * null if no matches found.
75 * DBRefEntry objects to search
77 * array of sources to select
80 public static DBRefEntry[] selectRefs(DBRefEntry[] dbrefs,
83 if (dbrefs == null || sources == null)
87 HashSet<String> srcs = new HashSet<String>();
88 for (String src : sources)
93 List<DBRefEntry> res = new ArrayList<DBRefEntry>();
94 for (DBRefEntry dbr : dbrefs)
96 String source = getCanonicalName(dbr.getSource());
97 if (srcs.contains(source))
105 DBRefEntry[] reply = new DBRefEntry[res.size()];
106 return res.toArray(reply);
112 * isDasCoordinateSystem
118 * @return boolean true if Source DBRefEntry is compatible with DAS
119 * CoordinateSystem name
122 public static boolean isDasCoordinateSystem(String string,
123 DBRefEntry dBRefEntry)
125 if (string == null || dBRefEntry == null)
129 String coordsys = dasCoordinateSystemsLookup.get(string.toLowerCase());
130 return coordsys == null ? false : coordsys.equals(dBRefEntry
135 * look up source in an internal list of database reference sources and return
136 * the canonical jalview name for the source, or the original string if it has
140 * @return canonical jalview source (one of jalview.datamodel.DBRefSource.*)
143 public static String getCanonicalName(String source)
149 String canonical = canonicalSourceNameLookup.get(source.toLowerCase());
150 return canonical == null ? source : canonical;
154 * Returns an array of those references that match the given entry, or null if
155 * no matches. Currently uses a comparator which matches if
157 * <li>database sources are the same</li>
158 * <li>accession ids are the same</li>
159 * <li>both have no mapping, or the mappings are the same</li>
163 * Set of references to search
168 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, DBRefEntry entry)
170 return searchRefs(ref, entry,
171 matchDbAndIdAndEitherMapOrEquivalentMapList);
175 * Returns an array of those references that match the given accession id
177 * <li>database sources are the same</li>
178 * <li>accession ids are the same</li>
179 * <li>both have no mapping, or the mappings are the same</li>
183 * Set of references to search
188 public static DBRefEntry[] searchRefs(DBRefEntry[] ref, String accId)
190 return searchRefs(ref, new DBRefEntry("", "", accId), matchId);
194 * Returns an array of those references that match the given entry, according
195 * to the given comparator. Returns null if no matches.
198 * an array of database references to search
200 * an entry to compare against
204 static DBRefEntry[] searchRefs(DBRefEntry[] refs, DBRefEntry entry,
205 DbRefComp comparator)
207 if (refs == null || entry == null)
211 List<DBRefEntry> rfs = new ArrayList<DBRefEntry>();
212 for (int i = 0; i < refs.length; i++)
214 if (comparator.matches(entry, refs[i]))
219 return rfs.size() == 0 ? null : rfs.toArray(new DBRefEntry[rfs.size()]);
224 public boolean matches(DBRefEntry refa, DBRefEntry refb);
228 * match on all non-null fields in refa
230 // TODO unused - remove?
231 public static DbRefComp matchNonNullonA = new DbRefComp()
234 public boolean matches(DBRefEntry refa, DBRefEntry refb)
236 if (refa.getSource() == null
237 || refb.getSource().equals(refa.getSource()))
239 if (refa.getVersion() == null
240 || refb.getVersion().equals(refa.getVersion()))
242 if (refa.getAccessionId() == null
243 || refb.getAccessionId().equals(refa.getAccessionId()))
245 if (refa.getMap() == null
246 || (refb.getMap() != null && refb.getMap().equals(
259 * either field is null or field matches for all of source, version, accession
262 // TODO unused - remove?
263 public static DbRefComp matchEitherNonNull = new DbRefComp()
266 public boolean matches(DBRefEntry refa, DBRefEntry refb)
268 if (nullOrEqual(refa.getSource(), refb.getSource())
269 && nullOrEqual(refa.getVersion(), refb.getVersion())
270 && nullOrEqual(refa.getAccessionId(), refb.getAccessionId())
271 && nullOrEqual(refa.getMap(), refb.getMap()))
280 * accession ID and DB must be identical. Version is ignored. Map is either
281 * not defined or is a match (or is compatible?)
283 // TODO unused - remove?
284 public static DbRefComp matchDbAndIdAndEitherMap = new DbRefComp()
287 public boolean matches(DBRefEntry refa, DBRefEntry refb)
289 if (refa.getSource() != null && refb.getSource() != null
290 && refb.getSource().equals(refa.getSource()))
292 // We dont care about version
293 if (refa.getAccessionId() != null && refb.getAccessionId() != null
294 // FIXME should be && not || here?
295 || refb.getAccessionId().equals(refa.getAccessionId()))
297 if ((refa.getMap() == null || refb.getMap() == null)
298 || (refa.getMap() != null && refb.getMap() != null && refb
299 .getMap().equals(refa.getMap())))
310 * accession ID and DB must be identical. Version is ignored. No map on either
311 * or map but no maplist on either or maplist of map on a is the complement of
312 * maplist of map on b.
314 // TODO unused - remove?
315 public static DbRefComp matchDbAndIdAndComplementaryMapList = new DbRefComp()
318 public boolean matches(DBRefEntry refa, DBRefEntry refb)
320 if (refa.getSource() != null && refb.getSource() != null
321 && refb.getSource().equals(refa.getSource()))
323 // We dont care about version
324 if (refa.getAccessionId() != null && refb.getAccessionId() != null
325 || refb.getAccessionId().equals(refa.getAccessionId()))
327 if ((refa.getMap() == null && refb.getMap() == null)
328 || (refa.getMap() != null && refb.getMap() != null))
330 if ((refb.getMap().getMap() == null && refa.getMap().getMap() == null)
331 || (refb.getMap().getMap() != null
332 && refa.getMap().getMap() != null && refb
333 .getMap().getMap().getInverse()
334 .equals(refa.getMap().getMap())))
346 * accession ID and DB must be identical. Version is ignored. No map on both
347 * or or map but no maplist on either or maplist of map on a is equivalent to
348 * the maplist of map on b.
350 // TODO unused - remove?
351 public static DbRefComp matchDbAndIdAndEquivalentMapList = new DbRefComp()
354 public boolean matches(DBRefEntry refa, DBRefEntry refb)
356 if (refa.getSource() != null && refb.getSource() != null
357 && refb.getSource().equals(refa.getSource()))
359 // We dont care about version
360 // if ((refa.getVersion()==null || refb.getVersion()==null)
361 // || refb.getVersion().equals(refa.getVersion()))
363 if (refa.getAccessionId() != null && refb.getAccessionId() != null
364 || refb.getAccessionId().equals(refa.getAccessionId()))
366 if (refa.getMap() == null && refb.getMap() == null)
370 if (refa.getMap() != null
371 && refb.getMap() != null
372 && ((refb.getMap().getMap() == null && refa.getMap()
373 .getMap() == null) || (refb.getMap().getMap() != null
374 && refa.getMap().getMap() != null && refb
375 .getMap().getMap().equals(refa.getMap().getMap()))))
386 * accession ID and DB must be identical, or null on a. Version is ignored. No
387 * map on either or map but no maplist on either or maplist of map on a is
388 * equivalent to the maplist of map on b.
390 public static DbRefComp matchDbAndIdAndEitherMapOrEquivalentMapList = new DbRefComp()
393 public boolean matches(DBRefEntry refa, DBRefEntry refb)
395 if (refa.getSource() != null && refb.getSource() != null
396 && refb.getSource().equals(refa.getSource()))
398 // We dont care about version
400 if (refa.getAccessionId() == null
401 || refa.getAccessionId().equals(refb.getAccessionId()))
403 if (refa.getMap() == null || refb.getMap() == null)
407 if ((refa.getMap() != null && refb.getMap() != null)
408 && (refb.getMap().getMap() == null && refa.getMap()
410 || (refb.getMap().getMap() != null
411 && refa.getMap().getMap() != null && (refb
412 .getMap().getMap().equals(refa.getMap().getMap()))))
423 * accession ID only must be identical.
425 public static DbRefComp matchId = new DbRefComp()
428 public boolean matches(DBRefEntry refa, DBRefEntry refb)
430 if (refa.getAccessionId() != null && refb.getAccessionId() != null
431 && refb.getAccessionId().equals(refa.getAccessionId()))
440 * Parses a DBRefEntry and adds it to the sequence, also a PDBEntry if the
443 * Used by file parsers to generate DBRefs from annotation within file (eg
450 * where to annotate with reference
451 * @return parsed version of entry that was added to seq (if any)
453 public static DBRefEntry parseToDbRef(SequenceI seq, String dbname,
454 String version, String acn)
456 DBRefEntry ref = null;
459 String locsrc = DBRefUtils.getCanonicalName(dbname);
460 if (locsrc.equals(DBRefSource.PDB))
463 * Check for PFAM style stockhom PDB accession id citation e.g.
466 Regex r = new com.stevesoft.pat.Regex(
467 "([0-9][0-9A-Za-z]{3})\\s*(.?)\\s*;\\s*([0-9]+)-([0-9]+)");
468 if (r.search(acn.trim()))
470 String pdbid = r.stringMatched(1);
471 String chaincode = r.stringMatched(2);
472 if (chaincode == null)
476 // String mapstart = r.stringMatched(3);
477 // String mapend = r.stringMatched(4);
478 if (chaincode.equals(" "))
482 // construct pdb ref.
483 ref = new DBRefEntry(locsrc, version, pdbid + chaincode);
484 PDBEntry pdbr = new PDBEntry();
486 pdbr.setType(PDBEntry.Type.PDB);
487 pdbr.setProperty(new Hashtable());
488 pdbr.setChainCode(chaincode);
489 // pdbr.getProperty().put("CHAIN", chaincode);
494 System.err.println("Malformed PDB DR line:" + acn);
500 ref = new DBRefEntry(locsrc, version, acn);
511 * Returns true if either object is null, or they are equal
517 public static boolean nullOrEqual(Object o1, Object o2)
519 if (o1 == null || o2 == null)
523 return (o1 == null ? o2.equals(o1) : o1.equals(o2));
527 * Selects just the DNA or protein references from a set of references
530 * if true, select references to 'standard' DNA databases, else to
531 * 'standard' peptide databases
533 * a set of references to select from
536 public static DBRefEntry[] selectDbRefs(boolean selectDna,
539 return selectRefs(refs, selectDna ? DBRefSource.DNACODINGDBS
540 : DBRefSource.PROTEINDBS);
541 // could attempt to find other cross
542 // refs here - ie PDB xrefs
543 // (not dna, not protein seq)