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 a direct 'isA' relationship
45 private Map<Term, List<Term>> termIsA;
47 public synchronized static SequenceOntology getInstance()
51 instance = new SequenceOntology();
57 * Private constructor to enforce use of singleton. Parses and caches the SO
60 private SequenceOntology()
62 termsByDescription = new HashMap<String, Term>();
63 termIsA = new HashMap<Term, List<Term>>();
65 loadOntologyZipFile("so-xp-simple.obo");
69 * Loads the given ontology file from a zip file with ".zip" appended
73 protected void loadOntologyZipFile(String ontologyFile)
75 ZipInputStream zipStream = null;
78 InputStream inStream = this.getClass().getResourceAsStream(
79 "/" + ontologyFile + ".zip");
80 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
82 while ((entry = zipStream.getNextEntry()) != null)
84 if (entry.getName().equals(ontologyFile))
86 loadOboFile(zipStream);
94 closeStream(zipStream);
99 * Closes the input stream, swallowing all exceptions
103 protected void closeStream(InputStream is)
110 } catch (IOException e)
118 * Reads, parses and stores the OBO file data
121 * @throws ParseException
122 * @throws IOException
124 protected void loadOboFile(InputStream is) throws ParseException,
127 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
128 OboParser parser = new OboParser();
129 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
130 isA = ontology.getTerm("is_a");
135 * Stores a lookup table of terms by description. Note that description is not
136 * guaranteed unique. Where duplicate descriptions are found, try to discard
137 * the term that is flagged as obsolete. However we do store obsolete terms
138 * where there is no duplication of description.
140 protected void storeTermNames()
142 for (Term term : ontology.getTerms())
144 if (term instanceof Impl)
146 String description = term.getDescription();
147 if (description != null)
149 Term replaced = termsByDescription.get(description);
150 if (replaced != null)
152 boolean newTermIsObsolete = isObsolete(term);
153 boolean oldTermIsObsolete = isObsolete(replaced);
154 if (newTermIsObsolete && !oldTermIsObsolete)
156 System.err.println("Ignoring " + term.getName()
157 + " as obsolete and duplicated by "
158 + replaced.getName());
161 else if (!newTermIsObsolete && oldTermIsObsolete)
163 System.err.println("Ignoring " + replaced.getName()
164 + " as obsolete and duplicated by " + term.getName());
168 System.err.println("Warning: " + term.getName()
169 + " has replaced " + replaced.getName()
170 + " for lookup of '" + description + "'");
173 termsByDescription.put(description, term);
180 * Answers true if the term has property "is_obsolete" with value true, else
186 public static boolean isObsolete(Term term)
188 Annotation ann = term.getAnnotation();
193 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
197 } catch (NoSuchElementException e)
199 // fall through to false
206 * Test whether the given Sequence Ontology term is nucleotide_match (either
207 * directly or via is_a relationship)
210 * SO name or description
213 public boolean isNucleotideMatch(String soTerm)
215 return isA(soTerm, "nucleotide_match");
219 * Test whether the given Sequence Ontology term is protein_match (either
220 * directly or via is_a relationship)
223 * SO name or description
226 public boolean isProteinMatch(String soTerm)
228 return isA(soTerm, "protein_match");
232 * Test whether the given Sequence Ontology term is polypeptide (either
233 * directly or via is_a relationship)
236 * SO name or description
239 public boolean isPolypeptide(String soTerm)
241 return isA(soTerm, "polypeptide");
245 * Returns true if the given term has a (direct or indirect) 'isA'
246 * relationship with the parent
252 public boolean isA(String child, String parent)
254 Term childTerm = getTerm(child);
255 Term parentTerm = getTerm(parent);
257 return termIsA(childTerm, parentTerm);
261 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
267 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
270 * null term could arise from a misspelled SO description
272 if (childTerm == null || parentTerm == null)
278 * recursive search endpoint:
280 if (childTerm == parentTerm)
286 * lazy initialisation - find all of a term's parents (recursively)
287 * the first time this is called, and save them in a map.
289 if (!termIsA.containsKey(childTerm))
291 findParents(childTerm);
294 List<Term> parents = termIsA.get(childTerm);
295 for (Term parent : parents)
297 if (termIsA(parent, parentTerm))
307 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
312 protected synchronized void findParents(Term childTerm)
314 List<Term> result = new ArrayList<Term>();
315 for (Triple triple : ontology.getTriples(childTerm, null, isA))
317 Term parent = triple.getObject();
321 * and search for the parent's parents recursively
325 termIsA.put(childTerm, result);
329 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
330 * "sequence_location"), or null if not found.
335 protected Term getTerm(String nameOrDescription)
337 Term t = termsByDescription.get(nameOrDescription);
342 t = ontology.getTerm(nameOrDescription);
343 } catch (NoSuchElementException e)