Search in sources :

Example 1 with PCAScatterPlotDialog

use of uk.ac.babraham.SeqMonk.Displays.PCAPlot.PCAScatterPlotDialog in project SeqMonk by s-andrews.

the class TsneDataStoreResult method runTsne.

private void runTsne() {
    File tempDir;
    try {
        pd.progressUpdated("Creating temp directory", 0, 1);
        tempDir = TempDirectory.createTempDirectory();
        // System.err.println("Temp dir is "+tempDir.getAbsolutePath());
        pd.progressUpdated("Writing R script", 0, 1);
        // Get the template script
        Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Displays/TsneDataStorePlot/tsne_template.r"));
        // Substitute in the variables we need to change
        template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
        template.setValue("ITERATIONS", "" + iterations);
        template.setValue("PERPLEXITY", "" + perplexity);
        // Write the script file
        File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
        PrintWriter pr = new PrintWriter(scriptFile);
        pr.print(template.toString());
        pr.close();
        // Write the count data
        File countFile = new File(tempDir.getAbsoluteFile() + "/data.txt");
        pr = new PrintWriter(countFile);
        StringBuffer sb;
        pd.progressUpdated("Writing count data", 0, 1);
        for (int p = 0; p < usedProbes.length; p++) {
            sb = new StringBuffer();
            for (int d = 0; d < stores.length; d++) {
                if (d > 0)
                    sb.append("\t");
                sb.append(stores[d].getValueForProbe(usedProbes[p]));
            }
            pr.println(sb.toString());
        }
        pr.close();
        pd.progressUpdated("Running R Script", 0, 1);
        RScriptRunner runner = new RScriptRunner(tempDir);
        RProgressListener listener = new RProgressListener(runner);
        runner.addProgressListener(new ProgressRecordDialog("R Session", runner));
        runner.runScript();
        while (true) {
            if (listener.cancelled()) {
                pd.progressCancelled();
                return;
            }
            if (listener.exceptionReceived()) {
                pd.progressExceptionReceived(listener.exception());
                return;
            }
            if (listener.complete())
                break;
            Thread.sleep(500);
        }
        // We can now parse and store the results
        // Tsne only ever returns 2 dimensions so we'll fake up a variances
        // result which says that we only have 2 components with 50% each
        variances = new float[] { 50, 50 };
        File tsneFile = new File(tempDir.getAbsolutePath() + "/tsne_data.txt");
        BufferedReader br = new BufferedReader(new FileReader(tsneFile));
        String line = br.readLine();
        pcaResults = new float[stores.length][variances.length];
        int storeIndex = 0;
        while ((line = br.readLine()) != null) {
            String[] sections = line.split("\t");
            for (int i = 0; i < variances.length; i++) {
                pcaResults[storeIndex][i] = Float.parseFloat(sections[i + 1]);
            }
            ++storeIndex;
        }
        br.close();
        // We don't get weights from Tsne
        runner.cleanUp();
    } catch (Exception e) {
        pd.progressExceptionReceived(e);
        return;
    }
    pd.progressComplete("tsne_analysis", this);
    new PCAScatterPlotDialog(this);
}
Also used : RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) IOException(java.io.IOException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter) PCAScatterPlotDialog(uk.ac.babraham.SeqMonk.Displays.PCAPlot.PCAScatterPlotDialog)

Aggregations

BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ProgressRecordDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog)1 PCAScatterPlotDialog (uk.ac.babraham.SeqMonk.Displays.PCAPlot.PCAScatterPlotDialog)1 RProgressListener (uk.ac.babraham.SeqMonk.R.RProgressListener)1 RScriptRunner (uk.ac.babraham.SeqMonk.R.RScriptRunner)1 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)1 Template (uk.ac.babraham.SeqMonk.Utilities.Templates.Template)1