001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package demo;
022
023import org.biojava.nbio.core.sequence.io.util.IOUtils;
024import org.biojava.nbio.ws.alignment.qblast.BlastProgramEnum;
025import org.biojava.nbio.ws.alignment.qblast.NCBIQBlastAlignmentProperties;
026import org.biojava.nbio.ws.alignment.qblast.NCBIQBlastOutputProperties;
027import org.biojava.nbio.ws.alignment.qblast.NCBIQBlastService;
028
029import java.io.*;
030
031import static org.biojava.nbio.ws.alignment.qblast.BlastAlignmentParameterEnum.ENTREZ_QUERY;
032
033/**
034 * A simple demo showing {@link NCBIQBlastService} usage
035 *
036 * @author Gediminas Rimsa
037 */
038public class NCBIQBlastServiceDemo {
039        private static final String BLAST_OUTPUT_FILE = "blastOutput.xml";
040
041        private static final String SEQUENCE = "MKWVTFISLLFLFSSAYSRGVFRRDAHKSEVAHRFKDLGEENFKALVLIAFAQYLQQCPFEDHVKLVNEVTEFAKTCVADESAENCDKS";
042
043        public static void main(String[] args) {
044                NCBIQBlastService service = null;
045        if (args.length == 1) {
046            service = new NCBIQBlastService(args[0]);
047        } else {
048            service = new NCBIQBlastService();
049        }
050
051                // set alignment options
052                NCBIQBlastAlignmentProperties props = new NCBIQBlastAlignmentProperties();
053                props.setBlastProgram(BlastProgramEnum.blastp);
054                props.setBlastDatabase("swissprot");
055                props.setAlignmentOption(ENTREZ_QUERY, "\"serum albumin\"[Protein name] AND mammals[Organism]");
056
057                // set output options
058                NCBIQBlastOutputProperties outputProps = new NCBIQBlastOutputProperties();
059
060                // Example of two possible ways of setting output options (in this case, it was already set by constructor)
061//              outputProps.setAlignmentNumber(100);
062//              outputProps.setOutputOption(BlastOutputParameterEnum.ALIGNMENTS, "100");
063
064                String rid = null;
065                FileWriter writer = null;
066                BufferedReader reader = null;
067                try {
068                        // send blast request and save request id
069                        rid = service.sendAlignmentRequest(SEQUENCE, props);
070
071                        while (!service.isReady(rid)) {
072                                System.out.println("Waiting for results. Sleeping for 5 seconds");
073                                Thread.sleep(5000);
074                        }
075
076                        // read results when they are ready
077                        InputStream in = service.getAlignmentResults(rid, outputProps);
078                        reader = new BufferedReader(new InputStreamReader(in));
079
080                        File f = new File(BLAST_OUTPUT_FILE);
081                        System.out.println("Saving query results in file " + f.getAbsolutePath());
082                        writer = new FileWriter(f);
083
084                        String line;
085                        while ((line = reader.readLine()) != null) {
086                                writer.write(line + System.getProperty("line.separator"));
087                        }
088                } catch (Exception e) {
089                        System.out.println(e.getMessage());
090                        e.printStackTrace();
091                } finally {
092                        IOUtils.close(writer);
093                        IOUtils.close(reader);
094                        service.sendDeleteRequest(rid);
095                }
096        }
097}