edit only the 1st column of a fasta header by removing strings after '-'

63 Views Asked by At

I have a fasta file with the following header structure:

>Saurogobio_punctatus-NC_080528.1|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA

Where each section is separated by a pipe '|', and the first section is a combination of species_name-accessionID.

I want to remove the accesionIDs after the hyphen '-', but keep everything else. Like this:

>Saurogobio_punctatus|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA

I've tried:

sed -E '/^>/s/(\|[^-]*)-.*$/\1/' input.fasta > output.fasta

But this removes everything after the hyphen '-':

>Saurogobio_punctatus
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA

I've used this piece of code before to edit my header and include the taxid= before my 2nd column:

awk 'BEGIN { FS=OFS="|" } /^>/ { print $1, "taxid=", $2, $3; next } { print }' file.fa > edit_file

I was wondering if there is a way to maybe combine these 2 commands, where i edit my first column and then reprint the rest, but i don't know how to do it :(

I appreciate any help with this!

3

There are 3 best solutions below

2
Cyrus On BEST ANSWER

I suggest with sed:

sed 's/-[^|]*//' file

Output to stdout:

>Saurogobio_punctatus|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA

See: The Stack Overflow Regular Expressions FAQ

0
markp-fuso On

One awk idea:

awk '
BEGIN { FS=OFS="|" }
/^>/  { n=index($1,"-"); $1=substr($1,1,n-1) }
1
' input.fasta

This generates:

>Saurogobio_punctatus|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA
0
jared_mamrot On

Another potential awk option:

awk '/>/ {gsub("-[^\\|]*\\|", "|", $0); print} !/>/ {print}' input
>Saurogobio_punctatus|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA

Or, a better option, using bioinformatics software designed for the task (https://bioinf.shenwei.me/seqkit/):

seqkit replace -p "\-[^|]*|" -r "" input
>Saurogobio_punctatus|taxid=1771284|cellularorganisms,Eukaryota,Opisthokonta,Metazoa
GCTAGCGTAGCTTAATATAAAGCATAACACTGAAGATGTTAAGATGAGCCCTAA