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.
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.
seqkit grep -f ids.txt reads.fastq.gz -o subset.fastq.gzExpected output
[INFO] 512 patterns loaded from file2Confirm 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.
seqkit stats subset.fastq.gzExpected output
file format type num_seqs sum_len min_len avg_len max_len
subset.fastq.gz FASTQ DNA 512 77,312 151 151 1513Invert 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.
seqkit grep -v -f ids.txt reads.fastq.gz -o cleaned.fastq.gzExpected output
[INFO] 512 patterns loaded from fileTroubleshooting
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`.