1 package jalview.ext.so;
3 import jalview.io.gff.SequenceOntologyI;
5 import java.io.BufferedInputStream;
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.text.ParseException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.NoSuchElementException;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipInputStream;
20 import org.biojava.nbio.ontology.Ontology;
21 import org.biojava.nbio.ontology.Term;
22 import org.biojava.nbio.ontology.Term.Impl;
23 import org.biojava.nbio.ontology.Triple;
24 import org.biojava.nbio.ontology.io.OboParser;
25 import org.biojava.nbio.ontology.utils.Annotation;
28 * A wrapper class that parses the Sequence Ontology and exposes useful access
29 * methods. This version uses the BioJava parser.
31 public class SequenceOntology implements SequenceOntologyI
34 * the parsed Ontology data as modelled by BioJava
36 private Ontology ontology;
39 * the ontology term for the isA relationship
44 * lookup of terms by user readable name (NB not guaranteed unique)
46 private Map<String, Term> termsByDescription;
49 * Map where key is a Term and value is a (possibly empty) list of
50 * all Terms to which the key has an 'isA' relationship, either
51 * directly or indirectly (A isA B isA C)
53 private Map<Term, List<Term>> termIsA;
55 private List<String> termsFound;
57 private List<String> termsNotFound;
60 * Package private constructor to enforce use of singleton. Parses and caches
61 * the SO OBO data file.
63 public SequenceOntology()
65 termsFound = new ArrayList<String>();
66 termsNotFound = new ArrayList<String>();
67 termsByDescription = new HashMap<String, Term>();
68 termIsA = new HashMap<Term, List<Term>>();
70 loadOntologyZipFile("so-xp-simple.obo");
74 * Loads the given ontology file from a zip file with ".zip" appended
78 protected void loadOntologyZipFile(String ontologyFile)
80 ZipInputStream zipStream = null;
83 String zipFile = ontologyFile + ".zip";
84 System.out.println("Loading Sequence Ontology from " + zipFile);
85 InputStream inStream = this.getClass().getResourceAsStream(
87 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
89 while ((entry = zipStream.getNextEntry()) != null)
91 if (entry.getName().equals(ontologyFile))
93 loadOboFile(zipStream);
101 closeStream(zipStream);
106 * Closes the input stream, swallowing all exceptions
110 protected void closeStream(InputStream is)
117 } catch (IOException e)
125 * Reads, parses and stores the OBO file data
128 * @throws ParseException
129 * @throws IOException
131 protected void loadOboFile(InputStream is) throws ParseException,
134 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
135 OboParser parser = new OboParser();
136 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
137 isA = ontology.getTerm("is_a");
142 * Stores a lookup table of terms by description. Note that description is not
143 * guaranteed unique. Where duplicate descriptions are found, try to discard
144 * the term that is flagged as obsolete. However we do store obsolete terms
145 * where there is no duplication of description.
147 protected void storeTermNames()
149 for (Term term : ontology.getTerms())
151 if (term instanceof Impl)
153 String description = term.getDescription();
154 if (description != null)
156 Term replaced = termsByDescription.get(description);
157 if (replaced != null)
159 boolean newTermIsObsolete = isObsolete(term);
160 boolean oldTermIsObsolete = isObsolete(replaced);
161 if (newTermIsObsolete && !oldTermIsObsolete)
163 System.err.println("Ignoring " + term.getName()
164 + " as obsolete and duplicated by "
165 + replaced.getName());
168 else if (!newTermIsObsolete && oldTermIsObsolete)
170 System.err.println("Ignoring " + replaced.getName()
171 + " as obsolete and duplicated by " + term.getName());
175 System.err.println("Warning: " + term.getName()
176 + " has replaced " + replaced.getName()
177 + " for lookup of '" + description + "'");
180 termsByDescription.put(description, term);
187 * Answers true if the term has property "is_obsolete" with value true, else
193 public static boolean isObsolete(Term term)
195 Annotation ann = term.getAnnotation();
200 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
204 } catch (NoSuchElementException e)
206 // fall through to false
213 * Test whether the given Sequence Ontology term is nucleotide_match (either
214 * directly or via is_a relationship)
217 * SO name or description
220 public boolean isNucleotideMatch(String soTerm)
222 return isA(soTerm, NUCLEOTIDE_MATCH);
226 * Test whether the given Sequence Ontology term is protein_match (either
227 * directly or via is_a relationship)
230 * SO name or description
233 public boolean isProteinMatch(String soTerm)
235 return isA(soTerm, PROTEIN_MATCH);
239 * Test whether the given Sequence Ontology term is polypeptide (either
240 * directly or via is_a relationship)
243 * SO name or description
246 public boolean isPolypeptide(String soTerm)
248 return isA(soTerm, POLYPEPTIDE);
252 * Returns true if the given term has a (direct or indirect) 'isA'
253 * relationship with the parent
260 public boolean isA(String child, String parent)
262 if (child == null || parent == null)
267 * optimise trivial checks like isA("CDS", "CDS")
269 if (child.equals(parent))
275 Term childTerm = getTerm(child);
276 if (childTerm != null)
284 Term parentTerm = getTerm(parent);
286 return termIsA(childTerm, parentTerm);
290 * Records a valid term queried for, for reporting purposes
294 private void termFound(String term)
296 synchronized (termsFound)
298 if (!termsFound.contains(term))
300 termsFound.add(term);
306 * Records an invalid term queried for, for reporting purposes
310 private void termNotFound(String term)
312 synchronized (termsNotFound)
314 if (!termsNotFound.contains(term))
316 System.err.println("SO term " + term + " invalid");
317 termsNotFound.add(term);
323 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
329 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
332 * null term could arise from a misspelled SO description
334 if (childTerm == null || parentTerm == null)
340 * recursive search endpoint:
342 if (childTerm == parentTerm)
348 * lazy initialisation - find all of a term's parents (recursively)
349 * the first time this is called, and save them in a map.
351 if (!termIsA.containsKey(childTerm))
353 findParents(childTerm);
356 List<Term> parents = termIsA.get(childTerm);
357 for (Term parent : parents)
359 if (termIsA(parent, parentTerm))
362 * add (great-)grandparents to parents list as they are discovered,
363 * for faster lookup next time
365 if (!parents.contains(parentTerm))
367 parents.add(parentTerm);
377 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
382 protected synchronized void findParents(Term childTerm)
384 List<Term> result = new ArrayList<Term>();
385 for (Triple triple : ontology.getTriples(childTerm, null, isA))
387 Term parent = triple.getObject();
391 * and search for the parent's parents recursively
395 termIsA.put(childTerm, result);
399 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
400 * "sequence_location"), or null if not found.
405 protected Term getTerm(String nameOrDescription)
407 Term t = termsByDescription.get(nameOrDescription);
412 t = ontology.getTerm(nameOrDescription);
413 } catch (NoSuchElementException e)
421 public boolean isSequenceVariant(String soTerm)
423 return isA(soTerm, SEQUENCE_VARIANT);
427 * Sorts (case-insensitive) and returns the list of valid terms queried for
430 public List<String> termsFound()
432 synchronized (termsFound)
434 Collections.sort(termsFound, String.CASE_INSENSITIVE_ORDER);
440 * Sorts (case-insensitive) and returns the list of invalid terms queried for
443 public List<String> termsNotFound()
445 synchronized (termsNotFound)
447 Collections.sort(termsNotFound, String.CASE_INSENSITIVE_ORDER);
448 return termsNotFound;