This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extracting fasta file according to Ids

Hello,

I have a huge Fasta file. I would like to extract all the sequences with id such that the ids start from NM?

I tried following two commands which did not work for me.

awk‘BEGIN{RS=”>”}/NM/{print“>”$0}’huge.fasta

grep '^>NM' -B 1 huge.fasta > Nm.fasta

Could someone help me with a better solution. thank you in advance.

#fasta #extractsequence

is your huge fasta a multi-line fasta or single line fasta?

1 answer

Index your FASTA file:

samtools faidx input.fasta

Then get ids which start with "NM" and pipe them into samtools faidx to retrieve only those sequences:

cut -f 1 input.fasta.fai | egrep "^NM" | xargs samtools faidx input.fasta > result.fasta

Log in to answer this question.