Search in sources :

Example 1 with RProgressListener

use of uk.ac.babraham.SeqMonk.R.RProgressListener in project SeqMonk by s-andrews.

the class SeqMonkInformationPanel method actionPerformed.

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("set_temp_dir")) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Select a Cache Directory");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setTempDirectory(chooser.getSelectedFile());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("set_genomes_dir")) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Select a Genomes Directory");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setGenomeBase(chooser.getSelectedFile());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("set_r")) {
        // First try to autodetect the location
        String autoDetect = RVersionTest.autoDetectRLocation();
        if (autoDetect != null) {
            int answer = JOptionPane.showConfirmDialog(this, "Found an R installation at '" + autoDetect + "' use this?", "R detected", JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                SeqMonkPreferences.getInstance().setRLocation(autoDetect);
                try {
                    SeqMonkPreferences.getInstance().savePreferences();
                    populatePanel();
                    return;
                } catch (IOException ioe) {
                    throw new IllegalStateException(ioe);
                }
            }
        }
        // If we can't auto-detect, or if they don't want to use that, then let them
        // choose where R is.
        JOptionPane.showMessageDialog(this, "Couldn't auto-detect an R installation.  Please manually select the location of the R executable");
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Find R executable");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            SeqMonkPreferences.getInstance().setRLocation(chooser.getSelectedFile().getAbsolutePath());
            try {
                SeqMonkPreferences.getInstance().savePreferences();
                populatePanel();
            } catch (IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        }
    } else if (e.getActionCommand().equals("install_r")) {
        // R sucks
        // 
        // If the user has R installed as an admin user, but they're running as a normal
        // user then a non-interactive session (such as the ones we use here), won't
        // put up a prompt to create a local library.
        // 
        // To get around this we can create the local library folder within the R script,
        // which is great, except that R checks for the existence of this library at
        // the start of the session, so making it during the session still doesn't allow
        // us to install anything.  We therefore have to run a completely separate script
        // just to make the directory
        // Do the local library script first
        // Now do the actual package install
        Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    File tempDir = TempDirectory.createTempDirectory();
                    // Get the template script
                    Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/create_local_library.r"));
                    // Write the script file
                    File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
                    PrintWriter pr = new PrintWriter(scriptFile);
                    pr.print(template.toString());
                    pr.close();
                    RScriptRunner runner = new RScriptRunner(tempDir);
                    RProgressListener listener = new RProgressListener(runner);
                    runner.addProgressListener(new ProgressRecordDialog("R Session", runner));
                    runner.runScript();
                    while (true) {
                        if (listener.cancelled()) {
                            return;
                        }
                        if (listener.exceptionReceived()) {
                            return;
                        }
                        if (listener.complete())
                            break;
                        Thread.sleep(500);
                    }
                    runner.cleanUp();
                    File tempDir2 = TempDirectory.createTempDirectory();
                    // Get the template script
                    Template template2 = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/load_required_modules.r"));
                    // Write the script file
                    File scriptFile2 = new File(tempDir2.getAbsoluteFile() + "/script.r");
                    PrintWriter pr2 = new PrintWriter(scriptFile2);
                    pr2.print(template2.toString());
                    pr2.close();
                    RScriptRunner runner2 = new RScriptRunner(tempDir2);
                    RProgressListener listener2 = new RProgressListener(runner2);
                    runner2.addProgressListener(new ProgressRecordDialog("R Session", runner2));
                    runner2.runScript();
                    while (true) {
                        if (listener2.cancelled()) {
                            return;
                        }
                        if (listener2.exceptionReceived()) {
                            return;
                        }
                        if (listener2.complete())
                            break;
                        Thread.sleep(500);
                    }
                    runner2.cleanUp();
                    populatePanel();
                } catch (Exception ex) {
                    throw new IllegalStateException(ex);
                }
            }
        });
        t.start();
    } else if (e.getActionCommand().equals("clean_cache")) {
        Thread t = new Thread(new Runnable() {

            public void run() {
                ProgressDialog pd = new ProgressDialog("Cleaning the cache");
                File[] tempFiles = SeqMonkPreferences.getInstance().tempDirectory().listFiles();
                for (int f = 0; f < tempFiles.length; f++) {
                    pd.progressUpdated("Deleting file " + (f + 1) + " out of " + tempFiles.length, f, tempFiles.length);
                    if (Calendar.getInstance().getTimeInMillis() - tempFiles[f].lastModified() < 1000L * 60 * 60 * 12) {
                        // We only deal with things which are at least 12 hours old
                        continue;
                    }
                    // This might be a simple temp data file
                    if (tempFiles[f].isFile() && tempFiles[f].getName().startsWith("seqmonk") && tempFiles[f].getName().endsWith(".temp")) {
                        tempFiles[f].delete();
                    } else // This might be a temp directory
                    if (tempFiles[f].isDirectory() && tempFiles[f].getName().startsWith("seqmonk") && tempFiles[f].getName().contains("temp")) {
                        File[] files = tempFiles[f].listFiles();
                        for (int g = 0; g < files.length; g++) {
                            if (!files[g].delete()) {
                                throw new IllegalStateException(new IOException("Failed to delete " + files[g].getAbsolutePath()));
                            }
                        }
                        // Now remove the directory
                        tempFiles[f].delete();
                    }
                }
                pd.progressComplete("cache_clean", null);
                populatePanel();
            }
        });
        t.start();
    } else if (e.getActionCommand().equals("update_genomes")) {
        updateGenomesButton.setEnabled(false);
        GenomeUpgrader upgrader = new GenomeUpgrader();
        upgrader.addProgressListener(new ProgressDialog("Updating installed genomes"));
        upgrader.addProgressListener(new ProgressListener() {

            public void progressWarningReceived(Exception e) {
            }

            public void progressUpdated(String message, int current, int max) {
            }

            public void progressExceptionReceived(Exception e) {
            }

            public void progressComplete(String command, Object result) {
                updateGenomesButton.setVisible(false);
                updates = null;
                genomeUpdateLabel.setIcon(tickIcon);
                genomeUpdateLabelText.setText("All of your installed genomes are up to date");
            }

            public void progressCancelled() {
            }
        });
        upgrader.upgradeGenomes(updates);
    } else {
        throw new IllegalArgumentException("Didn't understand action '" + e.getActionCommand() + "'");
    }
}
Also used : GenomeUpgrader(uk.ac.babraham.SeqMonk.Network.GenomeUpgrader) IOException(java.io.IOException) ProgressDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressDialog.ProgressDialog) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) JFileChooser(javax.swing.JFileChooser) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) ProgressListener(uk.ac.babraham.SeqMonk.DataTypes.ProgressListener) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 2 with RProgressListener

