Skip to content

Commit

Permalink
Fix output validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bartcharbon committed Oct 15, 2024
1 parent 57328f1 commit fc61d96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,33 @@ public RecordWriter create(VcfMetadata vcfMetadata, Settings settings) {
PipedOutputStream pipedOut = new PipedOutputStream();
Thread writerThread;
try {
writerThread = getWriterThread(pipedOut, writerSettings);
writerThread = createWriterThread(pipedOut, writerSettings);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
VariantContextWriter vcfWriter = createVcfWriter(writerSettings, pipedOut);
VariantContextWriter vcfWriter = createVcfWriter(pipedOut);
VCFHeader vcfHeader = createHeader(vcfMetadata, settings);
vcfWriter.writeHeader(vcfHeader);

return new RecordWriterImpl(vcfWriter, writerThread);
}

private static VariantContextWriter createVcfWriter(WriterSettings settings, OutputStream outputStream) {
Path outputVcfPath = settings.getOutputVcfPath();
if (settings.isOverwriteOutput()) {
private static Thread createWriterThread(PipedOutputStream pipedOut, WriterSettings writerSettings) throws IOException {
Path outputVcfPath = writerSettings.getOutputVcfPath();
if (writerSettings.isOverwriteOutput()) {
try {
Files.deleteIfExists(outputVcfPath);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else if (Files.exists(outputVcfPath)) {
throw new IllegalArgumentException(
format("cannot create '%s' because it already exists.", outputVcfPath));
format("cannot create '%s' because it already exists.", outputVcfPath));
}
return getWriterThread(pipedOut, writerSettings);
}

private static VariantContextWriter createVcfWriter(OutputStream outputStream) {
return new VariantContextWriterBuilder()
.clearOptions()
.setOutputStream(outputStream)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.molgenis.vcf.decisiontree.runner;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.molgenis.vcf.decisiontree.runner.info.VepMetadataMapperImpl.ALLELE_NUM;

import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.variantcontext.VariantContextBuilder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -69,12 +72,13 @@ public static class ModifiedVcfWriter {
private ModifiedVcfWriter(){}

public static Thread getWriterThread(PipedOutputStream pipedOut, WriterSettings settings) throws IOException {
Path outputVcfPath = settings.getOutputVcfPath();
PipedInputStream pipedIn = new PipedInputStream(pipedOut);

// Create a thread to write the modified output to a file
Thread writerThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(pipedIn));
BufferedWriter finalWriter = new BufferedWriter(new FileWriter(settings.getOutputVcfPath().toFile()))) {
BufferedWriter finalWriter = new BufferedWriter(new FileWriter(outputVcfPath.toFile()))) {

String line;
while ((line = reader.readLine()) != null) {
Expand Down

0 comments on commit fc61d96

Please sign in to comment.