Replace FASTA file headers
1
0
Entering edit mode
3.6 years ago

Hello everyone,

I try to replace the headers A of a FASTA file (file.fasta) with headers B.

For this, I have a list which match the headers names.

>A_1     >B_1
>A_2     >B_2
>A_3     >B_3
etc...

I am using this loop to replace the headers:

cat list | while read f ;
do
 echo $f > temp_file
 A=$(awk '{print $1}' temp_file ) ;
 B=$(awk '{print $2}' temp_file  ) ;
 sed -i  's/$A/$B/g' file.fasta ;
done

But this simple loop does not work. Would anyone know if I have made an obvious mistake?

fasta FASTA sed loop • 1.2k views
ADD COMMENT
1
Entering edit mode

you already asked several questions on this forum without commenting or validating (green mark on the left) any answer. Please review the answers: Keep metadata gc column using GenomicRanges ; merging overlaping ranges with GenomicRanges ; ...

ADD REPLY
0
Entering edit mode

My apologizes, I thought the thumb up was the way to validate the answer.

ADD REPLY
1
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one if they work.
Upvote|Bookmark|Accept

ADD REPLY
1
Entering edit mode

I have made an obvious mistake?

https://stackoverflow.com/questions/6697753/

ADD REPLY
0
Entering edit mode

The name in your B fie is a prefix or a different name? Because if it is a prefix you can replace it directly in the A file. And btw, to use bash variables inside of sed you need to use double-quotes.

ADD REPLY
0
Entering edit mode

Use dedicated tools such as seqtk and seqkit to replace fasta headers from a file.

ADD REPLY
2
Entering edit mode
3.6 years ago
Joe 21k

You can turn your substitutions file into a list of sed commands and run that on the file. Assuming your subs file is separated by spaces:

Run: sed -i -e 's|^|s\/|g' -e 's| \+|\/|g' -e 's|$|\/g|g' subs.txt on the file containing the substitutions, which gives:

s/>A_1/>B_1/g
s/>A_2/>B_2/g
s/>A_3/>B_3/g

Then,

sed -i -f subs.txt myfile.fasta should give you the edited file.

ADD COMMENT

Login before adding your answer.

Traffic: 1766 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