001package org.biojava.ontology;
002
003import java.io.BufferedReader;
004import java.io.InputStreamReader;
005
006import org.biojava.bio.BioError;
007import org.biojava.ontology.io.TabDelimParser;
008import org.biojava.utils.ClassTools;
009
010/**
011 * Tools for manipulating ontologies.
012 *
013 * @author Matthew Pocock
014 */
015public final class OntoTools {
016  private static final Ontology CORE_ONTOLOGY;
017  private static final OntologyFactory DEFAULT_FACTORY;
018  private static final IntegerOntology CORE_INTEGER;
019  //private static final Ontology CORE_STRING;
020
021  // public static final Term TYPE;
022  public static final Term RELATION;
023  public static final Term ANY;
024  public static final Term NONE;
025  public static final Term IS_A;
026  public static final Term PART_OF;
027  // public static final Term SUB_TYPE_OF;
028  // public static final Term INSTANCE_OF;
029  // public static final Term DOMAIN;
030  // public static final Term CO_DOMAIN;
031  // public static final Term HAS_DOMAIN;
032  // public static final Term HAS_CO_DOMAIN;
033
034  // public static final Term BOOLEAN;
035  // public static final Term TRUE;
036  // public static final Term FALSE;
037  // public static final Term PREDICATE;
038
039  // public static final Term AND;
040  // public static final Term OR;
041  // public static final Term XOR;
042  // public static final Term EQUAL;
043  // public static final Term NOT_EQUAL;
044  // public static final Term IMPLIES;
045
046  public static final Term REFLEXIVE;
047  public static final Term SYMMETRIC;
048  public static final Term TRANSITIVE;
049  public static final Term EQUIVALENCE;
050  public static final Term PARTIAL_ORDER;
051
052  static {
053    DEFAULT_FACTORY = new OntologyFactory() {
054      public Ontology createOntology(String name, String desc)
055      throws OntologyException {
056        return new Ontology.Impl(name, desc);
057      }
058    };
059
060    try {
061      BufferedReader reader = new BufferedReader(
062        new InputStreamReader(
063          ClassTools.getClassLoader(OntoTools.class).getResourceAsStream(
064            "org/biojava/ontology/core.onto"
065          )
066        )
067      );
068
069      CORE_INTEGER = new IntegerOntology();
070      CORE_ONTOLOGY = new TabDelimParser().parse(
071              reader,
072              DEFAULT_FACTORY
073      );
074
075      // TYPE = CORE_ONTOLOGY.getTerm("type");
076      RELATION = CORE_ONTOLOGY.getTerm("relation");
077      ANY = CORE_ONTOLOGY.getTerm("any");
078      NONE = CORE_ONTOLOGY.getTerm("none");
079      IS_A = CORE_ONTOLOGY.getTerm("is-a");
080      PART_OF = CORE_ONTOLOGY.getTerm("part-of");
081      
082      // SUB_TYPE_OF = CORE_ONTOLOGY.getTerm("sub_type_of");
083      // INSTANCE_OF = CORE_ONTOLOGY.getTerm("instance_of");
084      // DOMAIN = CORE_ONTOLOGY.getTerm("domain");
085      // CO_DOMAIN = CORE_ONTOLOGY.getTerm("co_domain");
086      // HAS_DOMAIN = CORE_ONTOLOGY.getTerm("has_domain");
087      // HAS_CO_DOMAIN = CORE_ONTOLOGY.getTerm("has_co_domain");
088
089      // BOOLEAN = CORE_ONTOLOGY.getTerm("boolean");
090      // TRUE = CORE_ONTOLOGY.getTerm("true");
091      // FALSE = CORE_ONTOLOGY.getTerm("false");
092      // PREDICATE = CORE_ONTOLOGY.getTerm("predicate");
093
094      // AND = CORE_ONTOLOGY.getTerm("and");
095      // OR = CORE_ONTOLOGY.getTerm("or");
096      // XOR = CORE_ONTOLOGY.getTerm("xor");
097      // EQUAL = CORE_ONTOLOGY.getTerm("equal");
098      // NOT_EQUAL = CORE_ONTOLOGY.getTerm("not_equal");
099      // IMPLIES = CORE_ONTOLOGY.getTerm("implies");
100
101      REFLEXIVE = CORE_ONTOLOGY.getTerm("reflexive");
102      EQUIVALENCE = CORE_ONTOLOGY.getTerm("equivalence");
103      SYMMETRIC = CORE_ONTOLOGY.getTerm("symmetric");
104      TRANSITIVE = CORE_ONTOLOGY.getTerm("transitive");
105      PARTIAL_ORDER = CORE_ONTOLOGY.getTerm("partial-order");
106
107    } catch (Exception e) {
108      throw new BioError("Could not initialize OntoTools", e);
109    }
110  }
111
112
113  private OntoTools() {}
114
115  /**
116   * Get the Ontology that defines our core "central dogma".
117   *
118   * <p>This contains definitions that we have to have, such as <code>any</code>,
119   * <code>predicate</code>, <code>is-a</code> and <code>transient</code>. These
120   * are our axioms, upon which the default interpreters build.</p>
121   *
122   * @return the "core" Ontology
123   */
124  public static Ontology getCoreOntology() {
125    return CORE_ONTOLOGY;
126  }
127
128  /**
129   * Get the Ontology that defines integers.
130   *
131   * <p>This contains a term for each and every integer. I haven't decided yet
132   * if it contains terms for arithmatic.</p>
133   *
134   * @return the integer Ontology
135   */
136  public static IntegerOntology getIntegerOntology() {
137    return CORE_INTEGER;
138  }
139
140  public static OntologyFactory getDefaultFactory() {
141    return DEFAULT_FACTORY;
142  }
143}