Monday, October 19, 2009

CXF JAX-RS IllegalAnnotationException - Class has two properties of the same name

I am currently using Apache CXF to provide RESTful web services for the Australian PhenomeBank.

I was attempting to assign annotations of private field members of a class as follows:

    @XmlElement(name="mutationType")
    @XmlElementWrapper(name="mutationTypes")
    private Set<MutationType> mutationTypes  = EnumSet.noneOf(MutationType.class);

What I had forgot to do was assign the XmlAccessorType as follows:

@XmlRootElement(name="SearchCriteria")
@XmlAccessorType(XmlAccessType.FIELD)
public class SearchCriteria

Thursday, October 15, 2009

Ant OutOfMemoryException

Currently where I work we use Ant to deploy our application to a Tomcat server. Recently, I integrated several new libraries for Apache's CXF web services and reran my ant script to do the deployment and encountered this:

deploy:
[echo] Deploying to 150.203.7.14:8080 ...

BUILD FAILED
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61)

at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)


To resolve this I ended up setting the ANT_OPTS environment variable on my Windows machine as follows:

C:\workspace\PhenomeBank>set ANT_OPTS=-Xmx500m

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());