<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi all,<div><br></div><div>I'm a little stumped as to how to successfully count the depths of all reads at a specific locus in a sam/bam file. &nbsp;I know I can do this with GATK DepthOfCoverage but I wanted to do some more customized things with my script yet I haven't figured out how to get the right base. &nbsp;I was a bit surprised there wasn't (or that it's not well documented) a method to get the individuals read's base at a specific position while getting the $refbase is quite easy. &nbsp;(I'm betting such a method exists and is just not documented well)</div><div><br></div><div>At any rate, gaps in the alignment are the cause for my problems, so if anyone knows a simpler way to do call the bases correctly, or a clever algorithm to deal with this issue, it would be much appreciated. &nbsp;Here's what I have for code and it works except in cases where there are multiple gaps in the reference sequence, e.g. the alignment below should be T-T here not C-C but is shifted due to the second gap. &nbsp;</div><div><br></div><div><img id="16a2bc7a-138b-4b1a-930f-a3511792bc6f" height="56" width="607" apple-width="yes" apple-height="yes" src="cid:4D6D308D-BABF-4405-953C-1709CE992486@knome.com"></div><div><br></div><div><div>#!/progs/bin/perl</div><div>use strict;</div><div>use warnings;</div><div>use Bio::DB::Sam;</div><div>use Bio::DB::Bam::AlignWrapper;</div><div>use Pod::Usage;</div><div>use Getopt::Long;</div><div>use Bio::DB::Bam::Pileup;</div><div>use Term::ANSIColor;</div></div><div><br></div><div><div>my $sam = Bio::DB::Sam-&gt;new(-bam &nbsp;=&gt;$BAM,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; -fasta=&gt; $FASTA);</div><div>getBases($chr, $pos, $pos);</div><div><br></div><div><br></div><div>sub getBases {</div><div>&nbsp; &nbsp; my $print = 1;</div><div>&nbsp; &nbsp; my ($chr, $start_query, $end_query) = @_;</div><div>&nbsp; &nbsp; my @alignments = $sam-&gt;get_features_by_location(-seq_id =&gt; $chr,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -start &nbsp;=&gt; $start_query,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -end &nbsp; &nbsp;=&gt; $end_query);</div><div>&nbsp; &nbsp; my $refbase;</div><div>&nbsp; &nbsp; my ($a_count, $t_count, $g_count, $c_count, $n_count, $del_count, $ins_count) = (0, 0, 0, 0, 0, 0, 0);</div><div>&nbsp; &nbsp; for my $a (@alignments) {</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $start &nbsp;= $a-&gt;start;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $end &nbsp; &nbsp;= $a-&gt;end;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $query_start = $a-&gt;query-&gt;start; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $query_end &nbsp; = $a-&gt;query-&gt;end;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $ref_dna &nbsp; = $a-&gt;dna; &nbsp; &nbsp; &nbsp; &nbsp;# reference sequence bases</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my ($ref, $matches, $query) = $a-&gt;padded_alignment;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $offset = 0;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if ($ref =~ /^([-]+)[ATCG]+/){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $offset = length($1);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; #print "$offset\n";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $refbase = $sam-&gt;segment($chr,$start_query,$start_query)-&gt;dna; &nbsp; &nbsp; &nbsp; &nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; printAlignment($ref, $matches, $query, $start_query, $start, $offset);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $base = substr($query, $start_query-$start+$offset, 1);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (!$base){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $a_count++ if ($base eq "A");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $t_count++ if ($base eq "T");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $c_count++ if ($base eq "C");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $g_count++ if ($base eq "G");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $n_count++ if ($base eq "N");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; $del_count++ if ($base eq "-");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my @scores &nbsp; &nbsp;= $a-&gt;qscore; &nbsp; &nbsp; # per-base quality scores</div><div>&nbsp; &nbsp; &nbsp; &nbsp; my $match_qual= $a-&gt;qual; &nbsp; &nbsp; &nbsp; # quality of the match</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; my $total_depth = $a_count + $t_count + $c_count + $g_count + $n_count + $del_count;</div><div>&nbsp; &nbsp; if ($print == 1){</div><div>&nbsp; &nbsp; &nbsp;# &nbsp; print "$start_query\tref base: $ref_base\n";&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "$chr:$start_query($refbase)\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "A:$a_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "T:$t_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "C:$c_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "G:$g_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "N:$n_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "D:$del_count\t";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; print "Total:$total_depth\n";</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; return ($a_count, $t_count, $c_count, $g_count, $n_count, $del_count, $ins_count);</div><div>}</div><div>sub printAlignment{</div><div>&nbsp; &nbsp; my ($ref, $matches, $query, $start_query, $start, $offset) = @_;</div><div>&nbsp; &nbsp; print substr($ref, 0, $start_query-$start+$offset);</div><div>&nbsp; &nbsp; print (color("red"), substr($ref, $start_query-$start+$offset, 1), color("reset"));</div><div>&nbsp; &nbsp; print substr($ref, $start_query-$start+$offset+1),"\n";</div><div>&nbsp; &nbsp; print substr($matches, 0, $start_query-$start+$offset);</div><div>&nbsp; &nbsp; print (color("red"), substr($matches, $start_query-$start+$offset, 1), color("reset"));</div><div>&nbsp; &nbsp; print substr($matches, $start_query-$start+$offset+1),"\n";</div><div>&nbsp; &nbsp; print substr($query, 0, $start_query-$start+$offset);</div><div>&nbsp; &nbsp; print (color("red"), substr($query, $start_query-$start+$offset, 1), color("reset"));</div><div>&nbsp; &nbsp; print substr($query, $start_query-$start+$offset+1),"\n";</div><div>}</div></div><div><br></div></div></body></html>