bioLockJ.module.agent.RScriptAgent.java Source code

Java tutorial

Introduction

Here is the source code for bioLockJ.module.agent.RScriptAgent.java

Source

/**
 * @UNCC Fodor Lab
 * @author Michael Sioda
 * @email msioda@uncc.edu
 * @date Feb 18, 2017
 * @disclaimer    This code is free software; you can redistribute it and/or
 *             modify it under the terms of the GNU General Public License
 *             as published by the Free Software Foundation; either version 2
 *             of the License, or (at your option) any later version,
 *             provided that any use properly credits the author.
 *             This program is distributed in the hope that it will be useful,
 *             but WITHOUT ANY WARRANTY; without even the implied warranty of
 *             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *             GNU General Public License for more details at http://www.gnu.org *
 */
package bioLockJ.module.agent;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.apache.commons.io.FileUtils;
import bioLockJ.AppController;
import bioLockJ.Config;
import bioLockJ.Constants;
import bioLockJ.Log;
import bioLockJ.Module;
import bioLockJ.util.MetadataUtil;
import bioLockJ.util.r.RScript;
import bioLockJ.util.r.RScriptUtil;

/**
 * This class builds the R script.
 */
public class RScriptAgent extends Module {

    /**
     * Set the y-label based on if logNormal or not.
     */
    @Override
    public void checkDependencies() throws Exception {
        Config.requireString(Config.REPORT_LOG_BASE);
        Config.requireExistingFile(Config.INPUT_METADATA);
        Config.requireString(EXE_RSCRIPT);
        Config.requirePositiveInteger(R_TIMEOUT);
        RScriptUtil.checkDependencies();
    }

    /**
     * Build the R script.
     */
    @Override
    public void executeProjectFile() throws Exception {
        initializeObjects();
        RScriptUtil.buildScript(this, true, true);
        prepDownload();
    }

    /**
     * Run EXE_RSCRIPT command on the R Script
     */
    @Override
    public String[] executeScriptCommand() throws Exception {
        final String[] cmd = new String[2];
        cmd[0] = Config.requireString(EXE_RSCRIPT);
        cmd[1] = getMainScript().getAbsolutePath();
        Log.out.info("Executing Script: " + getMainScript().getName());
        return cmd;
    }

    /**
     * Get the name of the rScript from the prop file.
     */
    @Override
    public File getMainScript() throws Exception {
        if (rScript == null) {
            rScript = new File(getScriptDir().getAbsolutePath() + File.separator + R_SCRIPT_NAME);
            addScriptFile(rScript);
        }
        return rScript;
    }

    /**
     * The R Script should run quickly, so far timeout = 10 minutes appears to work well.
     */
    @Override
    public int getScriptTimeout() throws Exception {
        return Config.requirePositiveInteger(R_TIMEOUT);
    }

    protected void prepDownload() throws Exception {
        final String downloadDir = getDownloadDir();
        if (downloadDir == null) {
            return;
        }

        final File newScript = new File(getOutputDir().getAbsolutePath() + File.separator + rScript.getName());
        final BufferedReader reader = new BufferedReader(new FileReader(rScript));
        final BufferedWriter writer = new BufferedWriter(new FileWriter(newScript));
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.startsWith(RScript.OUTPUT_DIR + " =")) {
                line = RScript.OUTPUT_DIR + " = \"" + downloadDir + LOCAL + File.separator + "\"";
            }
            if (line.startsWith(SCRIPT_DIR + " =")) {
                line = SCRIPT_DIR + " = \"" + downloadDir + "\"";
            }
            if (line.startsWith(RScript.INPUT_DIR + " =")) {
                line = RScript.INPUT_DIR + " = \"" + downloadDir + TABLES + File.separator + "\"";
            }
            writer.write(line + Constants.RETURN);
        }

        reader.close();
        writer.close();

        final String localPath = getOutputDir().getAbsolutePath() + File.separator + LOCAL;
        File f = new File(localPath + File.separator + RScript.getDataDirName());
        f.mkdirs();
        f = new File(localPath + File.separator + RScript.getGraphicsDirName());
        f.mkdirs();
        f = new File(localPath + File.separator + RScript.getLogDataDirName());
        f.mkdirs();
        f = new File(localPath + File.separator + RScript.getLogGraphicsDirName());
        f.mkdirs();
        f = new File(localPath + File.separator + RScript.R_LOG_DIR);
        f.mkdirs();

        final File tableDir = new File(getOutputDir().getAbsolutePath() + File.separator + TABLES);
        tableDir.mkdirs();
        FileUtils.copyDirectory(getInputDir(), tableDir);
    }

    private void initializeObjects() throws Exception {

        File f = new File(getOutputDir().getAbsolutePath() + File.separator + RScript.getDataDirName());
        if (!f.exists()) {
            f.mkdirs();
        }

        f = new File(getOutputDir().getAbsolutePath() + File.separator + RScript.getGraphicsDirName());
        if (!f.exists()) {
            f.mkdirs();
        }

        f = new File(getOutputDir().getAbsolutePath() + File.separator + RScript.getLogDataDirName());
        if (!f.exists()) {
            f.mkdirs();
        }

        f = new File(getOutputDir().getAbsolutePath() + File.separator + RScript.getLogGraphicsDirName());
        if (!f.exists()) {
            f.mkdirs();
        }

        if (Config.getBoolean(RScript.R_DEBUG)) {
            f = new File(getOutputDir().getAbsolutePath() + File.separator + RScript.R_LOG_DIR);
            if (!f.exists()) {
                f.mkdirs();
            }
        }

        if (Config.getBoolean(Config.REPORT_NUM_READS)) {
            if (MetadataUtil.getAttributeNames().contains(Constants.NUM_READS)) {
                MetadataUtil.addNumericField(Constants.NUM_READS);
            } else {
                Log.out.warn("Unable to report " + Constants.NUM_READS + " - value not found in metadata: "
                        + MetadataUtil.getMetadata().getAbsolutePath());
            }
        }

        if (Config.getBoolean(Config.REPORT_NUM_HITS)) {
            if (MetadataUtil.getAttributeNames().contains(Constants.NUM_HITS)) {
                MetadataUtil.addNumericField(Constants.NUM_HITS);
            } else {
                Log.out.warn("Unable to report " + Constants.NUM_HITS + " - value not found in metadata: "
                        + MetadataUtil.getMetadata().getAbsolutePath());
            }
        }

    }

    public static File getLogSummaryStatsDir() throws Exception {
        final File f = new File(
                AppController.getRequiredModule(RScriptAgent.class.getName()).getOutputDir().getAbsolutePath()
                        + File.separator + RScript.getLogDataDirName());

        if (!f.exists()) {
            throw new Exception("Directory not found: " + f.getAbsolutePath());
        }
        return f;
    }

    public static File getSummaryStatsDir() throws Exception {

        final File f = new File(
                AppController.getRequiredModule(RScriptAgent.class.getName()).getOutputDir().getAbsolutePath()
                        + File.separator + RScript.getDataDirName());

        if (!f.exists()) {
            throw new Exception("Directory not found: " + f.getAbsolutePath());
        }
        return f;
    }

    private File rScript = null;

    private static final String EXE_RSCRIPT = "exe.rScript";
    private static final String R_SCRIPT_NAME = "BioLockJ.R";
    private static final String R_TIMEOUT = "r.timeout";
    private static final String LOCAL = "local";
    private static final String SCRIPT_DIR = "scriptDir";
    private static final String TABLES = "tables";

}