use of uk.ac.babraham.SeqMonk.R.RProgressListener in project SeqMonk by s-andrews.

the class LimmaFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    // We need to make a temporary directory, save the data into it, write out the R script
    // and then run it an collect the list of results, then clean up.
    // Make up the list of DataStores in each replicate set
    DataStore[] fromStores = replicateSets[0].dataStores();
    DataStore[] toStores = replicateSets[1].dataStores();
    File tempDir;
    try {
        progressUpdated("Creating temp directory", 0, 1);
        tempDir = TempDirectory.createTempDirectory();
        // System.err.println("Temp dir is "+tempDir.getAbsolutePath());
        progressUpdated("Writing R script", 0, 1);
        // Get the template script
        Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Filters/LimmaFilter/limma_template.r"));
        // Substitute in the variables we need to change
        template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
        // Say which p value column we're filtering on
        if (multiTest) {
            template.setValue("CORRECTED", "adj.P.Val");
        } else {
            template.setValue("CORRECTED", "P.Value");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fromStores.length; i++) {
            if (i > 0)
                sb.append(",");
            sb.append("\"from\"");
        }
        for (int i = 0; i < toStores.length; i++) {
            sb.append(",");
            sb.append("\"to\"");
        }
        template.setValue("CONDITIONS", sb.toString());
        template.setValue("PVALUE", "" + cutoff);
        // 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() + "/counts.txt");
        pr = new PrintWriter(countFile);
        sb = new StringBuffer();
        sb.append("probe");
        for (int i = 0; i < fromStores.length; i++) {
            sb.append("\t");
            sb.append("from");
            sb.append(i);
        }
        for (int i = 0; i < toStores.length; i++) {
            sb.append("\t");
            sb.append("to");
            sb.append(i);
        }
        pr.println(sb.toString());
        progressUpdated("Writing count data", 0, 1);
        Probe[] probes = startingList.getAllProbes();
        float value;
        for (int p = 0; p < probes.length; p++) {
            if (p % 1000 == 0) {
                progressUpdated("Writing count data", p, probes.length);
            }
            sb = new StringBuffer();
            sb.append(p);
            for (int i = 0; i < fromStores.length; i++) {
                sb.append("\t");
                value = fromStores[i].getValueForProbe(probes[p]);
                sb.append(value);
            }
            for (int i = 0; i < toStores.length; i++) {
                sb.append("\t");
                value = toStores[i].getValueForProbe(probes[p]);
                sb.append(value);
            }
            pr.println(sb.toString());
        }
        pr.close();
        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()) {
                progressCancelled();
                return;
            }
            if (listener.exceptionReceived()) {
                progressExceptionReceived(listener.exception());
                return;
            }
            if (listener.complete())
                break;
            Thread.sleep(500);
        }
        // We can now parse the results and put the hits into a new probe list
        ProbeList newList;
        newList = new ProbeList(startingList, "", "", "FDR");
        File hitsFile = new File(tempDir.getAbsolutePath() + "/hits.txt");
        BufferedReader br = new BufferedReader(new FileReader(hitsFile));
        String line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] sections = line.split("\t");
            int probeIndex = Integer.parseInt(sections[0]);
            float pValue;
            if (multiTest) {
                pValue = Float.parseFloat(sections[sections.length - 2]);
            } else {
                pValue = Float.parseFloat(sections[sections.length - 3]);
            }
            newList.addProbe(probes[probeIndex], pValue);
        }
        br.close();
        runner.cleanUp();
        // TODO: Show log of R session?
        filterFinished(newList);
    } catch (Exception ioe) {
        progressExceptionReceived(ioe);
        return;
    }
