This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filtering fasta file based on identifier

Hi, I have a fasta file with many segments and I want to filter out all the segments that have a "P" in the identifier of the segment. Is there a conventional way to do so? Thanks.

fasta filter

Thank you!

2 answers

bioawk -c fastx '$name ~ /P/ { print ">"$name; print $seq }' <sequences.fa

If you wanna take all except those with a "P",

bioawk -c fastx '$name ! /P/ { print ">"$name; print $seq }' <sequences.fa

bioawk here

Neat. I've not found a use for bioawk before but this seems perfect.

It kinda clicked out of the blue for me yesterday. Now I'm gonna add this to my arsenal of regular-use tools :-)

how can give the transcriptome.fasta and headerlist.txt in this command?

What are those two files?

just awk

awk '/^>/{N=0} /^>P/{N=1} {if(N)print}' *.fa

Maybe

/^>\S*P\S*/

To match identifiers (up to the first space) that contain P rather than just identifiers that start with P.

Would this not print only headers, Pierre?

no, if there is no 'next' statement, awk continues to scan all the patterns.

Oops, I read it wrong. I read it as the if(N) print being in the same {} as the N=1. My bad!

But where does the ouput go? Sorry for my ignorance.

"standard out" or "stdout". You can redirect this to a file like:

awk '/^>/{N=0} /^>P/{N=1} {if(N)print}' *.fa > out.fa

Log in to answer this question.