How to Grep the complete sequences containing a specific motif in a fasta file? Also, I want to include the lines beginning with a ">" before these target sequences.
The image is not shown so I will add this link of example because typing > in biostar is kinda misleading: https://drive.google.com/file/d/0B1pci7ps8bLganZXWFNFcWZGd1k/view?usp=sharing
1 answer
First, you'd have to change your sequences so that the DNA is all in one line, without this step you'd miss possible motifs hits that have line breaks in them.
From Pierre Lindenbaum: A: Multiline Fasta To Single Line Fasta
awk '/^>/ {printf("\n%s\n",$0);next; } { printf("%s",$0);} END {printf("\n");}' < file.fa > one_line.fa
Then you can use grep -B 1 to get the hit with its preceding line, let's also use LC_ALL=C to speed things up:
LC_ALL=C grep -B 1 KME one_line.fa
that should print all sequence names and their sequence where 'KME' is present.
Log in to answer this question.
Test file:
To extract all sequences with KME in them and one can ignore the case as well in the example code:
Download seqkit here. -s = match only sequence; -r = pattern is regular expression; -i = ignore case; -p = search pattern
if fasta sequences are linearized (i.e all sequences are in a single line), then code would be: