This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filter fastq file by first two base pairs

Hi, I have the fastq file example.fq and I would like to get only reads that start with a TG. Does anybody have advice for how to do this? I've been searching and and all the filtering systems I've seen look at read length or elements in the read id, but not in the sequence.

Thank you!

fastq

3 answers

gunzip -c file.fastq.gz |\
    paste  - - - -  | awk -F '\t' '($2 ~ /^[Tt][Gg]/)' | tr "\t" "\n"

Works like a charm, thank you! Do you think you could explain how it works?

Another possibility, just having fun:

grep --no-group-separator -A2 -B1 '^TG' test.fq | grep --no-group-separator -A3 '^@HISEQ'

The reads ID in my fq file start with @HISEQ. Change @HISEQ with whatever your read IDs start with.

With BBMap:

bbduk.sh in=example.fq out=filtered.fq k=2 literal=TG rcomp=f mm=f restrictleft=2

This will also work on fasta.

Log in to answer this question.