1 package jalview.io.gff;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.ArrayList;
8 import java.util.HashMap;
11 import java.util.NoSuchElementException;
13 import org.biojava.nbio.ontology.Ontology;
14 import org.biojava.nbio.ontology.Term;
15 import org.biojava.nbio.ontology.Term.Impl;
16 import org.biojava.nbio.ontology.Triple;
17 import org.biojava.nbio.ontology.io.OboParser;
20 * A wrapper class that parses the Sequence Ontology and exposes useful access
21 * methods. This version uses the BioJava parser.
23 public class SequenceOntology
25 private static SequenceOntology instance = new SequenceOntology();
27 private Ontology ontology;
32 * lookup of terms by user readable name (NB not guaranteed unique)
34 private Map<String, Term> termsByDescription;
37 * Map where key is a Term and value is a (possibly empty) list of
38 * all Terms to which the key has a direct 'isA' relationship
40 private Map<Term, List<Term>> termIsA;
42 public static SequenceOntology getInstance()
48 * Private constructor to enforce use of singleton. Parses and caches the SO
51 private SequenceOntology()
53 termsByDescription = new HashMap<String, Term>();
54 termIsA = new HashMap<Term, List<Term>>();
56 OboParser parser = new OboParser();
57 InputStream inStream = null;
60 inStream = this.getClass().getResourceAsStream("/so-xp-simple.obo");
62 BufferedReader oboFile = new BufferedReader(new InputStreamReader(
64 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
65 isA = ontology.getTerm("is_a");
78 } catch (IOException e)
86 protected void storeTermNames()
88 for (Term term : ontology.getTerms())
90 if (term instanceof Impl)
92 String description = term.getDescription();
93 if (description != null)
95 // System.out.println(term.getName() + "=" + term.getDescription());
96 Term replaced = termsByDescription.put(description, term);
99 System.err.println("Warning: " + term.getName()
100 + " has replaced " + replaced.getName()
101 + " for lookup of description "
110 * Test whether the given Sequence Ontology term is nucleotide_match (either
111 * directly or via is_a relationship)
116 public boolean isNucleotideMatch(String soTerm)
118 return isA(soTerm, "nucleotide_match");
122 * Test whether the given Sequence Ontology term is protein_match (either
123 * directly or via is_a relationship)
128 public boolean isProteinMatch(String soTerm)
130 return isA(soTerm, "protein_match");
133 public boolean isPolypeptide(String soTerm)
135 return isA(soTerm, "polypeptide");
139 * Returns true if the given term has a (direct or indirect) 'isA'
140 * relationship with the parent
146 public boolean isA(String child, String parent)
148 Term childTerm = getTerm(child);
149 Term parentTerm = getTerm(parent);
151 return termIsA(childTerm, parentTerm);
155 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
161 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
164 * null child term arises from a misspelled SO description
166 if (childTerm == null || parentTerm == null)
172 * recursive search endpoint:
174 if (childTerm == parentTerm)
179 * lazy initialisation - find all of a term's parents the first
180 * time this is called, and save them in a map.
182 if (!termIsA.containsKey(childTerm))
184 findParents(childTerm);
187 List<Term> parents = termIsA.get(childTerm);
188 for (Term parent : parents)
190 if (termIsA(parent, parentTerm))
200 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
205 protected synchronized void findParents(Term childTerm)
207 List<Term> result = new ArrayList<Term>();
208 for (Triple triple : ontology.getTriples(childTerm, null, isA))
210 Term parent = triple.getObject();
214 * and search for the parent's parents recursively
218 termIsA.put(childTerm, result);
222 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
223 * "sequence_location"), or null if not found.
228 protected Term getTerm(String nameOrDescription)
230 Term t = termsByDescription.get(nameOrDescription);
235 t = ontology.getTerm(nameOrDescription);
236 } catch (NoSuchElementException e)