This is a test version of Biostars. For the public version, visit https://www.biostars.org.
From Single Line To Fasta Format ?

Hi, I'm parsing some sequences and I need to transform them from a single line to fasta format... The format I have now is something like this:

Name_of_the_specie AGTAGAGATTGAGATGAGGAGATTCCCATAGAGTTCGAATAGCTGTAATAGAG
Name_of_the_specie2 AGGAGGTTTGAGAGTTTTGAGAGGTGGGGAGAGTTTTTTAAAGGGGGAAAGAAGGAGAATGAGAGGGTTGAG  
Name_of_the_ specie_3 ACATGCGATGAGGGCCCTTTATAATTTAATTGCGCTAAATAGATCTCCCTTTAGGGATATTGAGGAAGAGGAGGGATTAGGAGATTAGAGAGATAT

and so on... (>300)

I want to make a single file containing all those sequences in fasta format, if possible using perl/awk/sed.

Thanks!

fasta parsing perl awk

4 answers

Assuming the name of the file doesn't have spaces, neither does the sequence and the new sequence starts in a new line - this should be straightforward

awk '{print ">"$1 "n" $2}' file_name > new_filename

the command would print > before the name ($1) and after a new line "n" you would get the sequence ($2) - which would be the fasta format

>Name_of_the_specie 
AGTAGAGATTGAGATGAGGAGATTCCCATAGAGTTCGAATAGCTGTAATAGAG 
>Name_of_the_specie2 
AGGAGGTTTGAGAGTTTTGAGAGGTGGGGAGAGTTTTTTAAAGGGGGAAAGAAGGAGAATGAGAGGGTTGAG
>Name_of_the_ specie_3 
ACATGCGATGAGGGCCCTTTATAATTTAATTGCGCTAAATAGATCTCCCT TTAGGGATATTGAGGAAGAGGAGGGATTAGGAGATTAGAGAGATAT

thank you, I was trying to do it in awk and I was doing it wrong... now I know for the next time !

If it is one line per name and sequence, you can use this command:

perl -ane 'print ">$F[o]\n$F[1]\n";' yourFile.txt > yourFile.fasta

many thanks! :)

If your file is really as simple as you say then just do:

sed 's/^/>/' file1 >file2
sed 's/ /\n/' file2 >file3.fasta

as long as there are no spaces in your species name...

thanks! yes, I didn't have spaces in the names :)

You can do it in one line and avoid that intermediate file: sed -e 's/^/>/' -e 's/\ /\n/' inputfile > outputfile

Mabeuf,David and ngsgene, many thanks!

All the solutions work perfectly! :)

Use comments for replies please, not answers.

Log in to answer this question.