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();
}
}
}
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.
what do you cant to do ? do you want to call an external WebService for BLAST from your app ? Beware of the quotas.
yeah I guess thats what I want to do... is there something like that?