From 72866b1c7145cf29af184b137b0153423c43b077 Mon Sep 17 00:00:00 2001 From: jose miguel mut Date: Tue, 24 Jan 2017 15:42:32 +0000 Subject: [PATCH] add GenotypedVcfJobParametersValidator and test --- .../GenotypedVcfJobParametersValidator.java | 70 ++++++ ...atisticsLoaderStepParametersValidator.java | 2 +- ...gregatedVcfJobParametersValidatorTest.java | 16 +- ...enotypedVcfJobParametersValidatorTest.java | 234 ++++++++++++++++++ 4 files changed, 316 insertions(+), 6 deletions(-) create mode 100644 src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidator.java create mode 100644 src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidatorTest.java diff --git a/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidator.java b/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidator.java new file mode 100644 index 000000000..4e7d4c016 --- /dev/null +++ b/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidator.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 EMBL - European Bioinformatics Institute + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.ac.ebi.eva.pipeline.parameters.validation.job; + +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersInvalidException; +import org.springframework.batch.core.JobParametersValidator; +import org.springframework.batch.core.job.CompositeJobParametersValidator; +import org.springframework.batch.core.job.DefaultJobParametersValidator; + +import uk.ac.ebi.eva.pipeline.parameters.JobParametersNames; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.AnnotationLoaderStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.FileLoaderStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.PopulationStatisticsGeneratorStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.PopulationStatisticsLoaderStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.VariantLoaderStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.VepAnnotationGeneratorStepParametersValidator; +import uk.ac.ebi.eva.pipeline.parameters.validation.step.VepInputGeneratorStepParametersValidator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Validates the job parameters necessary to execute an {@link uk.ac.ebi.eva.pipeline.jobs.GenotypedVcfJob} + */ +public class GenotypedVcfJobParametersValidator extends DefaultJobParametersValidator { + + @Override + public void validate(JobParameters parameters) throws JobParametersInvalidException { + compositeJobParametersValidator(parameters).validate(parameters); + } + + private CompositeJobParametersValidator compositeJobParametersValidator(JobParameters jobParameters) { + List jobParametersValidators = new ArrayList<>(); + + jobParametersValidators.add(new VariantLoaderStepParametersValidator()); + jobParametersValidators.add(new FileLoaderStepParametersValidator()); + + Boolean skipAnnotation = Boolean.valueOf(jobParameters.getString(JobParametersNames.ANNOTATION_SKIP)); + if (!skipAnnotation) { + jobParametersValidators.add(new VepInputGeneratorStepParametersValidator()); + jobParametersValidators.add(new VepAnnotationGeneratorStepParametersValidator()); + jobParametersValidators.add(new AnnotationLoaderStepParametersValidator()); + } + + Boolean skipStats = Boolean.valueOf(jobParameters.getString(JobParametersNames.STATISTICS_SKIP)); + if (!skipStats) { + jobParametersValidators.add(new PopulationStatisticsGeneratorStepParametersValidator()); + jobParametersValidators.add(new PopulationStatisticsLoaderStepParametersValidator()); + } + + CompositeJobParametersValidator compositeJobParametersValidator = new CompositeJobParametersValidator(); + compositeJobParametersValidator.setValidators(jobParametersValidators); + return compositeJobParametersValidator; + } + +} diff --git a/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/step/PopulationStatisticsLoaderStepParametersValidator.java b/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/step/PopulationStatisticsLoaderStepParametersValidator.java index 4d4e1f2a5..db4d4c9ff 100644 --- a/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/step/PopulationStatisticsLoaderStepParametersValidator.java +++ b/src/main/java/uk/ac/ebi/eva/pipeline/parameters/validation/step/PopulationStatisticsLoaderStepParametersValidator.java @@ -40,7 +40,7 @@ * {@link PopulationStatisticsLoaderStep} */ public class PopulationStatisticsLoaderStepParametersValidator extends DefaultJobParametersValidator { - + public PopulationStatisticsLoaderStepParametersValidator() { super(new String[]{JobParametersNames.INPUT_STUDY_ID, JobParametersNames.INPUT_VCF_ID, diff --git a/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/AggregatedVcfJobParametersValidatorTest.java b/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/AggregatedVcfJobParametersValidatorTest.java index 4aa38404c..439634949 100644 --- a/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/AggregatedVcfJobParametersValidatorTest.java +++ b/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/AggregatedVcfJobParametersValidatorTest.java @@ -67,7 +67,7 @@ public void setUp() throws Exception { requiredParameters.put(JobParametersNames.INPUT_STUDY_TYPE, new JobParameter("COLLECTION")); // annotation - requiredParameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); + requiredParameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("false")); annotationParameters = new TreeMap<>(); annotationParameters.put(JobParametersNames.OUTPUT_DIR_ANNOTATION, new JobParameter(dir)); @@ -98,6 +98,13 @@ public void allJobParametersAreValid() throws JobParametersInvalidException { parameters.putAll(optionalParameters); validator.validate(new JobParameters(parameters)); } + @Test + public void allRequiredJobParametersAreValid() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(annotationParameters); + validator.validate(new JobParameters(parameters)); + } @Test(expected = JobParametersInvalidException.class) public void dbNameIsRequiredSkippingAnnotation() throws JobParametersInvalidException { @@ -105,6 +112,7 @@ public void dbNameIsRequiredSkippingAnnotation() throws JobParametersInvalidExce parameters.putAll(requiredParameters); parameters.putAll(optionalParameters); parameters.remove(JobParametersNames.DB_NAME); + parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); validator.validate(new JobParameters(parameters)); } @@ -114,7 +122,6 @@ public void dbNameIsRequiredWithoutSkippingAnnotation() throws JobParametersInva parameters.putAll(requiredParameters); parameters.putAll(optionalParameters); parameters.putAll(annotationParameters); - parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("false")); parameters.remove(JobParametersNames.DB_NAME); validator.validate(new JobParameters(parameters)); } @@ -126,6 +133,7 @@ public void annotationParametersAreNotRequiredIfAnnotationIsSkipped() throws Job Map parameters = new TreeMap<>(); parameters.putAll(requiredParameters); parameters.putAll(optionalParameters); + parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); validator.validate(new JobParameters(parameters)); } @@ -134,12 +142,11 @@ public void annotationParametersAreRequiredIfAnnotationIsNotSkipped() throws Job Map parameters = new TreeMap<>(); parameters.putAll(requiredParameters); parameters.putAll(optionalParameters); - parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("false")); validator.validate(new JobParameters(parameters)); } /** - * The parameters APP_VEP_CACHE_SPECIES is chosen as one belonging to the annotation parameters. We don't check + * The parameter APP_VEP_CACHE_SPECIES is chosen as one belonging to the annotation parameters. We don't check * for every annotation parameter, because in AnnotationLoaderStepParametersValidatorTest, * VepAnnotationGeneratorStepParametersValidatorTest and VepInputGeneratorStepParametersValidatorTest, it is already * checked that every missing required parameter makes the validation fail. @@ -151,7 +158,6 @@ public void appVepCacheSpeciesIsRequiredIfAnnotationIsNotSkipped() throws JobPar parameters.putAll(annotationParameters); parameters.putAll(optionalParameters); parameters.remove(JobParametersNames.APP_VEP_CACHE_SPECIES); - parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("false")); validator.validate(new JobParameters(parameters)); } } diff --git a/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidatorTest.java b/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidatorTest.java new file mode 100644 index 000000000..77e5cdc6b --- /dev/null +++ b/src/test/java/uk/ac/ebi/eva/pipeline/parameters/validation/job/GenotypedVcfJobParametersValidatorTest.java @@ -0,0 +1,234 @@ +/* + * Copyright 2017 EMBL - European Bioinformatics Institute + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.ac.ebi.eva.pipeline.parameters.validation.job; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.springframework.batch.core.JobParameter; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersInvalidException; + +import uk.ac.ebi.eva.pipeline.parameters.JobParametersNames; +import uk.ac.ebi.eva.test.rules.PipelineTemporaryFolderRule; + +import java.util.Map; +import java.util.TreeMap; + +/** + * Tests that the arguments necessary to run a {@link uk.ac.ebi.eva.pipeline.jobs.AggregatedVcfJob} are + * correctly validated + */ +public class GenotypedVcfJobParametersValidatorTest { + + private GenotypedVcfJobParametersValidator validator; + + @Rule + public PipelineTemporaryFolderRule temporaryFolder = new PipelineTemporaryFolderRule(); + + private Map requiredParameters; + + private Map annotationParameters; + + private Map statsParameters; + + private Map optionalParameters; + + @Before + public void setUp() throws Exception { + validator = new GenotypedVcfJobParametersValidator(); + final String dir = temporaryFolder.getRoot().getCanonicalPath(); + + requiredParameters = new TreeMap<>(); + + // variant load step + requiredParameters.put(JobParametersNames.DB_NAME, new JobParameter("database")); + requiredParameters.put(JobParametersNames.DB_COLLECTIONS_VARIANTS_NAME, new JobParameter("variants")); + requiredParameters.put(JobParametersNames.INPUT_STUDY_ID, new JobParameter("inputStudyId")); + requiredParameters.put(JobParametersNames.INPUT_VCF_ID, new JobParameter("inputVcfId")); + requiredParameters.put(JobParametersNames.INPUT_VCF_AGGREGATION, new JobParameter("NONE")); + requiredParameters.put(JobParametersNames.INPUT_VCF, + new JobParameter(temporaryFolder.newFile().getCanonicalPath())); + + // file load step + requiredParameters.put(JobParametersNames.DB_COLLECTIONS_FILES_NAME, new JobParameter("collectionsFilesName")); + requiredParameters.put(JobParametersNames.INPUT_STUDY_NAME, new JobParameter("inputStudyName")); + requiredParameters.put(JobParametersNames.INPUT_STUDY_TYPE, new JobParameter("COLLECTION")); + + // skips + requiredParameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("false")); + requiredParameters.put(JobParametersNames.STATISTICS_SKIP, new JobParameter("false")); + + // annotation + annotationParameters = new TreeMap<>(); + annotationParameters.put(JobParametersNames.OUTPUT_DIR_ANNOTATION, new JobParameter(dir)); + annotationParameters.put(JobParametersNames.APP_VEP_CACHE_SPECIES, new JobParameter("Human")); + annotationParameters.put(JobParametersNames.APP_VEP_CACHE_VERSION, new JobParameter("100_A")); + annotationParameters.put(JobParametersNames.APP_VEP_NUMFORKS, new JobParameter("6")); + annotationParameters.put(JobParametersNames.APP_VEP_CACHE_PATH, + new JobParameter(temporaryFolder.getRoot().getCanonicalPath())); + annotationParameters.put(JobParametersNames.APP_VEP_PATH, + new JobParameter(temporaryFolder.newFile().getCanonicalPath())); + annotationParameters.put(JobParametersNames.INPUT_FASTA, + new JobParameter(temporaryFolder.newFile().getCanonicalPath())); + + // statistics + statsParameters = new TreeMap<>(); + statsParameters.put(JobParametersNames.OUTPUT_DIR_STATISTICS, new JobParameter(dir)); + + + // optionals + optionalParameters = new TreeMap<>(); + optionalParameters.put(JobParametersNames.CONFIG_CHUNK_SIZE, new JobParameter("100")); + optionalParameters.put(JobParametersNames.CONFIG_RESTARTABILITY_ALLOW, new JobParameter("true")); + optionalParameters.put(JobParametersNames.STATISTICS_OVERWRITE, new JobParameter("true")); + } + + // The next tests show behaviour about the required parameters + + @Test + public void allJobParametersAreValid() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.putAll(statsParameters); + validator.validate(new JobParameters(parameters)); + } + + @Test + public void allRequiredJobParametersAreValid() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(annotationParameters); + parameters.putAll(statsParameters); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void dbNameIsRequiredSkippingAnnotationAndStats() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.remove(JobParametersNames.DB_NAME); + parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); + parameters.put(JobParametersNames.STATISTICS_SKIP, new JobParameter("true")); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void dbNameIsRequiredWithoutSkippingAnnotation() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.remove(JobParametersNames.DB_NAME); + parameters.put(JobParametersNames.STATISTICS_SKIP, new JobParameter("true")); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void dbNameIsRequiredWithoutSkippingStats() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(statsParameters); + parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); + parameters.remove(JobParametersNames.DB_NAME); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void dbNameIsRequiredWithoutSkippingAnnotationAndStats() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.putAll(statsParameters); + parameters.remove(JobParametersNames.DB_NAME); + validator.validate(new JobParameters(parameters)); + } + + // The next tests show what happens when not all the annotation parameters are present + + @Test + public void annotationParametersAreNotRequiredIfAnnotationIsSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(statsParameters); + parameters.put(JobParametersNames.ANNOTATION_SKIP, new JobParameter("true")); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void annotationParametersAreRequiredIfAnnotationIsNotSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(statsParameters); + validator.validate(new JobParameters(parameters)); + } + + /** + * The parameter APP_VEP_CACHE_SPECIES is chosen as one belonging to the annotation parameters. We don't check + * for every annotation parameter, because in AnnotationLoaderStepParametersValidatorTest, + * VepAnnotationGeneratorStepParametersValidatorTest and VepInputGeneratorStepParametersValidatorTest, it is already + * checked that every missing required parameter makes the validation fail. + */ + @Test(expected = JobParametersInvalidException.class) + public void appVepCacheSpeciesIsRequiredIfAnnotationIsNotSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.putAll(statsParameters); + parameters.remove(JobParametersNames.APP_VEP_CACHE_SPECIES); + validator.validate(new JobParameters(parameters)); + } + + // The next tests show what happens when not all the stats parameters are present + + @Test + public void statsParametersAreNotRequiredIfStatsIsSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.put(JobParametersNames.STATISTICS_SKIP, new JobParameter("true")); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void statsParametersAreRequiredIfStatsIsNotSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + validator.validate(new JobParameters(parameters)); + } + + @Test(expected = JobParametersInvalidException.class) + public void outputDirStatistitcsIsRequiredIfStatsIsNotSkipped() throws JobParametersInvalidException { + Map parameters = new TreeMap<>(); + parameters.putAll(requiredParameters); + parameters.putAll(optionalParameters); + parameters.putAll(annotationParameters); + parameters.putAll(statsParameters); + parameters.remove(JobParametersNames.OUTPUT_DIR_STATISTICS); + validator.validate(new JobParameters(parameters)); + } +}