For calculation:
reads=10000000
bam=your.bam
fraction=$(samtools idxstats $bam | cut -f3 | awk -v ct=$reads 'BEGIN {total=0} {total += $1} END {print ct/total}')
samtools view -b -s ${fraction} foo.bam > sampled.bam
Note that a) this may not give exactly 10mio reads due to some rounding errors, and b) this will give 10mio reads which in a paired-end bam file equals 5mio "pairs", so for 10mio pairs it would need to be 20000000 for reads.
Does anyone know whether samtools is smart enough to keeps both mates of a pair, I actually do not know?
Reduce read count as in select
a). Any 10M reads or
b).only high quality top 10M reads?
seqtk (https://github.com/lh3/seqtk) expects FASTA or FASTQ as input.
reformat.shfrom BBMap suite.Use one of the sampling options below:
Along with any of processing parameters you want: