Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata json as a required parameter #62

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ contain the following:

## Usage
```
usage: java -jar vcf-inheritance-matcher.jar -i <arg> [-o <arg>] [-pd
usage: java -jar vcf-inheritance-matcher.jar -i <arg> -m <arg> [-o <arg>] [-pd
<arg>] [-pb <arg>] [-np <arg>] [-c] [-f] [-d]
-i,--input <arg> Input VCF file (.vcf or .vcf.gz).
-m,--metadata <arg> VCF metadata file (.json).
-o,--output <arg> Output VCF file (.vcf or .vcf.gz).
-pd,--pedigree <arg> Comma-separated list of pedigree files
(.ped).
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.molgenis</groupId>
<artifactId>vip-inheritance-matcher</artifactId>
<version>3.3.1</version>
<version>3.3.2</version>

<name>vip-inheritance-matcher</name>
<description>Annotates VCF samples with mendelian violation and possible compound flags and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AppCommandLineOptions {
static final String OPT_INPUT_LONG = "input";
static final String OPT_OUTPUT = "o";
static final String OPT_OUTPUT_LONG = "output";
static final String OPT_METADATA = "m";
static final String OPT_METADATA_LONG = "metadata";
static final String OPT_PED = "pd";
static final String OPT_PED_LONG = "pedigree";
static final String OPT_PROBANDS = "pb";
Expand Down Expand Up @@ -44,6 +46,13 @@ class AppCommandLineOptions {
.longOpt(OPT_OUTPUT_LONG)
.desc("Output VCF file (.vcf or .vcf.gz).")
.build());
appOptions.addOption(
Option.builder(OPT_METADATA)
.hasArg(true)
.required()
.longOpt(OPT_METADATA_LONG)
.desc("VCF metadata file (.json).")
.build());
appOptions.addOption(
Option.builder(OPT_PED)
.hasArg(true)
Expand Down Expand Up @@ -97,6 +106,7 @@ static Options getAppVersionOptions() {
static void validateCommandLine(CommandLine commandLine) {
validateInput(commandLine);
validateOutput(commandLine);
validateMetadata(commandLine);
}

private static void validateInput(CommandLine commandLine) {
Expand Down Expand Up @@ -132,4 +142,25 @@ private static void validateOutput(CommandLine commandLine) {
format("Output file '%s' already exists", outputPath));
}
}

private static void validateMetadata(CommandLine commandLine) {
Path metadataPath = Path.of(commandLine.getOptionValue(OPT_METADATA));
if (!Files.exists(metadataPath)) {
throw new IllegalArgumentException(
format("Metadata file '%s' does not exist.", metadataPath));
}
if (Files.isDirectory(metadataPath)) {
throw new IllegalArgumentException(
format("Metadata file '%s' is a directory.", metadataPath));
}
if (!Files.isReadable(metadataPath)) {
throw new IllegalArgumentException(
format("Metadata file '%s' is not readable.", metadataPath));
}
String inputPathStr = metadataPath.toString();
if (!inputPathStr.endsWith(".json")) {
throw new IllegalArgumentException(
format("Metadata file '%s' is not a .json file.", inputPathStr));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void run(String... args) {
private Settings mapSettings(CommandLine commandLine) {
String inputPathValue = commandLine.getOptionValue(OPT_INPUT);
Path inputPath = Path.of(inputPathValue);
Path metadataPath = Path.of(commandLine.getOptionValue(OPT_METADATA));

Path outputPath;
if (commandLine.hasOption(OPT_OUTPUT)) {
Expand Down Expand Up @@ -111,7 +112,7 @@ private Settings mapSettings(CommandLine commandLine) {

return Settings.builder().inputVcfPath(inputPath).inputPedPaths(pedPaths)
.outputPath(outputPath).probands(probandNames).overwrite(overwriteOutput).
pathogenicClasses(pathogenicClasses).debug(debugMode)
pathogenicClasses(pathogenicClasses).metadataPath(metadataPath).debug(debugMode)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Settings {
Path inputVcfPath;
List<Path> inputPedPaths;
Path outputPath;
Path metadataPath;
List<String> probands;
Set<String> pathogenicClasses;
boolean overwrite;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.molgenis.vcf.inheritance.matcher.util;

import org.molgenis.vcf.inheritance.matcher.model.Settings;
import org.molgenis.vcf.utils.metadata.FieldMetadataService;

public interface VepMetadataServiceFactory {
FieldMetadataService create();
FieldMetadataService create(Settings settings);
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
package org.molgenis.vcf.inheritance.matcher.util;

import org.molgenis.vcf.inheritance.matcher.model.Settings;
import org.molgenis.vcf.utils.metadata.FieldMetadataService;
import org.molgenis.vcf.utils.metadata.FieldMetadataServiceImpl;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Quirky class to enable reuse of {@link FieldMetadataService} from vip-utils
*/
@Component
public class VepMetadataServiceFactoryImpl implements VepMetadataServiceFactory {
private static final String EMPTY_METADATA_JSON = """
{
"format": {
},
"info": {
"CSQ": {
"nestedFields": {
}
}
}
}
""";

@Override
@SuppressWarnings("java:S5443")
public FieldMetadataService create() {
File json;
try {
Path path = Files.createTempFile("metadata", ".json");
byte[] buf = EMPTY_METADATA_JSON.getBytes(StandardCharsets.UTF_8);
Files.write(path, buf);
json = path.toFile();
json.deleteOnExit();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return new FieldMetadataServiceImpl(json);
public FieldMetadataService create(Settings settings) {
return new FieldMetadataServiceImpl(settings.getMetadataPath().toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public VcfReaderFactoryImpl(VepMetadataServiceFactoryImpl vepMetadataServiceFact
public VcfReader create(Settings settings) {
Path inputVcfPath = settings.getInputVcfPath();
VCFFileReader vcfFileReader = new VCFFileReader(inputVcfPath.toFile(), false);
VepMetadata vepMetadata = new VepMetadata(vcfFileReader.getFileHeader(), vepMetadataServiceFactoryImpl.create());
VepMetadata vepMetadata = new VepMetadata(vcfFileReader.getFileHeader(), vepMetadataServiceFactoryImpl.create(settings));
return new VcfReader(vcfFileReader, new VcfRecordFactoryImpl(vepMetadata), settings.getPathogenicClasses());
}
}
12 changes: 8 additions & 4 deletions src/test/java/org/molgenis/vcf/inheritance/matcher/AppIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class AppIT {
@Test
void testNoVep() throws IOException {
String inputFile = ResourceUtils.getFile("classpath:integration_noVEPinheritance.vcf").toString();
String metadataFile = ResourceUtils.getFile("classpath:metadata.json").toString();
String pedigree = ResourceUtils.getFile("classpath:pedigree_complex.ped").toString();
String outputFile = sharedTempDir.resolve("actual.vcf").toString();

String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree};
String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree, "-m", metadataFile};
SpringApplication.run(App.class, args);

String outputVcf = Files.readString(Path.of(outputFile));
Expand All @@ -35,9 +36,10 @@ void testNoVep() throws IOException {
@Test
void testNoPed() throws IOException {
String inputFile = ResourceUtils.getFile("classpath:integration.vcf").toString();
String metadataFile = ResourceUtils.getFile("classpath:metadata.json").toString();
String outputFile = sharedTempDir.resolve("actual.vcf").toString();

String[] args = {"-i", inputFile, "-o", outputFile, "-c", "P"};
String[] args = {"-i", inputFile, "-o", outputFile, "-c", "P", "-m", metadataFile};
SpringApplication.run(App.class, args);

String outputVcf = Files.readString(Path.of(outputFile));
Expand All @@ -51,10 +53,11 @@ void testNoPed() throws IOException {
@Test
void testProband() throws IOException {
String inputFile = ResourceUtils.getFile("classpath:integration.vcf").toString();
String metadataFile = ResourceUtils.getFile("classpath:metadata.json").toString();
String pedigree = ResourceUtils.getFile("classpath:pedigree_complex.ped").toString();
String outputFile = sharedTempDir.resolve("actual.vcf").toString();

String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree, "-pb", "Patient,Patient2", "--force" ,"-c","P,B"};
String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree, "-pb", "Patient,Patient2", "--force" ,"-c","P,B", "-m", metadataFile};
SpringApplication.run(App.class, args);

String outputVcf = Files.readString(Path.of(outputFile));
Expand All @@ -68,10 +71,11 @@ void testProband() throws IOException {
@Test
void testNoParents() throws IOException {
String inputFile = ResourceUtils.getFile("classpath:integration.vcf").toString();
String metadataFile = ResourceUtils.getFile("classpath:metadata.json").toString();
String pedigree = ResourceUtils.getFile("classpath:pedigree_fam_no_parents.ped").toString();
String outputFile = sharedTempDir.resolve("actual.vcf").toString();

String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree, "-pb", "Patient,Patient2", "--force"};
String[] args = {"-i", inputFile, "-o", outputFile, "-pd", pedigree, "-pb", "Patient,Patient2", "--force", "-m", metadataFile};
SpringApplication.run(App.class, args);

String outputVcf = Files.readString(Path.of(outputFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.molgenis.vcf.inheritance.matcher.model.Settings;

import java.nio.file.Path;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class VepMetadataServiceFactoryImplTest {
private VepMetadataServiceFactoryImpl vepMetadataServiceFactoryImpl;
Expand All @@ -13,7 +19,9 @@ void setUp() {

@Test
void create() {
Settings settings = mock(Settings.class);
when(settings.getMetadataPath()).thenReturn(Path.of("TEST"));
// test that no exception is thrown
vepMetadataServiceFactoryImpl.create();
vepMetadataServiceFactoryImpl.create(settings);
}
}
Loading