Sunday, September 27, 2009

Searching and Displaying Terms OBO

Here is some sample code on how to search for a term and display its properties:



package au.edu.apf.phenomebank.ontology;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Collection;
import java.util.LinkedList;

import org.obo.dataadapter.DefaultOBOParser;
import org.obo.dataadapter.OBOParseEngine;
import org.obo.datamodel.Dbxref;
import org.obo.datamodel.IdentifiedObject;
import org.obo.datamodel.Link;
import org.obo.datamodel.LinkDatabase;
import org.obo.datamodel.NestedValue;
import org.obo.datamodel.OBOObject;
import org.obo.datamodel.OBOSession;
import org.obo.datamodel.PropertyValue;
import org.obo.datamodel.Synonym;
import org.obo.util.TextUtil;

/**
* Testing ontology parsing
*
*
* @author Philip Wu
*/
public class OntologyTest {

public static void main (String[] args) throws Exception {
System.out.println("Start");


DefaultOBOParser parser = new DefaultOBOParser();
OBOParseEngine engine = new OBOParseEngine(parser);

String path = "C:/Documents and Settings/a295029/Desktop/latest_MPheno_OBO.ontology";
Collection paths = new LinkedList();
paths.add(path);
engine.setPaths(paths);

engine.parse();

OBOSession session = parser.getSession();


// Using the Ontology
IdentifiedObject obj = session.getObject("MP:0009788");
if (obj != null) {
if (obj instanceof OBOObject) {
OBOObject term = (OBOObject) obj;
dumpTerm(System.out, term);
}
}


System.out.println("Finished");
}

public static void dumpNestedValue(PrintStream ps, NestedValue nv) {
if (nv!= null) {
ps.print(" Name: "+nv.getName());
if (nv.getPropertyValues() != null) {
for (PropertyValue pv : nv.getPropertyValues()) {
ps.println("Property: "+pv.getProperty()+"="+pv.getValue());
}
}
}
}

public static void dumpTerm(PrintStream ps, OBOObject term) {
String id = term.getID();

ps.println("ID:"+id);
ps.println("Name: "+term.getName());

if (term.getIDExtension() != null) {
ps.println("ID Extension: ");
dumpNestedValue(ps, term.getIDExtension());
}

if (term.getPropertyValues() != null && term.getPropertyValues().size() > 0) {
ps.println("Property values");
dumpPropertyValues(ps, term.getPropertyValues());
}

ps.println("Definition: "+term.getDefinition());
if (term.getDefinitionExtension() != null) {
dumpNestedValue(ps, term.getDefinitionExtension());
}

for (Dbxref dbxref : TextUtil.getAllDbxrefs(term)) {
ps.println("Def dbxref. ID: "+dbxref.getID()+ " DatabaseID: "+dbxref.getDatabaseID()+" Database: "+ dbxref.getDatabase()+" Desc: "+dbxref.getDesc());

}

for (Synonym syn : term.getSynonyms()) {
ps.println("Synonym. ID: "+syn.getID()+ " Text:"+syn.getText());
}

for (Link link : term.getParents()) {
ps.println("Parent. ID: "+ link.getParent().getID());
}
}

public static void dumpPropertyValues(PrintStream ps, Collection<PropertyValue> propertyValues) {
if (propertyValues != null) {
for (PropertyValue pv : propertyValues) {
ps.println("Property: "+pv.getProperty()+"="+pv.getValue());
}
}
}

}

Thursday, September 24, 2009

Java OBO Parser

I've been task with importing a large OBO file into a database. However, I had trouble finding a parser pre-bundled as a set of JAR files. There is one Java OBO parser that exists but it lives within a much larger application found here:

http://www.geneontology.org/GO.java.obo.parser.shtml

He goes into some details about the writing some code, but isn't clear about which JAR files from the SVN source that are needed to get it working. Here I will provide the list of 3 JARs needed for a Java OBO parser:

For some sample code see below:


DefaultOBOParser parser = new DefaultOBOParser();
OBOParseEngine engine = new OBOParseEngine(parser);

String path = "C:/MPheno_OBO.ontology";
Collection paths = new LinkedList();
paths.add(path);
engine.setPaths(paths);

engine.parse();
To import the OBO file, I plan writing my own implementation of OBOParser for better memory usage.

Cheers,
Phil