Question: Renaming Entries In A Fasta File
gravatar for thiago84naka
5.5 years ago by
thiago84naka80 wrote:


I have never made a script in my life.

The ploblem is how to change the fasta names like this input file:


in this output file (change original names to numbers in ascending order, starting with 1):


I'm so grateful for helping. Regards, Naka

fasta • 23k views
ADD COMMENTlink modified 23 months ago by noirot.celine50 • written 5.5 years ago by thiago84naka80

Thank you very much for all the answers!!!

ADD REPLYlink written 5.5 years ago by thiago84naka80

Welcome to biostar. Its great that you received so many good answers to your question. Next time make your thank you as a comment to the existing answers or as a comment or edit to your original question, and not as a separate answer.

ADD REPLYlink written 5.5 years ago by Obi Griffith16k
gravatar for Pierre Lindenbaum
5.5 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum105k wrote:
 awk '/^>/{print ">" ++i; next}{print}' < file.fasta
ADD COMMENTlink written 5.5 years ago by Pierre Lindenbaum105k

Sir can we modify above awk syntax in this way

instead of printing like




it prints like 




for that purpose , where and how do i put the text "chromosome"

please help me out

ADD REPLYlink written 3.8 years ago by Raghav100

@Raghav: If you wanted to add chromosome in the header with the counter, simply add it in the ">" portion of the one-liner.

 awk '/^>/{print ">chromosome" ++i; next}{print}' < file.fasta
ADD REPLYlink written 3.6 years ago by

How can we add "chr" just after >? I don't want to change anything else. For example:

2L I want it to become chr2L

ADD REPLYlink written 20 months ago by saswati.s20100

Hello there, (I already solved this)

I am trying to understand your script line to modified. So, is the script saying?: For every line ('/) where you find a > (^>/) print the > and then add (+) a counter (+), then next print what follows.

In my case the names are like:

M02137:143:000000000-APU54:1:1101:21985:13014 1:N:0:10

M02137:143:000000000-APU54:1:1112:18691:9995 1:N:0:10

etc. I want to leave only what is different.

awk '/^>/{print ">" remove "M02137:143:000000000-APU54:1:"; next}{print}' < file.fasta

And can I do this in ssh? ( I don't think I have awk installed)

Many thanks in advance for your time,

Caro PS: I am new to HTS/NGS and don't know much about programming

ADD REPLYlink modified 19 months ago • written 19 months ago by cdiaza0

This doesn't work if the read spans in multiple lines ?

ADD REPLYlink written 16 months ago by Picasa340

@Pierre Lindenbaum Hi, 1. how can I modify this command to add genus_species name after > in every entry and yet keep most of the information in the the header ie. my entries are like this

lcl|HF546977.1_cds_CCO27433.1_1 [gene=cox1] [protein=cytochrome c oxidase subunit 1] [protein_id=CCO27433.1] [location.......]

and want to have the entries name like this

genus_species HF546977.1_cds_CCO27433.1_1 [gene=cox1] [protein=cytochrome c oxidase subunit 1]

By using

awk '/^>/{print ">genus_species gene." ++i; next}{print}' < file.fa

I got,

genus_species gene.1 and so on

  1. and how can I add output file in the command line

Having the genus_species name in the beginning is requires as I'll be comparing different species and also, I don't want to loose the ids and protein names for ease of downstream analysis.

ADD REPLYlink modified 15 months ago • written 15 months ago by mirza80

Hello Pierre, Thank you for your useful code. May I please ask how can I modify the code to keep everything else in the sequence and just to add the sample name in front and that too for the batch of files.

e.g. my file looks like :

>M03691:51:000000000-BD94Y:1:1101:14841:1381 1:N:0:1



Now I want to add Sample name after > and keep everything else as it it.

This process I want to do for a batch of files. Any help will be really great. Thanks, Mitra

ADD REPLYlink written 6 months ago by Mitra0
gravatar for Istvan Albert
5.5 years ago by
Istvan Albert ♦♦ 75k
University Park, USA
Istvan Albert ♦♦ 75k wrote:

The Fastx Renamer tool can do this as well:

$ more test.fa 
>GS6SIDE04J1T1R xy=4004_1485
>GS6SIDE04J0352 xy=3996_712
>GS6SIDE04JM7EM xy=3837_2988

$ ~/Downloads/bin/fastx_renamer -i test.fa -n COUNT 
ADD COMMENTlink written 5.5 years ago by Istvan Albert ♦♦ 75k
gravatar for David Langenberger
5.5 years ago by
David Langenberger8.1k wrote:

Try this:

cat youFile.fa | perl -ane 'if(/\>/){$a++;print ">$a\n"}else{print;}' > youFile_new.fa
ADD COMMENTlink written 5.5 years ago by David Langenberger8.1k

Instead of useless cat, try: perl -ane 'if(/\>/){$a++;print ">$a\n"}else{print;}' youFile.fa > youFile_new.fa

ADD REPLYlink modified 5.5 years ago • written 5.5 years ago by Matt Shirley8.2k
gravatar for noirot.celine
23 months ago by
noirot.celine50 wrote:

Here is a generic way to convert ncbi headers to simple header

>gi|1002620271|ref|NC_029525.1| Coturnix japonica isolate 7356 chromosome 10, Coturnix japonica 2.0, whole genome shotgun sequence



By sed :

sed 's/^[^ ]*[|]\([^|]*\)[|] .*$/>\1/' Coturnix_japonica.fasta > Coturnix_japonica_rename.fasta
ADD COMMENTlink written 23 months ago by noirot.celine50

Great. Thank you for this creative usage of sed.

ADD REPLYlink written 21 months ago by SomeoneElse0

Thank you so much! Just what I was looking for. It worked a treat!

ADD REPLYlink written 17 months ago by kwathen-dunn10

@noirot.celine Hi, I have a similar problem and your above command didn't work for me (I am really new to linux environment). I have different fasta files. Some of my fasta headers are like this (augustus output file)

g1134t1 geneg1134 I want to keep the header and just add the species_genus name after >

or better like this

Species_genus gene1134

Similarly, for file with headers like this,

AG1IA_00006 contig1:1338:4722:+ [translate_table: standard]

I want to keep >AG1IA_00006 and since the ids in files are also not in continuation, so simply renaming in series won't help.

p.s. my OS= Ubuntu16.04

ADD REPLYlink modified 13 months ago • written 13 months ago by mirza80

Oh, god... Thank Handbook for linking me here to see the magic!!!

ADD REPLYlink written 5 months ago by mckf1111120

Great! :D It worked perfectly well. Thanks

ADD REPLYlink written 7 weeks ago by marianocollantes0
gravatar for AGS
5.5 years ago by
Brooklyn, ny
AGS230 wrote:

I'd use faSimplify

ADD COMMENTlink written 5.5 years ago by AGS230
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 868 users visited in the last hour