Re-naming just part of header in fasta file
2
0
Entering edit mode
8.8 years ago
seta ★ 1.9k

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 • 3.1k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
8.8 years ago
michael.ante ★ 3.9k

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

ADD COMMENT
0
Entering edit mode

Thank you very much friend

ADD REPLY
0
Entering edit mode
8.8 years ago
Anima Mundi ★ 2.9k

In Python:

import re

line = 'CHANGE_ME|foo||bar|'

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

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

Login before adding your answer.

Traffic: 2176 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6