package org.broadinstitute.sting.gatk.walkers;

import org.broadinstitute.sting.gatk.LocusContext;
import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker;
import org.broadinstitute.sting.gatk.refdata.rodSAMPileup;
import org.broadinstitute.sting.utils.cmdLine.Argument;
import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.BasicPileup;
import org.broadinstitute.sting.utils.ReadBackedPileup;

/**
 * Created by IntelliJ IDEA.
 * User: mdepristo
 * Date: Feb 22, 2009
 * Time: 3:22:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class ValidatingPileupWalker extends LocusWalker<Integer, ValidationStats> {
    @Argument(fullName="verbose",required=false,defaultValue="false")
    public boolean VERBOSE;

    public Integer map(RefMetaDataTracker tracker, char ref, LocusContext context) {
        ReadBackedPileup pileup = new ReadBackedPileup(ref, context);

        rodSAMPileup truePileup = (rodSAMPileup)tracker.lookup("pileup", null);
        if ( truePileup == null )
            Utils.scareUser(String.format("No pileup data available at %s given GATK's output of %s -- this walker requires samtools pileup data over all bases",
                    context.getLocation(), pileup.getBases()));

        String pileupDiff = BasicPileup.pileupDiff(pileup, truePileup);
        if ( pileupDiff != null ) {
            out.printf("%s vs. %s%n", pileup.getPileupString(), truePileup.getPileupString());
            throw new RuntimeException(String.format("Pileups aren't equal: %s", pileupDiff));
        }

        return pileup.size();
    }

    // Given result of map function
    public ValidationStats reduceInit() { return new ValidationStats(); }
    public ValidationStats reduce(Integer value, ValidationStats sum) {
        sum.nLoci++;
        sum.nBases += value;
        return sum;
    }
}

class ValidationStats {
    public long nLoci = 0;
    public long nBases = 0;

    public ValidationStats() {
    }

    public String toString() {
        return String.format("Validated %d sites covered by %d bases%n", nLoci, nBases);
    }
}
@@ -317,16 +317,62 @@
 .Utils;%0A
+import org.broadinstitute.sting.utils.Pileup;%0A
 import o
@@ -746,15 +746,28 @@
 me=%22
-verbose
+continue_after_error
 %22,re
@@ -824,15 +824,31 @@
 ean 
-VERBOSE
+CONTINUE_AFTER_AN_ERROR
 ;%0A%0A 
@@ -939,26 +939,16 @@
         
-ReadBacked
 Pileup p
@@ -991,25 +991,24 @@
 ntext);%0A
-%0A
         
 rodSAMPi
@@ -999,22 +999,16 @@
         
-rodSAM
 Pileup t
@@ -1020,22 +1020,16 @@
 leup = (
-rodSAM
 Pileup)t
@@ -1059,16 +1059,25 @@
  null);%0A
+        %0A
         
@@ -1101,17 +1101,169 @@
 = null )
+ %7B
 %0A
+            System.out.printf(%22No truth pileup data available at %25s%25n%22, pileup.getPileupString());%0A            if ( ! CONTINUE_AFTER_AN_ERROR ) %7B%0A    
         
@@ -1436,16 +1436,28 @@
         
+     
+       
 context.
@@ -1492,17 +1492,51 @@
 es()));%0A
-%0A
+            %7D%0A        %7D else %7B%0A    
         
@@ -1603,24 +1603,28 @@
 p);%0A        
+    
 if ( pileupD
@@ -1651,16 +1651,20 @@
         
+  
+  
 out.prin
@@ -1730,32 +1730,91 @@
 ileupString());%0A
+                if ( ! CONTINUE_AFTER_AN_ERROR ) %7B%0A        
             thro
@@ -1888,24 +1888,56 @@
 leupDiff));%0A
+                %7D%0A            %7D%0A
         %7D%0A%0A 

Support for -continue_after_errors flag in the validating pileup walker in case you want to see errors as they arise, rather than aborting greedily