Skip to content

Commit

Permalink
Merge pull request #27 from molgenis/fix/format_multi_missing
Browse files Browse the repository at this point in the history
Fix #26 missing values for format fields with multiple values
  • Loading branch information
bartcharbon authored Jul 6, 2023
2 parents 408fb0d + f8cbb14 commit 3ad10b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/VcfWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,19 @@ function writeFieldValueSingle(field: FieldMetadata, value: Value, missingValue
return vcf;
}

function writeFieldValueMultiple(field: FieldMetadata, values: ValueArray, separator: string): string {
function writeFieldValueMultiple(
field: FieldMetadata,
values: ValueArray,
separator: string,
missingValue = MISSING
): string {
const vcf = [];

for (const infoValue of values) {
if (field.nested) {
vcf.push(writeFieldValueNested(field.nested, infoValue as ValueArray));
} else {
vcf.push(writeFieldValue(field, infoValue, ""));
vcf.push(writeFieldValue(field, infoValue, missingValue));
}
}

Expand All @@ -164,7 +169,7 @@ function writeFieldValueNested(nestedField: NestedFieldMetadata, nestedValues: V
if (infoField.number.count === 1) {
vcf.push(writeFieldValueSingle(infoField, nestedValues[index], ""));
} else {
vcf.push(writeFieldValueMultiple(infoField, nestedValues[index] as ValueArray, "&"));
vcf.push(writeFieldValueMultiple(infoField, nestedValues[index] as ValueArray, "&", ""));
}
}
return vcf.join(nestedField.separator);
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/VcfWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ test("parse and write vcf: Samples none", () => {
expect(writeVcf(parseVcf(vcfSamples), { samples: [] })).toBe(expectedVcfSamples);
});

test("parse and write vcf: Samples missing values", () => {
const expectedVcfSamplesMissingValues = `##fileformat=VCFv4.2
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
##contig=<ID=1,length=249250621,assembly=b37>
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSAMPLE0\tSAMPLE1\tSAMPLE2
1\t1\t.\tA\tG\t.\t.\t.\tGT:HQ:GQ\t0|1:.:1\t0/1:.,.:2\t1/1:.,4:3
`;
expect(writeVcf(parseVcf(vcfSamplesMissingValues))).toBe(expectedVcfSamplesMissingValues);
});

const vcfIdRefAltQualFilter = `##fileformat=VCFv4.2
##FILTER=<ID=q10,Description="Quality below 10">
##FILTER=<ID=q20,Description="Quality below 20">
Expand Down Expand Up @@ -131,3 +143,12 @@ const vcfSamples = `##fileformat=VCFv4.2
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSAMPLE0\tSAMPLE1\tSAMPLE2
1\t1\t.\tA\tG\t.\t.\t.\tGT:AD:GQ:HQ\t0|1:1,50:1:2,3\t0/1:20,80:.:4,5\t1/1
`;

const vcfSamplesMissingValues = `##fileformat=VCFv4.2
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
##contig=<ID=1,length=249250621,assembly=b37>
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tSAMPLE0\tSAMPLE1\tSAMPLE2
1\t1\t.\tA\tG\t.\t.\t.\tGT:HQ:GQ\t0|1:.:1\t0/1:.,.:2\t1/1:.,4:3
`;

0 comments on commit 3ad10b2

Please sign in to comment.