From 856905ee5b41faf058b7234aa99849c375f9ccf9 Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Wed, 13 Jun 2012 14:01:35 -0400 Subject: [PATCH] Cleanup Genotypes -- Renamed getAttribute to getExtendedAttribute, as this is really what this function does -- Added a few more genotype tests --- .../indels/SomaticIndelDetectorWalker.java | 2 +- .../walkers/phasing/PhaseByTransmission.java | 6 ++--- .../validation/GenotypeAndValidateWalker.java | 2 +- .../evaluators/ValidationReport.java | 4 ++-- .../sting/utils/SequenceDictionaryUtils.java | 6 ++--- .../utils/variantcontext/CommonInfo.java | 10 ++++---- .../utils/variantcontext/FastGenotype.java | 7 ++---- .../sting/utils/variantcontext/Genotype.java | 24 +++++++++---------- .../utils/variantcontext/SlowGenotype.java | 6 ++--- .../writer/BCF2FieldWriter.java | 6 ++--- .../variantcontext/writer/VCFWriter.java | 2 +- .../variantcontext/GenotypeUnitTest.java | 6 ++--- .../VariantContextBenchmark.java | 2 +- .../VariantContextTestProvider.java | 22 +++++++++++++++++ 14 files changed, 62 insertions(+), 43 deletions(-) diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/indels/SomaticIndelDetectorWalker.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/indels/SomaticIndelDetectorWalker.java index d7b7decb7..e220b1f8d 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/indels/SomaticIndelDetectorWalker.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/indels/SomaticIndelDetectorWalker.java @@ -617,7 +617,7 @@ public class SomaticIndelDetectorWalker extends ReadWalker { throw new StingException("Read "+read.getReadName()+" from "+getToolkit().getSourceFileForReaderID(getToolkit().getReaderIDForRead(read))+ "has no Normal/Tumor tag associated with it"); -// String rg = (String)read.getAttribute("RG"); +// String rg = (String)read.getExtendedAttribute("RG"); // if ( rg == null ) // throw new UserException.MalformedBam(read, "Read "+read.getReadName()+" has no read group in merged stream. RG is required for somatic calls."); diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java index 3cd14e849..aa4b4ab78 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/phasing/PhaseByTransmission.java @@ -807,7 +807,7 @@ public class PhaseByTransmission extends RodWalker, HashMa updateTrioMetricsCounters(phasedMother,phasedFather,phasedChild,mvCount,metricsCounters); mvfLine = String.format("%s\t%d\t%s\t%s\t%s\t%s\t%s:%s:%s:%s\t%s:%s:%s:%s\t%s:%s:%s:%s", vc.getChr(),vc.getStart(),vc.getFilters(),vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY),sample.toString(), - phasedMother.getAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedMother.getGenotypeString(),phasedMother.getDP(),Arrays.asList(phasedMother.getAD()), + phasedMother.getExtendedAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedMother.getGenotypeString(),phasedMother.getDP(),Arrays.asList(phasedMother.getAD()), phasedMother.getLikelihoodsString(), phasedFather.getGenotypeString(),phasedFather.getDP(),Arrays.asList(phasedFather.getAD()),phasedFather.getLikelihoodsString(), phasedChild.getGenotypeString(),Arrays.asList(phasedChild.getDP()),phasedChild.getAD(),phasedChild.getLikelihoodsString()); if(!(phasedMother.getType()==mother.getType() && phasedFather.getType()==father.getType() && phasedChild.getType()==child.getType())) @@ -819,7 +819,7 @@ public class PhaseByTransmission extends RodWalker, HashMa metricsCounters.put(NUM_GENOTYPES_MODIFIED,metricsCounters.get(NUM_GENOTYPES_MODIFIED)+1); mvfLine = String.format("%s\t%d\t%s\t%s\t%s\t%s\t%s:%s:%s:%s\t.:.:.:.\t%s:%s:%s:%s", vc.getChr(),vc.getStart(),vc.getFilters(),vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY),sample.toString(), - phasedMother.getAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedMother.getGenotypeString(),phasedMother.getDP(),Arrays.asList(phasedMother.getAD()),phasedMother.getLikelihoodsString(), + phasedMother.getExtendedAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedMother.getGenotypeString(),phasedMother.getDP(),Arrays.asList(phasedMother.getAD()),phasedMother.getLikelihoodsString(), phasedChild.getGenotypeString(),phasedChild.getDP(),Arrays.asList(phasedChild.getAD()),phasedChild.getLikelihoodsString()); } } @@ -830,7 +830,7 @@ public class PhaseByTransmission extends RodWalker, HashMa metricsCounters.put(NUM_GENOTYPES_MODIFIED,metricsCounters.get(NUM_GENOTYPES_MODIFIED)+1); mvfLine = String.format("%s\t%d\t%s\t%s\t%s\t%s\t.:.:.:.\t%s:%s:%s:%s\t%s:%s:%s:%s", vc.getChr(),vc.getStart(),vc.getFilters(),vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY),sample.toString(), - phasedFather.getAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedFather.getGenotypeString(),phasedFather.getDP(),Arrays.asList(phasedFather.getAD()),phasedFather.getLikelihoodsString(), + phasedFather.getExtendedAttribute(TRANSMISSION_PROBABILITY_TAG_NAME),phasedFather.getGenotypeString(),phasedFather.getDP(),Arrays.asList(phasedFather.getAD()),phasedFather.getLikelihoodsString(), phasedChild.getGenotypeString(),phasedChild.getDP(),Arrays.asList(phasedChild.getAD()),phasedChild.getLikelihoodsString()); } diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/validation/GenotypeAndValidateWalker.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/validation/GenotypeAndValidateWalker.java index 0930d5136..62473031f 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/validation/GenotypeAndValidateWalker.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/validation/GenotypeAndValidateWalker.java @@ -423,7 +423,7 @@ public class GenotypeAndValidateWalker extends RodWalker 2 ) { return SiteStatus.POLY; -//// System.out.printf("multiple alleles %s = %s%n", vc.getAlleles(), vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY)); +//// System.out.printf("multiple alleles %s = %s%n", vc.getAlleles(), vc.getExtendedAttribute(VCFConstants.ALLELE_COUNT_KEY)); // // todo -- omg this is painful. We need a better approach to dealing with multi-valued attributes -// for ( String v : (List)vc.getAttribute(VCFConstants.ALLELE_COUNT_KEY) ) +// for ( String v : (List)vc.getExtendedAttribute(VCFConstants.ALLELE_COUNT_KEY) ) // ac += Integer.valueOf(v); //// System.out.printf(" ac = %d%n", ac); } diff --git a/public/java/src/org/broadinstitute/sting/utils/SequenceDictionaryUtils.java b/public/java/src/org/broadinstitute/sting/utils/SequenceDictionaryUtils.java index ee47a0f2b..d7a390692 100755 --- a/public/java/src/org/broadinstitute/sting/utils/SequenceDictionaryUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/SequenceDictionaryUtils.java @@ -225,9 +225,9 @@ public class SequenceDictionaryUtils { return false; // todo -- reenable if we want to be really strict here -// if (me.getAttribute(SAMSequenceRecord.MD5_TAG) != null && that.getAttribute(SAMSequenceRecord.MD5_TAG) != null) { -// final BigInteger thisMd5 = new BigInteger((String)me.getAttribute(SAMSequenceRecord.MD5_TAG), 16); -// final BigInteger thatMd5 = new BigInteger((String)that.getAttribute(SAMSequenceRecord.MD5_TAG), 16); +// if (me.getExtendedAttribute(SAMSequenceRecord.MD5_TAG) != null && that.getExtendedAttribute(SAMSequenceRecord.MD5_TAG) != null) { +// final BigInteger thisMd5 = new BigInteger((String)me.getExtendedAttribute(SAMSequenceRecord.MD5_TAG), 16); +// final BigInteger thatMd5 = new BigInteger((String)that.getExtendedAttribute(SAMSequenceRecord.MD5_TAG), 16); // if (!thisMd5.equals(thatMd5)) { // return false; // } diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/CommonInfo.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/CommonInfo.java index c0c9f36ce..fb0d7140d 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/CommonInfo.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/CommonInfo.java @@ -226,12 +226,12 @@ final class CommonInfo { return Boolean.valueOf((String)x); // throws an exception if this isn't a string } -// public String getAttributeAsString(String key) { return (String.valueOf(getAttribute(key))); } // **NOTE**: will turn a null Object into the String "null" -// public int getAttributeAsInt(String key) { Object x = getAttribute(key); return x instanceof Integer ? (Integer)x : Integer.valueOf((String)x); } -// public double getAttributeAsDouble(String key) { Object x = getAttribute(key); return x instanceof Double ? (Double)x : Double.valueOf((String)x); } -// public boolean getAttributeAsBoolean(String key) { Object x = getAttribute(key); return x instanceof Boolean ? (Boolean)x : Boolean.valueOf((String)x); } +// public String getAttributeAsString(String key) { return (String.valueOf(getExtendedAttribute(key))); } // **NOTE**: will turn a null Object into the String "null" +// public int getAttributeAsInt(String key) { Object x = getExtendedAttribute(key); return x instanceof Integer ? (Integer)x : Integer.valueOf((String)x); } +// public double getAttributeAsDouble(String key) { Object x = getExtendedAttribute(key); return x instanceof Double ? (Double)x : Double.valueOf((String)x); } +// public boolean getAttributeAsBoolean(String key) { Object x = getExtendedAttribute(key); return x instanceof Boolean ? (Boolean)x : Boolean.valueOf((String)x); } // public Integer getAttributeAsIntegerNoException(String key) { try {return getAttributeAsInt(key);} catch (Exception e) {return null;} } // public Double getAttributeAsDoubleNoException(String key) { try {return getAttributeAsDouble(key);} catch (Exception e) {return null;} } -// public String getAttributeAsStringNoException(String key) { if (getAttribute(key) == null) return null; return getAttributeAsString(key); } +// public String getAttributeAsStringNoException(String key) { if (getExtendedAttribute(key) == null) return null; return getAttributeAsString(key); } // public Boolean getAttributeAsBooleanNoException(String key) { try {return getAttributeAsBoolean(key);} catch (Exception e) {return null;} } } \ No newline at end of file diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/FastGenotype.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/FastGenotype.java index 80d5c6d7d..b23d07cce 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/FastGenotype.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/FastGenotype.java @@ -25,11 +25,8 @@ package org.broadinstitute.sting.utils.variantcontext; -import com.google.java.contract.Ensures; import com.google.java.contract.Requires; -import org.broad.tribble.util.ParsingUtils; import org.broadinstitute.sting.utils.codecs.vcf.VCFConstants; -import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import java.util.*; @@ -156,12 +153,12 @@ public final class FastGenotype extends Genotype { } @Override public List getFilters() { - return (List)getAttribute(VCFConstants.GENOTYPE_FILTER_KEY, Collections.emptyList()); + return (List) getExtendedAttribute(VCFConstants.GENOTYPE_FILTER_KEY, Collections.emptyList()); } @Override public boolean filtersWereApplied() { - return hasAttribute(VCFConstants.GENOTYPE_FILTER_KEY); + return hasExtendedAttribute(VCFConstants.GENOTYPE_FILTER_KEY); } @Override public int[] getPL() { diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/Genotype.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/Genotype.java index 6df6d5f70..06f9606e3 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/Genotype.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/Genotype.java @@ -420,7 +420,7 @@ public abstract class Genotype implements Comparable { * @return true if key has a value in the extendedAttributes */ @Requires({"key != null", "! isForbiddenKey(key)"}) - public boolean hasAttribute(final String key) { + public boolean hasExtendedAttribute(final String key) { return getExtendedAttributes().containsKey(key); } @@ -432,19 +432,19 @@ public abstract class Genotype implements Comparable { * @return a value (potentially) null associated with key, or defaultValue if no association exists */ @Requires({"key != null", "! isForbiddenKey(key)"}) - @Ensures("hasAttribute(key) || result == defaultValue") - public Object getAttribute(final String key, final Object defaultValue) { - return hasAttribute(key) ? getExtendedAttributes().get(key) : defaultValue; + @Ensures("hasExtendedAttribute(key) || result == defaultValue") + public Object getExtendedAttribute(final String key, final Object defaultValue) { + return hasExtendedAttribute(key) ? getExtendedAttributes().get(key) : defaultValue; } /** - * Same as #getAttribute with a null default + * Same as #getExtendedAttribute with a null default * * @param key * @return */ - public Object getAttribute(final String key) { - return getAttribute(key, null); + public Object getExtendedAttribute(final String key) { + return getExtendedAttribute(key, null); } /** @@ -468,7 +468,7 @@ public abstract class Genotype implements Comparable { @Deprecated public String getAttributeAsString(String key, String defaultValue) { - Object x = getAttribute(key); + Object x = getExtendedAttribute(key); if ( x == null ) return defaultValue; if ( x instanceof String ) return (String)x; return String.valueOf(x); // throws an exception if this isn't a string @@ -476,7 +476,7 @@ public abstract class Genotype implements Comparable { @Deprecated public int getAttributeAsInt(String key, int defaultValue) { - Object x = getAttribute(key); + Object x = getExtendedAttribute(key); if ( x == null || x == VCFConstants.MISSING_VALUE_v4 ) return defaultValue; if ( x instanceof Integer ) return (Integer)x; return Integer.valueOf((String)x); // throws an exception if this isn't a string @@ -484,7 +484,7 @@ public abstract class Genotype implements Comparable { @Deprecated public double getAttributeAsDouble(String key, double defaultValue) { - Object x = getAttribute(key); + Object x = getExtendedAttribute(key); if ( x == null ) return defaultValue; if ( x instanceof Double ) return (Double)x; return Double.valueOf((String)x); // throws an exception if this isn't a string @@ -510,7 +510,7 @@ public abstract class Genotype implements Comparable { } else if (key.equals(VCFConstants.DEPTH_KEY)) { return getDP(); } else { - return getAttribute(key); + return getExtendedAttribute(key); } } @@ -526,7 +526,7 @@ public abstract class Genotype implements Comparable { } else if (key.equals(VCFConstants.DEPTH_KEY)) { return hasDP(); } else { - return hasAttribute(key); + return hasExtendedAttribute(key); } } diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/SlowGenotype.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/SlowGenotype.java index 6f7dda2bd..e5dd1451a 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/SlowGenotype.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/SlowGenotype.java @@ -118,13 +118,13 @@ public class SlowGenotype extends Genotype { @Override public double getLog10PError() { return commonInfo.getLog10PError(); } @Override - public boolean hasAttribute(String key) { return commonInfo.hasAttribute(key); } + public boolean hasExtendedAttribute(String key) { return commonInfo.hasAttribute(key); } @Override - public Object getAttribute(String key) { return commonInfo.getAttribute(key); } + public Object getExtendedAttribute(String key) { return commonInfo.getAttribute(key); } @Override - public Object getAttribute(String key, Object defaultValue) { + public Object getExtendedAttribute(String key, Object defaultValue) { return commonInfo.getAttribute(key, defaultValue); } diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/BCF2FieldWriter.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/BCF2FieldWriter.java index 4a2077138..4d915ea2e 100644 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/BCF2FieldWriter.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/BCF2FieldWriter.java @@ -143,12 +143,12 @@ public abstract class BCF2FieldWriter { } public void addGenotype(final BCF2Encoder encoder, final VariantContext vc, final Genotype g) throws IOException { - final Object fieldValue = g.getAttribute(getField(), null); + final Object fieldValue = g.getExtendedAttribute(getField(), null); getFieldEncoder().encodeValue(encoder, fieldValue, encodingType, nValuesPerGenotype); } protected int numElements(final VariantContext vc, final Genotype g) { - return getFieldEncoder().numElements(vc, g.getAttribute(getField())); + return getFieldEncoder().numElements(vc, g.getExtendedAttribute(getField())); } private final int computeMaxSizeOfGenotypeFieldFromValues(final VariantContext vc) { @@ -179,7 +179,7 @@ public abstract class BCF2FieldWriter { // the only value that is dynamic are integers final List values = new ArrayList(vc.getNSamples()); for ( final Genotype g : vc.getGenotypes() ) { - for ( final Object i : BCF2Utils.toList(g.getAttribute(getField(), null)) ) { + for ( final Object i : BCF2Utils.toList(g.getExtendedAttribute(getField(), null)) ) { values.add((Integer)i); // we know they are all integers } } diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/VCFWriter.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/VCFWriter.java index 68de0656f..1e87349df 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/VCFWriter.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/writer/VCFWriter.java @@ -378,7 +378,7 @@ class VCFWriter extends IndexingVariantContextWriter { outputValue = sb.toString(); } } else { - Object val = g.hasAttribute(field) ? g.getAttribute(field) : VCFConstants.MISSING_VALUE_v4; + Object val = g.hasExtendedAttribute(field) ? g.getExtendedAttribute(field) : VCFConstants.MISSING_VALUE_v4; // some exceptions if ( field.equals(VCFConstants.GENOTYPE_FILTER_KEY ) ) { diff --git a/public/java/test/org/broadinstitute/sting/utils/variantcontext/GenotypeUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/variantcontext/GenotypeUnitTest.java index e0a037105..36b2ade53 100644 --- a/public/java/test/org/broadinstitute/sting/utils/variantcontext/GenotypeUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/variantcontext/GenotypeUnitTest.java @@ -74,9 +74,9 @@ public class GenotypeUnitTest extends BaseTest { // public boolean hasLog10PError() // public double getLog10PError() // public double getPhredScaledQual() -// public boolean hasAttribute(String key) -// public Object getAttribute(String key) -// public Object getAttribute(String key, Object defaultValue) +// public boolean hasExtendedAttribute(String key) +// public Object getExtendedAttribute(String key) +// public Object getExtendedAttribute(String key, Object defaultValue) // public String getAttributeAsString(String key, String defaultValue) // public int getAttributeAsInt(String key, int defaultValue) // public double getAttributeAsDouble(String key, double defaultValue) diff --git a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextBenchmark.java b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextBenchmark.java index f9bf6f0dd..7c522eadf 100644 --- a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextBenchmark.java +++ b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextBenchmark.java @@ -315,7 +315,7 @@ public class VariantContextBenchmark extends SimpleBenchmark { // case GET_ATTRIBUTE_STRING: // return new FunctionToBenchmark() { // public void run(final org.broadinstitute.sting.utils.variantcontext.v13.VariantContext vc) { -// vc.getAttribute("AN", null); +// vc.getExtendedAttribute("AN", null); // } // }; // diff --git a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextTestProvider.java b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextTestProvider.java index ddaef78ab..64f9cbd6a 100644 --- a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextTestProvider.java +++ b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextTestProvider.java @@ -335,6 +335,28 @@ public class VariantContextTestProvider { GenotypeBuilder.create("hap", Arrays.asList(ref))); addGenotypeTests(site, + GenotypeBuilder.create("noCall", noCall), + GenotypeBuilder.create("dip", Arrays.asList(ref, alt1)), + GenotypeBuilder.create("hap", Arrays.asList(ref))); + + addGenotypeTests(site, + GenotypeBuilder.create("noCall", noCall), + GenotypeBuilder.create("noCall2", noCall), + GenotypeBuilder.create("dip", Arrays.asList(ref, alt1)), + GenotypeBuilder.create("hap", Arrays.asList(ref))); + + addGenotypeTests(site, + GenotypeBuilder.create("dip", Arrays.asList(ref, alt1)), + GenotypeBuilder.create("tet", Arrays.asList(ref, alt1, alt1))); + + addGenotypeTests(site, + GenotypeBuilder.create("noCall", noCall), + GenotypeBuilder.create("dip", Arrays.asList(ref, alt1)), + GenotypeBuilder.create("tet", Arrays.asList(ref, alt1, alt1))); + + addGenotypeTests(site, + GenotypeBuilder.create("noCall", noCall), + GenotypeBuilder.create("noCall2", noCall), GenotypeBuilder.create("dip", Arrays.asList(ref, alt1)), GenotypeBuilder.create("tet", Arrays.asList(ref, alt1, alt1)));