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
32 * selected commonly used values for quick reference
35 public static final String CDS = "CDS";
38 public static final String SEQUENCE_VARIANT = "sequence_variant";
41 public static final String EXON = "exon";
44 public static final String TRANSCRIPT = "transcript";
47 * singleton instance of this class
49 private static SequenceOntology instance;
52 * the parsed Ontology data as modelled by BioJava
54 private Ontology ontology;
57 * the ontology term for the isA relationship
62 * lookup of terms by user readable name (NB not guaranteed unique)
64 private Map<String, Term> termsByDescription;
67 * Map where key is a Term and value is a (possibly empty) list of
68 * all Terms to which the key has an 'isA' relationship, either
69 * directly or indirectly (A isA B isA C)
71 private Map<Term, List<Term>> termIsA;
74 * Returns singleton instance
78 public synchronized static SequenceOntology getInstance()
82 instance = new SequenceOntology();
88 * Private constructor to enforce use of singleton. Parses and caches the SO
91 private SequenceOntology()
93 termsByDescription = new HashMap<String, Term>();
94 termIsA = new HashMap<Term, List<Term>>();
96 loadOntologyZipFile("so-xp-simple.obo");
100 * Loads the given ontology file from a zip file with ".zip" appended
102 * @param ontologyFile
104 protected void loadOntologyZipFile(String ontologyFile)
106 ZipInputStream zipStream = null;
109 String zipFile = ontologyFile + ".zip";
110 System.out.println("Loading Sequence Ontology from " + zipFile);
111 InputStream inStream = this.getClass().getResourceAsStream(
113 zipStream = new ZipInputStream(new BufferedInputStream(inStream));
115 while ((entry = zipStream.getNextEntry()) != null)
117 if (entry.getName().equals(ontologyFile))
119 loadOboFile(zipStream);
122 } catch (Exception e)
127 closeStream(zipStream);
132 * Closes the input stream, swallowing all exceptions
136 protected void closeStream(InputStream is)
143 } catch (IOException e)
151 * Reads, parses and stores the OBO file data
154 * @throws ParseException
155 * @throws IOException
157 protected void loadOboFile(InputStream is) throws ParseException,
160 BufferedReader oboFile = new BufferedReader(new InputStreamReader(is));
161 OboParser parser = new OboParser();
162 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
163 isA = ontology.getTerm("is_a");
168 * Stores a lookup table of terms by description. Note that description is not
169 * guaranteed unique. Where duplicate descriptions are found, try to discard
170 * the term that is flagged as obsolete. However we do store obsolete terms
171 * where there is no duplication of description.
173 protected void storeTermNames()
175 for (Term term : ontology.getTerms())
177 if (term instanceof Impl)
179 String description = term.getDescription();
180 if (description != null)
182 Term replaced = termsByDescription.get(description);
183 if (replaced != null)
185 boolean newTermIsObsolete = isObsolete(term);
186 boolean oldTermIsObsolete = isObsolete(replaced);
187 if (newTermIsObsolete && !oldTermIsObsolete)
189 System.err.println("Ignoring " + term.getName()
190 + " as obsolete and duplicated by "
191 + replaced.getName());
194 else if (!newTermIsObsolete && oldTermIsObsolete)
196 System.err.println("Ignoring " + replaced.getName()
197 + " as obsolete and duplicated by " + term.getName());
201 System.err.println("Warning: " + term.getName()
202 + " has replaced " + replaced.getName()
203 + " for lookup of '" + description + "'");
206 termsByDescription.put(description, term);
213 * Answers true if the term has property "is_obsolete" with value true, else
219 public static boolean isObsolete(Term term)
221 Annotation ann = term.getAnnotation();
226 if (Boolean.TRUE.equals(ann.getProperty("is_obsolete")))
230 } catch (NoSuchElementException e)
232 // fall through to false
239 * Test whether the given Sequence Ontology term is nucleotide_match (either
240 * directly or via is_a relationship)
243 * SO name or description
246 public boolean isNucleotideMatch(String soTerm)
248 return isA(soTerm, "nucleotide_match");
252 * Test whether the given Sequence Ontology term is protein_match (either
253 * directly or via is_a relationship)
256 * SO name or description
259 public boolean isProteinMatch(String soTerm)
261 return isA(soTerm, "protein_match");
265 * Test whether the given Sequence Ontology term is polypeptide (either
266 * directly or via is_a relationship)
269 * SO name or description
272 public boolean isPolypeptide(String soTerm)
274 return isA(soTerm, "polypeptide");
278 * Returns true if the given term has a (direct or indirect) 'isA'
279 * relationship with the parent
285 public boolean isA(String child, String parent)
288 * optimise trivial checks like isA("CDS", "CDS")
290 if (child.equals(parent))
295 Term childTerm = getTerm(child);
296 Term parentTerm = getTerm(parent);
298 return termIsA(childTerm, parentTerm);
302 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
308 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
311 * null term could arise from a misspelled SO description
313 if (childTerm == null || parentTerm == null)
319 * recursive search endpoint:
321 if (childTerm == parentTerm)
327 * lazy initialisation - find all of a term's parents (recursively)
328 * the first time this is called, and save them in a map.
330 if (!termIsA.containsKey(childTerm))
332 findParents(childTerm);
335 List<Term> parents = termIsA.get(childTerm);
336 for (Term parent : parents)
338 if (termIsA(parent, parentTerm))
341 * add (great-)grandparents to parents list as they are discovered,
342 * for faster lookup next time
344 if (!parents.contains(parentTerm))
346 parents.add(parentTerm);
356 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
361 protected synchronized void findParents(Term childTerm)
363 List<Term> result = new ArrayList<Term>();
364 for (Triple triple : ontology.getTriples(childTerm, null, isA))
366 Term parent = triple.getObject();
370 * and search for the parent's parents recursively
374 termIsA.put(childTerm, result);
378 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
379 * "sequence_location"), or null if not found.
384 protected Term getTerm(String nameOrDescription)
386 Term t = termsByDescription.get(nameOrDescription);
391 t = ontology.getTerm(nameOrDescription);
392 } catch (NoSuchElementException e)
400 public boolean isSequenceVariant(String soTerm)
402 return isA(soTerm, "sequence_variant");