Skip to content

Commit

Permalink
Removed unnecessary print line from the bgen writer.
Browse files Browse the repository at this point in the history
Renamed erroneously named method.
Resolved issues still present in the vcf field picker
  • Loading branch information
CAWarmerdam committed Dec 15, 2022
1 parent 79f8df9 commit c3588bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public boolean arePhasedProbabilitiesPresent(GeneticVariant variant) {
LinkedHashSet<VcfGenotypeFormat> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,36 @@ public VcfGenotypeFormat getVcfGenotypeFormat(

List<String> 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;
}
}
Expand All @@ -90,26 +102,48 @@ 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<VcfGenotypeFormat> genotypeDosageFieldPrecedence) {

List<String> 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;
}
}

return false;
}

public boolean isPreferredGenotypeFormatPresent(VcfRecord vcfRecord) {
List<String> formatIdentifiers = Arrays.asList(vcfRecord.getFormat());
return isGenotypeFormatPresent(formatIdentifiers, preferredGenotypeFormat);
}

private boolean isGenotypeFormatPresent(List<String> formatIdentifiers, VcfGenotypeFormat genotypeFormat) {
return formatIdentifiers.contains(this.getGenotypeFormatIdentifier(genotypeFormat));
}

public VcfGenotypeFormat getPreferredGenotypeFormat() {
return preferredGenotypeFormat;
}
Expand Down

0 comments on commit c3588bd

Please sign in to comment.