import charite.christo.interfaces.SequenceBlaster;
import charite.christo.interfaces.HasControlPanel;
import charite.christo.strap.extensions.Blaster_ebi_ac_uk;
import charite.christo.strap.extensions.Blaster_local_Wu;
import charite.christo.strap.extensions.Blaster_local_NCBI;
import charite.christo.ByteArray;
import charite.christo.blast.BlastResult;
import java.util.*;
import javax.swing.*;
import java.io.*;
import static java.lang.System.*;
import charite.christo.CacheResult;

/**
   java DemoSequenceBlaster

   This demo shows how Blast is used to find sequences that are similar
   to a given Sequence.  For performing large numbers of Blast runs
   please use the locally installed Blast rather than a web service.

*/
public class DemoSequenceBlaster {
    public static void main(String[] argv) throws Exception {

        /* The cache is located in ~/.StrapAlign/cached/ */
        CacheResult.setEnabled(true);

        /* Initialize a SequenceBlaster one of the following:
           Blaster_ebi_ac_uk, Blaster_local_Wu, Blaster_local_NCBI */

        final SequenceBlaster blaster=new Blaster_ebi_ac_uk();

        /*For a local blast installation an environment variable is pointing to
          the directory with databases.  */
        if (blaster instanceof Blaster_local_NCBI) out.println(" Variable BLASTDB="+System.getenv("BLASTDB"));
        if (blaster instanceof Blaster_local_Wu) out.println(" Variable WUBLASTDB="+System.getenv("WUBLASTDB"));

        blaster.setAAQuerySequence("KVFFKAGLLGLLEEMRDDKLAEIITATQARCRGFLM");

        out.print("The following databases are available: "+Arrays.asList(blaster.getAvailableDatabases()));

        /* Show A control panel to watch log messages */
        if (blaster instanceof HasControlPanel) {
            final JFrame f=new JFrame();
            f.setSize(300,300);
            f.getContentPane().add(((HasControlPanel)blaster).getControlPanel());
            f.show();
        }
        blaster.setDatabase("pdb");

        /* If this was already computed, then it might be in the cache. */
        final ByteArray xmlFromCache=blaster.getResultXml();
        final ByteArray xml;

        if (xmlFromCache==null) {
            /* computation takes a lot of time */
            blaster.compute();
            xml=blaster.getResultXml();
        } else xml=xmlFromCache;

        try {
            xml.write(new FileOutputStream(new File("output.xml")));
            out.println("The xml result is found in the file output.xml");
        } catch(IOException ioex) { out.println("Writing blast result: "+ioex); }

        /* BlastResult is an object oriented model of the Blast Result  */
        final BlastResult result=new BlastResult(xml);

        try {
            final int charactersPerLine=60;
            result.writeFile(new File("output.txt"), charactersPerLine);
            out.println("The result is found in the file output.txt");
        } catch(IOException ioex) { out.println("Writing blast result: "+ioex); }

        /* The cache is saved to disk and the  Java Virtual Machine terminated */
        /* Save the Cache containing computed results */
        CacheResult.save();
        System.exit(0);
    }
}