This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Grep search question

Hi,

I was wondering if I have 2 sequences of interest: A and B.

Is it possible to do a grep search for reads (in a fastq):

  • that contain just sequence "A" and exclude "B" (and vice versa)
  • that contain both sequence "A" and "B" together

if so, would someone be able to provide/refer me to the commands to do so?

Thank you so much!

fastq sequence grep sequencing fasta

Keep in mind that grep will only do perfect matches. You may want to use a NGS data specific tool like seqkit grep or bbduk.sh in filter mode, if you want to allow for other cases than perfect matches.

1 answer

You can do it with:

# Grep sequences with A
grep $A file.fastq -B 1 -A 2 > hasA.fastq
# Grep B where A is found
grep $B hasA.fastq -B 1 -A 2 > hasAandB.fastq
# Remove lines in file 1 that are found in file 2
comm -23 hasA.fastq hasAandB.fastq > hasAnotB.fastq

Replace $A and $B with the sequences or set the value of the variables to the sequences you are searching.

Log in to answer this question.