Introduction to OWL Web Ontology Language for Medical and Biosciences Applications (Continued)

Previous  Next
Contents  References

Traversing the BioPax OWL Ontology with Protégé Java API's

The Protégé project includes a library for reading and manipulating OWL documents.  This is described in The Protégé-OWL API - Programmer's Guide15.  This section demonstrates the Protégé OWL API's for reading an OWL file.   The program traverses the BioPax Level 2 OWL file and generates HTML documentation for the ontology.  The source code is in file BioPaxLevel2.java.  The generated HTML file is bio_pax_level2.html.

The sample program begins with a package declaration and import statements to import the Protégé-OWL libraries.


package net.medicalcomputing.owl.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;

public class BioPaxLevel2 {

The next section defines a main method


public static void
main(String[] args) {

String uri = "http://www.biopax.org/release/biopax-level2.owl";
StringBuffer html = new StringBuffer(HEADER);
StringBuffer detail = new StringBuffer();

try {
OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);
html.append(getClassList(owlModel));

The method ProtegeOWL.createJenaOWLModelFromURI(uri) loads the BioPax OWL document from the URL http://www.biopax.org/release/biopax-level2.owl generating an OWL model (owlModel). The following line uses the OWL model to generate a list of class names.  This is done in the following method.



private static
StringBuffer getClassList(OWLModel owlModel) {
StringBuffer sb = new StringBuffer(
" <p>Go to BioPax Class:&nbsp;&nbsp;" +
" <select id='jump_select' onchange='jump(this)';>\n");
Collection classes = owlModel.getUserDefinedOWLNamedClasses();
for (Iterator it = classes.iterator(); it.hasNext();) {
OWLNamedClass cls = (OWLNamedClass) it.next();
sb.append(" <option value='" + cls.getName() + "'>" + cls.getName() + "</option>\n");
}
sb.append(
  " </select></p>\n" +
" </form>\n");
return sb;
}

The main method then iterates over the classes again to give a place for the select widget to jump to.

Collection classes = owlModel.getUserDefinedOWLNamedClasses();
int i = 0;
for (Iterator it = classes.iterator(); it.hasNext();) {
OWLNamedClass cls = (OWLNamedClass) it.next();
detail.append(
" <a name='" + cls.getName() + "'/>" +
" <h2>Class " + cls.getName() + "</h2>\n" +
" <p>Parent: ");

The next few lines find the parent class, which can hyperlink to it's own definition.


Collection superclasses = cls.getNamedSuperclasses();
for (Iterator superclassIt = superclasses.iterator(); superclassIt.hasNext(); ) {
String parent = ((OWLNamedClass)superclassIt.next()).getName();
detail.append("<a href='#" + parent + "'>" + parent + "</a> ");
}
detail.append("</p>\n");

Finally, the program appends the comments to the HTML file.


detail.append(
" <p id='" + cls.getName() + "' tabindex='" + i + "'/>\n");
Collection comments = cls.getComments();
for (Iterator commentIt = comments.iterator(); commentIt.hasNext(); ) {
detail.append(commentIt.next());
}
detail.append("</p<\n");
i++;
}
html.append(detail);

The markup tags for the end of the HTML file are added and the buffer is written out to the file bio_pax_level2.html using the method writeToFile(). To run the program enter the follow command at a command prompt.


>java -cp classes;lib/AbstractSyntax.jar;lib/commons-lang-2.0.jar;lib/commons-logging.jar;lib/icu4j.jar;lib/jena.jar;lib/OWLDoc.jar;lib/protege.jar;\
lib/protege-owl.jar;lib/standard-extensions.jar net.medicalcomputing.owl.example.BioPaxLevel2

Assuming that the class file is in a directory called 'classes' and all the jar files are in a directory called 'lib'.  The class file is BioPaxLevel2.class.

Previous  Next
Contents  References


Please send ideas and opinions by email at webmaster@medicalcomputing.net or add comments to my blog.  The content may become part of the web site.

© Alex Amies 2006