This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how to change fasta headers?

Dear all I have a file containing multiple fasta sequnces like

>lcl|NZ_CP018664.1_prot_WP_000637306.1_3741 [locus_tag=AUO97_RS19225] [protein=hypothetical protein] [protein_id=WP_000637306.1] [location=complement(4001198..4001389)] [gbkey=CDS]
MIVSNNFAVPYYLNVRKEKGMTAYYWATHQSQLALFDSYELAYRFYFPSRHILIRSEIKAFAQ
>lcl|NZ_CP018664.1_prot_WP_000572517.1_3742 [locus_tag=AUO97_RS19230] [protein=FadR family transcriptional regulator] [protein_id=WP_000572517.1] [location=complement(4001417..4002115)] [gbkey=CDS]
MIEQIQKRSLVDEVIHVIRQNIKNDIWKVDEKIPTEPELVQGLGVGRNTIREAIKILEYLGVLEVKQGLG

I want to change headers of this file like

>WP_000637306.1
MIVSNNFAVPYYLNVRKEKGMTAYYWATHQSQLALFDSYELAYRFYFPSRHILIRSEIKAFAQ
>WP_000572517.1
MIEQIQKRSLVDEVIHVIRQNIKNDIWKVDEKIPTEPELVQGLGVGRNTIREAIKILEYLGVLEVKQGLG
sequence

3 answers

An awk solution:

$ awk '$0 ~ "^>" {match($0, /protein_id=([0-9A-Z_\.]+)/, protein); print ">"protein[1]; next;}1' input.fa > output.fa

fin swimmer

$ sed '/>/ s/.*_\([A-Z]\{2\}_[0-9]\+.[0-9]\).*/>\1/g' test.fa

or

$ sed '/>/ s/.*=\([A-Z]\+_[0-9]\+.[0-9]\).*/>\1/g' test.fa

>WP_000637306.1
MIVSNNFAVPYYLNVRKEKGMTAYYWATHQSQLALFDSYELAYRFYFPSRHILIRSEIKAFAQ
>WP_000572517.1
MIEQIQKRSLVDEVIHVIRQNIKNDIWKVDEKIPTEPELVQGLGVGRNTIREAIKILEYLGVLEVKQGLG

For case insensitive ID:

$ sed '/>/ s/.*_\([A-Za-z]\{2\}_[0-9]\+.[0-9]\).*/>\1/g' test.fa

and if the sequences are like

>WP_000580188.1hypotheticalprotein[Acinetobacterbaumannii]
MIGQQRNILATLGIDVWIPRTQVCQKNNAHTLWRDQVVEPHESITVPTIDVPAFEQKNTQPQVLEIPKVVEEPPIVVAEVSQPEILVEKPKVIEQETITPFELQAYCLEKCVIFVDVTALETEEKQLWANIQKAKVGQYSELRWPFPLAAYQDQRGVGSYIQGFLDAVAAEKKILCLGKCTYIQHANIIHLASLKEMLDKPLLKKRLWQLMQDNNE 
>WP_000807316.1MULTISPECIES:glycerol-3-phosphatedehydrogenase(NAD(P)(+))[Acinetobactercalcoaceticus/baumanniicomplex]
 MAEFKFTDLVEPVAV

How can I change to

>WP_000580188.1
MIGQQRNILATLGIDVWIPRTQVCQKNNAHTLWRDQVVEPHESITVPTIDVPAFEQKNTQPQVLEIPKVVEEPPIVVAEVSQPEILVEKPKVIEQETITPFELQAYCLEKCVIFVDVTALETEEKQLWANIQKAKVGQYSELRWPFPLAAYQDQRGVGSYIQGFLDAVAAEKKILCLGKCTYIQHANIIHLASLKEMLDKPLLKKRLWQLMQDNNE 

>WP_000807316.1
MAEFKFTDLVEPVAV

Hello sharmatina189059,

  • Please use the formatting bar (especially the code option) to present your post better. I've done it for you this time.
    code_formatting
  • There is no connection between your input and output example. Please review this and provide a correct one.

Thanks.

fin swimmer

$ sed '/>/ s/\(^>[0-9A-Z_]\+\.[0-9]\+\).*/\1/g' input.fa > output.fa

I wonder how you get to (or where you got them from) those kind of fasta headers? they're really violating all possible rules for fasta header formatting ...

But I have retrieved it from NCBI ftp site and the format is same. AM I doing some error?

don't think it's you.

The ones in your original post are OK, but the ones you posted here above are missing all the spaces. if you retrieve them this way from NCBI I would let them know that something is off

try this: sharmatina189059

$ sed '/>/ s/^\(>\w\+.\w\).*/\1/g' file.fa

or

$ sed '/>/ s/^\(.*\.\w\).*/\1/g' file.fa

>WP_000580188.1
MIGQQRNILATLGIDVWIPRTQVCQKNNAHTLWRDQVVEPHESITVPTIDVPAFEQKNTQPQVLEIPKVVEEPPIVVAEVSQPEILVEKPKVIEQETITPFELQAYCLEKCVIFVDVTALETEEKQLWANIQKAKVGQYSELRWPFPLAAYQDQRGVGSYIQGFLDAVAAEKKILCLGKCTYIQHANIIHLASLKEMLDKPLLKKRLWQLMQDNNE 
>WP_000807316.1
 MAEFKFTDLVEPVAV

Dear colleague, I think that you may also find useful the SEDA software (http://sing-group.org/seda/ ).

As you can see in the manual (https://www.sing-group.org/seda/manual ), it has a lot of different customizable operations to process FASTA files, including operations to reformat sequence headers as you need (see the Rename header operation).

It also includes a specific NCBI Rename operation (https://www.sing-group.org/seda/manual/operations.html#ncbi-rename ) aimed to replace accession codes with species names and taxonomy information.

With best regards,

Hugo.

Log in to answer this question.