1 package jalview.io.gff;
3 import java.io.BufferedReader;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
12 import java.util.NoSuchElementException;
14 import org.biojava.nbio.ontology.Ontology;
15 import org.biojava.nbio.ontology.Term;
16 import org.biojava.nbio.ontology.Term.Impl;
17 import org.biojava.nbio.ontology.Triple;
18 import org.biojava.nbio.ontology.io.OboParser;
21 * A wrapper class that parses the Sequence Ontology and exposes useful access
22 * methods. This version uses the BioJava parser.
24 public class SequenceOntology
26 private static SequenceOntology instance = new SequenceOntology();
28 private Ontology ontology;
33 * lookup of terms by user readable name (NB not guaranteed unique)
35 private Map<String, Term> termsByDescription;
38 * Map where key is a Term and value is a (possibly empty) list of
39 * all Terms to which the key has a direct 'isA' relationship
41 private Map<Term, List<Term>> termIsA;
43 public static SequenceOntology getInstance()
49 * Private constructor to enforce use of singleton. Parses and caches the SO
52 private SequenceOntology()
54 termsByDescription = new HashMap<String, Term>();
55 termIsA = new HashMap<Term, List<Term>>();
57 OboParser parser = new OboParser();
58 InputStream inStream = null;
61 inStream = new FileInputStream(
62 "/Users/gmcarstairs/Documents/ontologies/so-xp-simple.obo");
64 BufferedReader oboFile = new BufferedReader(new InputStreamReader(
66 ontology = parser.parseOBO(oboFile, "SO", "the SO ontology");
67 isA = ontology.getTerm("is_a");
80 } catch (IOException e)
88 protected void storeTermNames()
90 for (Term term : ontology.getTerms())
92 if (term instanceof Impl)
94 String description = term.getDescription();
95 if (description != null)
97 // System.out.println(term.getName() + "=" + term.getDescription());
98 Term replaced = termsByDescription.put(description, term);
101 System.err.println("Warning: " + term.getName()
102 + " has replaced " + replaced.getName()
103 + " for lookup of description "
112 * Test whether the given Sequence Ontology term is nucleotide_match (either
113 * directly or via is_a relationship)
118 public boolean isNucleotideMatch(String soTerm)
120 return isA(soTerm, "nucleotide_match");
124 * Test whether the given Sequence Ontology term is protein_match (either
125 * directly or via is_a relationship)
130 public boolean isProteinMatch(String soTerm)
132 return isA(soTerm, "protein_match");
135 public boolean isPolypeptide(String soTerm)
137 return isA(soTerm, "polypeptide");
141 * Returns true if the given term has a (direct or indirect) 'isA'
142 * relationship with the parent
148 public boolean isA(String child, String parent)
150 Term childTerm = getTerm(child);
151 Term parentTerm = getTerm(parent);
153 return termIsA(childTerm, parentTerm);
157 * Returns true if the childTerm 'isA' parentTerm (directly or indirectly).
163 protected synchronized boolean termIsA(Term childTerm, Term parentTerm)
166 * null child term arises from a misspelled SO description
168 if (childTerm == null || parentTerm == null)
174 * recursive search endpoint:
176 if (childTerm == parentTerm)
181 * lazy initialisation - find all of a term's parents the first
182 * time this is called, and save them in a map.
184 if (!termIsA.containsKey(childTerm))
186 findParents(childTerm);
189 List<Term> parents = termIsA.get(childTerm);
190 for (Term parent : parents)
192 if (termIsA(parent, parentTerm))
202 * Finds all the 'isA' parents of the childTerm and stores them as a (possibly
207 protected synchronized void findParents(Term childTerm)
209 List<Term> result = new ArrayList<Term>();
210 for (Triple triple : ontology.getTriples(childTerm, null, isA))
212 Term parent = triple.getObject();
216 * and search for the parent's parents recursively
220 termIsA.put(childTerm, result);
224 * Returns the Term for a given name (e.g. "SO:0000735") or description (e.g.
225 * "sequence_location"), or null if not found.
230 protected Term getTerm(String nameOrDescription)
232 Term t = termsByDescription.get(nameOrDescription);
237 t = ontology.getTerm(nameOrDescription);
238 } catch (NoSuchElementException e)