text: user549: 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?
  
  user2388: A totally different tactic...it helps if you aren't super picky about how many reads you have at the end.
  Illumina read names contain the lane, tile, and xy coordinates of the read in them.  If you filtered by these, you would be guaranteed to get either both members of a pair, or neither.  I would recommend trying to avoid the edges of the flowcell.
  
  user-1: With this function, you can subsample a given BAM file to a user-specified number of reads using SamBamba. The function automatically calculates the scaling factor. Ideas in part taken from here
  function SubSample {
  
  ## Calculate the sampling factor based on the intended number of reads:
  FACTOR=$(samtools idxstats $1 | cut -f3 | awk -v COUNT=$2 'BEGIN {total=0} {total += $1} END {print COUNT/total}')
  
  if [[ $FACTOR > 1 ]]
    then 
    echo '[ERROR]: Requested number of reads exceeds total read count in' $1 '-- exiting' && exit 1
  fi
  
  sambamba view -s $FACTOR -f bam -l 5 $1
  
  }
  
  ## Usage example, selecting 100.000 reads:
  SubSample in.bam 100000 > subsampled.bam
  
  Note that $2 is the number of reads. Say you choose 10000 reads but have paired-end data, you'll end up with 5000 fragments.
  
  user161: 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 > subsampled.bam
  
  will subsample 10 percent mapped reads with 42 as the seed for the random number generator.
  
original_text: 549: <p>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?</p>
   2388: <p>A totally different tactic...it helps if you aren't super picky about how many reads you have at the end.</p>
  
  <p>Illumina read names contain the lane, tile, and xy coordinates of the read in them.  If you filtered by these, you would be guaranteed to get either both members of a pair, or neither.  I would recommend trying to avoid the edges of the flowcell.</p>
   -1: <p>With this function, you can subsample a given BAM file to a user-specified number of reads using SamBamba. The function automatically calculates the scaling factor. Ideas in part taken from <a href="http://crazyhottommy.blogspot.com/2016/05/downsampling-for-bam-files-to-certain.html" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>function SubSample {
  
  ## Calculate the sampling factor based on the intended number of reads:
  FACTOR=<span class="math-container">$(samtools idxstats $</span>1 | cut -f3 | awk -v COUNT=<span class="math-container">$2 'BEGIN {total=0} {total += $</span>1} END {print COUNT/total}')
  
  if [[ <span class="math-container">$FACTOR &gt; 1 ]]
    then 
    echo '[ERROR]: Requested number of reads exceeds total read count in' $</span>1 '-- exiting' &amp;&amp; exit 1
  fi
  
  sambamba view -s <span class="math-container">$FACTOR -f bam -l 5 $</span>1
  
  }
  
  ## Usage example, selecting 100.000 reads:
  SubSample in.bam 100000 &gt; subsampled.bam
  </code></pre>
  
  <p>Note that <code>$2</code> is the number of reads. Say you choose 10000 reads but have paired-end data, you'll end up with 5000 fragments.</p>
   161: <p><a href="http://www.htslib.org/doc/samtools.html" rel="noreferrer">samtools</a> has a subsampling option:</p>
  
  <blockquote>
    <p>-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]</p>
  </blockquote>
  
  <pre><code>samtools view -bs 42.1 in.bam &gt; subsampled.bam
  </code></pre>
  
  <p>will subsample 10 percent mapped reads with 42 as the seed for the random number generator.</p>
  