// filterFinished(newList);
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 3 with RProgressListener

use of uk.ac.babraham.SeqMonk.R.RProgressListener in project SeqMonk by s-andrews.

the class LogisticRegressionFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    // We need to make a temporary directory, save the data into it, write out the R script
    // and then run it an collect the list of results, then clean up.
    // Make up the list of DataStores in each replicate set
    DataStore[] fromStores = replicateSets[0].dataStores();
    DataStore[] toStores = replicateSets[1].dataStores();
    if (minObservations < 3)
        minObservations = 3;
    File tempDir;
    try {
        Probe[] probes = startingList.getAllProbes();
        if (resample) {
            // We need to check that the data stores are quantitated
            for (int i = 0; i < fromStores.length; i++) {
                if (!fromStores[i].isQuantitated()) {
                    progressExceptionReceived(new SeqMonkException("Data Store " + fromStores[i].name() + " wasn't quantitated"));
                    return;
                }
                for (int p = 0; p < probes.length; p++) {
                    float value = fromStores[i].getValueForProbe(probes[p]);
                    if ((!Float.isNaN(value)) && (value < 0 || value > 100)) {
                        progressExceptionReceived(new SeqMonkException("Data Store " + fromStores[i].name() + " had a value outside the range 0-100 (" + value + ")"));
                        return;
                    }
                }
            }
            for (int i = 0; i < toStores.length; i++) {
                if (!toStores[i].isQuantitated()) {
                    progressExceptionReceived(new SeqMonkException("Data Store " + toStores[i].name() + " wasn't quantitated"));
                    return;
                }
                for (int p = 0; p < probes.length; p++) {
                    float value = toStores[i].getValueForProbe(probes[p]);
                    if ((!Float.isNaN(value)) && (value < 0 || value > 100)) {
                        progressExceptionReceived(new SeqMonkException("Data Store " + toStores[i].name() + " had a value outside the range 0-100 (" + value + ")"));
                        return;
                    }
                }
            }
        }
        progressUpdated("Creating temp directory", 0, 1);
        tempDir = TempDirectory.createTempDirectory();
        System.err.println("Temp dir is " + tempDir.getAbsolutePath());
        progressUpdated("Writing R script", 0, 1);
        // Get the template script
        Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Filters/LogisticRegressionFilter/logistic_regression_template.r"));
        // Write the count data
        // Sort these so we can get probes from the same chromosome together
        Arrays.sort(probes);
        PrintWriter pr = null;
        String lastChr = "";
        // Rather than not testing probes based on their absolute difference
        // we should just post-filter them.  The easiest way to do this will
        // be to not test (as we do now) but explicity pass in the number of
        // tests we should have performed to the multiple testing correction.
        int numberOfTestsToCorrectBy = 0;
        PROBE: for (int p = 0; p < probes.length; p++) {
            if (!probes[p].chromosome().name().equals(lastChr)) {
                if (pr != null)
                    pr.close();
                File outFile = new File(tempDir.getAbsoluteFile() + "/data_chr" + probes[p].chromosome().name() + ".txt");
                pr = new PrintWriter(outFile);
                lastChr = probes[p].chromosome().name();
                pr.println("id\tgroup\treplicate\tstate\tcount");
            }
            if (p % 1000 == 0) {
                progressUpdated("Writing data for chr" + lastChr, p, probes.length);
            }
            int[] fromMethCounts = new int[fromStores.length];
            int[] fromUnmethCounts = new int[fromStores.length];
            int[] toMethCounts = new int[toStores.length];
            int[] toUnmethCounts = new int[toStores.length];
            for (int i = 0; i < fromStores.length; i++) {
                long[] reads = fromStores[i].getReadsForProbe(probes[p]);
                int totalCount = 0;
                int methCount = 0;
                if (resample) {
                    float value = fromStores[i].getValueForProbe(probes[p]);
                    if (Float.isNaN(value)) {
                        continue PROBE;
                    }
                    totalCount = reads.length;
                    methCount = Math.round((totalCount * value) / 100f);
                } else {
                    for (int r = 0; r < reads.length; r++) {
                        totalCount++;
                        if (SequenceRead.strand(reads[r]) == Location.FORWARD) {
                            ++methCount;
                        }
                    }
                }
                fromMethCounts[i] = methCount;
                fromUnmethCounts[i] = totalCount - methCount;
            }
            for (int i = 0; i < toStores.length; i++) {
                long[] reads = toStores[i].getReadsForProbe(probes[p]);
                int totalCount = 0;
                int methCount = 0;
                if (resample) {
                    float value = toStores[i].getValueForProbe(probes[p]);
                    if (Float.isNaN(value)) {
                        continue PROBE;
                    }
                    totalCount = reads.length;
                    methCount = Math.round((totalCount * value) / 100f);
                } else {
                    for (int r = 0; r < reads.length; r++) {
                        totalCount++;
                        if (SequenceRead.strand(reads[r]) == Location.FORWARD) {
                            ++methCount;
                        }
                    }
                }
                toMethCounts[i] = methCount;
                toUnmethCounts[i] = totalCount - methCount;
            }
            // Check to see we meet the requirements for the min amount of information
            // and the min diff.
            int totalFromMeth = 0;
            int totalFrom = 0;
            int totalToMeth = 0;
            int totalTo = 0;
            int validFrom = 0;
            for (int i = 0; i < fromStores.length; i++) {
                totalFromMeth += fromMethCounts[i];
                totalFrom += fromMethCounts[i];
                totalFrom += fromUnmethCounts[i];
                if (fromMethCounts[i] + fromUnmethCounts[i] >= minObservations) {
                    ++validFrom;
                }
            }
            int validTo = 0;
            for (int i = 0; i < toStores.length; i++) {
                totalToMeth += toMethCounts[i];
                totalTo += toMethCounts[i];
                totalTo += toUnmethCounts[i];
                if (toMethCounts[i] + toUnmethCounts[i] >= minObservations) {
                    ++validTo;
                }
            }
            // have enough data in all stores to go ahead and do the test.
            if (validFrom < fromStores.length || validTo < toStores.length) {
                // We don't have enough data to measure this one
                continue;
            }
            // At this point we have to count this probe as valid for the
            // purposes of multiple testing correction
            ++numberOfTestsToCorrectBy;
            float[] fromPercentages = new float[validFrom];
            float[] toPercentages = new float[validTo];
            int lastFromIndex = 0;
            int lastToIndex = 0;
            for (int i = 0; i < fromMethCounts.length; i++) {
                if (fromMethCounts[i] + fromUnmethCounts[i] == 0)
                    continue;
                fromPercentages[lastFromIndex] = fromMethCounts[i] * 100f / (fromMethCounts[i] + fromUnmethCounts[i]);
                ++lastFromIndex;
            }
            for (int i = 0; i < toMethCounts.length; i++) {
                if (toMethCounts[i] + toUnmethCounts[i] == 0)
                    continue;
                toPercentages[lastToIndex] = toMethCounts[i] * 100f / (toMethCounts[i] + toUnmethCounts[i]);
                ++lastToIndex;
            }
            for (int i = 0; i < fromMethCounts.length; i++) {
                pr.println(p + "\tfrom\tfrom" + i + "\tmeth\t" + fromMethCounts[i]);
                pr.println(p + "\tfrom\tfrom" + i + "\tunmeth\t" + fromUnmethCounts[i]);
            }
            for (int i = 0; i < toMethCounts.length; i++) {
                pr.println(p + "\tto\tto" + i + "\tmeth\t" + toMethCounts[i]);
                pr.println(p + "\tto\tto" + i + "\tunmeth\t" + toUnmethCounts[i]);
            }
        }
        pr.close();
        // Sanity check to make sure we have something to work with.
        if (numberOfTestsToCorrectBy == 0) {
            progressExceptionReceived(new IllegalStateException("No probes had enough data to test."));
        }
        // Now we can complete the template
        // Substitute in the variables we need to change
        template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
        template.setValue("CORRECTCOUNT", "" + numberOfTestsToCorrectBy);
        template.setValue("PVALUE", "" + pValueCutoff);
        if (multiTest) {
            template.setValue("MULTITEST", "TRUE");
        } else {
            template.setValue("MULTITEST", "FALSE");
        }
        // Write the script file
        File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
        pr = new PrintWriter(scriptFile);
        pr.print(template.toString());
        pr.close();
        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()) {
                progressCancelled();
                pr.close();
                return;
            }
            if (listener.exceptionReceived()) {
                progressExceptionReceived(new SeqMonkException("R Script failed"));
                pr.close();
                return;
            }
            if (listener.complete())
                break;
            Thread.sleep(500);
        }
        // We can now parse the results and put the hits into a new probe list
        ProbeList newList;
        if (multiTest) {
            newList = new ProbeList(startingList, "", "", "FDR");
        } else {
            newList = new ProbeList(startingList, "", "", "p-value");
        }
        File hitsFile = new File(tempDir.getAbsolutePath() + "/hits.txt");
        BufferedReader br = new BufferedReader(new FileReader(hitsFile));
        String line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] sections = line.split("\t");
            String[] indexSections = sections[0].split("\\.");
            int probeIndex = Integer.parseInt(indexSections[indexSections.length - 1]);
            float pValue = Float.parseFloat(sections[sections.length - 1]);
            newList.addProbe(probes[probeIndex], pValue);
        }
        br.close();
        runner.cleanUp();
        filterFinished(newList);
    } catch (Exception ioe) {
        progressExceptionReceived(ioe);
        return;
    }
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 4 with RProgressListener

