This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to split fasta by '>' into a file each containing one sequence, and have the name of that file be the ID?

So far I have this

awk '/^>/{s=++d".fasta"} {print > s}' file.fasta

This splits the file just as I want it, but it produces new files called 1.fasta, 2.fasta, 3.fasta and so on. Is there a method of splitting it that has the new file name as the ID of the sequence inside?

Or failing that, is there a quick way of renaming fasta's based on their ID?

fasta bash split

3 answers

faSplit ( http://hgdownload.cse.ucsc.edu/admin/exe/linux.x86_64/faSplit ) utility by Jim Kent from UCSC.

faSplit byname your_file.fa outRoot/

Thanks, this is good. I want to use this in combination with a find command, could you tell me why this isn't working?

for files in `find . -type f -name '*.consensus.fasta' -not -path "*/temp/*"`
do
    faSplit byname $files outRoot
done

What is not working? Did you make a real directory to replace outRoot?

Hi, yes I did make a more suitable directory! Just didn't include it because the name is sensitive. I meant just looking at the loop, It's so simple but It just doesn't work.

You need to include the trailing / after the directory name for this to work right. Try this.

for files in `find . -type f -name '*.consensus.fasta' -not -path "*/temp/*"`
do
    faSplit byname $files outRoot/
done

The perl script found here does what you want:

When creating this multi-entry FASTA file, one should take care to make the first word after the > symbol a unique value, as it will be used as the file name for that sequence.

create the filename with sprintf

   echo -e ">hello\nAAA\n>world\nATGCA" |\
    awk '/^>/ {fout=sprintf("%s.fasta",substr($0,2));}{print >> fout;}'

Log in to answer this question.