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; /** * This class demonstrates the Protege OWL API's for reading an OWL file. It * traverses the BioPax Level 2 OWL file and generates HTML documentation for * the ontology. */ public class BioPaxLevel2 { // first part of the HTML file private static final String HEADER = "\n" + " \n" + " \n" + " \n" + " BioPax Level 2\n" + " \n" + " \n" + "

BioPax Level 2

\n" + "
\n"; // last part of the HTML file private static final String FOOTER = "
\n" + " \n" + "\n"; // gets a list of classes and puts them in a select HTML widget. The private static StringBuffer getClassList(OWLModel owlModel) { StringBuffer sb = new StringBuffer( "

Go to BioPax Class:  " + "

\n"); return sb; } /** * Writes the given content to a file on disk * @param fileName the name of the file to write to * @param content the content to put in the file. */ private static void writeToFile(String fileName, StringBuffer content) { System.out.println("Generating file " + fileName); PrintWriter writer = null; try { writer = new PrintWriter(new File(fileName)); writer.println(content.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (writer != null) { writer.close(); } } } /** Entry point for the example. It loads the BioPax ontology from the web, iterates * over the classes defined in the ontology, and generates HTML from the class names, * parent classes, and comments. * @param args no arguments are expected. */ 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)); Collection classes = owlModel.getUserDefinedOWLNamedClasses(); int i = 0; for (Iterator it = classes.iterator(); it.hasNext();) { OWLNamedClass cls = (OWLNamedClass) it.next(); detail.append( " " + "

Class " + cls.getName() + "

\n" + "

Parent: "); Collection superclasses = cls.getNamedSuperclasses(); for (Iterator superclassIt = superclasses.iterator(); superclassIt.hasNext(); ) { String parent = ((OWLNamedClass)superclassIt.next()).getName(); detail.append("" + parent + " "); } detail.append("

\n"); detail.append( "

\n"); Collection comments = cls.getComments(); for (Iterator commentIt = comments.iterator(); commentIt.hasNext(); ) { detail.append(commentIt.next()); } detail.append("

\n"); i++; } html.append(detail); } catch (Exception e) { e.printStackTrace(); } finally { html.append(FOOTER); } writeToFile("bio_pax_level2.html", html); } }