Search in sources :

Example 1 with GenomeUpgrader

use of uk.ac.babraham.SeqMonk.Network.GenomeUpgrader 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)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 JFileChooser (javax.swing.JFileChooser)1 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)1 ProgressDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressDialog.ProgressDialog)1 ProgressRecordDialog (uk.ac.babraham.SeqMonk.Dialogs.ProgressRecordDialog)1 GenomeUpgrader (uk.ac.babraham.SeqMonk.Network.GenomeUpgrader)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