Example usage for java.lang Process destroy

List of usage examples for java.lang Process destroy

Introduction

In this page you can find the example usage for java.lang Process destroy.

Prototype

public abstract void destroy();

Source Link

Document

Kills the process.

Usage

From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java

/**
 * <pre>This will execute a command (with args). Under normal operation, 
 * if the exit code of the command is not zero, an exception will be thrown.
 * If the parameter exceptionOnExitValueNeZero is set to true, the 
 * results of the call will be returned regardless of the exit status.
 * Example call: execCommand(new String[]{"/bin/bash", "-c", "cd /someFolder; runSomeScript.sh"}, true);
 * </pre>/*from ww w  .ja  v a2s  .  com*/
 * @param arguments are the commands
 * @param exceptionOnExitValueNeZero if this is set to false, the 
 * results of the call will be returned regardless of the exit status
 * @return the output text of the command, and the error and return code if exceptionOnExitValueNeZero is false.
 */
public static CommandResult execCommand(String[] arguments, boolean exceptionOnExitValueNeZero) {
    Process process = null;

    StringBuilder commandBuilder = new StringBuilder();
    for (int i = 0; i < arguments.length; i++) {
        commandBuilder.append(arguments[i]).append(" ");
    }
    String command = commandBuilder.toString();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Running command: " + command);
    }
    StreamGobbler<Object> outputGobbler = null;
    StreamGobbler<Object> errorGobbler = null;
    try {
        process = Runtime.getRuntime().exec(arguments);

        outputGobbler = new StreamGobbler<Object>(process.getInputStream(), ".out", command);
        errorGobbler = new StreamGobbler<Object>(process.getErrorStream(), ".err", command);

        Future<Object> futureOutput = retrieveExecutorService().submit(outputGobbler);
        Future<Object> futureError = retrieveExecutorService().submit(errorGobbler);

        try {
            process.waitFor();
        } finally {

            //finish running these threads
            try {
                futureOutput.get();
            } finally {
                //ignore if cant get
            }
            try {
                futureError.get();
            } finally {
                //ignore if cant get
            }
        }
    } catch (Exception e) {
        LOG.error("Error running command: " + command, e);
        throw new RuntimeException("Error running command", e);
    } finally {
        try {
            process.destroy();
        } catch (Exception e) {
        }
    }

    //was not successful???
    if (process.exitValue() != 0 && exceptionOnExitValueNeZero) {
        String message = "Process exit status=" + process.exitValue() + ": out: "
                + (outputGobbler == null ? null : outputGobbler.getResultString()) + ", err: "
                + (errorGobbler == null ? null : errorGobbler.getResultString());
        LOG.error(message + ", on command: " + command);
        throw new RuntimeException(message);
    }

    int exitValue = process.exitValue();
    return new CommandResult(outputGobbler.getResultString(), errorGobbler.getResultString(), exitValue);
}

From source file:de.huberlin.wbi.cuneiform.core.cre.LocalThread.java