use of uk.ac.babraham.SeqMonk.R.RProgressListener in project SeqMonk by s-andrews.

the class DESeqFilter method generateProbeList.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.Filters.ProbeFilter#generateProbeList()
	 */
@Override
protected void generateProbeList() {
    // We need to make a temporary directory, save the data into it, write out the R script
    // and then run it an collect the list of results, then clean up.
    // Make up the list of DataStores in each replicate set
    DataStore[][] storeGroups = new DataStore[replicateSets.length][];
    for (int r = 0; r < replicateSets.length; r++) {
        storeGroups[r] = replicateSets[r].dataStores();
    }
    File tempDir;
    try {
        progressUpdated("Creating temp directory", 0, 1);
        tempDir = TempDirectory.createTempDirectory();
        // System.err.println("Temp dir is "+tempDir.getAbsolutePath());
        progressUpdated("Writing R script", 0, 1);
        // Get the template script
        Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Filters/DESeqFilter/deseq_template.r"));
        // Substitute in the variables we need to change
        template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
        // Say which p value column we're filtering on
        if (multiTest) {
            template.setValue("CORRECTED", "padj");
        } else {
            template.setValue("CORRECTED", "pvalue");
        }
        if (independentFiltering) {
            template.setValue("INDEPENDENT", "TRUE");
        } else {
            template.setValue("INDEPENDENT", "FALSE");
        }
        StringBuffer sb = new StringBuffer();
        for (int s = 0; s < storeGroups.length; s++) {
            for (int i = 0; i < storeGroups[s].length; i++) {
                if (!(s == 0 && i == 0)) {
                    sb.append(",");
                }
                sb.append("\"group" + s + "\"");
            }
        }
        template.setValue("CONDITIONS", sb.toString());
        template.setValue("PVALUE", "" + cutoff);
        // 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() + "/counts.txt");
        pr = new PrintWriter(countFile);
        sb = new StringBuffer();
        sb.append("probe");
        for (int s = 0; s < storeGroups.length; s++) {
            for (int i = 0; i < storeGroups[s].length; i++) {
                sb.append("\t");
                sb.append("group");
                sb.append(s);
                sb.append("_");
                sb.append(i);
            }
        }
        pr.println(sb.toString());
        progressUpdated("Writing count data", 0, 1);
        Probe[] probes = startingList.getAllProbes();
        float value;
        for (int p = 0; p < probes.length; p++) {
            if (p % 1000 == 0) {
                progressUpdated("Writing count data", p, probes.length);
            }
            sb = new StringBuffer();
            sb.append(p);
            for (int s = 0; s < storeGroups.length; s++) {
                for (int i = 0; i < storeGroups[s].length; i++) {
                    sb.append("\t");
                    value = storeGroups[s][i].getValueForProbe(probes[p]);
                    if (value != (int) value) {
                        progressExceptionReceived(new IllegalArgumentException("Inputs to the DESeq filter MUST be raw, incorrected counts, not things like " + value));
                        pr.close();
                        return;
                    }
                    sb.append(value);
                }
            }
            pr.println(sb.toString());
        }
        pr.close();
        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()) {
                progressCancelled();
                return;
            }
            if (listener.exceptionReceived()) {
                progressExceptionReceived(listener.exception());
                return;
            }
            if (listener.complete())
                break;
            Thread.sleep(500);
        }
        // We can now parse the results and put the hits into a new probe list
        ProbeList newList;
        newList = new ProbeList(startingList, "", "", "FDR");
        File hitsFile = new File(tempDir.getAbsolutePath() + "/hits.txt");
        BufferedReader br = new BufferedReader(new FileReader(hitsFile));
        String line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] sections = line.split("\t");
            int probeIndex = Integer.parseInt(sections[0]);
            float pValue = Float.parseFloat(sections[sections.length - 1]);
            newList.addProbe(probes[probeIndex], pValue);
        }
        br.close();
        runner.cleanUp();
        // TODO: Show log of R session?
        filterFinished(newList);
    } catch (Exception ioe) {
        progressExceptionReceived(ioe);
        return;
    }
