diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariants.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariants.java index d26ab08f7..2b18eda20 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariants.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariants.java @@ -77,32 +77,49 @@ public class FilterLiftedVariants extends RodWalker { writer.writeHeader(vcfHeader); } - private void filterAndWrite(byte[] ref, VariantContext vc) { + /** + * Determines whether records should be filtered; if not, writes them to the output + * + * @param ref the reference context + * @param vc the VariantContext to process + * @return true if the record is not filtered, false otherwise + */ + protected boolean filterOrWrite(final byte[] ref, final VariantContext vc) { + if ( ref == null ) throw new IllegalArgumentException("Cannot filter based on a null reference array"); + if ( vc == null ) throw new IllegalArgumentException("Cannot filter a null Variant Context"); totalLocs++; - boolean failed = false; - byte[] recordRef = vc.getReference().getBases(); - for (int i = 0; i < recordRef.length && i < MAX_VARIANT_SIZE; i++) { - if ( recordRef[i] != ref[i] ) { - failed = true; - break; + boolean filter = false; + final byte[] recordRef = vc.getReference().getBases(); + + // this can happen for records that get placed at the ends of chromosomes + if ( recordRef.length > ref.length ) { + filter = true; + } else { + for (int i = 0; i < recordRef.length && i < MAX_VARIANT_SIZE; i++) { + if ( recordRef[i] != ref[i] ) { + filter = true; + break; + } } } - if ( failed ) + if ( filter ) failedLocs++; else writer.add(vc); + + return !filter; } public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) { if ( tracker == null ) return 0; - Collection VCs = tracker.getValues(variantCollection.variants, context.getLocation()); - for ( VariantContext vc : VCs ) - filterAndWrite(ref.getBases(), vc); + final Collection VCs = tracker.getValues(variantCollection.variants, context.getLocation()); + for ( final VariantContext vc : VCs ) + filterOrWrite(ref.getBases(), vc); return 0; } @@ -114,4 +131,4 @@ public class FilterLiftedVariants extends RodWalker { public void onTraversalDone(Integer result) { System.out.println("Filtered " + failedLocs + " records out of " + totalLocs + " total records."); } -} \ No newline at end of file +} diff --git a/public/java/test/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariantsUnitTest.java b/public/java/test/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariantsUnitTest.java new file mode 100644 index 000000000..83d571748 --- /dev/null +++ b/public/java/test/org/broadinstitute/sting/gatk/walkers/variantutils/FilterLiftedVariantsUnitTest.java @@ -0,0 +1,54 @@ +/* +* Copyright (c) 2012 The Broad Institute +* +* Permission is hereby granted, free of charge, to any person +* obtaining a copy of this software and associated documentation +* files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following +* conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR +* THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package org.broadinstitute.sting.gatk.walkers.variantutils; + +import org.broadinstitute.sting.BaseTest; +import org.broadinstitute.variant.variantcontext.Allele; +import org.broadinstitute.variant.variantcontext.VariantContext; +import org.broadinstitute.variant.variantcontext.VariantContextBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; + + +public class FilterLiftedVariantsUnitTest extends BaseTest { + + @Test + public void testIndelAtEndOfContig() { + + final List alleles = new ArrayList<>(2); + alleles.add(Allele.create("AAAAA", true)); + alleles.add(Allele.create("A", false)); + final VariantContext vc = new VariantContextBuilder("test", "1", 10, 14, alleles).make(); + + final FilterLiftedVariants filter = new FilterLiftedVariants(); + + Assert.assertFalse(filter.filterOrWrite(new byte[]{'A'}, vc)); + } + +}