Accessing Biomart Web Service Using Java
2
2
Entering edit mode
12.5 years ago
Manjulapra ▴ 70

Hi All, I am newbie in using Biomart. I want to access the COSMIC Biomart using the Martservices and Java. The documentation says that I should POST the XML file to there link and that is it. I tried with the following code but it returns nothing.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;



public class accessDB {

    public static void saveDbData(String xmlName)throws Exception{


try {
    URL url = new URL("http://www.biomart.org/martservice");

String fileContents=readFileAsString(xmlName);

URLConnection urlc = url.openConnection();

urlc.setDoOutput(true);

urlc.setDoInput(true);

PrintWriter pw = new PrintWriter(urlc.getOutputStream());
pw.write(fileContents);

pw.close();

BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);

in.close();

}

catch (Exception e) {


}
    }

        private static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
    }

When I change the pw and input a the file buffer without a string it gives a general information page. Can somebody help me please. What is missing here?

Thank you

biomart webservice java • 4.0k views
ADD COMMENT
0
Entering edit mode

Oh, About the XML file, it's just a simple file saved using XML button in the Biomart Web Interface.

ADD REPLY
0
Entering edit mode

can you show use this XML file please.

ADD REPLY
0
Entering edit mode

you don't need to 'new char[1024];' for each loop; flush your ouputstream, don't ignore the exception, you don't need this PrintWriter (write the bytes in the output sream), etc...

ADD REPLY
0
Entering edit mode

you should put a main() method in your code so people can actually test it

ADD REPLY
6
Entering edit mode
12.5 years ago
Joachim ★ 2.9k

As far as I can tell it seems that the URL is wrong to start with. The correct base URL for an 0.7 mart would be http://www.biomart.org/biomart/martservice/result where a ?query=... parameter determines the actual query (... is the XML text).

For example:

I can get an XML query text as shown below from www.biomart.org, which I then prefix with the string "query=".

query=<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Query>
<Query  virtualSchemaName = "default" formatter = "TSV" header = "0" uniqueRows = "0" count = "" datasetConfigVersion = "0.6" >         
    <Dataset name = "hsapiens_gene_ensembl" interface = "default" >
        <Filter name = "chromosome_name" value = "1"/>
        <Filter name = "with_illumina_humanwg_6_v3" excluded = "0"/>
        <Attribute name = "ensembl_gene_id" />
        <Attribute name = "ensembl_transcript_id" />
    </Dataset>
</Query>

This query can be send to the server via:

curl -d@query.xml http://www.biomart.org/biomart/martservice/result

The server then responds by sending you TSV output as:

ENSG00000200913 ENST00000364043
ENSG00000202031 ENST00000365161
ENSG00000207421 ENST00000384690
ENSG00000206680 ENST00000383953
...

Now, coming back to your question, the complete URL query string would be as follows:

http://www.biomart.org/biomart/martservice/result?query=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20Query%3E%0A%3CQuery%20%20virtualSchemaName%20%3D%20%22default%22%20formatter%20%3D%20%22TSV%22%20header%20%3D%20%220%22%20uniqueRows%20%3D%20%220%22%20count%20%3D%20%22%22%20datasetConfigVersion%20%3D%20%220.6%22%20%3E%0A%0A%20%20%20%20%20%20%20%20%3CDataset%20name%20%3D%20%22hsapiens_gene_ensembl%22%20interface%20%3D%20%22default%22%20%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFilter%20name%20%3D%20%22chromosome_name%22%20value%20%3D%20%221%22%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFilter%20name%20%3D%20%22with_illumina_humanwg_6_v3%22%20excluded%20%3D%20%220%22%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CAttribute%20name%20%3D%20%22ensembl_gene_id%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CAttribute%20name%20%3D%20%22ensembl_transcript_id%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FDataset%3E%0A%3C%2FQuery%3E

You can test this getting this exact URL with curl:

curl 'http://www.biomart.org/biomart/martservice/result?query=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20Query%3E%0A%3CQuery%20%20virtualSchemaName%20%3D%20%22default%22%20formatter%20%3D%20%22TSV%22%20header%20%3D%20%220%22%20uniqueRows%20%3D%20%220%22%20count%20%3D%20%22%22%20datasetConfigVersion%20%3D%20%220.6%22%20%3E%0A%0A%20%20%20%20%20%20%20%20%3CDataset%20name%20%3D%20%22hsapiens_gene_ensembl%22%20interface%20%3D%20%22default%22%20%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFilter%20name%20%3D%20%22chromosome_name%22%20value%20%3D%20%221%22%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CFilter%20name%20%3D%20%22with_illumina_humanwg_6_v3%22%20excluded%20%3D%20%220%22%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CAttribute%20name%20%3D%20%22ensembl_gene_id%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CAttribute%20name%20%3D%20%22ensembl_transcript_id%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FDataset%3E%0A%3C%2FQuery%3E'

All that besides, it might be easier though to use the new BioMart 0.8 Central Portal (http://central.biomart.org), which already generates Java code for you.

Joachim

ADD COMMENT
0
Entering edit mode

+1 for the full query string as stated in the documentation.

ADD REPLY
0
Entering edit mode

Thank you all, I am working on the information given, and thank you for telling that java code is available in Central. BTW, where should we download the libraries? Thank you again

ADD REPLY
0
Entering edit mode
12.5 years ago
Graslevy ▴ 240

One thing jumps out already; you are not declaring your http method. You must explicitly send a 'POST' request for the server to respond.

ADD COMMENT
0
Entering edit mode

you don't need to tell the URLConnection about the method: http://www.exampledepot.com/egs/java.net/Post.html

ADD REPLY
0
Entering edit mode

The setRequestMethod("POST") is necessary I am afraid.

ADD REPLY
0
Entering edit mode

Haven't tested the code yet but I assume the setRequestMethod("POST") is essential. I stand corrected otherwise...

ADD REPLY

Login before adding your answer.

Traffic: 2555 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6