This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extracting accession number from header using sed

Hello! I'm trying to figure out how to extract the accession numbers from the headers. (about 120 headers) I have to use sed and can't seem to figure it out. Here is a sample of what my file looks like:

>Ref.49_cpx.GM.03.N26677.HQ385479 

ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

I need the part after the last period in the header. So the "HQ385479" part. Thanks in advance for the help!

sed accession number

I need the part after the last period in the header.

Do you want to keep the rest of the alignments intact? I assume so but please clarify.

Edit: Looks like you want to keep just the accessions based on a response below.

You could do (if all accession lines start with Ref) grep "^>Ref" input.txt | sed 's/^.*\.//g' > accession

Ah yes completely forgot I could use grep first. Thanks.

Not necessary to use grep. input (copy/pasted the first sequence and changed the id at the end, as second sequence):

>Ref.49_cpx.GM.03.N26677.HQ385479 
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC
>Ref.49_cpx.GM.03.N26677.HQ385478
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

output:

$ sed -e  '/>/!d; s/.*\.//g' test.fa 
HQ385479 
HQ385478

2 answers

Since the OP has specifically requested sed

$  sed -i 's/^.*\.//g' input.txt

Gives:

HQ385479

ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

Thanks! Is there a way within the sed command to remove all the nucleotide sequences as well so I'm just left with all the accession numbers? This is my first time doing any bioinformatics and I am still learning the whole programming/coding side of it all.

You can use this:

awk -F. 'NF>1{print $NF}' input.txt > output.txt

For future reference: hightlight the text you want to format as code and then click on the "101" button in the edit window to apply the formatting.

Is your file a fasta formatted file? (Header lines beginning with >)? Or is it exactly as you posted above?

It begins with > Didn't copy in correctly

I would just chain it to grep personally, but now the solution is getting a bit less elegant.

cat input.txt | grep ">" | sed 's/^.*\.//g'

no need to grep. code can be: $ sed -e '/>/ s/.*\./>/' input.fa

Before:

$ cat test1.fa 
>Ref.49_cpx.GM.03.N26677.HQ385479 
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC
>Ref.49_cpx.GM.03.N26677.HQ385478
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

after:

$ sed -e '/>/ s/.*\./>/' test1.fa 
>HQ385479 
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC
>HQ385478
ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

This doesn't work. It still returns the sequence and the OP doesn't want that, which is why I used grep. It probably can be accomplished with sed only, but this is the simplest solution that occurred to me.

very minor change to code. Please add > as replacement. So that sequence is still in fasta format.

$ sed -e 's/^.*\./>/g' test1.fa 
>HQ385479 

ATGAGAGTGATGGAGACATGGATG-------------ATTTGCAAAATTG
G------TGG---------------------------AGAGGGGGTCTC

The OP said he doesn't want the sequence, just the accession itself (I assuming they're making a list for a table or similar), so there's no need to sub in the ">".

okay. didn't read OP in full :)

 awk -F. 'NF>1{print $NF}' input.txt

Log in to answer this question.