How To Perform A Blast Search From A Java Application?
5
4
Entering edit mode
13.5 years ago
Niklas ▴ 60

Hello everyone,

I would like to perform a Blast Search form within a Java application, i.e. I want to submit the query and evaluate the results. I have stumbled upon several jars on the web, but none of them was well documented.

I would love something like a nice tutorial or at least a decent documentation.

Any help is appreciated!

Regards Niklas

EDIT: Ok, i should have said this from the beginning: The whole thing runs on google's appengine, so I can't use any external programs or processes.

Re-EDIT: I ended up using the NCBI Blast URL API (http://blast.ncbi.nlm.nih.gov/blast_overview.shtml#access). It works very well and has a very good documentation.

java biojava blast • 13k views
ADD COMMENT
2
Entering edit mode

Just read about the Google App Engine. This is not the right environment for your requirements. You need to adapt either your requirements (aka running Blast vs. using some Java alignment library) or get a "real server". It's not so hard, so I recommend the latter.

ADD REPLY
0
Entering edit mode

what do you cant to do ? do you want to call an external WebService for BLAST from your app ? Beware of the quotas.

ADD REPLY
0
Entering edit mode

yeah I guess thats what I want to do... is there something like that?

ADD REPLY
5
Entering edit mode
13.5 years ago

First, use ${JAVA_HOME}/bin/XJC to generate some java stubs for BLAST-XML in the directory src from the blast DTD (new package is named 'blast')

xjc -d src -p blast -dtd "http://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.dtd"

you can then use ProcessBuilder to send a query to blast and use javax.xml.bind.JAXBContext and javax.xml.bind.Unmarshaller to parse the XML result without any external library.

package blast;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;



public class CallBlast
    {
    private String blastAllPath="/path/ncbi/build/blastall";
    private String blastDB="/path2b/testDB";
    private blast.BlastOutput run(String name,String sequence) throws IOException
        {
        File fasta=File.createTempFile("blast", ".fa");
        File blast=File.createTempFile("blast", ".xml");
        try
            {
            PrintWriter out=new PrintWriter(fasta);
            out.println(">"+name);
            out.println(sequence.replaceAll("[0-9 \t\n\r]",""));
            out.flush();
            out.close();


            ProcessBuilder pb = new ProcessBuilder(
                this.blastAllPath,
                "-p","blastn",
                "-d",blastDB,
                "-m","7",
                "-i",fasta.getAbsolutePath(),
                "-o",blast.getAbsolutePath()
                );
            Process proc = pb.start();
            if(proc.waitFor()!=0) throw new RuntimeException("error occured");
            JAXBContext jc = JAXBContext.newInstance("blast");

            Unmarshaller u = jc.createUnmarshaller();
            u.setSchema(null);



            /** read the result */

             return  (blast.BlastOutput)u.unmarshal(blast);

            }
        catch(Exception err)
            {
            throw new RuntimeException(err);
            }
        finally
            {
            blast.delete();
            fasta.delete();
            }
        }
    public static void main(String[] args)
        {
        try {
            CallBlast app=new CallBlast();
            blast.BlastOutput blast=app.run("MYSequence","ACAATACCTCCACCGCCATGCCTTTAAAATTTTACTTCTTCCGCCAAGCTCCTCCCC");
            BlastOutputIterations iterations=blast.getBlastOutputIterations();
            for(Iteration iteration:iterations.getIteration())
                {
                IterationHits hits=iteration.getIterationHits();
                for(Hit hit:hits.getHit())
                    {
                    System.out.println("def:"+hit.getHitDef());
                    System.out.println("len:"+hit.getHitLen());
                    System.out.println();
                    }
                }
            }
        catch (Exception e)
            {
            e.printStackTrace();
            }
        }
    }
ADD COMMENT
3
Entering edit mode
13.5 years ago
Michael 54k

Edit:

Yes, maybe web-services is another option, SOA is the future anyway. You can find BLAST web-services using BioCatalogue. I recommend the EBI SOAP services. You can connect to them using pure Java. Here is the query.

Edit: full documentation here.

If you only have only few requests, this might work. Otherwise setting up a cloud computing machine for blast might be another option, but I doubt this will be easy.

If you have to run the process from Java use ProcessBuilder, you have full control:

List<String> args = new ArrayList<String>();
args.add("blast"); args.add(" your best blast args! ");
ProcessBuilder builder = new ProcessBuilder(args);
try {
   Process p = builder.start();
} catch (IOException e) {
// do something
}

Use BioJava to parse the result. The documentation is maybe a bit dated and many people call biojava names, but that's possibly as good as it gets...

ADD COMMENT
0
Entering edit mode

Hey, thanks for the quick answer. One problem though: the whole thing runs on Googles App Engine, which AFAIK does not allow Processes.... So the best thing I can do would be something like: Send Request, actively wait for result, evaluate result...

ADD REPLY
0
Entering edit mode

puh.... no idea about that but I think it would be odd if it wouldn't.

ADD REPLY
0
Entering edit mode

thanks, I will give those web services a try!

ADD REPLY
1
Entering edit mode
10.6 years ago

If you want to roll your own BLAST webservice, I recently wrote a lightweight wrapper for BLAST/BLAST+ in nodejs:

https://npmjs.org/package/blast-wrapper

ADD COMMENT
0
Entering edit mode
11.9 years ago

From experience, these web robots seem slow, complex to program and always stop working after website redesigns. It seems simpler to run BLAST locally, calling it from Java as an external process and capturing the output (the output can be in XML so is easy to parse). If the task is to do many parallel independent searches (so one per thread = one per CPU core), a good desktop machine with six core i7 processor runs at comparable speed. Or, if you already think that the future is the Cloud, why not to run BLAST on the AWS machine?

ADD COMMENT
0
Entering edit mode
10.6 years ago
jockbanan ▴ 420

There is a nice tutorial focused exactly on this topic (using BioJava and NCBI blast URLAPI) here:

http://alextblog.blogspot.cz/2012/05/ncbi-blast-jaxb-biojava-blasting-like.html

I tried this approach before and it worked perfectly.

ADD COMMENT

Login before adding your answer.

Traffic: 2703 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