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.HashMap;
11 import java.util.List;
13 import java.util.NoSuchElementException;
14 import java.util.zip.ZipEntry;
15 import java.util.zip.ZipInputStream;
17 import org.biojava.nbio.ontology.Ontology;
18 import org.biojava.nbio.ontology.Term;
19 import org.biojava.nbio.ontology.Term.Impl;
20 import org.biojava.nbio.ontology.Triple;
21 import org.biojava.nbio.ontology.io.OboParser;
22 import org.biojava.nbio.ontology.utils.Annotation;
25 * A wrapper class that parses the Sequence Ontology and exposes useful access
26 * methods. This version uses the BioJava parser.
28 public class SequenceOntology
30 private static SequenceOntology instance;
32 private Ontology ontology;
37 * lookup of terms by user readable name (NB not guaranteed unique)
39 private Map<String, Term> termsByDescription;
42 * Map where key is a Term and value is a (possibly empty) list of
43 * all Terms to which the key has an 'isA' relationship, either
44 * directly or indirectly (A isA B isA C)
46 private Map<Term, List<Term>> termIsA;
49 * Returns singleton instance
53 public synchronized static SequenceOntology getInstance()
57 instance = new SequenceOntology();
63 * Private constructor to enforce use of singleton. Parses and caches the SO
66 private SequenceOntology()
68 termsByDescription = new HashMap<String, Term>();
69 termIsA = new HashMap<Term, List<Term>>();
71 loadOntologyZipFile("so-xp-simple.obo");
75 * Loads the given ontology file from a zip file with ".zip" appended
79 protected void loadOntologyZipFile(String ontologyFile)
81 ZipInputStream zipStream = null;
84 InputStream inStream = this.getClass().getResourceAsStream(
85 "/" + ontologyFile + ".zip");
86 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
88 while ((entry = zipStream.getNextEntry()) != null)
90 if (entry.getName().equals(ontologyFile))
92 loadOboFile(zipStream);
100 closeStream(zipStream);
105 * Closes the input stream, swallowing all exceptions
109 protected void closeStream(InputStream is)
116 } catch (IOException e)
124 * Reads, parses and stores the OBO file data
127 * @throws ParseException
128 * @throws IOException
130 protected void loadOboFile(InputStream is) throws ParseException,
133 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
134 OboParser parser = new OboParser();
135 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
136 isA = ontology.getTerm("is_a");
141 * Stores a lookup table of terms by description. Note that description is not
142 * guaranteed unique. Where duplicate descriptions are found, try to discard
143 * the term that is flagged as obsolete. However we do store obsolete terms
144 * where there is no duplication of description.
146 protected void storeTermNames()
148 for (Term term : ontology.getTerms())
150 if (term instanceof Impl)
152 String description = term.getDescription();
153 if (description != null)
155 Term replaced = termsByDescription.get(description);
156 if (replaced != null)
158 boolean newTermIsObsolete = isObsolete(term);
159 boolean oldTermIsObsolete = isObsolete(replaced);
160 if (newTermIsObsolete && !oldTermIsObsolete)
162 System.err.println("Ignoring " + term.getName()
163 + " as obsolete and duplicated by "
164 + replaced.getName());
167 else if (!newTermIsObsolete && oldTermIsObsolete)
169 System.err.println("Ignoring " + replaced.getName()
170 + " as obsolete and duplicated by " + term.getName());
174 System.err.println("Warning: " + term.getName()
175 + " has replaced " + replaced.getName()
176 + " for lookup of '" + description + "'");
179 termsByDescription.put(description, term);
186 * Answers true if the term has property "is_obsolete" with value true, else
192 public static boolean isObsolete(Term term)
194 Annotation ann = term.getAnnotation();
199 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
203 } catch (NoSuchElementException e)
205 // fall through to false
212 * Test whether the given Sequence Ontology term is nucleotide_match (either
213 * directly or via is_a relationship)
216 * SO name or description
219 public boolean isNucleotideMatch(String soTerm)
221 return isA(soTerm, "nucleotide_match");
225 * Test whether the given Sequence Ontology term is protein_match (either
226 * directly or via is_a relationship)
229 * SO name or description
232 public boolean isProteinMatch(String soTerm)
234 return isA(soTerm, "protein_match");
238 * Test whether the given Sequence Ontology term is polypeptide (either
239 * directly or via is_a relationship)
242 * SO name or description
245 public boolean isPolypeptide(String soTerm)
247 return isA(soTerm, "polypeptide");
251 * Returns true if the given term has a (direct or indirect) 'isA'
252 * relationship with the parent
258 public boolean isA(String child, String parent)
260 Term childTerm = getTerm(child);
261 Term parentTerm = getTerm(parent);
263 return termIsA(childTerm, parentTerm);
267 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
273 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
276 * null term could arise from a misspelled SO description
278 if (childTerm == null || parentTerm == null)
284 * recursive search endpoint:
286 if (childTerm == parentTerm)
292 * lazy initialisation - find all of a term's parents (recursively)
293 * the first time this is called, and save them in a map.
295 if (!termIsA.containsKey(childTerm))
297 findParents(childTerm);
300 List<Term> parents = termIsA.get(childTerm);
301 for (Term parent : parents)
303 if (termIsA(parent, parentTerm))
306 * add (great-)grandparents to parents list as they are discovered,
307 * for faster lookup next time
309 if (!parents.contains(parentTerm))
311 parents.add(parentTerm);
321 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
326 protected synchronized void findParents(Term childTerm)
328 List<Term> result = new ArrayList<Term>();
329 for (Triple triple : ontology.getTriples(childTerm, null, isA))
331 Term parent = triple.getObject();
335 * and search for the parent's parents recursively
339 termIsA.put(childTerm, result);
343 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
344 * "sequence_location"), or null if not found.
349 protected Term getTerm(String nameOrDescription)
351 Term t = termsByDescription.get(nameOrDescription);
356 t = ontology.getTerm(nameOrDescription);
357 } catch (NoSuchElementException e)