diff --git a/hmf-common/pom.xml b/hmf-common/pom.xml index fd9ce2e60c..22c6cd03f3 100644 --- a/hmf-common/pom.xml +++ b/hmf-common/pom.xml @@ -62,6 +62,18 @@ junit test + + commons-io + commons-io + 2.16.1 + compile + + + commons-io + commons-io + 2.16.1 + compile + diff --git a/hmf-common/src/main/java/com/hartwig/hmftools/common/circos/CircosExecution.java b/hmf-common/src/main/java/com/hartwig/hmftools/common/circos/CircosExecution.java index 8f3a778619..e2a4ac0d2e 100644 --- a/hmf-common/src/main/java/com/hartwig/hmftools/common/circos/CircosExecution.java +++ b/hmf-common/src/main/java/com/hartwig/hmftools/common/circos/CircosExecution.java @@ -2,8 +2,12 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; +import java.util.Map; +import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; @@ -44,10 +48,22 @@ public Integer generateCircos( LOGGER.info(String.format("generating " + outputFile + " via command: %s", String.join(" ", command))); + ProcessBuilder processBuilder = new ProcessBuilder(command); + Map environment = processBuilder.environment(); + + // Circos writes the temporary file circos.colorlist. However, this file would be overwritten multiple times when invoking circos + // from multiple threads which results in a race condition. We make sure that circos writes to different temporary directories to + // avoid this race condition + Path tempDir = Files.createTempDirectory("circos"); + environment.put("TMPDIR", tempDir.toString()); + // must redirect error stream to stdout, as circos print some errors to stdout - Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); + Process process = processBuilder.redirectErrorStream(true).start(); + int result = process.waitFor(); + FileUtils.deleteDirectory(tempDir.toFile()); + if(result != 0) { System.err.print(new String(process.getInputStream().readAllBytes()));