| Version 3 (modified by leo, 6 months ago) |
|---|
Lens: Object-Tree Mapping Utility
Lens is a utility for mapping between Java object and tree-structured data (e.g., XML, JSON, Silk, etc.). Our Lens utility automatically detects public fileds in your class definition, and maps data souces to the object instances.
Download
Download xerial-core-(version)-starndalone.jar library, then append it to your class path.
An Quick Example (XML to Objects)
Supporse you have the following XML data (saved as gene.xml) describing a gene object:
<gene id="1"> <name>gene1</name> <chr>chr1</chr> <start>1000</start> <end>4000</end> <strand>+</strand> </gene>
To parse this XML data, you have to describe a Gene class with public fields corresponding to the XML elements:
class Gene
{
public int id;
public String name;
public String chr;
public String strand;
public long start;
public long end;
// public default constructor
public Gene() {}
}
By using Lens.loadXML(target class, input reader) method, you can create an instance of the Gene object from the XML file (gene.xml):
BufferedReader xmlReader = new BufferedReader(new FileReader("gene.xml"));
Gene gene = Lens.loadXML(Gene.class, xmlReader);
// gene.id == 1
// gene.name == "gene1"
// gene.chr == "chr1"
// gene.start == 1000
// gene.end == 4000
Lens automatically converts string data appeared in the XML file into appropriate data type (integer, String, etc. ) by seeing field or setter methods in the class definition.
Mapping Database Query Results to Java Objects
When your data is stored in an SQLite database, you have to use SQL queries to retreive data.
Table Data
id|target|start|end|strand| 0|chrX|70518318|70518344|+| 1|chr12|51949963|51949989|-| 2|chr6|48444655|48444681|-| 3|chr13|109111185|109111211|-| 4|chr5|133731340|133731366|-|
Sample Code
// Invoke some SQL query using a JDBC connection
ResultSet rs = conn.executeQuery(String.format("select * from gene where start >= %d order by start", start));
// execute an SQL query, then bind the result rows to Gene object
List<Gene> result = Lens.loadJDBCResultSet(Gene.class, rs);
// draw the gene objects to some image
for(Gene eachGene : result)
{
// ... do some drawings
}
Mapping to Nested Objects
- genelist.xml
<genelist> <description>gene list</description> <gene id="1"> <name>gene1</name> <chr>chr1</chr> <start>1000</start> <end>4000</end> <strand>+</strand> </gene> <gene id="2"> <name>gene1</name> <chr>chr4</chr> <start>3000</start> <end>8000</end> <strand>-</strand> </gene> </genelist>
class GeneList
{
public List<Gene> gene;
public String description;
}
BufferedReader xmlReader = new BufferedReader(new FileReader("genelist.xml"));
GeneList geneList = Lens.loadXML(GeneList.class, xmlReader);
// Output object as JSON
System.out.println(Lens.toJSON(geneList));
// Output object as Silk
System.out.println(Lens.toSilk(geneList));


