diff --git a/Genotype-IO/src/main/java/org/molgenis/genotype/bgen/BgenGenotypeWriter.java b/Genotype-IO/src/main/java/org/molgenis/genotype/bgen/BgenGenotypeWriter.java index aaaf4bf33..fc0dee0fe 100644 --- a/Genotype-IO/src/main/java/org/molgenis/genotype/bgen/BgenGenotypeWriter.java +++ b/Genotype-IO/src/main/java/org/molgenis/genotype/bgen/BgenGenotypeWriter.java @@ -301,8 +301,7 @@ private void addMetaData(File bgenFile, BgenixWriter bgenixWriter) throws IOExce byte[] firstBytes = new byte[1000]; randomAccessBgenFile.read(firstBytes, 0, 1000); - //Add current time in int. - System.out.println((System.currentTimeMillis() / 1000L)); + // Add current time in int. // Create and write new metadata. BgenixMetadata m = new BgenixMetadata( bgenFile.getName(), diff --git a/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeData.java b/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeData.java index 8f91658af..8454bbc2d 100644 --- a/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeData.java +++ b/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeData.java @@ -324,7 +324,7 @@ public boolean arePhasedProbabilitiesPresent(GeneticVariant variant) { LinkedHashSet haplotypeProbabilitiesFields = getVcfHaplotypeFormats(variant); // If the requested format is set and present for this variant base decision on this format - return genotypeFormatSupplier.VcfGenotypeFormatReadable( + return genotypeFormatSupplier.vcfGenotypeFormatReadable( vcfRecord, haplotypeProbabilitiesFields); } diff --git a/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeField/VcfGenotypeFormatSupplier.java b/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeField/VcfGenotypeFormatSupplier.java index ee946a9c9..d63b93e42 100644 --- a/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeField/VcfGenotypeFormatSupplier.java +++ b/Genotype-IO/src/main/java/org/molgenis/genotype/vcf/VcfGenotypeField/VcfGenotypeFormatSupplier.java @@ -54,24 +54,36 @@ public VcfGenotypeFormat getVcfGenotypeFormat( List formatIdentifiers = Arrays.asList(vcfRecord.getFormat()); - if (preferredGenotypeFormat != null - && genotypeDosageFieldPrecedence.contains(preferredGenotypeFormat) - && formatIdentifiers.contains(this.getGenotypeFormatIdentifier(preferredGenotypeFormat))) { - return preferredGenotypeFormat; - } - - if (this.forcePreferredGenotypeFormat) { - throw new GenotypeDataException(String.format( - "Preferred genotype format field (%s) is unavailable for vcf record: %n%s (%s:%s). " + - "Available format fields: %s", - preferredGenotypeFormatIdentifier, - String.join(", ", vcfRecord.getIdentifiers()), - vcfRecord.getChromosome(), vcfRecord.getPosition(), - String.join(", ", vcfRecord.getFormat()))); + // Check if the preferred genotype format is set + if (preferredGenotypeFormat != null) { + // If it is set, check if it is available, and, if it is not, if we should write exceptions or not. + if (genotypeDosageFieldPrecedence.contains(preferredGenotypeFormat) + && isGenotypeFormatPresent(formatIdentifiers, preferredGenotypeFormat)) { + return preferredGenotypeFormat; + } else if (this.forcePreferredGenotypeFormat) { + if (!isGenotypeFormatPresent(formatIdentifiers, preferredGenotypeFormat)) { + throw new GenotypeDataException(String.format( + "Preferred genotype format field (%s) is unavailable for vcf record: %n%s (%s:%s). " + + "Available format fields: %s", + preferredGenotypeFormatIdentifier, + String.join(", ", vcfRecord.getIdentifiers()), + vcfRecord.getChromosome(), vcfRecord.getPosition(), + String.join(", ", vcfRecord.getFormat()))); + } else if (!genotypeDosageFieldPrecedence.contains(preferredGenotypeFormat)) { + throw new GenotypeDataException(String.format( + "Preferred genotype format field (%s) cannot be used. " + + "Requested to load vcf record %n%s (%s:%s). " + + "Possible format fields: %s", + preferredGenotypeFormatIdentifier, + String.join(", ", vcfRecord.getIdentifiers()), + vcfRecord.getChromosome(), vcfRecord.getPosition(), + String.join(", ", Arrays.toString(genotypeDosageFieldPrecedence.toArray())))); + } + } } for (VcfGenotypeFormat genotypeFormat: genotypeDosageFieldPrecedence) { - if (formatIdentifiers.contains(this.getGenotypeFormatIdentifier(genotypeFormat))) { + if (isGenotypeFormatPresent(formatIdentifiers, genotypeFormat)) { return genotypeFormat; } } @@ -90,19 +102,32 @@ public VcfGenotypeFormat getVcfGenotypeFormat( * the genotype field formats from the precedence list can be read from the vcf record. * If nothing matches these conditions, false is returned. */ - public boolean VcfGenotypeFormatReadable( + public boolean vcfGenotypeFormatReadable( VcfRecord vcfRecord, LinkedHashSet genotypeDosageFieldPrecedence) { List formatIdentifiers = Arrays.asList(vcfRecord.getFormat()); + // Test if the preferred genotype format is present + // Test if we should suppress exception if this is not the case + // Test if the + if (preferredGenotypeFormat != null) { - return genotypeDosageFieldPrecedence.contains(preferredGenotypeFormat) - && formatIdentifiers.contains(this.getGenotypeFormatIdentifier(preferredGenotypeFormat)); + if (isGenotypeFormatPresent(formatIdentifiers, preferredGenotypeFormat)) { + return genotypeDosageFieldPrecedence.contains(preferredGenotypeFormat); + } else if (this.forcePreferredGenotypeFormat) { + throw new GenotypeDataException(String.format( + "Preferred genotype format field (%s) is unavailable for vcf record: %n%s (%s:%s). " + + "Available format fields: %s", + preferredGenotypeFormatIdentifier, + String.join(", ", vcfRecord.getIdentifiers()), + vcfRecord.getChromosome(), vcfRecord.getPosition(), + String.join(", ", vcfRecord.getFormat()))); + } } for (VcfGenotypeFormat genotypeFormat: genotypeDosageFieldPrecedence) { - if (formatIdentifiers.contains(this.getGenotypeFormatIdentifier(genotypeFormat))) { + if (isGenotypeFormatPresent(formatIdentifiers, genotypeFormat)) { return true; } } @@ -110,6 +135,15 @@ public boolean VcfGenotypeFormatReadable( return false; } + public boolean isPreferredGenotypeFormatPresent(VcfRecord vcfRecord) { + List formatIdentifiers = Arrays.asList(vcfRecord.getFormat()); + return isGenotypeFormatPresent(formatIdentifiers, preferredGenotypeFormat); + } + + private boolean isGenotypeFormatPresent(List formatIdentifiers, VcfGenotypeFormat genotypeFormat) { + return formatIdentifiers.contains(this.getGenotypeFormatIdentifier(genotypeFormat)); + } + public VcfGenotypeFormat getPreferredGenotypeFormat() { return preferredGenotypeFormat; }