@@ -0,0 +1,2758 @@
+%0Apackage org.broadinstitute.sting.playground.gatk.walkers;%0A%0Aimport net.sf.samtools.*;%0Aimport org.broadinstitute.sting.gatk.*;%0Aimport org.broadinstitute.sting.gatk.refdata.ReferenceOrderedData;%0Aimport org.broadinstitute.sting.gatk.refdata.ReferenceOrderedDatum;%0Aimport org.broadinstitute.sting.gatk.refdata.rodDbSNP;%0Aimport org.broadinstitute.sting.gatk.refdata.rodGFF;%0Aimport org.broadinstitute.sting.gatk.walkers.LocusWalker;%0Aimport org.broadinstitute.sting.gatk.LocusContext;%0Aimport org.broadinstitute.sting.playground.gatk.walkers.AlleleFrequencyWalker;%0Aimport org.broadinstitute.sting.playground.utils.AlleleFrequencyEstimate;%0Aimport org.broadinstitute.sting.utils.cmdLine.Argument;%0A%0Aimport java.util.*;%0A%0Apublic class CoverageBySample extends LocusWalker%3CString, String%3E %0A%7B%0A    List%3CString%3E sample_names = null;%0A%0A    public void initialize() %0A    %7B %0A        GenomeAnalysisTK toolkit = this.getToolkit();%0A        SAMFileHeader header = toolkit.getSamReader().getFileHeader();%0A        List%3CSAMReadGroupRecord%3E read_groups = header.getReadGroups();%0A%0A        sample_names    = new ArrayList%3CString%3E();%0A%0A        for (int i = 0; i %3C read_groups.size(); i++)%0A        %7B%0A            String sample_name = read_groups.get(i).getSample();%0A            sample_names.add(sample_name);%0A        %7D %0A    %7D%0A%0A    public String map(List%3CReferenceOrderedDatum%3E rodData, char ref, LocusContext context) %0A    %7B%0A        String line = context.getLocation().getContig() + %22 %22 + context.getLocation().getStart() + %22 %22 ;%0A        for (int i = 0; i %3C sample_names.size(); i++)%0A        %7B%0A            int count = countReadsBySample(context, sample_names.get(i));%0A            line += %22 %22 + count;%0A        %7D%0A        line += %22%5Cn%22;%0A        return line;%0A    %7D%0A%0A    private int countReadsBySample(LocusContext context, String sample_name)%0A    %7B%0A        int count = 0;%0A        for (int i = 0; i %3C context.getReads().size(); i++)%0A        %7B%0A            SAMRecord read = context.getReads().get(i);%0A            Integer offset = context.getOffsets().get(i);%0A            String RG = (String)(read.getAttribute(%22RG%22));%0A            String sample = read.getHeader().getReadGroup(RG).getSample();%0A            if (sample == sample_name) %0A            %7B %0A                count += 1;%0A            %7D%0A        %7D%0A        return count;%0A    %7D%0A%0A    public void onTraversalDone() %0A    %7B%0A        return;%0A    %7D%0A%0A    public String reduceInit() %0A    %7B %0A        String header = %22contig offset%22;%0A        for (int i = 0; i %3C sample_names.size(); i++)%0A        %7B%0A            header += %22 %22 + sample_names.get(i);%0A        %7D%0A        header += %22%5Cn%22;%0A        out.print(header);%0A        return header;%0A    %7D%0A%0A    public String reduce(String line, String sum) %0A    %7B%0A        out.print(line);%0A        return sum + line; %0A    %7D%0A%0A    %0A%7D%0A

Print coverage per reference base for each sample in a merged BAM file. This is a good example for how to untangle a merged BAM file.