Hello I have a lot of sequences in a FASTA file, and I want to extarct a specific sequence knowing the header ID. for example the header of a sequence is:

NODE_19_length_5758_cluster_19_candidate_1

I know that with `grep` I can extract the header, but i want the below sequences to appear on stdout.

How can I do this on bash?





The proposed solutions are probably all fine but have the limitation that they first have to iteratively find the correct sequence which can take time if the file is (very) large. The by far simplest solution would be to use `samtools faidx` which in a first step indexes your fasta file and then can use this index to retrieve any sequence in basically no time. The index generation takes like 10 seconds for the entire mouse genome and is therefore not really a limitation, and it only has to be done once per fasta file:

    samtools faidx your.fasta NODE_19_length_5758_cluster_19_candidate_1

For many sequences manually:

    samtools faidx your.fasta seq1 seq2 seq(...N)

or if you want it automated

    (upstream cmd creating "\n"-sep list of seq.names) | xargs samtools faidx your.fasta

Please be sure to browse existing threads, this has been asked many times before.

https://www.biostars.org/p/49820/

https://www.biostars.org/p/329774/


