This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Re-naming just part of header in fasta file

Hi all,

I usually use some command to rename the header, but they changed the whole header. I want to re-name just part of the header in fasta file. In fact, the header is like below:

Contig1|m.1 Contig1|g.1  ORF Contig1|g.1 Contig1|m.1 type:complete len:427 (-)                                                                                         Contig1:277-1557(-)

I want to change just the first part (before the sign of | ). Could you please help me out with this issue?

fasta

How do you want to change it? What have you tried? Also, post more than one example. btw "|" is called pipe..

2 answers

Hi Seta, in case you want to just rename it, you can use sed:

sed 's/>Contig\([0-9]*\)|/>foo\1|/g' in.fa > out.fa

Presuming your fasta-line starts with ">", this sed command takes the ">Contig" and any number before the "|" and replaces it with "foo", or whatever you like to have, and the "\1" keeps the number after the "Contig". After the number the "|" is added to have the same structure again.

If you don't have the ">" included use "$" to state to use only the first occurrence.

Cheers

Michael

Thank you very much friend

In Python:

import re

line = 'CHANGE_ME|foo||bar|'

s = re.search('(?<=|)\w+', line)

fixed_line = line.replace(s.group(0), 'CHANGED')

Log in to answer this question.