BamTools provides both a programmer's API and an end-user's
toolkit for handling BAM files.
