1 package jalview.io.gff;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.text.ParseException;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
14 import java.util.NoSuchElementException;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipInputStream;
18 import org.biojava.nbio.ontology.Ontology;
19 import org.biojava.nbio.ontology.Term;
20 import org.biojava.nbio.ontology.Term.Impl;
21 import org.biojava.nbio.ontology.Triple;
22 import org.biojava.nbio.ontology.io.OboParser;
23 import org.biojava.nbio.ontology.utils.Annotation;
26 * A wrapper class that parses the Sequence Ontology and exposes useful access
27 * methods. This version uses the BioJava parser.
29 class SequenceOntology implements SequenceOntologyI
32 * the parsed Ontology data as modelled by BioJava
34 private Ontology ontology;
37 * the ontology term for the isA relationship
42 * lookup of terms by user readable name (NB not guaranteed unique)
44 private Map<String, Term> termsByDescription;
47 * Map where key is a Term and value is a (possibly empty) list of
48 * all Terms to which the key has an 'isA' relationship, either
49 * directly or indirectly (A isA B isA C)
51 private Map<Term, List<Term>> termIsA;
53 private List<String> termsFound;
55 private List<String> termsNotFound;
58 * Package private constructor to enforce use of singleton. Parses and caches
59 * the SO OBO data file.
63 termsFound = new ArrayList<String>();
64 termsNotFound = new ArrayList<String>();
65 termsByDescription = new HashMap<String, Term>();
66 termIsA = new HashMap<Term, List<Term>>();
68 loadOntologyZipFile("so-xp-simple.obo");
72 * Loads the given ontology file from a zip file with ".zip" appended
76 protected void loadOntologyZipFile(String ontologyFile)
78 ZipInputStream zipStream = null;
81 String zipFile = ontologyFile + ".zip";
82 System.out.println("Loading Sequence Ontology from " + zipFile);
83 InputStream inStream = this.getClass().getResourceAsStream(
85 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
87 while ((entry = zipStream.getNextEntry()) != null)
89 if (entry.getName().equals(ontologyFile))
91 loadOboFile(zipStream);
99 closeStream(zipStream);
104 * Closes the input stream, swallowing all exceptions
108 protected void closeStream(InputStream is)
115 } catch (IOException e)
123 * Reads, parses and stores the OBO file data
126 * @throws ParseException
127 * @throws IOException
129 protected void loadOboFile(InputStream is) throws ParseException,
132 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
133 OboParser parser = new OboParser();
134 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
135 isA = ontology.getTerm("is_a");
140 * Stores a lookup table of terms by description. Note that description is not
141 * guaranteed unique. Where duplicate descriptions are found, try to discard
142 * the term that is flagged as obsolete. However we do store obsolete terms
143 * where there is no duplication of description.
145 protected void storeTermNames()
147 for (Term term : ontology.getTerms())
149 if (term instanceof Impl)
151 String description = term.getDescription();
152 if (description != null)
154 Term replaced = termsByDescription.get(description);
155 if (replaced != null)
157 boolean newTermIsObsolete = isObsolete(term);
158 boolean oldTermIsObsolete = isObsolete(replaced);
159 if (newTermIsObsolete && !oldTermIsObsolete)
161 System.err.println("Ignoring " + term.getName()
162 + " as obsolete and duplicated by "
163 + replaced.getName());
166 else if (!newTermIsObsolete && oldTermIsObsolete)
168 System.err.println("Ignoring " + replaced.getName()
169 + " as obsolete and duplicated by " + term.getName());
173 System.err.println("Warning: " + term.getName()
174 + " has replaced " + replaced.getName()
175 + " for lookup of '" + description + "'");
178 termsByDescription.put(description, term);
185 * Answers true if the term has property "is_obsolete" with value true, else
191 public static boolean isObsolete(Term term)
193 Annotation ann = term.getAnnotation();
198 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
202 } catch (NoSuchElementException e)
204 // fall through to false
211 * Test whether the given Sequence Ontology term is nucleotide_match (either
212 * directly or via is_a relationship)
215 * SO name or description
218 public boolean isNucleotideMatch(String soTerm)
220 return isA(soTerm, NUCLEOTIDE_MATCH);
224 * Test whether the given Sequence Ontology term is protein_match (either
225 * directly or via is_a relationship)
228 * SO name or description
231 public boolean isProteinMatch(String soTerm)
233 return isA(soTerm, PROTEIN_MATCH);
237 * Test whether the given Sequence Ontology term is polypeptide (either
238 * directly or via is_a relationship)
241 * SO name or description
244 public boolean isPolypeptide(String soTerm)
246 return isA(soTerm, POLYPEPTIDE);
250 * Returns true if the given term has a (direct or indirect) 'isA'
251 * relationship with the parent
258 public boolean isA(String child, String parent)
260 if (child == null || parent == null)
265 * optimise trivial checks like isA("CDS", "CDS")
267 if (child.equals(parent))
273 Term childTerm = getTerm(child);
274 if (childTerm != null)
282 Term parentTerm = getTerm(parent);
284 return termIsA(childTerm, parentTerm);
288 * Records a valid term queried for, for reporting purposes
292 private void termFound(String term)
294 synchronized (termsFound)
296 if (!termsFound.contains(term))
298 termsFound.add(term);
304 * Records an invalid term queried for, for reporting purposes
308 private void termNotFound(String term)
310 synchronized (termsNotFound)
312 if (!termsNotFound.contains(term))
314 System.err.println("SO term " + term + " invalid");
315 termsNotFound.add(term);
321 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
327 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
330 * null term could arise from a misspelled SO description
332 if (childTerm == null || parentTerm == null)
338 * recursive search endpoint:
340 if (childTerm == parentTerm)
346 * lazy initialisation - find all of a term's parents (recursively)
347 * the first time this is called, and save them in a map.
349 if (!termIsA.containsKey(childTerm))
351 findParents(childTerm);
354 List<Term> parents = termIsA.get(childTerm);
355 for (Term parent : parents)
357 if (termIsA(parent, parentTerm))
360 * add (great-)grandparents to parents list as they are discovered,
361 * for faster lookup next time
363 if (!parents.contains(parentTerm))
365 parents.add(parentTerm);
375 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
380 protected synchronized void findParents(Term childTerm)
382 List<Term> result = new ArrayList<Term>();
383 for (Triple triple : ontology.getTriples(childTerm, null, isA))
385 Term parent = triple.getObject();
389 * and search for the parent's parents recursively
393 termIsA.put(childTerm, result);
397 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
398 * "sequence_location"), or null if not found.
403 protected Term getTerm(String nameOrDescription)
405 Term t = termsByDescription.get(nameOrDescription);
410 t = ontology.getTerm(nameOrDescription);
411 } catch (NoSuchElementException e)
419 public boolean isSequenceVariant(String soTerm)
421 return isA(soTerm, SEQUENCE_VARIANT);
425 * Sorts (case-insensitive) and returns the list of valid terms queried for
428 public List<String> termsFound()
430 synchronized (termsFound)
432 Collections.sort(termsFound, String.CASE_INSENSITIVE_ORDER);
438 * Sorts (case-insensitive) and returns the list of invalid terms queried for
441 public List<String> termsNotFound()
443 synchronized (termsNotFound)
445 Collections.sort(termsNotFound, String.CASE_INSENSITIVE_ORDER);
446 return termsNotFound;