The following (experimental) C-code generates a 'Wiggle Track Format' (WIG) file containing the coverage/depth data extracted from a BAM file.
It uses the C API of samtools to loop over the mapped short reads of the BAM file, and for each short-read, it scans the CIGAR description and increase the number of hits seen on the chromosome at a given position.
It takes as an optional UCSC knownGene.txt file to restrict the output to the known translated regions.
The C code
This Makefile generates a random pair of FASTQ files for the human chr22, maps them with BWA , creates a BAM and generates the WIG file:
ResultThe resulting WIG file was uploaded as a custom track in the UCSC genome browser: