added support for writing counts to a file
close file
bam output is optional