Question: Accessing Biomart Web Service Using Java
gravatar for Manjulapra
7.8 years ago by
Sri Lanka
Manjulapra70 wrote:

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.


public class accessDB {

    public static void saveDbData(String xmlName)throws Exception{

try {
    URL url = new URL("");

String fileContents=readFileAsString(xmlName);

URLConnection urlc = url.openConnection();



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


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

String inputLine;

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




catch (Exception e) {


        private static String readFileAsString(String filePath)
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while(( != -1){
            String readData = String.valueOf(buf, 0, numRead);
            buf = new char[1024];
        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 • 2.6k views
ADD COMMENTlink written 7.8 years ago by Manjulapra70

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

ADD REPLYlink written 7.8 years ago by Manjulapra70

can you show use this XML file please.

ADD REPLYlink written 7.8 years ago by Pierre Lindenbaum121k

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 REPLYlink written 7.8 years ago by Pierre Lindenbaum121k

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

ADD REPLYlink written 7.8 years ago by Jeremy Leipzig18k
gravatar for Joachim
7.8 years ago by
San Francisco, California
Joachim2.8k wrote:

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 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, 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" />

This query can be send to the server via:

curl -d@query.xml

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:!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 '!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 (, which already generates Java code for you.


ADD COMMENTlink written 7.8 years ago by Joachim2.8k

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

ADD REPLYlink written 7.8 years ago by Graslevy240

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 REPLYlink written 7.8 years ago by Manjulapra70
gravatar for Graslevy
7.8 years ago by
Graslevy240 wrote:

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 COMMENTlink written 7.8 years ago by Graslevy240

you don't need to tell the URLConnection about the method:

ADD REPLYlink written 7.8 years ago by Pierre Lindenbaum121k

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

ADD REPLYlink written 7.8 years ago by Graslevy240

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

ADD REPLYlink written 7.8 years ago by Graslevy240
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1564 users visited in the last hour