Randomly subsample a FASTQ file with seqtk
Draw a reproducible random subset of reads from a FASTQ file with seqtk sample, keeping paired mates in sync via a shared seed.
Prerequisites
- seqtk installed (`seqtk sample`)
- A FASTQ file (gzipped input is fine)
1Subsample a fixed number of reads
Draw 10,000 reads at random with a fixed seed so the subset is reproducible. seqtk writes plain FASTQ to stdout, so redirect it to a file; the follow-up line counts the reads to confirm. To take a percentage instead of a count, pass a fraction: `seqtk sample -s100 reads.fastq.gz 0.1 > sub.fastq` grabs 10%.
seqtk sample -s100 reads.fastq.gz 10000 > sub.fastq
echo $(($(wc -l < sub.fastq) / 4))Expected output
100002Subsample paired-end reads in sync
For paired-end data, run seqtk on R1 and R2 separately but with the SAME seed and the SAME target count. Identical seeds make seqtk pick the same read indices from both files, so mates stay paired. The final line prints both counts, which should match.
seqtk sample -s100 R1.fastq.gz 10000 > R1.sub.fastq
seqtk sample -s100 R2.fastq.gz 10000 > R2.sub.fastq
echo $(($(wc -l < R1.sub.fastq) / 4)) $(($(wc -l < R2.sub.fastq) / 4))Expected output
10000 10000Troubleshooting
Paired files fall out of sync (mates no longer line up)
You used different `-s` seeds on R1 and R2. Both mates must share the same seed and the same target count, e.g. `-s100 ... 10000` on each.
Output is not compressed (sub.fastq is plain text)
seqtk writes uncompressed FASTQ. Pipe through gzip: `seqtk sample -s100 reads.fastq.gz 10000 | gzip > sub.fastq.gz`.