View Single Post
Old 08-18-2012, 12:07 PM   #5
Location: Earth

Join Date: Mar 2012
Posts: 11

Originally Posted by Richard Finney View Post
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.
Hi Richard Finney,
Thank you for very helpful code. First code worked good for me.
pirates.genome is offline   Reply With Quote