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.
21 package jalview.ext.so;
23 import jalview.datamodel.ontology.OntologyBase;
24 import jalview.io.gff.SequenceOntologyI;
26 import java.io.BufferedInputStream;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.text.ParseException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.NoSuchElementException;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipInputStream;
41 import org.biojava.nbio.ontology.Ontology;
42 import org.biojava.nbio.ontology.Term;
43 import org.biojava.nbio.ontology.Term.Impl;
44 import org.biojava.nbio.ontology.Triple;
45 import org.biojava.nbio.ontology.io.OboParser;
46 import org.biojava.nbio.ontology.utils.Annotation;
49 * A wrapper class that parses the Sequence Ontology and exposes useful access
50 * methods. This version uses the BioJava parser.
52 public class SequenceOntology extends OntologyBase
53 implements SequenceOntologyI
56 * the parsed Ontology data as modelled by BioJava
58 private Ontology ontology;
61 * the ontology term for the isA relationship
66 * lookup of terms by user readable name (NB not guaranteed unique)
68 private Map<String, Term> termsByDescription;
71 * Map where key is a Term and value is a (possibly empty) list of
72 * all Terms to which the key has an 'isA' relationship, either
73 * directly or indirectly (A isA B isA C)
75 private Map<Term, List<Term>> termIsA;
77 private List<String> termsFound;
79 private List<String> termsNotFound;
82 * Package private constructor to enforce use of singleton. Parses and caches
83 * the SO OBO data file.
85 public SequenceOntology()
87 termsFound = new ArrayList<>();
88 termsNotFound = new ArrayList<>();
89 termsByDescription = new HashMap<>();
90 termIsA = new HashMap<>();
92 loadOntologyZipFile("so-xp-simple.obo");
96 * Loads the given ontology file from a zip file with ".zip" appended
100 protected void loadOntologyZipFile(String ontologyFile)
102 long now = System.currentTimeMillis();
103 ZipInputStream zipStream = null;
106 String zipFile = ontologyFile + ".zip";
107 InputStream inStream = this.getClass()
108 .getResourceAsStream("/" + zipFile);
109 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
111 while ((entry = zipStream.getNextEntry()) != null)
113 if (entry.getName().equals(ontologyFile))
115 loadOboFile(zipStream);
118 long elapsed = System.currentTimeMillis() - now;
119 System.out.println("Loaded Sequence Ontology from " + zipFile + " ("
121 } catch (Exception e)
126 closeStream(zipStream);
131 * Closes the input stream, swallowing all exceptions
135 protected void closeStream(InputStream is)
142 } catch (IOException e)
150 * Reads, parses and stores the OBO file data
153 * @throws ParseException
154 * @throws IOException
156 protected void loadOboFile(InputStream is)
157 throws ParseException, IOException
159 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
160 OboParser parser = new OboParser();
161 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
162 isA = ontology.getTerm("is_a");
167 * Stores a lookup table of terms by description. Note that description is not
168 * guaranteed unique. Where duplicate descriptions are found, try to discard
169 * the term that is flagged as obsolete. However we do store obsolete terms
170 * where there is no duplication of description.
172 protected void storeTermNames()
174 for (Term term : ontology.getTerms())
176 if (term instanceof Impl)
178 String description = term.getDescription();
179 if (description != null)
181 Term replaced = termsByDescription.get(description);
182 if (replaced != null)
184 boolean newTermIsObsolete = isObsolete(term);
185 boolean oldTermIsObsolete = isObsolete(replaced);
186 if (newTermIsObsolete && !oldTermIsObsolete)
188 System.err.println("Ignoring " + term.getName()
189 + " as obsolete and duplicated by "
190 + replaced.getName());
193 else if (!newTermIsObsolete && oldTermIsObsolete)
195 System.err.println("Ignoring " + replaced.getName()
196 + " as obsolete and duplicated by " + term.getName());
200 System.err.println("Warning: " + term.getName()
201 + " has replaced " + replaced.getName()
202 + " for lookup of '" + description + "'");
205 termsByDescription.put(description, term);
212 * Answers true if the term has property "is_obsolete" with value true, else
218 public static boolean isObsolete(Term term)
220 Annotation ann = term.getAnnotation();
225 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
229 } catch (NoSuchElementException e)
231 // fall through to false
238 * Test whether the given Sequence Ontology term is nucleotide_match (either
239 * directly or via is_a relationship)
242 * SO name or description
245 public boolean isNucleotideMatch(String soTerm)
247 return isA(soTerm, NUCLEOTIDE_MATCH);
251 * Test whether the given Sequence Ontology term is protein_match (either
252 * directly or via is_a relationship)
255 * SO name or description
258 public boolean isProteinMatch(String soTerm)
260 return isA(soTerm, PROTEIN_MATCH);
264 * Test whether the given Sequence Ontology term is polypeptide (either
265 * directly or via is_a relationship)
268 * SO name or description
271 public boolean isPolypeptide(String soTerm)
273 return isA(soTerm, POLYPEPTIDE);
277 * Returns true if the given term has a (direct or indirect) 'isA'
278 * relationship with the parent
285 public boolean isA(String child, String parent)
287 if (child == null || parent == null)
292 * optimise trivial checks like isA("CDS", "CDS")
294 if (child.equals(parent))
300 Term childTerm = getTerm(child);
301 if (childTerm != null)
309 Term parentTerm = getTerm(parent);
311 return termIsA(childTerm, parentTerm);
315 * Records a valid term queried for, for reporting purposes
319 private void termFound(String term)
321 synchronized (termsFound)
323 if (!termsFound.contains(term))
325 termsFound.add(term);
331 * Records an invalid term queried for, for reporting purposes
335 private void termNotFound(String term)
337 synchronized (termsNotFound)
339 if (!termsNotFound.contains(term))
341 System.err.println("SO term " + term + " invalid");
342 termsNotFound.add(term);
348 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
354 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
357 * null term could arise from a misspelled SO description
359 if (childTerm == null || parentTerm == null)
365 * recursive search endpoint:
367 if (childTerm == parentTerm)
373 * lazy initialisation - find all of a term's parents (recursively)
374 * the first time this is called, and save them in a map.
376 if (!termIsA.containsKey(childTerm))
378 findParents(childTerm);
381 List<Term> parents = termIsA.get(childTerm);
382 for (Term parent : parents)
384 if (termIsA(parent, parentTerm))
387 * add (great-)grandparents to parents list as they are discovered,
388 * for faster lookup next time
390 if (!parents.contains(parentTerm))
392 parents.add(parentTerm);
402 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
407 protected synchronized void findParents(Term childTerm)
409 List<Term> result = new ArrayList<>();
410 for (Triple triple : ontology.getTriples(childTerm, null, isA))
412 Term parent = triple.getObject();
416 * and search for the parent's parents recursively
420 termIsA.put(childTerm, result);
424 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
425 * "sequence_location"), or null if not found.
430 protected Term getTerm(String nameOrDescription)
432 Term t = termsByDescription.get(nameOrDescription);
437 t = ontology.getTerm(nameOrDescription);
438 } catch (NoSuchElementException e)
446 public boolean isSequenceVariant(String soTerm)
448 return isA(soTerm, SEQUENCE_VARIANT);
452 * Sorts (case-insensitive) and returns the list of valid terms queried for
455 public List<String> termsFound()
457 synchronized (termsFound)
459 Collections.sort(termsFound, String.CASE_INSENSITIVE_ORDER);
465 * Sorts (case-insensitive) and returns the list of invalid terms queried for
468 public List<String> termsNotFound()
470 synchronized (termsNotFound)
472 Collections.sort(termsNotFound, String.CASE_INSENSITIVE_ORDER);
473 return termsNotFound;