Bugfix for VCFWriter in the case where there are no genotypes in the VC but genotypes in the header

This commit is contained in:
Mark DePristo 2012-05-21 16:24:23 -04:00
parent 4846bf5c8e
commit 4bde24f020
2 changed files with 7 additions and 10 deletions

View File

@ -162,7 +162,7 @@ class BCF2Writer extends IndexingVariantContextWriter {
// info fields // info fields
final int nAlleles = vc.getNAlleles(); final int nAlleles = vc.getNAlleles();
final int nInfo = vc.getAttributes().size(); final int nInfo = vc.getAttributes().size();
final int nGenotypeFormatFields = VCFWriter.calcVCFGenotypeKeys(vc).size(); final int nGenotypeFormatFields = VCFWriter.calcVCFGenotypeKeys(vc, header).size();
final int nSamples = vc.getNSamples(); final int nSamples = vc.getNSamples();
encoder.encodeRawInt((nAlleles << 16) | (nInfo & 0x00FF), BCF2Type.INT32); encoder.encodeRawInt((nAlleles << 16) | (nInfo & 0x00FF), BCF2Type.INT32);
@ -207,7 +207,7 @@ class BCF2Writer extends IndexingVariantContextWriter {
} }
private byte[] buildSamplesData(final VariantContext vc) throws IOException { private byte[] buildSamplesData(final VariantContext vc) throws IOException {
List<String> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc); List<String> genotypeFields = VCFWriter.calcVCFGenotypeKeys(vc, header);
for ( final String field : genotypeFields ) { for ( final String field : genotypeFields ) {
if ( field.equals(VCFConstants.GENOTYPE_KEY) ) { if ( field.equals(VCFConstants.GENOTYPE_KEY) ) {
addGenotypes(vc); addGenotypes(vc);

View File

@ -254,13 +254,10 @@ class VCFWriter extends IndexingVariantContextWriter {
mWriter.write(VCFConstants.FIELD_SEPARATOR); mWriter.write(VCFConstants.FIELD_SEPARATOR);
mWriter.write(((LazyGenotypesContext)gc).getUnparsedGenotypeData().toString()); mWriter.write(((LazyGenotypesContext)gc).getUnparsedGenotypeData().toString());
} else { } else {
List<String> genotypeAttributeKeys = new ArrayList<String>(); List<String> genotypeAttributeKeys = calcVCFGenotypeKeys(vc, mHeader);
if ( vc.hasGenotypes() ) { if ( ! genotypeAttributeKeys.isEmpty() ) {
genotypeAttributeKeys.addAll(calcVCFGenotypeKeys(vc)); final String genotypeFormatString = ParsingUtils.join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);
}
if ( genotypeAttributeKeys.size() > 0 ) {
String genotypeFormatString = ParsingUtils.join(VCFConstants.GENOTYPE_FIELD_SEPARATOR, genotypeAttributeKeys);
mWriter.write(VCFConstants.FIELD_SEPARATOR); mWriter.write(VCFConstants.FIELD_SEPARATOR);
mWriter.write(genotypeFormatString); mWriter.write(genotypeFormatString);
@ -472,7 +469,7 @@ class VCFWriter extends IndexingVariantContextWriter {
* @param vc * @param vc
* @return an ordered list of genotype fields in use in VC. If vc has genotypes this will always include GT first * @return an ordered list of genotype fields in use in VC. If vc has genotypes this will always include GT first
*/ */
public static List<String> calcVCFGenotypeKeys(VariantContext vc) { public static List<String> calcVCFGenotypeKeys(final VariantContext vc, final VCFHeader header) {
Set<String> keys = new HashSet<String>(); Set<String> keys = new HashSet<String>();
boolean sawGoodGT = false; boolean sawGoodGT = false;
@ -504,7 +501,7 @@ class VCFWriter extends IndexingVariantContextWriter {
sortedList = newList; sortedList = newList;
} }
if ( sortedList.isEmpty() && vc.hasGenotypes() ) { if ( sortedList.isEmpty() && header.hasGenotypingData() ) {
// this needs to be done in case all samples are no-calls // this needs to be done in case all samples are no-calls
return Collections.singletonList(VCFConstants.GENOTYPE_KEY); return Collections.singletonList(VCFConstants.GENOTYPE_KEY);
} else { } else {