public VCFRecordCodec(final VCFHeader header, final boolean allowMissingFieldsInHeader) { this.vcfEncoder = new VCFEncoder(header, allowMissingFieldsInHeader, false); // Explicitly set the version because it's not available in the header itself. this.vcfDecoder.setVCFHeader(header, VCFHeaderVersion.VCF4_2); }
@SuppressWarnings("rawtypes") String formatVCFField(final Object val) { final String result; if ( val == null ) result = VCFConstants.MISSING_VALUE_v4; else if ( val instanceof Double ) result = formatVCFDouble((Double) val); else if ( val instanceof Boolean ) result = (Boolean)val ? "" : null; // empty string for true, null for false else if ( val instanceof List ) { result = formatVCFField(((List)val).toArray()); } else if ( val.getClass().isArray() ) { final int length = Array.getLength(val); if ( length == 0 ) return formatVCFField(null); final StringBuilder sb = new StringBuilder(formatVCFField(Array.get(val, 0))); for ( int i = 1; i < length; i++) { sb.append(','); sb.append(formatVCFField(Array.get(val, i))); } result = sb.toString(); } else result = val.toString(); return result; }
static boolean isMissingValue(final String s) { // we need to deal with the case that it's a list of missing values return (countOccurrences(VCFConstants.MISSING_VALUE_v4.charAt(0), s) + countOccurrences(',', s) == s.length()); }
else stringBuilder.append(formatQualValue(context.getPhredScaledQual())); stringBuilder.append(VCFConstants.FIELD_SEPARATOR); stringBuilder.append(getFilterString(context)).append(VCFConstants.FIELD_SEPARATOR); if ( ! this.header.hasInfoLine(field.getKey())) fieldIsMissingFromHeaderError(context, field.getKey(), "INFO"); final String outputValue = formatVCFField(field.getValue()); if (outputValue != null) infoFields.put(field.getKey(), outputValue); writeInfoString(infoFields, stringBuilder); for (final String format : genotypeAttributeKeys) if ( ! this.header.hasFormatLine(format)) fieldIsMissingFromHeaderError(context, format, "FORMAT"); stringBuilder.append(genotypeFormatString); final Map<Allele, String> alleleStrings = buildAlleleStrings(context); addGenotypeData(context, alleleStrings, genotypeAttributeKeys, stringBuilder);
else vcfOutput.append(formatQualValue(context.getPhredScaledQual())); vcfOutput.append(VCFConstants.FIELD_SEPARATOR) .append(getFilterString(context)).append(VCFConstants.FIELD_SEPARATOR); for (final Map.Entry<String, Object> field : context.getAttributes().entrySet()) { if (!this.header.hasInfoLine(field.getKey())) fieldIsMissingFromHeaderError(context, field.getKey(), "INFO"); final String outputValue = formatVCFField(field.getValue()); if (outputValue != null) infoFields.put(field.getKey(), outputValue); writeInfoString(infoFields, vcfOutput); for (final String format : genotypeAttributeKeys) if ( ! this.header.hasFormatLine(format)) fieldIsMissingFromHeaderError(context, format, "FORMAT"); vcfOutput.append(genotypeFormatString); final Map<Allele, String> alleleStrings = buildAlleleStrings(context); appendGenotypeData(context, alleleStrings, genotypeAttributeKeys, vcfOutput);
builder.append(genotypeFormatString); final VCFEncoder encoder = new VCFEncoder(header, true, false); final Map<Allele, String> alleleStrings = encoder.buildAlleleStrings(vc); encoder.addGenotypeData(vc, alleleStrings, genotypeAttributeKeys, builder);
writeAllele(g.getAllele(0), alleleMap, builder); for (int i = 1; i < g.getPloidy(); i++) { builder.append(g.isPhased() ? VCFConstants.PHASED : VCFConstants.UNPHASED); writeAllele(g.getAllele(i), alleleMap, builder); outputValue = formatVCFField(val); if ( isMissingValue(attrs.get(i))) attrs.remove(i); else break;
@Override public void encode(final VariantContext context) { this.outputStream.println(this.vcfEncoder.encode(context)); }
@Test(dataProvider = "VCFWriterDoubleFormatTestData") public void testVCFWriterDoubleFormatTestData(final double d, final String expected) { Assert.assertEquals(VCFEncoder.formatVCFDouble(d), expected, "Failed to pretty print double in VCFWriter"); }
@Test(dataProvider = "MissingFormatTestData") public void testMissingFormatFields(final VCFEncoder encoder, final VariantContext vc, final String expectedLastColumn, final Map<Allele, String> alleleMap, final List<String> genotypeFormatKeys) { final StringBuilder sb = new StringBuilder(); final String[] columns = new String[5]; encoder.addGenotypeData(vc, alleleMap, genotypeFormatKeys, sb); final int nCol = ParsingUtils.split(sb.toString(), columns, VCFConstants.FIELD_SEPARATOR_CHAR); Assert.assertEquals(columns[nCol-1], expectedLastColumn, "Format fields don't handle missing data in the expected way"); }
private String getFilterString(final VariantContext vc) { if (vc.isFiltered()) { for (final String filter : vc.getFilters()) { if (!this.header.hasFilterLine(filter)) fieldIsMissingFromHeaderError(vc, filter, "FILTER"); } return ParsingUtils.join(";", ParsingUtils.sortList(vc.getFilters())); } else if (vc.filtersWereApplied()) return VCFConstants.PASSES_FILTERS_v4; else return VCFConstants.UNFILTERED; }
else vcfOutput.append(formatQualValue(context.getPhredScaledQual())); vcfOutput.append(VCFConstants.FIELD_SEPARATOR) .append(getFilterString(context)).append(VCFConstants.FIELD_SEPARATOR); for (final Map.Entry<String, Object> field : context.getAttributes().entrySet()) { if (!this.header.hasInfoLine(field.getKey())) fieldIsMissingFromHeaderError(context, field.getKey(), "INFO"); final String outputValue = formatVCFField(field.getValue()); if (outputValue != null) infoFields.put(field.getKey(), outputValue); writeInfoString(infoFields, vcfOutput); for (final String format : genotypeAttributeKeys) if ( ! this.header.hasFormatLine(format)) fieldIsMissingFromHeaderError(context, format, "FORMAT"); vcfOutput.append(genotypeFormatString); final Map<Allele, String> alleleStrings = buildAlleleStrings(context); appendGenotypeData(context, alleleStrings, genotypeAttributeKeys, vcfOutput);
builder.append(genotypeFormatString); final VCFEncoder encoder = new VCFEncoder(header, true, false); final Map<Allele, String> alleleStrings = encoder.buildAlleleStrings(vc); encoder.addGenotypeData(vc, alleleStrings, genotypeAttributeKeys, builder);
writeAllele(g.getAllele(0), alleleMap, vcfoutput); for (int i = 1; i < g.getPloidy(); i++) { vcfoutput.append(g.isPhased() ? VCFConstants.PHASED : VCFConstants.UNPHASED); writeAllele(g.getAllele(i), alleleMap, vcfoutput); outputValue = formatVCFField(val); if (isMissingValue(attrs.get(i))) attrs.remove(i); else break;
@Override public void encode(final VariantContext context) { this.outputStream.println(this.vcfEncoder.encode(context)); }
private String getFilterString(final VariantContext vc) { if (vc.isFiltered()) { for (final String filter : vc.getFilters()) { if ( ! this.header.hasFilterLine(filter)) fieldIsMissingFromHeaderError(vc, filter, "FILTER"); } return ParsingUtils.join(";", ParsingUtils.sortList(vc.getFilters())); } else if (vc.filtersWereApplied()) return VCFConstants.PASSES_FILTERS_v4; else return VCFConstants.UNFILTERED; }
builder.append(genotypeFormatString); final VCFEncoder encoder = new VCFEncoder(header, true, false); final Map<Allele, String> alleleStrings = encoder.buildAlleleStrings(vc); encoder.addGenotypeData(vc, alleleStrings, genotypeAttributeKeys, builder);
public VCFRecordCodec(final VCFHeader header) { this.vcfEncoder = new VCFEncoder(header, false); // Explicitly set the version because it's not available in the header itself. this.vcfDecoder.setVCFHeader(header, VCFHeaderVersion.VCF4_1); }
writeAllele(g.getAllele(0), alleleMap, vcfoutput); for (int i = 1; i < g.getPloidy(); i++) { vcfoutput.append(g.isPhased() ? VCFConstants.PHASED : VCFConstants.UNPHASED); writeAllele(g.getAllele(i), alleleMap, vcfoutput); outputValue = formatVCFField(val); if (isMissingValue(attrs.get(i))) attrs.remove(i); else break;
String formatVCFField(final Object val) { final String result; if ( val == null ) result = VCFConstants.MISSING_VALUE_v4; else if ( val instanceof Double ) result = formatVCFDouble((Double) val); else if ( val instanceof Boolean ) result = (Boolean)val ? "" : null; // empty string for true, null for false else if ( val instanceof List ) { result = formatVCFField(((List)val).toArray()); } else if ( val.getClass().isArray() ) { final int length = Array.getLength(val); if ( length == 0 ) return formatVCFField(null); final StringBuilder sb = new StringBuilder(formatVCFField(Array.get(val, 0))); for ( int i = 1; i < length; i++) { sb.append(","); sb.append(formatVCFField(Array.get(val, i))); } result = sb.toString(); } else result = val.toString(); return result; }