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

Wednesday, August 5, 2009

Javascript Array from getElementsByTagName is LIVE!

The array returned by getElementsByTagName() function is LIVE!

What I mean by LIVE is that as you traverse the array and invoke changes to the DOM, the items in the array itself can change automatically! Here's an example:

var inputEls = divEl.getElementsByTagName('input');
for (var j=0; j < inputEls.length; j++) {
alert('inputEls length='+inputEls.length);
var inputEl = inputEls[j];
inputEl.parentNode.removeChild(inputEl);
}

What you'll actually see is that the length of the array automatically decreases with each iteration because I am removing those elements from the DOM.

To work around this issue I had to traverse the array backwards:

for(var i=inputEls.length-1;i>=0; i--) {

}

Tuesday, August 4, 2009

Java TreeSet missing elements

If you notice that some elements may be missing after having added them to a TreeSet then it is probably because your implementation of Comparator or Comparable is incorrect.

This is the key note to remember:

"If two elements are different but compareTo returns 0, then TreeSet will consider them equal, and the newly added element replaces the old one".

Wednesday, July 22, 2009

HibernateSystemException: attempted to assign id from null one-to-one property

I had a bidirectional relationship in my one-to-one mapping, however I had forgotten to set the objects in my Java code to reference each other. Once I had done that, the problem was resolved.

SpermAnalysis sa = new SpermAnalysis();
cryoSession.getControlMouse().setSpermAnalysis(sa);
// Bidirectional relationship
sa.setMouse(cryoSession.getControlMouse());

Determine Mime type from bytes

If you have an image file stored in the database but don't have the extension of the file, then you may not know what the mime type should be.

There is a handy Java library that will inspect the bytes to determine the Mime type. The library can be found here:

MimeUtil

It's fairly easy to use. An example code is shown below:

byte[] bytes;
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");
MimeUtil.getMimeTypes(bytes);
MimeUtil.unregisterMimeDetector("eu.medsea.mimeutil.detector.MimeMagicMimeDetector");


Tuesday, April 14, 2009

Java Bean validation using reflection


public static boolean validateBeanFields(Object obj) {

if (obj == null)
return false;

Class objClass = obj.getClass();
Method[] methods = objClass.getMethods();
for (Method aMethod : methods) {
String methodName = aMethod.getName();
if (methodName.startsWith("get")) {
try {
Object retObj = aMethod.invoke(obj, null);

if (retObj == null)
return false;
else if (retObj instanceof Enum) // skip enum values
continue;
else if (retObj instanceof String) {
if (((String)retObj).length() == 0)
return false;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
return true;
}