Accessing the MetNet API through R

Setup

The MetNet API can be accessed in R through the rJava package. For your convenience, summarized installation instructions are shown here, but if you run into trouble, please consult the original documentation.

The rJava package provides a gateway to from R to Java. It needs to be installed once. Each time you wish to use it, you need to indicate this through the library command. A seperate command is required to initialize the JVM. After that, you're ready to perform some simple tests on the lang.String class. This goes as follows:

Make sure the above instructions work and execute correctly before continuing.
Assuming you've copied both metnet3.jar and your MySQL JDBC driver to c:\, now enter the following commands:

Alternatively, if you start R from scratch and want to directly use the MetNet API, you enter the following sequence of instructions:

However, in some instances at least this didn't work on our test machine. If the direct approach doesn't work on your specific installation, the previous guidelines apply and should still allow you to get started.

Getting started

You can make sure a connection to the database is established by running the following command:

.jcall("edu/iastate/metnet/Pathway", "I", "count")

Once you've established that your database connection is good to go, you can start extracting some useful information. Let's start with another simple example. Suppose that you want to find out how many pathways are currently in the database:

.jcall("edu/iastate/metnet/Pathway", "I", "count")
.jcall("edu/iastate/metnet/Pathway", "I", "count", "Arabidopsis")

As you may have guessed, the first line will count how many pathways there are (total), the second line tells you how many of the pathways are specific for Arabidopsis. In true R fashion, you can use this in arithmetic operations, e.g. if you'd want to know how many non-Arabidopsis pathways there are:

.jcall("edu/iastate/metnet/Pathway", "I", "count") - .jcall("edu/iastate/metnet/Pathway", "I", "count", "Arabidopsis")

Let's get some information about ATP, which EntityID is 240:

ent=.jnew("edu/iastate/metnet/Entity", 240L)
.jfield(ent, , "name")

Not quite sure where to go from here? Try the .jmethods (or .jfields) command for reflection:

.jmethods(ent)

Some more examples

Let's see if ATP has any synonyms defined for it:

ent=.jnew("edu/iastate/metnet/Entity", 240L)
syns=ent$getSynonyms()
syns

Hmm, not very useful. What's a "Java-Object{[Ljava.lang.String;@7d8483}"? The solution is to give rJava a little more information as to what return type to expect. In this case, we tell it to expect an array of strings by specifying "[S":

syns=.jcall(ent, "[S", "getSynonyms")
syns

Success! This time we see that syns is converted into an R vector and contains 5 synonyms.

Entities are defined within a context. This context usually refers to a subcellular compartment such as cytosol, nucleus or endoplasmatic reticulum. In order to find out in which locations we may encounter ATP, run the following:

contexts=.jcall(ent, "Ledu/iastate/metnet/util/ContextVector;", "getContexts")
.jcall(contexts, "I", "size")
for (i in 1:.jcall(contexts, "I", "size")) {
	ctx = .jcall(contexts, "Ledu/iastate/metnet/Context;", "get", i)
	print(.jfield(ctx,,"cellLocation"))
}

Don't forget the ; in instructions such as .jcall(contexts, "Ledu/iastate/metnet/Context;", "get", i)! If you forget it, the command will not work.

Let's say that you have a LocusID for a gene (e.g. AT2G23800) and wish to find out what pathways are linked to it:

mygene = .jcall("edu/iastate/metnet/Entity", "Ledu/iastate/metnet/Entity;", "identify", "AT2G23800")
pathways = .jcall(mygene, "Ledu/iastate/metnet/util/PathwayVector;", "getPathways")
for (i in 1:.jcall(pathways, "I", "size")) {
	pw = .jcall(pathways, "Ledu/iastate/metnet/Pathway;", "get", i)
	print (.jfield(pw, ,"name"))
}

You can use the API to search our database as well. How about finding all the versions of the glycolysis pathway:

# find all versions of the glycolysis pathway
pathways = .jcall("edu/iastate/metnet/Pathway", "Ledu/iastate/metnet/util/PathwayVector;", "search", "glycolysis")
# how many pathways did we find?
sz = .jcall(pathways, "I", "size")

Now let's export each pathway to an individual SBML file:

# loop over all the pathways and export them to SBML files
for (i in 0:(sz-1)) {
	# get each individual pathway from the Vector
	pw = .jcall(pathways, "Ledu/iastate/metnet/Pathway;", "get", i)
	# generate a filename: glysolysis1.sbml, glysolysis2.sbml etc...
	fn = paste("c:/glycolysis",i,".sbml", sep="")
	# make a new Network object
	nw = .jnew("edu/iastate/metnet/Network")
	# attach the pathway to the network
	.jcall(nw, "V", "attachPathway", pw)
	# ask the network-object to export the contained pathway to SBML
	res = .jcall(nw, "Z", "toSbml", fn)
	# res is a boolean variable that indicates if the operation was successful
}

You can open these sbml-files in other software for further processing and analysis, e.g. CellDesigner:

You now have the basics of how to interface from R to the Java MetNet API (or any other Java package for that matter). So go ahead, spend some time familiarizing yourself with the environment. If you have any questions or suggestions on how to improve this page, leave us a note. Please understand however, that our focus is on further developing the Java API itself, and that we can only spend so much time on supporting additional platforms. If you have questions about specific API calls, refer to the Java documentation instead.