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

Hello! I'm building a database of a certain gene family. I downloaded the fastas from uniprot , concatenated the resulting fastas using cat and the fasta headers of each sequence have the following format:

> tr|D7RED9|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442 GN=nidA3 PE=3 SV=1

I'm performing an alignment with mmseqs2 and I need that the gene information (the GN= part) is the first string after the first pipe sign (|) on each fasta header. is there a way to do that using awk or R string manipulation?

I want that all my fasta headers have as first string just after the first pipe sign, the GN='gene name' part.

the expected result of each fasta header is the following:

> tr|GN=nidA3|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442 PE=3 SV=1

Thanks for your time

fasta r bash string

check if this works:

$ awk -F '[| ]' '/^>/ {$3=$11"|";$11="";$2=$2"|"; gsub(/\| /,"|",$0)}1' test.fa

> tr|GN=nidA3|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442  PE=3 SV=1
atgc

$ sed -r '/^>/ s/(tr\|)(.*\|)(.*)(GN=\w+)(.*)$/\1\4\|\3\5/' test.fa

> tr|GN=nidA3|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442  PE=3 SV=1
atgc

Replace sed with gsed on MacOS.

1 answer

Can you explain what do you need exactly? Just to extract the field you can use Perl RegEx:

$ echo "tr|D7RED9|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442 GN=nidA3 PE=3 SV=1" | perl -lne 'print $1 if (/GN=(\w+)/)'
nidA3

Thanks for your reply, sorry if I didn't explain well, the issue is that I have 334 sequences that are under a single fasta file. Each sequence on their fasta headers (starting with '>') have information about the sequence's gene identity. That information is given by the GN section but it appears at almost the end of the sequence.

What I need is that the gene information appears just after the first pipe sign on each one of the 334 fasta headers.

thanks, that is more clear, you can do with:

$ echo "tr|D7RED9|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442 GN=nidA3 PE=3 SV=1" | perl -pe 'if(/(GN=\w+)/) { $id=$1; s/\|/|$id|/}'
tr|GN=nidA3|D7RED9|D7RED9_9MYCO NidA3 (Fragment) OS=Mycobacterium sp. py145 OX=767442 GN=nidA3 PE=3 SV=1

Log in to answer this question.