Skip to content

Commit

Permalink
Integrate VariantWalkerBase with CountingVariantFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnbroad committed Oct 3, 2018
1 parent e52a2d8 commit ed21e64
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import htsjdk.variant.variantcontext.VariantContext;
import org.broadinstitute.hellbender.engine.filters.CountingReadFilter;
import org.broadinstitute.hellbender.engine.filters.CountingVariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilter;
import org.broadinstitute.hellbender.utils.SimpleInterval;

Expand All @@ -18,21 +19,22 @@ public abstract class TwoPassVariantWalker extends VariantWalker {
*/
@Override
public void traverse(){
final VariantFilter variantContextFilter = makeVariantFilter();
final CountingVariantFilter countingVariantFilter = makeVariantFilter();
final CountingReadFilter readFilter = makeReadFilter();

// First pass through the variants
logger.info("Starting first pass through the variants");
traverseVariants(variantContextFilter, readFilter, this::firstPassApply);
traverseVariants(countingVariantFilter, readFilter, this::firstPassApply);
logger.info("Finished first pass through the variants");

// Process the data accumulated during the first pass
afterFirstPass();

// Second pass
logger.info("Starting second pass through the variants");
traverseVariants(variantContextFilter, readFilter, this::secondPassApply);
traverseVariants(countingVariantFilter, readFilter, this::secondPassApply);

logger.info(countingVariantFilter.getSummaryLine());
logger.info(readFilter.getSummaryLine());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.vcf.VCFHeader;
import org.broadinstitute.hellbender.engine.filters.CountingReadFilter;
import org.broadinstitute.hellbender.engine.filters.CountingVariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary;
import org.broadinstitute.hellbender.transformers.VariantTransformer;
Expand Down Expand Up @@ -143,9 +144,10 @@ protected Stream<VariantContext> getTransformedVariantStream(final VariantFilter
*/
@Override
public void traverse() {
final CountingVariantFilter countingVariantfilter = makeVariantFilter();
final CountingReadFilter readFilter = makeReadFilter();
// Process each variant in the input stream.
getTransformedVariantStream( makeVariantFilter() )
getTransformedVariantStream(countingVariantfilter)
.forEach(variant -> {
final SimpleInterval variantInterval = new SimpleInterval(variant);
apply(variant,
Expand All @@ -155,6 +157,8 @@ public void traverse() {

progressMeter.update(variantInterval);
});

logger.info(countingVariantfilter.getSummaryLine());
}

/**
Expand All @@ -166,8 +170,8 @@ public void traverse() {
* Subclasses can extend to provide own filters (ie override and call super).
* Multiple filters can be composed by using {@link VariantFilter} composition methods.
*/
protected VariantFilter makeVariantFilter() {
return VariantFilterLibrary.ALLOW_ALL_VARIANTS;
protected CountingVariantFilter makeVariantFilter() {
return new CountingVariantFilter(VariantFilterLibrary.ALLOW_ALL_VARIANTS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
package org.broadinstitute.hellbender.engine.filters;

import htsjdk.variant.variantcontext.VariantContext;

/**
* Collects common variant filters.
*/
public final class VariantFilterLibrary {
public static VariantFilter ALLOW_ALL_VARIANTS = variant -> true;
public static VariantFilter NOT_SV_OR_SYMBOLIC = variant -> !variant.isSymbolicOrSV();
public static VariantFilter ALLOW_ALL_VARIANTS = new AllowAllVariantsVariantFilter();
public static VariantFilter NOT_SV_OR_SYMBOLIC = new NotSymbolicOrSVVariantFilter();
public static VariantFilter PASSES_FILTERS = new PassesFiltersVariantFilter();

/** Do not filter out any variants. */
public static class AllowAllVariantsVariantFilter implements VariantFilter {
private static final long serialVersionUID = 1L;
@Override public boolean test(final VariantContext variant) { return true; }
}

/** Filter out any variants that are symbolic or SV. */
public static class NotSymbolicOrSVVariantFilter implements VariantFilter {
private static final long serialVersionUID = 1L;
@Override public boolean test(final VariantContext variant) {
return !variant.isSymbolicOrSV();
}
}

/** Filter out any variants that fail (variant-level) filters. */
public static class PassesFiltersVariantFilter implements VariantFilter {
private static final long serialVersionUID = 1L;
@Override public boolean test(final VariantContext variant) {
return !variant.isFiltered();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.engine.*;
import org.broadinstitute.hellbender.engine.filters.VariantFilter;
import org.broadinstitute.hellbender.engine.filters.CountingVariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary;
import org.broadinstitute.hellbender.exceptions.GATKException;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.tools.funcotator.dataSources.DataSourceUtils;
import org.broadinstitute.hellbender.tools.funcotator.metadata.VcfFuncotationMetadata;
Expand Down Expand Up @@ -317,8 +320,8 @@ private void checkReferenceDictionaryIsSupersetOfVariantDictionary() {
}

@Override
protected VariantFilter makeVariantFilter() {
return funcotatorEngine.makeVariantFilter();
protected CountingVariantFilter makeVariantFilter() {
return new CountingVariantFilter(funcotatorEngine.makeVariantFilter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.broadinstitute.hellbender.engine.GATKTool;
import org.broadinstitute.hellbender.engine.ReferenceContext;
import org.broadinstitute.hellbender.engine.filters.VariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary;
import org.broadinstitute.hellbender.exceptions.GATKException;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.tools.funcotator.dataSources.DataSourceUtils;
Expand Down Expand Up @@ -216,13 +217,10 @@ public OutputRenderer createOutputRenderer(final LinkedHashMap<String, String> a
* @return A {@link VariantFilter} that will ignore any variants that have been filtered (if the user requested that the filter is turned on). Otherwise returns a no-op filter.
*/
public VariantFilter makeVariantFilter() {
return variant -> {
// Ignore variants that have been filtered if the user requests it:
if ( funcotatorArgs.removeFilteredVariants && variant.isFiltered() ) {
return false;
}
return true;
};
// Ignore variants that have been filtered if the user requests it:
return funcotatorArgs.removeFilteredVariants ?
VariantFilterLibrary.PASSES_FILTERS :
VariantFilterLibrary.ALLOW_ALL_VARIANTS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
import org.broadinstitute.barclay.argparser.Hidden;
import org.broadinstitute.barclay.help.DocumentedFeature;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.engine.filters.*;
import picard.cmdline.programgroups.VariantManipulationProgramGroup;
import org.broadinstitute.hellbender.engine.FeatureInput;
import org.broadinstitute.hellbender.engine.FeatureContext;
import org.broadinstitute.hellbender.engine.ReadsContext;
import org.broadinstitute.hellbender.engine.ReferenceContext;
import org.broadinstitute.hellbender.engine.filters.VariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantIDsVariantFilter;
import org.broadinstitute.hellbender.engine.filters.VariantFilterLibrary;
import org.broadinstitute.hellbender.engine.filters.VariantTypesVariantFilter;
import org.broadinstitute.hellbender.engine.VariantWalker;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.tools.walkers.annotator.ChromosomeCounts;
Expand Down Expand Up @@ -658,19 +655,19 @@ public void closeTool() {
* Create filters for variant types, ids, and genomic intervals.
*/
@Override
protected VariantFilter makeVariantFilter() {
VariantFilter compositeFilter = VariantFilterLibrary.ALLOW_ALL_VARIANTS;
protected CountingVariantFilter makeVariantFilter() {
CountingVariantFilter compositeFilter = new CountingVariantFilter(VariantFilterLibrary.ALLOW_ALL_VARIANTS);

if (!selectedTypes.isEmpty()) {
compositeFilter = compositeFilter.and(new VariantTypesVariantFilter(selectedTypes));
compositeFilter = compositeFilter.and(new CountingVariantFilter(new VariantTypesVariantFilter(selectedTypes)));
}

if (rsIDsToKeep != null && !rsIDsToKeep.isEmpty()) {
compositeFilter = compositeFilter.and(new VariantIDsVariantFilter(rsIDsToKeep));
compositeFilter = compositeFilter.and(new CountingVariantFilter(new VariantIDsVariantFilter(rsIDsToKeep)));
}

if (rsIDsToRemove != null && !rsIDsToRemove.isEmpty()) {
compositeFilter = compositeFilter.and(new VariantIDsVariantFilter(rsIDsToRemove).negate());
compositeFilter = compositeFilter.and(new CountingVariantFilter(new VariantIDsVariantFilter(rsIDsToRemove).negate()));
}

return compositeFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.broadinstitute.hellbender.exceptions.GATKException;
import org.broadinstitute.hellbender.utils.haplotype.HaplotypeBAMWriter;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import org.broadinstitute.hellbender.utils.io.Resource;
import org.broadinstitute.hellbender.utils.io.Resource;
import org.broadinstitute.hellbender.utils.python.StreamingPythonScriptExecutor;
import org.broadinstitute.hellbender.utils.read.GATKRead;
import org.broadinstitute.hellbender.utils.runtime.AsynchronousStreamWriter;
Expand Down Expand Up @@ -207,12 +207,12 @@ public boolean requiresReference() {
}

@Override
protected VariantFilter makeVariantFilter(){
if (filterSymbolicAndSV) {
return VariantFilterLibrary.NOT_SV_OR_SYMBOLIC;
} else {
return VariantFilterLibrary.ALLOW_ALL_VARIANTS;
}
protected CountingVariantFilter makeVariantFilter() {
return new CountingVariantFilter(
filterSymbolicAndSV ?
VariantFilterLibrary.NOT_SV_OR_SYMBOLIC:
VariantFilterLibrary.ALLOW_ALL_VARIANTS
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.broadinstitute.hellbender.utils.SimpleInterval;
import org.broadinstitute.hellbender.GATKBaseTest;

import org.broadinstitute.hellbender.utils.Utils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -102,4 +103,40 @@ public void testVariantTypeVariantFilter(VariantContext vc, Type[] types, boolea
VariantTypesVariantFilter vtvf = new VariantTypesVariantFilter(typesSet);
Assert.assertTrue(vtvf.test(vc) == expected);
}

@DataProvider
public Object[][] getNotSymbolicOrSVFilterTestVCs() {
return new Object[][]{
{snpVC, true},
{new VariantContextBuilder(snpVC).alleles( // SYMBOLIC
Arrays.asList(
Allele.create("A", true),
Allele.create("<CNV>", false))).make(), false},
{new VariantContextBuilder(snpVC).alleles( // SV = > 150 indel
Arrays.asList(
Allele.create("A", true),
Allele.create("A" + Utils.dupChar('G', 151), false))).make(), false},
};
}

@Test(dataProvider = "getNotSymbolicOrSVFilterTestVCs")
public void testNotSVOrSymbolicVariantFilter(VariantContext vc, boolean expected) {
Assert.assertEquals(VariantFilterLibrary.NOT_SV_OR_SYMBOLIC.test(vc), expected);
}

@DataProvider
public Object[][] getPassesFiltersFilterTestVCs() {
return new Object[][]{
{snpVC, true},
{new VariantContextBuilder(snpVC).unfiltered().make(), true},
{new VariantContextBuilder(snpVC).passFilters().make(), true},
{new VariantContextBuilder(snpVC).filter("unbelievable").make(), false},
};
}

@Test(dataProvider = "getPassesFiltersFilterTestVCs")
public void testPassesFiltersVariantFilter(VariantContext vc, boolean expected) {
Assert.assertEquals(VariantFilterLibrary.PASSES_FILTERS.test(vc), expected);
}

}

0 comments on commit ed21e64

Please sign in to comment.