// filterFinished(newList);
}
Also used : ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) RProgressListener(uk.ac.babraham.SeqMonk.R.RProgressListener) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Template(uk.ac.babraham.SeqMonk.Utilities.Templates.Template) ProgressRecordDialog(uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) RScriptRunner(uk.ac.babraham.SeqMonk.R.RScriptRunner) PrintWriter(java.io.PrintWriter)

Example 5 with RProgressListener

use of uk.ac.babraham.SeqMonk.R.RProgressListener 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

File (java.io.File)9 PrintWriter (java.io.PrintWriter)9 ProgressRecordDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog)9 RProgressListener (uk.ac.babraham.SeqMonk.R.RProgressListener)9 RScriptRunner (uk.ac.babraham.SeqMonk.R.RScriptRunner)9 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)9 Template (uk.ac.babraham.SeqMonk.Utilities.Templates.Template)9 BufferedReader (java.io.BufferedReader)8 FileReader (java.io.FileReader)8 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)6 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)6 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)6 IOException (java.io.IOException)3 FileNotFoundException (java.io.FileNotFoundException)1 HashMap (java.util.HashMap)1 JFileChooser (javax.swing.JFileChooser)1 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)1 ProgressDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressDialog.ProgressDialog)1 PCAScatterPlotDialog (uk.ac.babraham.SeqMonk.Displays.PCAPlot.PCAScatterPlotDialog)1 GenomeUpgrader (uk.ac.babraham.SeqMonk.Network.GenomeUpgrader)1