Search in sources :

Example 1 with BioNetGenExecutable

use of cbit.vcell.solvers.BioNetGenExecutable in project vcell by virtualcell.

the class BNGExecutorServiceNative method executeBNG.

/**
 * Insert the method's description here.
 * Creation date: (6/23/2005 3:57:30 PM)
 */
@Override
public BNGOutput executeBNG() throws BNGException {
    if (executable != null) {
        throw new BNGException("You can only run BNG one at a time!");
    }
    BNGOutput bngOutput = null;
    startTime = System.currentTimeMillis();
    File workingDir = null;
    try {
        workingDir = Files.createTempDirectory("Bng_working_").toFile();
        File bngInputFile = null;
        FileOutputStream fos = null;
        String tempFilePrefix = workingDir.getName();
        try {
            bngInputFile = new File(workingDir, tempFilePrefix + suffix_input);
            fos = new java.io.FileOutputStream(bngInputFile);
        } catch (java.io.IOException e) {
            if (LG.isEnabledFor(Level.WARN)) {
                LG.warn("error opening input file '" + bngInputFile, e);
            }
            e.printStackTrace(System.out);
            throw new RuntimeException("error opening input file '" + bngInputFile.getName() + ": " + e.getMessage());
        }
        PrintWriter inputFile = new PrintWriter(fos);
        System.out.println("Input file is: " + bngInputFile.getPath());
        inputFile.print(bngInput.getInputString());
        inputFile.close();
        // System.out.println("BNGExecutorService.executeBNG(): input = \n"+bngInput.getInputString());
        String[] cmd = null;
        OperatingSystemInfo osi = OperatingSystemInfo.getInstance();
        if (osi.isWindows()) {
            System.out.println("BNGExecutorService.executeBNG() " + (osi.is64bit() ? "64bit" : "32bit") + " as standalone executable\n");
            cmd = new String[] { ResourceUtil.getBNG2StandaloneWin(osi.is64bit()), bngInputFile.getAbsolutePath() };
        } else {
            // Execute as perl script
            System.out.println("BNGExecutorService.executeBNG() as perl script\n");
            String bngPerlFilePath = ResourceUtil.getBNG2_perl_file();
            cmd = new String[] { ResourceUtil.getPerlExe().getAbsolutePath(), bngPerlFilePath, bngInputFile.getAbsolutePath() };
        }
        // run BNG
        long timeoutMS = 0;
        if (timeoutDurationMS != null) {
            timeoutMS = timeoutDurationMS.longValue();
        }
        executable = new BioNetGenExecutable(cmd, timeoutMS, workingDir);
        executable.inheritCallbacks(getCallbacks());
        int[] expectedReturnCodes = new int[] { 0 };
        executable.start(expectedReturnCodes);
        String stdoutString;
        if (executable.getStatus() != org.vcell.util.exe.ExecutableStatus.STOPPED) {
            stdoutString = executable.getStdoutString();
        } else {
            stdoutString = "Stopped by user. Output from BioNetGen may be truncated";
        }
        if (executable.getExitValue() == 1) {
            String stderrString = executable.getStderrString();
            if (stderrString.contains("run_network")) {
                stdoutString = "run_network not supported on this operating system; partial data may be available\n" + executable.getStdoutString();
            }
        }
        File[] files = workingDir.listFiles();
        ArrayList<String> filenames = new ArrayList<String>();
        ArrayList<String> filecontents = new ArrayList<String>();
        for (File file : files) {
            String filename = file.getName();
            if (LG.isDebugEnabled()) {
                LG.debug("BNG executor trying to read " + filename);
            }
            filenames.add(filename);
            filecontents.add(FileUtils.readFileToString(file));
        }
        bngOutput = new BNGOutput(stdoutString, filenames.toArray(new String[0]), filecontents.toArray(new String[0]));
    } catch (ExecutableException | IOException ex) {
        if (LG.isEnabledFor(Level.WARN)) {
            LG.warn("error executable BNG", ex);
        }
        if (executable == null || executable.getStderrString().trim().length() == 0) {
            throw new BNGException("Error executing BNG", ex);
        }
        throw new BNGException(executable.getStderrString(), ex);
    } finally {
        if (workingDir != null) {
            File[] files = workingDir.listFiles();
            for (File file : files) {
                file.delete();
            }
            workingDir.delete();
        }
    }
    return bngOutput;
}
Also used : ExecutableException(org.vcell.util.exe.ExecutableException) OperatingSystemInfo(cbit.vcell.resource.OperatingSystemInfo) ArrayList(java.util.ArrayList) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) BioNetGenExecutable(cbit.vcell.solvers.BioNetGenExecutable) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

OperatingSystemInfo (cbit.vcell.resource.OperatingSystemInfo)1 BioNetGenExecutable (cbit.vcell.solvers.BioNetGenExecutable)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 ExecutableException (org.vcell.util.exe.ExecutableException)1