This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Alternative problem with editing fasta file headers - keeping Taxon name in brackets

Hi

I have the following headers for my fasta files downloaded from IMG/JGI

2648318750 Ga0098755_14192 DNA gyrase subunit B [Microbacterium sp. GCS4 : Ga0098755_14]

I would like this:

Microbacterium sp. GCS4 : Ga0098755_14

The strings/characters are all different for each header. I found this to try:

sed 's/.[([^]])].*/\1/g'

Its works, but I need to keep the '>' at the start to obviously denote each sequence in the fasta file. Is there some parenthesis I can add to keep this character alongside my current command?

Cheers in advance!

sequencing blast sequence fasta

Yes, you may use another pair of parenthesis to catch the '>' at the beginning of the line. On a Debian system I also have to use option '-r' to allow referencing of subpatterns.

echo  '>2648318750 Ga0098755_14192 DNA gyrase subunit B [Microbacterium sp. GCS4 : Ga0098755_14]'  \
| sed -r 's/^(>)[^]]*\[([^]]*)\].*/\1\2/g'

1 answer

Using awk ?

 awk  '/^>/ {i=index($0,"[");j=index($0,"]");print ">" substr($0,i+1,(j-i)-1); next;} {print;}' in.fa > out.fa

Sorry, that just ended up with a blank file? The file contains 20, 000 sequences, not just one if that means something else needs to be added

there is something wrong in the way you run my awk script:

$ echo -e '>2648318750 Ga0098755_14192 DNA gyrase subunit B [Microbacterium sp. GCS4 : Ga0098755_14]\nATACGACGATCGT\n>2648318750 Ga0098755_14192 DNA gyrase subunit B [Microbacterium sp. GCS4 : Ga0098755_14]\nATACGACGATCGT' | awk '/^>/ {i=index($0,"[");j=index($0,"]");print ">" substr($0,i+1,(j-i)-1); next;} {print;}'

>Microbacterium sp. GCS4 : Ga0098755_14
ATACGACGATCGT
>Microbacterium sp. GCS4 : Ga0098755_14
ATACGACGATCGT

Log in to answer this question.