This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to delete last 5 characters off of FASTA header?

Hi,

I am trying to remove the last 5 characters from my FASTA header in my sequencing data. I have ≈400,000 sequences and have tried to use sed command in terminal to do this for me.

Input text:

>1-4-8.45  
TAGGGAGA

Expected Output:

>1-4           
TAGGGAGA

How can I use sed command to remove the last 5 characters from my FASTA headers?

fasta header sed

Sure you want to do this? Your fasta headers may no longer remain unique. Edit: They are not unique to begin with. Never mind then.

To add context to this comment, OP previously had 2 different sequences with the exact same header in their input dataset.

2 answers

using sed, this solution is not consider the white spaces in header.

$ sed '/^>/s/.\{5\}$//' in.fa

for fasta and fastq file, bioawk https://github.com/lh3/bioawk is also good option, it can separate the $name and $comment in header.

$ bioawk -cfastx '{id=substr($name, 0, length($name) - 5); print ">"id"\n"$seq}'

What have you tried? This sort of problem has been addressed on the site multiple times

sed can match the first character of each line to pick lines where an operation is performed - you can use that to restrict the operation to just header lines. You can also capture the last five characters with the regex (.{5})$.

Please use these hints to get to the solution.

Log in to answer this question.