awk: Extract sequence from a multiple-fasta file and return each sequence with the corresponding name

This post is about how to extract from a multiple fasta file each sequence with the corresponding header

Yedomon Ange Bovys Zoclanclounon https://yedomon.netlify.app/ (Jeonbuk National University)http://www.jbnu.ac.kr/eng/
01-10-2021

Brainstorming

When working with the multiple-sequences fasta files, I need to split the file and use each of them for multiple sequence alignment purposes. Therefore, the following awk based script is very helpful to complete this task.

Solution


cat your_file.fasta | awk '{
        if (substr($0, 1, 1)==">") {filename=(substr($0,2) ".fasta")}
        print $0 > filename }'

In this small script the substr function was employed to return the header name from after each > symbol and print the sequence with its corresponding name.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Zoclanclounon (2021, Jan. 10). Diary of an Autodidact: awk: Extract sequence from a multiple-fasta file and return each sequence with the corresponding name. Retrieved from https://yedomon.netlify.app/posts/2021-01-10-awk-extract-sequence-from-a-multiple-fasta-file-and-return-each-sequence-with-the-corresponding-name/

BibTeX citation

@misc{zoclanclounon2021awk:,
  author = {Zoclanclounon, Yedomon Ange Bovys},
  title = {Diary of an Autodidact: awk: Extract sequence from a multiple-fasta file and return each sequence with the corresponding name},
  url = {https://yedomon.netlify.app/posts/2021-01-10-awk-extract-sequence-from-a-multiple-fasta-file-and-return-each-sequence-with-the-corresponding-name/},
  year = {2021}
}