I am attempting to run a comparison a batch of alignments (bam format) to assess spread. However, some of my datasets are larger than others and I wish to subsample down to an equal number of reads per sample.

If I wanted to randomly extract 1 Million reads from a bam file is there a method to do this?

Note: I am fully aware of the samtools and picard methods which allow you to reduce by a proportion (i.e. the flag `-s 0.33`) but that would not result in an fixed number of reads per sample, which is what I need, but a reduced proportion *per sample* which doesn't help.

Bam subsampling has previously been talked about it but always the proportional data reduction, not the fixed number required: https://www.biostars.org/p/44527/, https://www.biostars.org/p/76791/#76796, https://www.biostars.org/p/110041, https://broadinstitute.github.io/picard/command-line-overview.html

Edit: Also, I've come across [bamtools][1] but haven't been able to get it to work and seems to have not been updated in quite some time

 [1]: https://hcc-docs.unl.edu/display/HCCDOC/Running+BamTools+Commands
This is based on some of the earlier threads mentioned in the comments, but you can use the GNU `shuf` utility to get a fixed number of random lines in a file. [Recent versions][1] of the tool use [reservoir sampling][2], which will improve efficiency. You could run something like:

    shuf -n 1000000 input.sam > output.sam

To get the header correctly, you could do something like:

    cat <(samtools view -SH input.sam) <(samtools view -S input.sam | shuf -n 1000000) > output.sam

 [1]: http://lists.gnu.org/archive/html/coreutils/2013-12/msg00166.html
 [2]: http://en.wikipedia.org/wiki/Reservoir_sampling