Skip to content

Commit

Permalink
Improve test coverage + fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bartcharbon committed Oct 15, 2024
1 parent e56f572 commit 28dc7ef
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void close() {
writerThread.join();
} catch (InterruptedException e) {
writerThread.interrupt();
throw new RuntimeException(e);
throw new WriterThreadInterruptedException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.molgenis.vcf.decisiontree.filter;

import java.io.Serial;
import static java.lang.String.format;

public class WriterThreadInterruptedException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;

public WriterThreadInterruptedException(String message) {
super(format("VCF writer was interrupted with message: %s", message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class InvalidVcfLineException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;

public InvalidVcfLineException(String line) {
super(
format(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.molgenis.vcf.decisiontree.runner;

import org.molgenis.vcf.decisiontree.WriterSettings;

import java.io.*;
import java.nio.file.Path;

public class ModifiedVcfWriter {
public static final String CNV_TR = "<CNV:TR>";
public static final String PATTERN = "<CNV:TR\\d+>";

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(outputVcfPath.toFile()))) {

String line;
while ((line = reader.readLine()) != null) {
String modifiedContent = line.replaceAll(PATTERN, CNV_TR);

finalWriter.write(modifiedContent);
finalWriter.newLine();
}

} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
});

writerThread.start();
return writerThread;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.molgenis.vcf.decisiontree.runner;

import static java.lang.String.format;
import static org.molgenis.vcf.decisiontree.runner.VepHelper.ModifiedVcfWriter.getWriterThread;
import static org.molgenis.vcf.decisiontree.filter.SampleAnnotatorImpl.VIPC_S;
import static org.molgenis.vcf.decisiontree.filter.SampleAnnotatorImpl.VIPP_S;
import static org.molgenis.vcf.decisiontree.filter.SampleAnnotatorImpl.VIPL_S;
import static org.molgenis.vcf.decisiontree.runner.ModifiedVcfWriter.getWriterThread;
import static org.molgenis.vcf.utils.utils.HeaderUtils.fixVcfFilterHeaderLines;
import static org.molgenis.vcf.utils.utils.HeaderUtils.fixVcfFormatHeaderLines;
import static org.molgenis.vcf.utils.utils.HeaderUtils.fixVcfInfoHeaderLines;
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/molgenis/vcf/decisiontree/runner/VepHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.variantcontext.VariantContextBuilder;

import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.util.Strings;
import org.molgenis.vcf.decisiontree.WriterSettings;
import org.molgenis.vcf.decisiontree.filter.UnknownFieldException;
import org.molgenis.vcf.decisiontree.filter.VcfRecord;
import org.molgenis.vcf.decisiontree.filter.model.FieldType;
Expand Down Expand Up @@ -62,37 +59,4 @@ public VcfRecord createEmptyCsqRecord(VcfRecord vcfRecord,
singletonList(Strings.join(values, '|')));
return new VcfRecord(variantContextBuilder.make());
}

public static class ModifiedVcfWriter {
public static final String CNV_TR = "<CNV:TR>";
public static final String PATTERN = "<CNV:TR\\d+>";

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(outputVcfPath.toFile()))) {

String line;
while ((line = reader.readLine()) != null) {
String modifiedContent = line.replaceAll(PATTERN, CNV_TR);

finalWriter.write(modifiedContent);
finalWriter.newLine();
}

} catch (IOException ioException) {
throw new UncheckedIOException(ioException);
}
});

writerThread.start();
return writerThread;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.molgenis.vcf.decisiontree.filter;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class WriterThreadInterruptedExceptionTest {

@Test
void getMessage() {
assertEquals(
"VCF writer was interrupted with message: I don't want to interrupt, but ...",
new WriterThreadInterruptedException(
"I don't want to interrupt, but ...").getMessage());
}
}

0 comments on commit 28dc7ef

Please sign in to comment.