@Override
public void run() {

    Path scriptFile, location, successMarker, reportFile, callLocation, stdErrFile, stdOutFile;
    // Path lockMarker;
    Process process;
    int exitValue;
    Set<JsonReportEntry> report;
    JsonReportEntry entry;/*from w  ww.  jav a 2  s.c o m*/
    String line;
    StringBuffer buf;
    Path srcPath, destPath;
    ProcessBuilder processBuilder;
    Ticket ticket;
    String script, stdOut, stdErr;
    long tic, toc;
    JSONObject obj;
    Message msg;
    Charset cs;
    int trial;
    boolean suc;
    Exception ex;

    if (log.isDebugEnabled())
        log.debug("Starting up local thread for ticket " + invoc.getTicketId() + ".");

    if (invoc == null)
        throw new NullPointerException("Invocation must not be null.");

    ticket = invoc.getTicket();
    process = null;
    stdOut = null;
    stdErr = null;
    // lockMarker = null;
    script = null;
    successMarker = null;
    cs = Charset.forName("UTF-8");

    try {

        callLocation = Paths.get(System.getProperty("user.dir"));
        location = buildDir.resolve(String.valueOf(invoc.getTicketId()));
        // lockMarker = location.resolve( Invocation.LOCK_FILENAME );
        successMarker = location.resolve(Invocation.SUCCESS_FILENAME);
        reportFile = location.resolve(Invocation.REPORT_FILENAME);
        script = invoc.toScript();

        // if( Files.exists( lockMarker ) )
        //   throw new IOException( "Lock held on ticket "+invoc.getTicketId() );

        if (!Files.exists(successMarker)) {

            deleteIfExists(location);
            Files.createDirectories(location);

            // Files.createFile( lockMarker );

            scriptFile = invoc.getExecutablePath(location);

            Files.createFile(scriptFile,
                    PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-x---")));

            // write executable script
            try (BufferedWriter writer = Files.newBufferedWriter(scriptFile, cs, StandardOpenOption.CREATE)) {
                writer.write(script);
            }

            // write executable log entry
            try (BufferedWriter writer = Files.newBufferedWriter(reportFile, cs, StandardOpenOption.CREATE)) {
                writer.write(ticket.getExecutableLogEntry().toString());
                writer.write('\n');
            }

            for (String filename : invoc.getStageInList()) {

                if (filename.charAt(0) == '/')
                    throw new UnsupportedOperationException("Absolute path encountered '" + filename + "'.");

                srcPath = centralRepo.resolve(filename);
                destPath = location.resolve(filename);

                if (!Files.exists(srcPath)) {

                    srcPath = callLocation.resolve(filename);
                    if (log.isTraceEnabled())
                        log.trace("Resolving relative path '" + srcPath + "'.");
                } else

                if (log.isTraceEnabled())
                    log.trace("Resolving path to central repository '" + srcPath + "'.");

                if (log.isTraceEnabled())
                    log.trace("Trying to create symbolic link from '" + srcPath + "' to '" + destPath + "'.");

                if (!Files.exists(destPath.getParent()))
                    Files.createDirectories(destPath.getParent());

                Files.createSymbolicLink(destPath, srcPath);

            }

            // run script
            processBuilder = new ProcessBuilder(invoc.getCmd());
            processBuilder.directory(location.toFile());

            stdOutFile = location.resolve(Invocation.STDOUT_FILENAME);
            stdErrFile = location.resolve(Invocation.STDERR_FILENAME);

            processBuilder.redirectOutput(stdOutFile.toFile());
            processBuilder.redirectError(stdErrFile.toFile());

            trial = 1;
            suc = false;
            ex = null;
            tic = System.currentTimeMillis();
            do {
                try {
                    process = processBuilder.start();

                    suc = true;
                } catch (IOException e) {

                    ex = e;
                    if (log.isWarnEnabled())
                        log.warn("Unable to start process on trial " + (trial++) + " Waiting " + WAIT_INTERVAL
                                + "ms: " + e.getMessage());
                    Thread.sleep(WAIT_INTERVAL);
                }
            } while (suc == false && trial <= MAX_TRIALS);

            if (process == null) {

                ticketSrc.sendMsg(new TicketFailedMsg(cre, ticket, ex, script, null, null));
                // Files.delete( lockMarker );
                return;
            }

            exitValue = process.waitFor();
            toc = System.currentTimeMillis();

            try (BufferedWriter writer = Files.newBufferedWriter(reportFile, cs, StandardOpenOption.APPEND)) {

                obj = new JSONObject();
                obj.put(JsonReportEntry.LABEL_REALTIME, toc - tic);
                entry = invoc.createJsonReportEntry(tic, JsonReportEntry.KEY_INVOC_TIME, obj);

                writer.write(entry.toString());
                writer.write('\n');

                try (BufferedReader reader = Files.newBufferedReader(stdOutFile, cs)) {

                    buf = new StringBuffer();
                    while ((line = reader.readLine()) != null)
                        buf.append(line).append('\n');

                    stdOut = buf.toString();

                    if (!stdOut.isEmpty()) {
                        entry = invoc.createJsonReportEntry(JsonReportEntry.KEY_INVOC_STDOUT, stdOut);
                        writer.write(entry.toString());
                        writer.write('\n');
                    }
                }

                try (BufferedReader reader = Files.newBufferedReader(stdErrFile, cs)) {

                    buf = new StringBuffer();
                    while ((line = reader.readLine()) != null)
                        buf.append(line).append('\n');

                    stdErr = buf.toString();
                    if (!stdErr.isEmpty()) {

                        entry = invoc.createJsonReportEntry(JsonReportEntry.KEY_INVOC_STDERR, stdErr);
                        writer.write(entry.toString());
                        writer.write('\n');
                    }
                }

                if (exitValue == 0)

                    Files.createFile(successMarker);

                else {

                    ticketSrc.sendMsg(new TicketFailedMsg(cre, ticket, null, script, stdOut, stdErr));
                    // Files.delete( lockMarker );
                    return;

                }
            }

        }

        // gather report
        report = new HashSet<>();
        try (BufferedReader reader = Files.newBufferedReader(reportFile, cs)) {

            while ((line = reader.readLine()) != null) {

                line = line.trim();

                if (line.isEmpty())
                    continue;

                entry = new JsonReportEntry(line);

                // If the report comes from the hard cache then the run id
                // is different from the run id of this invocation. This is
                // corrected here.
                entry.setRunId(invoc.getRunId());

                report.add(entry);
            }

        }

        invoc.evalReport(report);

        // create link in central data repository
        for (String f : invoc.getStageOutList()) {

            srcPath = location.resolve(f);
            destPath = centralRepo.resolve(f);

            if (Files.exists(destPath))
                continue;

            if (log.isTraceEnabled())
                log.trace("Creating link from " + srcPath + " to " + destPath + ".");

            Files.createSymbolicLink(destPath, srcPath);
        }

        ticketSrc.sendMsg(new TicketFinishedMsg(cre, invoc.getTicket(), report));

        if (log.isTraceEnabled())
            log.trace("Local thread ran through without exception.");

        // Files.deleteIfExists( lockMarker );

    } catch (InterruptedException e) {

        if (log.isTraceEnabled())
            log.trace("Local thread has been interrupted.");
    } catch (Exception e) {

        if (log.isTraceEnabled())
            log.trace("Something went wrong. Deleting success marker if present.");

        if (successMarker != null)
            try {
                Files.deleteIfExists(successMarker);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        msg = new TicketFailedMsg(cre, ticket, e, script, stdOut, stdErr);

        ticketSrc.sendMsg(msg);

    } finally {

        if (process != null) {

            if (log.isDebugEnabled())
                log.debug("Stopping local thread for ticket " + invoc.getTicketId() + ".");

            process.destroy();
        }
    }
}

From source file:gov.pnnl.goss.gridappsd.simulation.SimulationManagerImpl.java

/**
 * This method is called by Process Manager to start a simulation
 * @param simulationId//www.j  a  va2s  .co  m
 * @param simulationFile
 */
@Override
public void startSimulation(int simulationId, File simulationFile, SimulationConfig simulationConfig) {

    try {
        logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                new Date().getTime(), "Starting simulation " + simulationId, LogLevel.INFO,
                ProcessStatus.STARTING, true), GridAppsDConstants.username);
    } catch (Exception e2) {
        log.warn("Error while reporting status " + e2.getMessage());
    }

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {

            Process gridlabdProcess = null;
            Process fncsProcess = null;
            Process fncsBridgeProcess = null;
            Process vvoAppProcess = null;
            InitializedTracker isInitialized = new InitializedTracker();
            try {

                File defaultLogDir = simulationFile.getParentFile();

                //Start FNCS
                //TODO, verify no errors on this
                String broker_location = "tcp://*:5570";
                if (simulationConfig != null && simulationConfig.model_creation_config != null
                        && simulationConfig.model_creation_config.schedule_name != null
                        && simulationConfig.model_creation_config.schedule_name.trim().length() > 0) {
                    broker_location = "tcp://" + simulationConfig.getSimulation_broker_location() + ":"
                            + String.valueOf(simulationConfig.getSimulation_broker_port());
                    File serviceDir = serviceManager.getServiceConfigDirectory();
                    //copy zipload_schedule.player file
                    try {
                        RunCommandLine.runCommand("cp " + serviceDir.getAbsolutePath() + File.separator + "etc"
                                + File.separator + "zipload_schedule.player "
                                + simulationFile.getParentFile().getAbsolutePath() + File.separator
                                + simulationConfig.model_creation_config.schedule_name + ".player");
                    } catch (Exception e) {
                        log.warn("Could not copy player file to working directory");
                    }
                }

                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(), "Calling " + getPath(GridAppsDConstants.FNCS_PATH) + " 2",
                        LogLevel.INFO, ProcessStatus.STARTING, true), GridAppsDConstants.username);

                ProcessBuilder fncsBuilder = new ProcessBuilder(getPath(GridAppsDConstants.FNCS_PATH), "2");
                fncsBuilder.redirectErrorStream(true);
                fncsBuilder.redirectOutput(
                        new File(defaultLogDir.getAbsolutePath() + File.separator + "fncs.log"));
                Map<String, String> fncsEnvironment = fncsBuilder.environment();
                fncsEnvironment.put("FNCS_BROKER", broker_location);
                fncsProcess = fncsBuilder.start();
                // Watch the process
                watch(fncsProcess, "FNCS");
                //TODO: check if FNCS is started correctly and send publish simulation status accordingly

                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(), "FNCS Co-Simulator started", LogLevel.INFO, ProcessStatus.RUNNING,
                        true), GridAppsDConstants.username);

                //client.publish(GridAppsDConstants.topic_simulationStatus+simulationId, "FNCS Co-Simulator started");

                //Start GridLAB-D
                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(),
                        "Calling " + getPath(GridAppsDConstants.GRIDLABD_PATH) + " " + simulationFile,
                        LogLevel.INFO, ProcessStatus.RUNNING, true), GridAppsDConstants.username);
                ProcessBuilder gridlabDBuilder = new ProcessBuilder(getPath(GridAppsDConstants.GRIDLABD_PATH),
                        simulationFile.getAbsolutePath());
                gridlabDBuilder.redirectErrorStream(true);
                gridlabDBuilder.redirectOutput(
                        new File(defaultLogDir.getAbsolutePath() + File.separator + "gridlabd.log"));
                //launch from directory containing simulation files
                gridlabDBuilder.directory(simulationFile.getParentFile());
                gridlabdProcess = gridlabDBuilder.start();
                // Watch the process
                watch(gridlabdProcess, "GridLABD");

                //TODO: check if GridLAB-D is started correctly and send publish simulation status accordingly

                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(), "GridLAB-D started", LogLevel.INFO, ProcessStatus.RUNNING, true),
                        GridAppsDConstants.username);

                //Start VVO Application
                //TODO filname really should be constant
                String vvoInputFile = simulationFile.getParentFile().getAbsolutePath() + File.separator
                        + "vvo_inputs.json";
                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(),
                        "Calling " + "python " + getPath(GridAppsDConstants.VVO_APP_PATH) + " " + simulationId
                                + " " + vvoInputFile,
                        LogLevel.INFO, ProcessStatus.RUNNING, true), GridAppsDConstants.username);
                ProcessBuilder vvoAppBuilder = new ProcessBuilder("python",
                        getPath(GridAppsDConstants.VVO_APP_PATH), "-f", vvoInputFile, "" + simulationId);
                vvoAppBuilder.redirectErrorStream(true);
                vvoAppBuilder.redirectOutput(
                        new File(defaultLogDir.getAbsolutePath() + File.separator + "vvo_app.log"));
                vvoAppProcess = vvoAppBuilder.start();
                // Watch the process
                watch(vvoAppProcess, "VVO Application");

                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(), "FNCS-GOSS Bridge started", LogLevel.INFO, ProcessStatus.RUNNING,
                        true), GridAppsDConstants.username);

                //Start GOSS-FNCS Bridge
                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(),
                        "Calling " + "python " + getPath(GridAppsDConstants.FNCS_BRIDGE_PATH) + " "
                                + simulationConfig.getSimulation_name(),
                        LogLevel.INFO, ProcessStatus.RUNNING, true), GridAppsDConstants.username);

                ProcessBuilder fncsBridgeBuilder = new ProcessBuilder("python",
                        getPath(GridAppsDConstants.FNCS_BRIDGE_PATH), simulationConfig.getSimulation_name(),
                        broker_location);
                fncsBridgeBuilder.redirectErrorStream(true);
                fncsBridgeBuilder.redirectOutput(
                        new File(defaultLogDir.getAbsolutePath() + File.separator + "fncs_goss_bridge.log"));
                fncsBridgeProcess = fncsBridgeBuilder.start();
                // Watch the process
                watch(fncsBridgeProcess, "FNCS GOSS Bridge");

                //TODO: check if bridge is started correctly and send publish simulation status accordingly

                logManager.log(new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                        new Date().getTime(), "FNCS-GOSS Bridge started", LogLevel.INFO, ProcessStatus.RUNNING,
                        true), GridAppsDConstants.username);

                //Subscribe to fncs-goss-bridge output topic
                client.subscribe(GridAppsDConstants.topic_FNCS_output,
                        new GossFncsResponseEvent(logManager, isInitialized, simulationId));

                int initAttempts = 0;
                while (!isInitialized.isInited && initAttempts < MAX_INIT_ATTEMPTS) {
                    //Send 'isInitialized' call to fncs-goss-bridge to check initialization until it is initialized.
                    //TODO add limiting how long it checks for initialized, or cancel if the fncs process exits
                    //This call would return true/false for initialization and simulation output of time step 0.
                    logManager.log(
                            new LogMessage(this.getClass().getName() + "-" + Integer.toString(simulationId),
                                    new Date().getTime(),
                                    "Checking fncs is initialized, currently " + isInitialized.isInited,
                                    LogLevel.INFO, ProcessStatus.RUNNING, true),
                            GridAppsDConstants.username);

                    client.publish(GridAppsDConstants.topic_FNCS_input, "{\"command\": \"isInitialized\"}");
                    initAttempts++;
                    Thread.sleep(1000);

                }

                if (initAttempts < MAX_INIT_ATTEMPTS) {
                    logManager.log(
                            new LogMessage(Integer.toString(simulationId), new Date().getTime(),
                                    "FNCS Initialized", LogLevel.INFO, ProcessStatus.RUNNING, true),
                            GridAppsDConstants.username);

                    //Send the timesteps by second for the amount of time specified in the simulation config
                    sendTimesteps(simulationConfig, simulationId);
                } else {
                    logManager.log(
                            new LogMessage(Integer.toString(simulationId), new Date().getTime(),
                                    "FNCS Initialization Failed", LogLevel.ERROR, ProcessStatus.ERROR, true),
                            GridAppsDConstants.username);

                }

                //call to stop the fncs broker
                client.publish(GridAppsDConstants.topic_FNCS_input, "{\"command\":  \"stop\"}");
                logManager.log(new LogMessage(Integer.toString(simulationId), new Date().getTime(),
                        "Simulation " + simulationId + " complete", LogLevel.INFO, ProcessStatus.COMPLETE,
                        true), GridAppsDConstants.username);
            } catch (Exception e) {
                log.error("Error during simulation", e);
                try {
                    logManager.log(new LogMessage(Integer.toString(simulationId), new Date().getTime(),
                            "Simulation error: " + e.getMessage(), LogLevel.ERROR, ProcessStatus.ERROR, true),
                            GridAppsDConstants.username);
                } catch (Exception e1) {
                    log.error("Error while reporting error status", e);
                }
            } finally {
                //shut down fncs broker and gridlabd and bridge if still running
                if (fncsProcess != null) {
                    fncsProcess.destroy();
                }
                if (gridlabdProcess != null) {
                    gridlabdProcess.destroy();
                }
                if (fncsBridgeProcess != null) {
                    fncsBridgeProcess.destroy();
                }
            }
        }
    });

    thread.start();
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public void doBlast(final String fasta, final String evaluestr, final boolean ids, final RunnableResult rr,
        boolean x) {
    /*File blastn;//from   w  w  w. ja  v  a  2  s .co  m
    File blastp;
    File makeblastdb;
    File blastx = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastx.exe" );
    if( !blastx.exists() ) {
       blastx = new File( "/opt/ncbi-blast-2.2.29+/bin/blastx" );
       if( !blastx.exists() ) {
    blastx = new File( "/usr/local/ncbi/blast/bin/blastx" );
    blastn = new File( "/usr/local/ncbi/blast/bin/blastn" );
    blastp = new File( "/usr/local/ncbi/blast/bin/blastp" );
            
    makeblastdb = new File( "/usr/local/ncbi/blast/bin/makeblastdb" );
       } else {
    blastn = new File( "/opt/ncbi-blast-2.2.29+/bin/blastn" );
    blastp = new File( "/opt/ncbi-blast-2.2.29+/bin/blastp" );
            
    makeblastdb = new File( "/opt/ncbi-blast-2.2.29+/bin/makeblastdb" );
       }
    } else {
       blastn = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastn.exe" );
       blastp = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastp.exe" );
               
       makeblastdb = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\makeblastdb.exe" );
    }*/

    String OS = System.getProperty("os.name").toLowerCase();
    int procs = Runtime.getRuntime().availableProcessors();
    String[] mcmds = { OS.indexOf("mac") >= 0 ? "/usr/local/bin/makeblastdb" : "makeblastdb", "-dbtype", "prot",
            "-title", "tmp", "-out", "tmp" };
    List<String> lcmd = new ArrayList<String>(Arrays.asList(mcmds));

    final ProcessBuilder mpb = new ProcessBuilder(lcmd);
    mpb.redirectErrorStream(true);
    try {
        final Process mp = mpb.start();

        new Thread() {
            public void run() {
                try {
                    OutputStream pos = mp.getOutputStream();
                    Writer ow = new OutputStreamWriter(pos);
                    for (Gene g : geneset.genelist) {
                        if (g.getTag() == null || g.getTag().length() == 0) {
                            GeneGroup gg = g.getGeneGroup();

                            if (gg != null) {
                                String name;
                                if (ids)
                                    name = g.id;
                                else {
                                    String addstr = "";
                                    Cog cog = gg.getCog(geneset.cogmap);
                                    String cazy = gg.getCommonCazy(geneset.cazymap);
                                    if (cog != null)
                                        addstr += "_" + cog.id;
                                    if (cazy != null) {
                                        if (addstr.length() > 0)
                                            addstr += cazy;
                                        addstr += "_" + cazy;
                                    }
                                    if (addstr.length() > 0)
                                        addstr += "_";

                                    name = g.name + addstr + "[" + g.id + "]";
                                    //pos.write( (">" + g.name + addstr + "[" + g.id + "]\n").getBytes() );
                                }
                                Sequence sb = g.tegeval.getProteinSequence();
                                sb.setName(name);
                                sb.writeSequence(ow);
                                /*for( int i = 0; i < sb.length(); i+=70 ) {
                                   pos.write( sb.substring(i, Math.min( sb.length(), i+70) ).getBytes() );
                                }
                                pos.write( '\n' );*/
                            }
                        }
                    }
                    ow.close();
                    pos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {
            public void run() {
                try {
                    InputStream pin = mp.getInputStream();
                    InputStreamReader rdr = new InputStreamReader(pin);
                    //FileReader fr = new FileReader( new File("c:/dot.blastout") );
                    BufferedReader br = new BufferedReader(rdr);
                    String line = br.readLine();
                    while (line != null) {
                        System.out.println(line);
                        line = br.readLine();
                    }
                    pin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.run();

        //File blastFile = x ? blastx : blastp; //dbType.equals("prot") ? type.equals("prot") ? blastp : blastx : blastn;

        String[] cmds = { OS.indexOf("mac") >= 0 ? "/usr/local/bin/blastp" : "blastp", "-query", "-", "-db",
                "tmp", "-evalue", evaluestr, "-num_threads", Integer.toString(procs) };
        lcmd = new ArrayList<String>(Arrays.asList(cmds));
        //String[] exts = extrapar.trim().split("[\t ]+");

        ProcessBuilder pb = new ProcessBuilder(lcmd);
        pb.redirectErrorStream(true);
        final Process p = pb.start();

        final Thread t = new Thread() {
            public void run() {
                try {
                    OutputStream pos = p.getOutputStream();
                    pos.write(fasta.getBytes());
                    pos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();

        final Thread t2 = new Thread() {
            public void run() {
                try {
                    System.err.println("WHY NOT");
                    InputStreamReader rdr = new InputStreamReader(p.getInputStream());
                    //FileReader fr = new FileReader( new File("c:/dot.blastout") );

                    String res = "";
                    BufferedReader br = new BufferedReader(rdr);
                    String line = br.readLine();
                    while (line != null) {
                        //System.err.println( line );
                        if (line.startsWith("> ")) {
                            int i = line.indexOf(' ', 2);
                            if (i == -1)
                                i = line.length();
                            String id = line.substring(2, i);

                            Gene g = geneset.genemap.get(id);
                            if (g != null) {
                                if (!isGeneview()) {
                                    /*i = geneset.allgenegroups.indexOf( g.getGeneGroup() );
                                    if( i != -1 && i < table.getRowCount() ) {
                                       int r = table.convertRowIndexToView( i );
                                       table.addRowSelectionInterval(r, r);
                                    }*/
                                    table.getSelectionModel().select(g.getGeneGroup());
                                } else {
                                    /*i = geneset.genelist.indexOf( g );
                                    if( i != -1 && i < table.getRowCount() ) {
                                       int r = table.convertRowIndexToView( i );
                                       table.addRowSelectionInterval(r, r);
                                    }*/
                                    gtable.getSelectionModel().select(g);
                                }
                            }

                            String stuff = line + "\n";
                            line = br.readLine();
                            while (line != null && !line.startsWith("Query=") && !line.startsWith("> ")) {
                                stuff += line + "\n";
                                line = br.readLine();
                            }
                            if (rr != null) {
                                rr.run(stuff);
                                //res += line+"\n";
                            }
                        } else
                            line = br.readLine();
                    }
                    br.close();
                    //System.err.println("wn done");
                    p.destroy();

                    if (rr != null)
                        rr.run("close");

                    /*if( rr != null ) {
                       rr.run( res );
                    }*/
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t2.start();
        //fr.close();
    } catch (IOException e2) {
        e2.printStackTrace();
    }
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public void doBlastn(final String fasta, final String evaluestr, final boolean ids, final RunnableResult rr,
        boolean show) {
    /*File blastn;//www  . jav a  2s  .  c  o m
    File blastp;
    File makeblastdb;
    File blastx = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastx.exe" );
    if( !blastx.exists() ) {
       blastx = new File( "/opt/ncbi-blast-2.2.29+/bin/blastx" );
       if( !blastx.exists() ) {
    blastx = new File( "/usr/local/ncbi/blast/bin/blastx" );
    blastn = new File( "/usr/local/ncbi/blast/bin/blastn" );
    blastp = new File( "/usr/local/ncbi/blast/bin/blastp" );
            
    makeblastdb = new File( "/usr/local/ncbi/blast/bin/makeblastdb" );
       } else {
    blastn = new File( "/opt/ncbi-blast-2.2.29+/bin/blastn" );
    blastp = new File( "/opt/ncbi-blast-2.2.29+/bin/blastp" );
            
    makeblastdb = new File( "/opt/ncbi-blast-2.2.29+/bin/makeblastdb" );
       }
    } else {
       blastn = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastn.exe" );
       blastp = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\blastp.exe" );
               
       makeblastdb = new File( "c:\\\\Program files\\NCBI\\blast-2.2.29+\\bin\\makeblastdb.exe" );
    }*/

    int procs = Runtime.getRuntime().availableProcessors();
    String[] mcmds = { "makeblastdb", "-dbtype", "nucl", "-title", "tmp", "-out", "tmp" };
    List<String> lcmd = new ArrayList<String>(Arrays.asList(mcmds));

    final ProcessBuilder mpb = new ProcessBuilder(lcmd);
    mpb.redirectErrorStream(true);
    try {
        final Process mp = mpb.start();

        new Thread() {
            public void run() {
                try {
                    OutputStream pos = mp.getOutputStream();
                    for (String cname : geneset.contigmap.keySet()) {
                        Sequence c = geneset.contigmap.get(cname);
                        if (ids)
                            pos.write((">" + c.id + "\n").getBytes());
                        else {
                            pos.write((">" + c.getName() + "\n").getBytes());
                        }
                        StringBuilder sb = c.getStringBuilder();
                        for (int i = 0; i < sb.length(); i += 70) {
                            pos.write(sb.substring(i, Math.min(sb.length(), i + 70)).getBytes());
                        }
                        pos.write('\n');
                    }
                    pos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {
            public void run() {
                try {
                    InputStream pin = mp.getInputStream();
                    InputStreamReader rdr = new InputStreamReader(pin);
                    //FileReader fr = new FileReader( new File("c:/dot.blastout") );
                    BufferedReader br = new BufferedReader(rdr);
                    String line = br.readLine();
                    while (line != null) {
                        System.out.println(line);
                        line = br.readLine();
                    }
                    pin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.run();

        //File blastFile = blastn; //dbType.equals("prot") ? type.equals("prot") ? blastp : blastx : blastn;

        String[] cmds1 = { "blastn", "-dust", "no", "-perc_identity", "99", "-word_size", "21", "-query", "-",
                "-db", "tmp", "-evalue", evaluestr, "-num_threads", Integer.toString(procs) };
        String[] cmds2 = { "blastn", "-query", "-", "-db", "tmp", "-evalue", evaluestr, "-num_threads",
                Integer.toString(procs) };
        String[] cmds = show ? cmds2 : cmds1;

        lcmd = new ArrayList<String>(Arrays.asList(cmds));
        //String[] exts = extrapar.trim().split("[\t ]+");

        ProcessBuilder pb = new ProcessBuilder(lcmd);
        pb.redirectErrorStream(true);
        final Process p = pb.start();

        final Thread t = new Thread() {
            public void run() {
                try {
                    OutputStream pos = p.getOutputStream();
                    pos.write(fasta.getBytes());
                    pos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();

        Map<String, Set<String>> tph = new HashMap<String, Set<String>>();
        Map<String, Map<String, String>> tvp = new HashMap<String, Map<String, String>>();
        Map<String, Map<String, String>> tmr = new HashMap<String, Map<String, String>>();

        Map<String, Integer> specindex = new LinkedHashMap<String, Integer>();
        Map<String, Integer> phindex = new LinkedHashMap<String, Integer>();

        /*final Thread t2 = new Thread() {
           public void run() {*/
        try {
            System.err.println("WHY NOT");
            InputStreamReader rdr = new InputStreamReader(p.getInputStream());
            //FileReader fr = new FileReader( new File("c:/dot.blastout") );

            String qspec = null;
            String query = null;
            String ctype = null;
            Annotation at = new Annotation();
            int o = 0;
            StringBuilder res = new StringBuilder();
            BufferedReader br = new BufferedReader(rdr);
            String line = br.readLine();
            res.append(line + "\n");
            while (line != null) {
                if (line.startsWith("Query= ")) {
                    query = line.substring(7, line.length());
                    int e = query.indexOf("CRISPR") - 1;
                    if (e > 0) {
                        qspec = query.substring(0, e);
                        qspec = Sequence.getSpec(qspec);
                        String rest = query.substring(e + 8);
                        int ri = rest.lastIndexOf('-');
                        if (ri != -1)
                            ctype = rest.substring(ri + 1);
                    } else {
                        System.err.println();
                    }

                    line = br.readLine();
                    res.append(line + "\n");
                    while (!line.startsWith("Length")) {
                        line = br.readLine();
                        res.append(line + "\n");
                    }
                    o = Integer.parseInt(line.substring(7));
                } else if (line.startsWith("> ")) {
                    String contname = line.substring(1).trim();
                    //line = br.readLine();
                    //res.append( line+"\n" );
                    //int o = Integer.parseInt( line.substring(7) );

                    Sequence cont = geneset.contigmap.get(contname);

                    if (cont != null) {
                        int start = -1;
                        int stop = 0;
                        line = br.readLine();
                        res.append(line + "\n");
                        String lastmatch = null;
                        while (line != null && !line.startsWith(">")
                                && !line.startsWith("Query=") /*&& !line.contains("Expect =")*/ ) {
                            if (line.startsWith("Sbjct")) {
                                String[] split = line.split("[\t ]+");
                                int k = Integer.parseInt(split[1]);
                                int m = Integer.parseInt(split[3]);
                                lastmatch = split[2];

                                if (start == -1)
                                    start = k;
                                stop = m;
                            }
                            line = br.readLine();
                            res.append(line + "\n");
                        }

                        if (start > stop) {
                            int tmp = start;
                            start = stop;
                            stop = tmp;
                        }

                        at.start = start;
                        at.stop = stop;

                        //if( stop - start < o*2 ) {
                        List<Annotation> lann = cont.getAnnotations();
                        if (lann != null) {
                            int k = Collections.binarySearch(lann, at);

                            //System.err.println( "kkk  " + k + "   " + lann.size() );

                            if (k < 0)
                                k = -(k + 1) - 1;

                            Annotation ann = lann.get(Math.max(0, k));

                            boolean yes = true;
                            if (ann.type != null && ann.type.contains("ummer")) {
                                yes = false;
                            }

                            int u = k - 1;
                            Annotation nann = null;
                            if (u >= 0 && u < lann.size())
                                nann = lann.get(u);

                            u = k + 1;
                            Annotation rann = null;
                            if (u >= 0 && u < lann.size())
                                rann = lann.get(u);

                            if (nann != null && nann.type != null && nann.type.contains("ummer")) {
                                yes = false;
                            }

                            if (rann != null && rann.type != null && rann.type.contains("ummer")) {
                                yes = false;
                            }

                            if (!yes) {
                                //System.err.println();
                            }

                            Gene g = ann.getGene();
                            String desig = ann.designation;

                            if (yes && g != null) { //ann.stop > at.start && ann.start < at.stop ) {
                                GeneGroup gg = g.getGeneGroup();
                                if (desig != null && desig.contains("phage")) {
                                    if (!phindex.containsKey(desig))
                                        phindex.put(desig, phindex.size());

                                    Map<String, String> tvps;
                                    String specname = qspec;//Sequence.nameFix(qspec, true);
                                    if (!specindex.containsKey(specname))
                                        specindex.put(specname, specindex.size());

                                    if (tvp.containsKey(specname)) {
                                        tvps = tvp.get(specname);
                                    } else {
                                        tvps = new HashMap<String, String>();
                                        tvp.put(specname, tvps);
                                    }
                                    tvps.put(desig, ctype);

                                    String contspec = cont.getSpec();
                                    System.err.println(query + " asdf " + contspec + " " + lastmatch + "  "
                                            + at.start + " " + at.stop + "  " + ann.start + " " + ann.stop
                                            + " rann " + (rann != null ? rann.start + "  " + rann.stop : "")
                                            + " nann " + (nann != null ? nann.start + "  " + nann.stop : ""));
                                    if (qspec.equals(contspec)) {
                                        if (tmr.containsKey(specname)) {
                                            tvps = tmr.get(specname);
                                        } else {
                                            tvps = new HashMap<String, String>();
                                            tmr.put(specname, tvps);
                                        }
                                        tvps.put(desig, ctype);
                                    }

                                    /*if( specname.contains("brockianus_MAT_338") ) {
                                       System.err.println();
                                    }*/
                                }

                                Platform.runLater(() -> {
                                    if (!isGeneview()) {
                                        /*int ggindex = geneset.allgenegroups.indexOf( gg );
                                        int i = table.convertRowIndexToView( ggindex );
                                        if( i != -1 ) table.addRowSelectionInterval(i, i);*/

                                        table.getSelectionModel().select(gg);
                                    } else {
                                        /*int gindex = geneset.genelist.indexOf( g );
                                        int i = table.convertRowIndexToView( gindex );
                                        table.addRowSelectionInterval(i, i);*/

                                        gtable.getSelectionModel().select(g);
                                    }
                                });
                            }

                            /*for( Annotation ann : lann ) {
                               if( ann.stop > start && ann.start < stop ) {
                            Gene g = ann.getGene();
                            if( g != null ) {
                               if( table.getModel() == groupModel ) {
                                  GeneGroup gg = g.getGeneGroup();
                                          
                                  int ggindex = allgenegroups.indexOf( gg );
                                  int i = table.convertRowIndexToView( ggindex );
                                  table.addRowSelectionInterval(i, i);
                               } else if( table.getModel() == defaultModel ) {
                                  int gindex = geneset.genelist.indexOf( g );
                                  int i = table.convertRowIndexToView( gindex );
                                  table.addRowSelectionInterval(i, i);
                               }
                            }
                               }
                            }*/
                        }
                        //}
                        continue;
                    }
                }

                /*int i = line.indexOf(' ', 2);
                if( i == -1 ) i = line.length();
                String id = line.substring(2, i);
                        
                Gene g = genemap.get( id );
                if( g != null ) {
                   if( table.getModel() == groupModel ) {
                      i = allgenegroups.indexOf( g.getGeneGroup() );
                      if( i != -1 && i < table.getRowCount() ) {
                         int r = table.convertRowIndexToView( i );
                         table.addRowSelectionInterval(r, r);
                      }
                   } else {
                      i = geneset.genelist.indexOf( g );
                      if( i != -1 && i < table.getRowCount() ) {
                         int r = table.convertRowIndexToView( i );
                         table.addRowSelectionInterval(r, r);
                      }
                   }
                }
                        
                String stuff = line+"\n";
                line = br.readLine();
                while( line != null && !line.startsWith("Query=") && !line.startsWith("> ") ) {
                   stuff += line+"\n";
                   line = br.readLine();
                }
                if( rr != null ) {
                   rr.run( stuff );
                   //res += line+"\n";
                }
                } //else*/
                line = br.readLine();
                res.append(line + "\n");
            }
            br.close();
            p.destroy();

            for (String specname : geneset.speccontigMap.keySet()) {
                List<Sequence> lseq = geneset.speccontigMap.get(specname);
                for (Sequence seq : lseq) {
                    List<Annotation> lann = seq.getAnnotations();
                    if (lann != null) {
                        for (Annotation a : lann) {
                            String desig = a.designation;
                            if (desig != null && desig.contains("phage") && phindex.containsKey(desig)) {
                                if (!specindex.containsKey(specname))
                                    specindex.put(specname, specindex.size());

                                Set<String> tvps;
                                if (tph.containsKey(specname)) {
                                    tvps = tph.get(specname);
                                } else {
                                    tvps = new HashSet<String>();
                                    tph.put(specname, tvps);
                                }
                                tvps.add(desig);
                            }
                        }
                    }
                }
            }

            int k = 0;
            int u = 0;
            Workbook wb = new XSSFWorkbook();
            Sheet sh = wb.createSheet("Phage");
            Row rw = sh.createRow(u++);
            //res = new StringBuilder();
            for (String ph : phindex.keySet()) {
                res.append("\t" + ph);
                rw.createCell(++k).setCellValue(ph);
            }
            res.append("\n");
            for (String rspec : specindex.keySet()) {
                String spec = Sequence.nameFix(rspec, true);
                rw = sh.createRow(u++);
                k = 0;
                rw.createCell(k++).setCellValue(spec);

                Map<String, String> set = tvp.get(rspec);
                res.append(spec);
                if (set != null) {
                    for (String ph : phindex.keySet()) {
                        if (set.containsKey(ph)) {
                            String type = set.get(ph);
                            if (type == null || type.length() == 0)
                                type = "yes";
                            res.append("\t" + type);
                            rw.createCell(k).setCellValue(type);
                        } else {
                            res.append("\t");
                        }

                        k++;
                    }
                }
                res.append("\n");
            }

            for (String ph : phindex.keySet()) {
                res.append("\t" + ph);
            }
            res.append("\n");

            u++;
            for (String rspec : specindex.keySet()) {
                String spec = Sequence.nameFix(rspec, true);

                rw = sh.createRow(u++);
                k = 0;
                rw.createCell(k++).setCellValue(spec);

                Map<String, String> set = tmr.get(rspec);
                res.append(spec);
                if (set != null) {
                    for (String ph : phindex.keySet()) {
                        if (set.containsKey(ph)) {
                            String type = set.get(ph);
                            if (type == null || type.length() == 0)
                                type = "yes";
                            res.append("\t" + type);
                            rw.createCell(k).setCellValue(type);
                        } else
                            res.append("\t");

                        k++;
                    }
                }
                res.append("\n");
            }

            u++;
            for (String rspec : specindex.keySet()) {
                String spec = Sequence.nameFix(rspec, true);

                rw = sh.createRow(u++);
                k = 0;
                rw.createCell(k++).setCellValue(spec);

                Set<String> set = tph.get(rspec);
                Map<String, String> setvp = tvp.get(rspec);
                res.append(spec);
                if (set != null) {
                    for (String ph : phindex.keySet()) {
                        if (set.contains(ph)) {
                            if (setvp != null && setvp.containsKey(ph)) {
                                res.append("\tyes wspacer");
                                rw.createCell(k).setCellValue("yes wspacer");
                            } else {
                                res.append("\tyes");
                                rw.createCell(k).setCellValue("yes");
                            }
                        } else
                            res.append("\t");

                        k++;
                    }
                }
                res.append("\n");
            }

            File file = new File("/Users/sigmar/phage.xlsx");
            FileOutputStream fos = new FileOutputStream(file);
            wb.write(fos);
            fos.close();

            Desktop.getDesktop().open(file);

            //if( !show ) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(800, 600);

            JTextArea ta = new JTextArea();
            ta.setFont(new Font("monospaced", Font.PLAIN, 12));
            ta.append(res.toString());
            JScrollPane sp = new JScrollPane(ta);
            frame.add(sp);

            frame.setVisible(true);

            FileWriter fw = new FileWriter("/Users/sigmar/file.txt");
            fw.write(res.toString());
            fw.close();

            if (rr != null)
                rr.run("close");
            //}

            /*if( rr != null ) {
             rr.run( res );
            }*/
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*   }
        };
        t2.start();*/
        //fr.close();
    } catch (IOException e2) {
        e2.printStackTrace();
    }
}

From source file:com.clark.func.Functions.java

/**
 * Performs the os command./*from w  w  w .j  a va  2 s  . c  o  m*/
 * 
 * @param cmdAttribs
 *            the command line parameters
 * @param max
 *            The maximum limit for the lines returned
 * @param timeout
 *            The timout amount in milliseconds or no timeout if the value
 *            is zero or less
 * @return the parsed data
 * @throws IOException
 *             if an error occurs
 */
static List<String> performCommand(String[] cmdAttribs, int max, long timeout) throws IOException {
    // this method does what it can to avoid the 'Too many open files' error
    // based on trial and error and these links:
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4801027
    // http://forum.java.sun.com/thread.jspa?threadID=533029&messageID=2572018
    // however, its still not perfect as the JDK support is so poor
    // (see commond-exec or ant for a better multi-threaded multi-os
    // solution)

    List<String> lines = new ArrayList<String>(20);
    Process proc = null;
    InputStream in = null;
    OutputStream out = null;
    InputStream err = null;
    BufferedReader inr = null;
    try {

        Thread monitor = ThreadMonitor.start(timeout);

        proc = openProcess(cmdAttribs);
        in = proc.getInputStream();
        out = proc.getOutputStream();
        err = proc.getErrorStream();
        inr = new BufferedReader(new InputStreamReader(in));
        String line = inr.readLine();
        while (line != null && lines.size() < max) {
            line = line.toLowerCase(Locale.ENGLISH).trim();
            lines.add(line);
            line = inr.readLine();
        }

        proc.waitFor();

        ThreadMonitor.stop(monitor);

        if (proc.exitValue() != 0) {
            // os command problem, throw exception
            throw new IOException("Command line returned OS error code '" + proc.exitValue() + "' for command "
                    + Arrays.asList(cmdAttribs));
        }
        if (lines.size() == 0) {
            // unknown problem, throw exception
            throw new IOException(
                    "Command line did not return any info " + "for command " + Arrays.asList(cmdAttribs));
        }
        return lines;

    } catch (InterruptedException ex) {
        throw new IOExceptionWithCause("Command line threw an InterruptedException " + "for command "
                + Arrays.asList(cmdAttribs) + " timeout=" + timeout, ex);
    } finally {
        closeQuietly(in);
        closeQuietly(out);
        closeQuietly(err);
        closeQuietly(inr);
        if (proc != null) {
            proc.destroy();
        }
    }
}