This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how split multiple fasta file into multiple file

Hi,

I would like to split multiple fasta file into multiple files where name of each file should be header name without ">" symbol.

For example:

>contig1
ATACTCTAATTATTA
>contig2
ATACTCTAATTATTA
>contig3
ATACTCTAATTATTA
>contig4
ATACTCTAATTATTA

files should contig1 contig2 contig 3

Number of configs are approximately 20,000.

I found a couple of awk script but it doesn't seem to work. If you feel it is redundant query, I apologize.

Thank you

r sequence rna-seq assembly genome

2 answers

There you go:

cat test.fa 
>contig1
ATACTCTAATTATTA
>contig2
ATACTCTAATTATTA
>contig3
ATACTCTAATTATTA
>contig4
ATACTCTAATTATTA

## basically as in: https://stackoverflow.com/questions/11818495/split-a-fasta-file-and-rename-on-the-basis-of-first-line
awk '/^>/ {OUT=substr($0,2) ".fa"}; OUT {print >OUT}' test.fa

ls
$ contig1.fa  contig2.fa  contig3.fa  contig4.fa  test.fa

foo

Thank you very much for reply

From that link,

First solution doesn't work. It doesn't give any output. awk -F '|' '/^>/ {F=sprintf("%s.fasta",$2); print > F;next;} {print >> F;}' <file name<="" p="">

Second script works but it give file names include (">"). That I don't want. awk -F "|" '/^>/ {close(F) ; F = $1".fasta"} {print >> F}' yourfile.fa

Thank you, I used rename command to remove first character from each file,

rename 's/^(.{1})//' *

Log in to answer this question.