From f97e7d2fb4d8af03bdb08d67ff2159d6769eafe0 Mon Sep 17 00:00:00 2001 From: carneiro Date: Fri, 29 Apr 2011 19:19:39 +0000 Subject: [PATCH] Walker that calculates the percentage of bases that are covered to at least 20x. Very useful! In oneoffs until someone else thinks it's as useful as I think it is ;) git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@5714 348d0f76-0448-11de-a6fe-93d51630548a --- .../walkers/Percent20xCoverage.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/oneoffprojects/walkers/Percent20xCoverage.java diff --git a/java/src/org/broadinstitute/sting/oneoffprojects/walkers/Percent20xCoverage.java b/java/src/org/broadinstitute/sting/oneoffprojects/walkers/Percent20xCoverage.java new file mode 100755 index 000000000..d8fbf041c --- /dev/null +++ b/java/src/org/broadinstitute/sting/oneoffprojects/walkers/Percent20xCoverage.java @@ -0,0 +1,59 @@ +package org.broadinstitute.sting.oneoffprojects.walkers; + +import org.broadinstitute.sting.commandline.Argument; +import org.broadinstitute.sting.commandline.Output; +import org.broadinstitute.sting.gatk.contexts.AlignmentContext; +import org.broadinstitute.sting.gatk.contexts.ReferenceContext; +import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; +import org.broadinstitute.sting.gatk.walkers.LocusWalker; +import org.broadinstitute.sting.gatk.walkers.TreeReducible; + +import java.io.PrintStream; + +/** + * Walks over the input data set, calculating the total number of covered loci for diagnostic purposes. + * Simplest example of a locus walker. + */ +public class Percent20xCoverage extends LocusWalker implements TreeReducible { + @Output(doc="Write count to this file instead of STDOUT") + PrintStream out; + + @Argument(fullName="target_coverage", shortName="coverage", doc="Set the target coverage", required=false) + private int targetCoverage = 20; + + + private long totalLoci; + private long totalCoverage; + + public void initialize () { + totalLoci = 0; + totalCoverage = 0; + } + + public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) { + totalLoci++; + int coverage = context.getBasePileup().size(); + totalCoverage += coverage; + if (coverage >= targetCoverage) + return 1; + return 0; + } + + public Long reduceInit() { return 0l; } + + public Long reduce(Integer value, Long sum) { + return value + sum; + } + + /** + * Reduces two subtrees together. In this case, the implementation of the tree reduce + * is exactly the same as the implementation of the single reduce. + */ + public Long treeReduce(Long lhs, Long rhs) { + return lhs + rhs; + } + + public void onTraversalDone( Long c ) { + out.println(Math.floor(totalCoverage/totalLoci) + "\t" + (double) c/totalLoci); + } +}