This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Add words at beginning and end of the same line for the FASTA header line with sed

I have the following line:

>A_1000
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC

I would like to convert the first line as follows:

>INITWORD/A_1000/FINALWORD
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC

I found a similar question that did allow me to append the end and the beginning as I needed (https://stackoverflow.com/questions/68541730/add-words-at-beginning-and-end-of-a-fasta-header-line-with-sed). However, it adds the FINALWORD to the next line.

I ran the following:

 sed 's%^>(.*)%>Initialword/\1/Finalword%' fasta _test.fasta > fasta_test2.fasta

Which returns:

>Initialword/A_0101M/Finalword 
ACTTTCGATCTCTTGTAGATCTGTTCTC...CACM
ACTTTCGATCTCTTGTAGATCTGTTCTC...CACM

But in the Fasta file it looks like:

>Initialword/A_0101 
/Finalword 
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC

How can I fix this to just add the text to the beginning and end of the header? What is the M at the end of each line in the file?

Thank you

fasta sed

kchambers58178 : I formatted the first two lines but I am not sure what you exactly want with last two. Please use the 101010 button in edit mode to apply code formatting for those two lines so they show up the way you want them to.

code_formatting

1 answer

This question has been resolved with this option:

dos2unix <input.fasta | sed -E 's%^>(.*)%>Initialword/\1/Finalword%' >output.fasta

alternative (after dos2unix):

$ sed -re '/^>/ s_^>_>initial/_;s_$_/final_' test.txt

>initial/A_1000/final
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC/final
ACTTTCGATCTCTTGTAGATCTGTTCTC...CAC/final

Log in to answer this question.