View Single Post
Old 08-16-2012, 07:54 AM   #4
Richard Finney
Senior Member
Location: bethesda

Join Date: Feb 2009
Posts: 700

SIMPLE VERSION ... using samtools depth and awk ...
#ploc = previous location, pchr=previous chromsome
#Set SAMT and BAMF to samtools and your bamfile.
export SAMT=/h1/finneyr/samtools-0.1.18/samtools
export BAMF=98023.bam
$SAMT depth $BAMF | awk '{ if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} ploc=$2; }

More complicated version that handles chr1->chr2 transition and flushes at the end. (I think, this is not completely debugged and and is just a one-off. Not all corner conditions maybe addressed.)

$SAMT depth $BAMF | \
awk '
if (pchr!=$1) { if (firsttime==1) { firsttime = 0;} else { printf("%s %d-%d\n",pchr,s,ploc);}s=$2}
else { if ($2!=(ploc+1)){if (ploc!=0){printf("%s %d-%d\n",$1,s,ploc);}s=$2} }
ploc=$2; pchr=$1
END{ printf("%s %d-%d\n",pchr,s,ploc);}

CAVEAT: samtools depth doesn't do cigar 'N' quite right so this won't work for RNA in the best way.

Last edited by Richard Finney; 08-16-2012 at 07:58 AM.
Richard Finney is offline   Reply With Quote