All protocols

Extract specific reads from a FASTQ by ID

Pull specific reads out of a FASTQ by matching their IDs against a list with seqkit grep, or invert the match to drop them.

SSSudipta SardarUpdated July 21, 2026

Prerequisites

  • seqkit installed (`seqkit version` works)
  • A FASTQ file (optionally gzipped)
  • A plain-text file of read IDs, one per line

1Extract the reads listed in ids.txt

Put one read ID per line in ids.txt with no leading "@". seqkit grep keeps only the reads whose IDs appear in that file and writes them to subset.fastq.gz; on success it logs how many patterns it loaded and prints nothing else.

bash
seqkit grep -f ids.txt reads.fastq.gz -o subset.fastq.gz

Expected output

[INFO] 512 patterns loaded from file

2Confirm the subset

Run seqkit stats on the output and check that num_seqs matches the number of IDs you asked for. This is the fastest sanity check that the match actually landed.

bash
seqkit stats subset.fastq.gz

Expected output

file             format  type  num_seqs  sum_len  min_len  avg_len  max_len
subset.fastq.gz  FASTQ   DNA        512   77,312      151      151      151

3Invert the match to remove those reads

Add -v to flip the selection and keep every read except the ones listed — handy for dropping known contaminant or duplicate IDs. The kept reads go to cleaned.fastq.gz.

bash
seqkit grep -v -f ids.txt reads.fastq.gz -o cleaned.fastq.gz

Expected output

[INFO] 512 patterns loaded from file

Troubleshooting

Zero reads matched even though the IDs look right

By default seqkit matches the ID up to the first space, so a "/1" or "/2" mate suffix or the full read description will miss. Match the whole header with `-n`, or use a regex pattern with `-r`.

Leading "@" in ids.txt

Strip it — the ID is the header text without the "@". For example, the read header `@SRR001.5 length=151` has the ID `SRR001.5`.