diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/PercentOfBasesCoveredWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/PercentOfBasesCoveredWalker.java new file mode 100644 index 000000000..9e77b5f04 --- /dev/null +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/PercentOfBasesCoveredWalker.java @@ -0,0 +1,64 @@ +package org.broadinstitute.sting.playground.gatk.walkers; + +import org.broadinstitute.sting.gatk.walkers.LocusWalker; +import org.broadinstitute.sting.gatk.walkers.TreeReducible; +import org.broadinstitute.sting.gatk.walkers.DataSource; +import org.broadinstitute.sting.gatk.walkers.By; +import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; +import org.broadinstitute.sting.gatk.contexts.ReferenceContext; +import org.broadinstitute.sting.gatk.contexts.AlignmentContext; +import org.broadinstitute.sting.utils.Pair; +import org.broadinstitute.sting.utils.cmdLine.Argument; + +/** + * Created by IntelliJ IDEA. + * User: Ghost + * Date: Nov 25, 2009 + * Time: 7:54:42 PM + * To change this template use File | Settings | File Templates. + */ +@By(DataSource.REFERENCE) +public class PercentOfBasesCoveredWalker extends LocusWalker> implements TreeReducible> { + @Argument(fullName="minimumDepth", shortName="minDepth", doc="Minimum depth beyond which to discount the base as uncovered", required=false) + int minDepth = 0; + @Argument(fullName="includeName",doc="include this name in the output of this walker (e.g. NAME: percent of bases covered . . .) ", required=false) + String name = null; + public void initialize() { + // do nothing + } + + public Pair reduceInit() { + return new Pair(0,1); // robust initialization -- seen one base with zero coverage + } + + public Boolean map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) { + return context.numReads() > minDepth; + } + + public Pair reduce(Boolean map, Pair prevReduce) { + if ( map.booleanValue() ) { + return new Pair(prevReduce.first+1,prevReduce.second+1); + } else { + return new Pair(prevReduce.first,prevReduce.second+1); + } + } + + public Pair treeReduce( Pair left, Pair right) { + right.set(right.first+left.first,right.second+left.second); + return right; + } + + public void onTraversalDone(Pair reduceFinal) { + String msg; + if (name == null) + msg = "Percent of bases covered at "+minDepth+"x="; + else + msg = name+": Percent of bases covered at "+minDepth+"x="; + + out.printf("%s%s",msg,pair2percent(reduceFinal)); + } + + private String pair2percent(Pair p ) { + return String.format("%d%s", (int) Math.floor( ( ( double) p.first*100.0) / p.second ), "%" ); + } +}