This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Reformatting Fasta Files

Hi,

I am new to regular expressions and programming in general and I need help with reformatting FASTA files from

>AB000667.1.2469.3417   root;Eukaryota;Chordata;Actinopteri;Pleuronectiformes;Paralichthyidae;Paralichthys;Paralichthys olivaceus
CAAAGGCTTGGTCCTGACTTTACTGTCGACTCTAACTAGACTTACACATGCAAGTATCCG
CCCCCCTGTGAGAATGCCCATAACGCCCTGCTCGGGAACAAGGAGCTGGCATCAGGCACA
...

some of them have:

>AB002132.1._1.403

in the header

and I want to change it to:

>AccessionNumber.Version SpeciesName
nucleotide sequence
nucleotide sequence, continued
nucleotide sequence, continued
etc...

The requirments include:

  • The sequence name is just the Accession number followed by the version number
  • This is followed by a space and then the species name. There should be a space between the genus and species name

Thanks!

regular expressions fasta header

1 answer

$ sed '/>/ s/\(.*\)\s.*;\(.*\)$/\1\2/g' test.fa 

>AB000667.1.2469.3417  Paralichthys olivaceus
CAAAGGCTTGGTCCTGACTTTACTGTCGACTCTAACTAGACTTACACATGCAAGTATCCG
CCCCCCTGTGAGAATGCCCATAACGCCCTGCTCGGGAACAAGGAGCTGGCATCAGGCACA

input:

$ cat test.fa 

>AB000667.1.2469.3417   root;Eukaryota;Chordata;Actinopteri;Pleuronectiformes;Paralichthyidae;Paralichthys;Paralichthys olivaceus
CAAAGGCTTGGTCCTGACTTTACTGTCGACTCTAACTAGACTTACACATGCAAGTATCCG
CCCCCCTGTGAGAATGCCCATAACGCCCTGCTCGGGAACAAGGAGCTGGCATCAGGCACA

More the test data is provided, better would be the solution to OP. Gritz122

What would I have to change the regular expression to if I just need the

Genus_species

 $  sed '/>/ s/.*;\(.*\)$/>\1/g;s/ /_/g' test.fa

>Paralichthys_olivaceus
CAAAGGCTTGGTCCTGACTTTACTGTCGACTCTAACTAGACTTACACATGCAAGTATCCG
CCCCCCTGTGAGAATGCCCATAACGCCCTGCTCGGGAACAAGGAGCTGGCATCAGGCACA

Thanks so much, huge help!

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

Log in to answer this question.