<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>

CC BY-SA 4.0
2018-12-12T18:31:35.037Z
2018-12-12T18:31:35.037Z
swbarnes2
<p>It have used both sambamba and samtools. Either one works great to downsample to a desired read depth after calculating the downsample factor.</p>
<p>Sambamba can use multiple threads by using <code>-t INT</code>, where INT is the number of threads. It can also set the compression level for the BAM file using <code>-l</code>.</p>
<pre><code>BAM=&quot;Sample.bam&quot;
READS=&quot;10000000&quot;
fraction=<span class="math-container">$(samtools idxstats $</span>BAM | cut -f3 | awk -v ct=<span class="math-container">$myReads 'BEGIN {total=0} {total += $</span>1} END {print ct/total}')

# With samtools
samtools view -b -s  <span class="math-container">${fraction} $</span>{BAM} &gt; output.bam

# With sambamba
sambamba view -f bam -l 5 -s  <span class="math-container">${fraction} $</span>{BAM} &gt; output.bam
</code></pre>

CC BY-SA 4.0
2023-09-15T00:17:45.753Z
2023-09-15T00:17:45.753Z
ricardo3889
<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>

CC BY-SA 3.0
2017-06-03T11:13:22.687Z
To avoid possible confusion in the future, it should be noted that this works by hashing the read name. If for some terrible reason the mates don't have the exact same name (e.g., `/1` and `/2` weren't stripped) then this will break.
Devon Ryan
CC BY-SA 3.0
2017-06-03T11:47:07.450Z
And also due to the way this works, the number of subsampled entries is not guaranteed to be the exact required percentage: some transformation of the hash residing in the [0,1] interval is compared against the sampling proportion. Depending on the read names present in the file, the number of sam entries effectively sampled will vary.
bli
CC BY-SA 3.0
2017-06-03T01:08:48.127Z
2017-06-03T01:08:48.127Z
Saket Choudhary
<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>

CC BY-SA 3.0
2017-06-03T02:03:01.850Z
Could you please give a bit more explanation as to *why* it would be useful to downsample reads? There are a few different ways to do it, and the answers will depend on your goal. For example, you can do a digital normalisation to reduce the input read count for genome assembly, but that method would not be appropriate for a quantitative analysis of reads (e.g. for RNASeq).
gringer
<sam>
How can I downsample a BAM file while keeping both reads in pairs?
# How can I downsample a BAM file while keeping both reads in pairs?

(Asked by: username_0 on 2017-06-02)

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?

**Tags:** sam
> **Comments:**
> **username_1**: Could you please give a bit more explanation as to *why* it would be useful to downsample reads? There are a few different ways to do it, and the answers will depend on your goal. For example, you can do a digital normalisation to reduce the input read count for genome assembly, but that method would not be appropriate for a quantitative analysis of reads (e.g. for RNASeq).  

---

## 3 answers

### Answer by username_4 on 2017-06-03. Score: 17 | Accepted answer

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.

> **Comments:**
> **username_5**: To avoid possible confusion in the future, it should be noted that this works by hashing the read name. If for some terrible reason the mates don't have the exact same name (e.g., `/1` and `/2` weren't stripped) then this will break.  
> **username_6**: And also due to the way this works, the number of subsampled entries is not guaranteed to be the exact required percentage: some transformation of the hash residing in the [0,1] interval is compared against the sampling proportion. Depending on the read names present in the file, the number of sam entries effectively sampled will vary.  

---

### Answer by username_2 on 2018-12-12. Score: 1

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.


---

### Answer by username_3 on 2023-09-15. Score: 0

It have used both sambamba and samtools. Either one works great to downsample to a desired read depth after calculating the downsample factor.
Sambamba can use multiple threads by using -t INT, where INT is the number of threads. It can also set the compression level for the BAM file using -l.
BAM="Sample.bam"
READS="10000000"
fraction=$(samtools idxstats $BAM | cut -f3 | awk -v ct=$myReads 'BEGIN {total=0} {total += $1} END {print ct/total}')

# With samtools
samtools view -b -s  ${fraction} ${BAM} > output.bam

# With sambamba
sambamba view -f bam -l 5 -s  ${fraction} ${BAM} > output.bam


---

