diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/variantstovcf/VariantsToVCF.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/variantstovcf/VariantsToVCF.java index a253a57ff..32c7f3040 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/variantstovcf/VariantsToVCF.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/variantstovcf/VariantsToVCF.java @@ -137,16 +137,18 @@ public class VariantsToVCF extends RefWalker { str.put("GQ", lod); if (depth > 0) str.put("DP", String.valueOf(depth)); - gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED, str)); + VCFGenotypeRecord rec = new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED); + for ( Map.Entry entry : str.entrySet() ) + rec.setField(entry.getKey(), entry.getValue()); + gt.add(rec); numSNPs++; snpQual += av.getNegLog10PError(); } else if (BaseUtils.simpleBaseToBaseIndex(ref.getBase()) != -1) { - Map str = new HashMap(); List alleles = new ArrayList(); alleles.add(new VCFGenotypeEncoding(String.valueOf(ref.getBase()))); alleles.add(new VCFGenotypeEncoding(String.valueOf(ref.getBase()))); - gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED, str)); + gt.add(new VCFGenotypeRecord(name, alleles, VCFGenotypeRecord.PHASE.UNPHASED)); numRefs++; } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeRecord.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeRecord.java index 42b4c0ad2..02c55858c 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeRecord.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeRecord.java @@ -58,20 +58,12 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked { * @param sampleName sample name * @param genotypes list of genotypes * @param phasing phasing - * @param otherFlags other flags */ - public VCFGenotypeRecord(String sampleName, List genotypes, PHASE phasing, Map otherFlags) { + public VCFGenotypeRecord(String sampleName, List genotypes, PHASE phasing) { mSampleName = sampleName; - if (genotypes != null) this.mGenotypeAlleles.addAll(genotypes); + if (genotypes != null) + this.mGenotypeAlleles.addAll(genotypes); mPhaseType = phasing; - if (otherFlags != null) { - // we need to be backwards compatible - if ( otherFlags.containsKey(OLD_DEPTH_KEY) ) { - otherFlags.put(DEPTH_KEY, otherFlags.get(OLD_DEPTH_KEY)); - otherFlags.remove(OLD_DEPTH_KEY); - } - mFields.putAll(otherFlags); - } } public void setVCFRecord(VCFRecord record) { @@ -82,6 +74,23 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked { mSampleName = name; } + /** + * Adds a field to the genotype record. + * Throws an exception if the key is GT, as that's computed internally. + * + * @param key the field name (use static variables above for common fields) + * @param value the field value + */ + public void setField(String key, String value) { + // make sure the GT field isn't being set + if ( key.equals(GENOTYPE_KEY) ) + throw new IllegalArgumentException("Setting the GT field is not allowed as that's done internally"); + // we need to be backwards compatible + if ( key.equals(OLD_DEPTH_KEY) ) + key = DEPTH_KEY; + mFields.put(key, value); + } + /** * determine the phase of the genotype * @@ -217,7 +226,8 @@ public class VCFGenotypeRecord implements Genotype, SampleBacked { } - @Override public String toString() { + @Override + public String toString() { return String.format("[VCFGenotype %s %s %s %s]", getLocation(), mSampleName, this.mGenotypeAlleles, mFields); } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java index 322c5627e..4af47af9f 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFGenotypeWriterAdapter.java @@ -192,18 +192,12 @@ public class VCFGenotypeWriterAdapter implements VCFGenotypeWriter { * @return a VCFGenotypeRecord for the no call situation */ private VCFGenotypeRecord createNoCallRecord(String sampleName) { - Map map = new HashMap(); - List alleles = new ArrayList(); alleles.add(new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE)); alleles.add(new VCFGenotypeEncoding(VCFGenotypeRecord.EMPTY_ALLELE)); - VCFGenotypeRecord record = new VCFGenotypeRecord(sampleName, - alleles, - VCFGenotypeRecord.PHASE.UNPHASED, - map); - return record; + return new VCFGenotypeRecord(sampleName, alleles, VCFGenotypeRecord.PHASE.UNPHASED); } /** @return true if we support multisample, false otherwise */ diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java index 687dc398d..761c7960d 100644 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFReader.java @@ -256,7 +256,7 @@ public class VCFReader implements Iterator, Iterable { */ public static VCFGenotypeRecord getVCFGenotype(String sampleName, String[] keyStrings, String genotypeString, String altAlleles[], char referenceBase) { // parameters to create the VCF genotype record - Map tagToValue = new HashMap(); + HashMap tagToValue = new HashMap(); VCFGenotypeRecord.PHASE phase = VCFGenotypeRecord.PHASE.UNKNOWN; List bases = new ArrayList(); @@ -294,7 +294,11 @@ public class VCFReader implements Iterator, Iterable { else if ( genotypeString.length() > 0 ) throw new RuntimeException("VCFReader: genotype string contained additional unprocessed fields: " + genotypeString + ". This most likely means that the format string is shorter then the value fields."); - return new VCFGenotypeRecord(sampleName, bases, phase, tagToValue); + + VCFGenotypeRecord rec = new VCFGenotypeRecord(sampleName, bases, phase); + for ( Map.Entry entry : tagToValue.entrySet() ) + rec.setField(entry.getKey(), entry.getValue()); + return rec; } diff --git a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFUtils.java b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFUtils.java index ffaeaec52..362e11b24 100755 --- a/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFUtils.java +++ b/java/src/org/broadinstitute/sting/utils/genotype/vcf/VCFUtils.java @@ -210,24 +210,21 @@ public class VCFUtils { * @return a VCFGenotypeRecord */ public static VCFGenotypeRecord createVCFGenotypeRecord(VCFParameters params, VCFGenotypeRecord gtype, VCFRecord vcfrecord) { - Map map = new HashMap(); - - // calculate the genotype quality and the read depth - map.put(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount())); - params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY); - double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE); - map.put(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual)); - params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY); List alleles = createAlleleArray(gtype); for (VCFGenotypeEncoding allele : alleles) { params.addAlternateBase(allele); } - VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), - alleles, - VCFGenotypeRecord.PHASE.UNPHASED, - map); + VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), alleles, VCFGenotypeRecord.PHASE.UNPHASED); + + // calculate the genotype quality and the read depth + record.setField(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount())); + params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY); + double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE); + record.setField(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual)); + params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY); + record.setVCFRecord(vcfrecord); return record; } @@ -241,24 +238,21 @@ public class VCFUtils { * @return a VCFGenotypeRecord */ public static VCFGenotypeRecord createVCFGenotypeRecord(VCFParameters params, VCFGenotypeCall gtype) { - Map map = new HashMap(); - - // calculate the RMS mapping qualities and the read depth - map.put(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount())); - params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY); - double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE); - map.put(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual)); - params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY); List alleles = createAlleleArray(gtype); for (VCFGenotypeEncoding allele : alleles) { params.addAlternateBase(allele); } - VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), - alleles, - VCFGenotypeRecord.PHASE.UNPHASED, - map); + VCFGenotypeRecord record = new VCFGenotypeRecord(gtype.getSampleName(), alleles, VCFGenotypeRecord.PHASE.UNPHASED); + + // calculate the RMS mapping qualities and the read depth + record.setField(VCFGenotypeRecord.DEPTH_KEY, String.valueOf(gtype.getReadCount())); + params.addFormatItem(VCFGenotypeRecord.DEPTH_KEY); + double qual = Math.min(10.0 * gtype.getNegLog10PError(), VCFGenotypeRecord.MAX_QUAL_VALUE); + record.setField(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY, String.format("%.2f", qual)); + params.addFormatItem(VCFGenotypeRecord.GENOTYPE_QUALITY_KEY); + return record; } diff --git a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFRecordTest.java b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFRecordTest.java index 4198c1149..70a6599ad 100755 --- a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFRecordTest.java +++ b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFRecordTest.java @@ -56,12 +56,12 @@ public class VCFRecordTest extends BaseTest { * @return a VCFGenotypeRecord */ private static VCFGenotypeRecord createGenotype(String name, String Allele1, String Allele2) { - Map keyValues = new HashMap(); - keyValues.put("AA", "2"); List Alleles = new ArrayList(); Alleles.add(new VCFGenotypeEncoding(Allele1)); Alleles.add(new VCFGenotypeEncoding(Allele2)); - return new VCFGenotypeRecord(name, Alleles, VCFGenotypeRecord.PHASE.PHASED, keyValues); + VCFGenotypeRecord rec = new VCFGenotypeRecord(name, Alleles, VCFGenotypeRecord.PHASE.PHASED); + rec.setField("AA", "2"); + return rec; } @Test diff --git a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFWriterTest.java b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFWriterTest.java index a1592a131..b40a4d930 100644 --- a/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFWriterTest.java +++ b/java/test/org/broadinstitute/sting/utils/genotype/vcf/VCFWriterTest.java @@ -85,13 +85,12 @@ public class VCFWriterTest extends BaseTest { List gt = new ArrayList(); for (String name : header.getGenotypeSamples()) { - Map str = new HashMap(); - str.put("bb","0"); - List myAlleles = new ArrayList(); myAlleles.add(new VCFGenotypeEncoding("C")); myAlleles.add(new VCFGenotypeEncoding("D1")); - gt.add(new VCFGenotypeRecord(name, myAlleles, VCFGenotypeRecord.PHASE.PHASED, str)); + VCFGenotypeRecord rec = new VCFGenotypeRecord(name, myAlleles, VCFGenotypeRecord.PHASE.PHASED); + rec.setField("bb", "0"); + gt.add(rec); } return new VCFRecord('A',"chr1",1,"RANDOM",altBases,0,".",infoFields, "GT:AA",gt);