package org.broadinstitute.sting.gatk.walkers;

import org.broadinstitute.sting.gatk.LocusContext;
import org.broadinstitute.sting.gatk.refdata.ReferenceOrderedDatum;
import org.broadinstitute.sting.gatk.refdata.rodDbSNP;
import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker;
import org.broadinstitute.sting.utils.cmdLine.Argument;
import org.broadinstitute.sting.utils.Utils;
import net.sf.samtools.SAMRecord;

import java.util.List;
import java.util.ArrayList;

/**
 * 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 PileupWalker extends LocusWalker<Integer, Integer> {
    @Argument(fullName="verbose",required=false,defaultValue="false")
    public boolean VERBOSE;
    
    public boolean FLAG_UNCOVERED_BASES = true;     // todo: how do I make this a command line argument?
    
    public void initialize() {
    }

    // Do we actually want to operate on the context?
    public boolean filter(RefMetaDataTracker tracker, char ref, LocusContext context) {
        return true;    // We are keeping all the reads
    }

    public Integer map(RefMetaDataTracker tracker, char ref, LocusContext context) {
        List<SAMRecord> reads = context.getReads();
        List<Integer> offsets = context.getOffsets();
        String bases = Utils.basePileupAsString(reads, offsets);
        String quals = Utils.qualPileupAsString(reads, offsets);

        if ( bases.equals("") && FLAG_UNCOVERED_BASES ) {
            bases = "*** UNCOVERED SITE ***";
        }

        String extras = "";
        if ( VERBOSE ) {
            extras += " BQ=" + Utils.join(",", Utils.qualPileup(reads, offsets));
            extras += " MQ=" + Utils.join(",", Utils.mappingQualPileup(reads));
        }

        String rodString = "";
        for ( ReferenceOrderedDatum datum : tracker.getAllRods() ) {
            if ( datum != null && ! (datum instanceof rodDbSNP)) {
                rodString += datum.toSimpleString();
            }
        }
        rodDbSNP dbsnp = (rodDbSNP)tracker.lookup("dbSNP", null);
        if ( dbsnp != null )
            rodString += dbsnp.toMediumString();

        if ( rodString != "" )
            rodString = "[ROD: " + rodString + "]";

        //if ( context.getLocation().getStart() % 1 == 0 ) {
        out.printf("%s: %s %s %s%s %s%n", context.getLocation(), ref, bases, quals, extras, rodString);
        //}

        return 1;
    }

    // Given result of map function
    public Integer reduceInit() { return 0; }
    public Integer reduce(Integer value, Integer sum) {
        return value + sum;
    }
}

@@ -1639,24 +1639,642 @@
 VERBOSE ) %7B%0A
+            String sqbases = Utils.secondaryBasePileupAsString(reads, offsets);%0A            String sqquals = Utils.secondaryQualPileupAsString(reads, offsets);%0A%0A            if (sqbases != null && sqquals != null) %7B%0A                assert(sqbases.length() == sqquals.length());%0A%0A                extras += %22 SQ=%22;%0A                for (int i = 0; i %3C sqbases.length(); i++) %7B%0A                    extras += sqbases.charAt(i);%0A                    extras += sqquals.charAt(i);%0A%0A                    if (i %3C sqbases.length() - 1) %7B%0A                        extras += ',';%0A                    %7D%0A                %7D%0A            %7D%0A%0A
             

Outputs the secondary bases and quals (if available) in verbose mode. Prefixed with the tag 'SQ='.