List of usage examples for org.apache.commons.exec CommandLine parse
public static CommandLine parse(final String line, final Map<String, ?> substitutionMap)
From source file:edu.cornell.med.icb.goby.modes.RunParallelMode.java
public void execute() throws IOException { final Slice slices[] = new Slice[numParts]; File file = new File(input); if (!(file.isFile() && file.exists() && file.canRead())) { System.err.println("Input file cannot be read: " + input); System.exit(1);/*from w ww . jav a 2 s .co m*/ } int i = 0; for (final Slice slice : slices) { slices[i++] = new Slice(); } final long fileLength = file.length(); final long sliceLength = fileLength / numParts; long currentOffset = 0; for (final Slice slice : slices) { slice.startOffset = currentOffset; slice.endOffset = currentOffset + sliceLength; currentOffset = slice.endOffset; } final ObjectOpenHashSet<String> allOutputs = new ObjectOpenHashSet<String>(); final ObjectOpenHashSet<String> allFastq = new ObjectOpenHashSet<String>(); final DoInParallel loop = new DoInParallel(numParts) { IsDone done = new IsDone(); @Override public void action(final DoInParallel forDataAccess, final String inputBasename, final int loopIndex) { try { CompactToFastaMode ctfm = new CompactToFastaMode(); ctfm.setInputFilename(input); ctfm.setOutputFormat(CompactToFastaMode.OutputFormat.FASTQ); ctfm.setStartPosition(slices[loopIndex].startOffset); ctfm.setEndPosition(slices[loopIndex].endOffset); String s = FilenameUtils.getBaseName(FilenameUtils.removeExtension(input)) + "-" + Integer.toString(loopIndex); String fastqFilename = s + "-input.fq"; allFastq.add(fastqFilename); File tmp1 = new File(s + "-tmp"); tmp1.deleteOnExit(); File output = new File(s + "-out"); output.deleteOnExit(); ctfm.setOutputFilename(fastqFilename); LOG.info(String.format("Extracting FASTQ for slice [%d-%d] loopIndex=%d %n", slices[loopIndex].startOffset, slices[loopIndex].endOffset, loopIndex)); ctfm.execute(); if (loopIndex > 0) { while (!done.isDone()) { // wait a bit to give the first thread the time to load the database and establish shared memory pool // System.out.println("sleep 5 thread "+loopIndex); sleep(5); } System.out.println("Thread " + loopIndex + " can now start."); } final Map<String, String> replacements = new HashMap<String, String>(); final String outputFilename = output.getName(); replacements.put("%read.fastq%", fastqFilename); replacements.put("%tmp1%", tmp1.getName()); replacements.put("%output%", outputFilename); final String transformedCommand = transform(processPartCommand, replacements); final DefaultExecutor executor = new DefaultExecutor(); OutputStream logStream = null; try { logStream = new LoggingOutputStream(getClass(), Level.INFO, ""); executor.setStreamHandler( new PumpStreamHandler(new StreamSignal(done, "scanning", logStream))); final CommandLine parse = CommandLine.parse(transformedCommand, replacements); LOG.info("About to execute: " + parse); final int exitValue = executor.execute(parse); LOG.info("Exit value = " + exitValue); if (new File(outputFilename + ".header").exists()) { // found output alignment: System.out.println("found output file: " + outputFilename); allOutputs.add(outputFilename + ".header"); } else { System.out.println("Warning: did not find output alignment: " + outputFilename); } } finally { IOUtils.closeQuietly(logStream); // remove the fastq file new File(fastqFilename).delete(); } } catch (IOException e) { LOG.error("Error processing index " + loopIndex + ", " + inputBasename, e); } } }; String[] parts = new String[numParts]; for (int j = 0; j < numParts; j++) { parts[j] = Integer.toString(j); } try { loop.execute(true, parts); } catch (Exception e) { System.err.println("An error occurred executing a parallel command: "); e.printStackTrace(); } System.out.printf("Preparing to concatenate %d outputs..%n", allOutputs.size()); final ConcatenateAlignmentMode concat = new ConcatenateAlignmentMode(); concat.setInputFileNames(allOutputs.toArray(new String[allOutputs.size()])); concat.setOutputFilename(output); concat.setAdjustQueryIndices(false); concat.setAdjustSampleIndices(false); concat.execute(); }