How can I downsample a BAM file while keeping both reads in pairs? I know how to downsample a BAM file to lower coverage. I know I can randomly select lines in SAM, but this procedure can't guarantee two reads in a pair are always sampled the same time. Is there a way to downsample BAM while keeping pairing information intact?
samtools has a subsampling option:


  -s FLOAT: 
  Integer part is used to seed the random number generator [0]. Part after the decimal point sets the fraction of templates/pairs to subsample [no subsampling]


samtools view -bs 42.1 in.bam &gt; subsampled.bam


will subsample 10 percent mapped reads with 42 as the seed for the random number generator.