Skip to content

Commit

Permalink
Replace File.createTempFile with IOUtils.createTempFile (#6780)
Browse files Browse the repository at this point in the history
* This ensure that temp files are deleted on shutdown
* Fixes #6771
  • Loading branch information
lbergelson committed Dec 24, 2022
1 parent 16fc4f1 commit ba2b5b3
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ else if (getIntervalsFromExistingWorkspace){
IOUtils.assertPathsAreReadable(vcfHeader);
final String header = GenomicsDBUtils.readEntireFile(vcfHeader);
try {
File tempHeader = File.createTempFile("tempheader", ".vcf");
tempHeader.deleteOnExit();
File tempHeader = IOUtils.createTempFile("tempheader", ".vcf");
Files.write(tempHeader.toPath(), header.getBytes(StandardCharsets.UTF_8));
mergedHeaderSequenceDictionary = VCFFileReader.getSequenceDictionary(tempHeader);
} catch (final IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.broadinstitute.hellbender.utils.bwa.BwaMemAligner;
import org.broadinstitute.hellbender.utils.bwa.BwaMemAlignment;
import org.broadinstitute.hellbender.utils.bwa.BwaMemIndex;
import org.broadinstitute.hellbender.utils.io.IOUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -64,8 +65,7 @@ public SingleSequenceReferenceAligner(final String name, final byte[] bases,
this.alignmentFilter = Utils.nonNull(alignmentFilter);
Utils.validate(bases.length > 0, "the reference contig bases sequence must have at least one base");
try {
final File fasta = File.createTempFile("ssvh-temp", ".fasta");
fasta.deleteOnExit();
final File fasta = IOUtils.createTempFile("ssvh-temp", ".fasta");
image = new File(fasta.getParentFile(), fasta.getName().replace(".fasta", ".img"));
image.deleteOnExit();
FastaReferenceWriter.writeSingleSequenceReference(fasta.toPath(), false, false, name, null, bases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.broadinstitute.hellbender.metrics.MetricsUtils;
import org.broadinstitute.hellbender.metrics.analysis.AlleleFrequencyQCMetric;
import org.broadinstitute.hellbender.utils.R.RScriptExecutor;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import org.broadinstitute.hellbender.utils.io.Resource;
import org.broadinstitute.hellbender.utils.report.GATKReport;
import org.broadinstitute.hellbender.utils.report.GATKReportTable;
Expand Down Expand Up @@ -59,15 +60,8 @@ public void onTraversalStart() {
metricOutput = outFile; // output file for summary metrics

// have to set the output file for variant eval; if not given a debug file to return the variant eval results
// from, this will just be a temporary file that wil lbe deleted after the tool runs
try {
outFile = debugFile == null ? File.createTempFile("variant_eval" ,".txt") : debugFile;
} catch (IOException e) {
throw new RuntimeException("Trouble creating temporary variant eval file", e);
}
if (debugFile == null) {
outFile.deleteOnExit();
}
// from, this will just be a temporary file that will be deleted after the tool runs
outFile = debugFile == null ? IOUtils.createTempFile("variant_eval" ,".txt") : debugFile;
sample = getHeaderForVariants().getOtherHeaderLine("sampleAlias").getValue();
super.onTraversalStart();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public InputStream getResourceContentsAsStream() {
* @throws IOException
*/
public static File getResourceContentsAsFile(final String resourcePath) throws IOException {
final File tmpResourceFile = File.createTempFile("tmp_read_resource_", ".config");
final File tmpResourceFile = IOUtils.createTempFile("tmp_read_resource_", ".config");
// use current classloader rather than system classloader to support dynamic environments like Spark executors
final InputStream resourceAsStream = Resource.class.getClassLoader().getResourceAsStream(resourcePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ private List<VariantContext> readVariants(String vcf) throws IOException {
File actualVcf;
// work around TribbleIndexedFeatureReader not reading header from .bgz files
if (vcf.endsWith(".bgz")) {
actualVcf = File.createTempFile(vcf, ".gz");
actualVcf.deleteOnExit();
actualVcf = IOUtils.createTempFile(vcf, ".gz");
Files.copy(new File(vcf), actualVcf);
} else {
actualVcf = new File(vcf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedInterval;
import org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedIntervalCollection;
import org.broadinstitute.hellbender.utils.SimpleInterval;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -44,9 +44,9 @@ public final class CombineSegmentBreakpointsIntegrationTest extends CommandLineP
private static final String SEGMENT_CALL_2 = "Segment_Call";

@Test
public void testRunWithExactSegments() throws IOException {
public void testRunWithExactSegments() {
// Segment intervals are the same in the input files. Therefore, the union should only generate more columns.
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
final Set<String> columnSet = Sets.newHashSet("MEAN_LOG2_COPY_RATIO", "CALL", "Segment_Mean", "Segment_Call");
final List<String> arguments = new ArrayList<>();
arguments.add("--" + CopyNumberStandardArgument.SEGMENTS_FILE_LONG_NAME);
Expand All @@ -71,13 +71,13 @@ public void testRunWithExactSegments() throws IOException {
Assert.assertEquals(regions.size(), 4);
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().size() == columnSet.size()));
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().keySet().containsAll(columnSet)));
Assert.assertTrue(regions.getRecords().stream().noneMatch(r -> r.getAnnotations().values().contains("")));
Assert.assertTrue(regions.getRecords().stream().noneMatch(r -> r.getAnnotations().containsValue("")));
}

@Test
public void testRunWithExactSameFiles() throws IOException {
public void testRunWithExactSameFiles() {
// Input files are exactly the same. Therefore, the union should only generate more columns.
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
final List<String> arguments = new ArrayList<>();
arguments.add("-" + StandardArgumentDefinitions.REFERENCE_SHORT_NAME);
arguments.add(REFERENCE_FILE);
Expand All @@ -100,14 +100,14 @@ public void testRunWithExactSameFiles() throws IOException {
Assert.assertEquals(regions.size(), 4);
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().size() == gtColumnSet.size()));
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().keySet().containsAll(gtColumnSet)));
Assert.assertTrue(regions.getRecords().stream().noneMatch(r -> r.getAnnotations().values().contains("")));
Assert.assertTrue(regions.getRecords().stream().noneMatch(r -> r.getAnnotations().containsValue("")));
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().get("MEAN_LOG2_COPY_RATIO_1").equals(r.getAnnotations().get("MEAN_LOG2_COPY_RATIO_2"))));
Assert.assertTrue(regions.getRecords().stream().allMatch(r -> r.getAnnotations().get("CALL_1").equals(r.getAnnotations().get("CALL_2"))));
}
@Test
public void testRunWithoutExactOverlaps() throws IOException {
public void testRunWithoutExactOverlaps() {
// This test is a bit more like the real world
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
final List<String> arguments = new ArrayList<>();
arguments.add("-" + StandardArgumentDefinitions.REFERENCE_SHORT_NAME);
arguments.add(REFERENCE_FILE);
Expand Down Expand Up @@ -135,9 +135,9 @@ public void testRunWithoutExactOverlaps() throws IOException {
}

@Test(expectedExceptions = UserException.BadInput.class)
public void testFailureIfNotAllColsOfInterestExist() throws IOException {
public void testFailureIfNotAllColsOfInterestExist() {
// This test is a bit more like the real world
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
final List<String> arguments = new ArrayList<>();
arguments.add("-" + StandardArgumentDefinitions.REFERENCE_SHORT_NAME);
arguments.add(REFERENCE_FILE);
Expand Down Expand Up @@ -187,11 +187,11 @@ private void assertUnionedSegFiles(final String segmentCall1, final String segme
}

@Test
public void testRunWithoutExactOverlapsAndLabels() throws IOException {
public void testRunWithoutExactOverlapsAndLabels() {
final String TEST = "test";
final String GT = "gt";
// This test is a bit more like the real world
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
final List<String> arguments = new ArrayList<>();
arguments.add("-" + StandardArgumentDefinitions.REFERENCE_SHORT_NAME);
arguments.add(REFERENCE_FILE);
Expand Down Expand Up @@ -224,18 +224,17 @@ public void testRunWithoutExactOverlapsAndLabels() throws IOException {
}

@Test(expectedExceptions = UserException.class)
public void testErrorWithoutSamHeadersAndNoReference() throws IOException {

final File outputFile = File.createTempFile("combineseg_", ".seg");
public void testErrorWithoutSamHeadersAndNoReference() {
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
runCombineSegmentBreakpoints(GROUND_TRUTH_SEGMENTS_FILE_NO_SAMHEADER, INPUT_SEGMENTS_FILE_NO_SAMHEADER,
outputFile, null);
}

@Test
public void testWithoutSamHeaderAndWithReference() throws IOException {
public void testWithoutSamHeaderAndWithReference() {

// This test is a bit more like the real world
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
runCombineSegmentBreakpoints(GROUND_TRUTH_SEGMENTS_FILE_NO_SAMHEADER, INPUT_SEGMENTS_FILE_NO_SAMHEADER, outputFile, REFERENCE_FILE);

Assert.assertTrue(outputFile.exists());
Expand All @@ -251,10 +250,10 @@ public void testWithoutSamHeaderAndWithReference() throws IOException {
}

@Test
public void testRunAndOneWithoutSamHeaderAndNoReference() throws IOException {
public void testRunAndOneWithoutSamHeaderAndNoReference() {

// This test is a bit more like the real world
final File outputFile = File.createTempFile("combineseg_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
runCombineSegmentBreakpoints(INPUT_SEGMENTS_FILE, GROUND_TRUTH_SEGMENTS_FILE_NO_SAMHEADER, outputFile, null);

Assert.assertTrue(outputFile.exists());
Expand All @@ -275,15 +274,15 @@ public void testRunAndOneWithoutSamHeaderAndNoReference() throws IOException {
}

@Test(expectedExceptions = UserException.BadInput.class)
public void testDisagreeingSamHeadersAndNoReference() throws IOException {
final File outputFile = File.createTempFile("combineseg_", ".seg");
public void testDisagreeingSamHeadersAndNoReference() {
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
runCombineSegmentBreakpoints(INPUT_SEGMENTS_FILE_ALT_SAMHEADER, GROUND_TRUTH_SEGMENTS_FILE, outputFile, null);
}

/** Reference is ignored if SAM Headers are specified. */
@Test(expectedExceptions = UserException.BadInput.class)
public void testDisagreeingSamHeadersAndReference() throws IOException {
final File outputFile = File.createTempFile("combineseg_", ".seg");
public void testDisagreeingSamHeadersAndReference() {
final File outputFile = IOUtils.createTempFile("combineseg_", ".seg");
runCombineSegmentBreakpoints(INPUT_SEGMENTS_FILE_ALT_SAMHEADER, GROUND_TRUTH_SEGMENTS_FILE, outputFile, REFERENCE_FILE);
}

Expand Down Expand Up @@ -324,8 +323,8 @@ public Object[][] provideOldFormats() {
}

@Test(dataProvider = "oldFormats")
public void testOldFormats(final String inputSegFile, final String oldFormatSegFile, final String meanColumn, final String callColumn, int numAnnotations) throws IOException {
final File outputFile = File.createTempFile("combineseg_old_formats_", ".seg");
public void testOldFormats(final String inputSegFile, final String oldFormatSegFile, final String meanColumn, final String callColumn, int numAnnotations) {
final File outputFile = IOUtils.createTempFile("combineseg_old_formats_", ".seg");
runCombineSegmentBreakpoints(inputSegFile, oldFormatSegFile, outputFile, REFERENCE_FILE, "MEAN_LOG2_COPY_RATIO", "CALL", meanColumn, callColumn);
final AnnotatedIntervalCollection regions = AnnotatedIntervalCollection.create(outputFile.toPath(),
Sets.newHashSet("MEAN_LOG2_COPY_RATIO", "CALL", meanColumn, callColumn));
Expand All @@ -335,9 +334,9 @@ public void testOldFormats(final String inputSegFile, final String oldFormatSegF
}

@Test(dataProvider = "realTsvFormats")
public void testRealFormats(final String segFile, final String externalSegFile, final String expectedResult, final String ... colsOfInterest) throws IOException {
public void testRealFormats(final String segFile, final String externalSegFile, final String expectedResult, final String ... colsOfInterest) {

final File outputFile = File.createTempFile("combineseg_realformats_gt_", ".seg");
final File outputFile = IOUtils.createTempFile("combineseg_realformats_gt_", ".seg");
runCombineSegmentBreakpoints(segFile, externalSegFile, outputFile, REFERENCE_FILE,
colsOfInterest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.tools.copynumber.arguments.CopyNumberStandardArgument;
import org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedIntervalCollection;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -23,11 +24,10 @@ public class MergeAnnotatedRegionsByAnnotationIntegrationTest extends CommandLin
* Very simple test, since this tool basically calls out to a class that does all the heavy lifting. Most testing
* is done in
* {@link org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedIntervalUtilsUnitTest}.
* @throws IOException
*/
@Test
public void testLegacySegFile() throws IOException {
final File outputFile = File.createTempFile("mergeannotatedregionsbyannotation", ".seg");
public void testLegacySegFile() {
final File outputFile = IOUtils.createTempFile("mergeannotatedregionsbyannotation", ".seg");
final String annotationToMatch = "Segment_Mean";
final List<String> arguments = new ArrayList<>();
arguments.add("--" + CopyNumberStandardArgument.SEGMENTS_FILE_LONG_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@
import org.broadinstitute.barclay.argparser.CommandLineException;
import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.testutils.ArgumentsBuilder;
import org.broadinstitute.hellbender.testutils.BaseTest;
import org.broadinstitute.hellbender.tools.copynumber.arguments.CopyNumberStandardArgument;
import org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedInterval;
import org.broadinstitute.hellbender.tools.copynumber.utils.annotatedinterval.AnnotatedIntervalCollection;
import org.broadinstitute.hellbender.utils.SimpleInterval;
import org.broadinstitute.hellbender.testutils.BaseTest;
import org.broadinstitute.hellbender.testutils.ArgumentsBuilder;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MergeAnnotatedRegionsIntegrationTest extends CommandLineProgramTest {
private static final String TEST_SUB_DIR = publicTestDir + "org/broadinstitute/hellbender/tools/copynumber/utils/";
private static final String SIMPLE_TEST_FILE = TEST_SUB_DIR + "merge-annotated-regions-simple-test.seg";
private static final String REF = hg19MiniReference;

@Test
public void basicTest() throws IOException {
public void basicTest() {
// This test is a bit more like the real world
final File outputFile = BaseTest.createTempFile("mergeannotatedregions", ".seg");
final ArgumentsBuilder arguments = new ArgumentsBuilder();
Expand Down
Loading

0 comments on commit ba2b5b3

Please sign in to comment.