This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Replace names in FASTA headers from the first white space

Hello, I have a list of sequences in a fasta file which looks like this:

>WP00001_00001 HP Protein 1
ATGCATGATCAGTTGACGT
>WP00002_00022 Protein Like/Protein1
ATGACTGACGTTGACGTAC
>WP00002_00007 Protein cluster2
ATGGCTAGCCATGTACATT

I want to replace the first white space with a pipe (|) and then replace all other white space in the header (description) with an underscore (_). So that the final output file should look like this:

>WP00001_00001|HP_Protein_1
ATGCATGATCAGTTGACGT
>WP00002_00022|Protein_Like/Protein1
ATGACTGACGTTGACGTAC
>WP00002_00007|Protein_cluster2
ATGGCTAGCCATGTACATT

Could you please help me with that?

Regards, PSP

fasta space headers

What have you tried? The forum has a number of "edit FASTA/Q header" posts.

2 answers

Perl-one-liner:

$ perl -pe 'if(/>/) { s/\s/|/; s/\s/_/g; s/_$/\n/ }' < in.fasta 
>WP00001_00001|HP_Protein_1
ATGCATGATCAGTTGACGT
>WP00002_00022|Protein_Like/Protein1
ATGACTGACGTTGACGTAC
>WP00002_00007|Protein_cluster2
ATGGCTAGCCATGTACATT

Thanks a lot! It solved my problem.

$ sed -re '/^>/ s/\s/|/;s/\s/_/g' test.fa  

>WP00001_00001|HP_Protein_1
ATGCATGATCAGTTGACGT
>WP00002_00022|Protein_Like/Protein1
ATGACTGACGTTGACGTAC
>WP00002_00007|Protein_cluster2
ATGGCTAGCCATGTACATT

Thanks a lot! It worked.

Log in to answer this question.