Question: Renaming Entries In A Fasta File
gravatar for thiago84naka
6.3 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 • 28k views
ADD COMMENTlink modified 2.7 years ago by noirot.celine50 • written 6.3 years ago by thiago84naka80

Thank you very much for all the answers!!!

ADD REPLYlink written 6.3 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 6.3 years ago by Obi Griffith17k
gravatar for Pierre Lindenbaum
6.3 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum116k wrote:
 awk '/^>/{print ">" ++i; next}{print}' < file.fasta
ADD COMMENTlink written 6.3 years ago by Pierre Lindenbaum116k

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 4.7 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 4.4 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 2.5 years 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 2.4 years ago • written 2.4 years ago by cdiaza0

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

ADD REPLYlink written 2.2 years ago by Picasa390

@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 2.1 years ago • written 2.1 years 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 16 months ago by Mitra0

Hello Pierre, when I use awk '/^>/{print ">" ++i; next}{print}' < file.fasta, the changes are made but not saved. I want to distinguish between two numbered contig.fasta files (each fasta is numbered 'contig 00001', 'contig00002' etc, I want to name the 1st contig.fasta 'shorter_contig 00001, 'shorter_contig 00002' and the 2nd.contig.fasta 'longer_contig 00001, 'longer_contig 00002' ) is there a way make the header modifications permanent? Thanks

ADD REPLYlink modified 5 months ago • written 5 months ago by Audrey0

the changes are made but not saved.

ADD REPLYlink written 5 months ago by Pierre Lindenbaum116k
gravatar for Istvan Albert
6.3 years ago by
Istvan Albert ♦♦ 78k
University Park, USA
Istvan Albert ♦♦ 78k 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 6.3 years ago by Istvan Albert ♦♦ 78k
gravatar for David Langenberger
6.3 years ago by
David Langenberger8.4k wrote:

Try this:

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

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

ADD REPLYlink modified 6.3 years ago • written 6.3 years ago by Matt Shirley8.7k
gravatar for noirot.celine
2.7 years 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 2.7 years ago by noirot.celine50

Great. Thank you for this creative usage of sed.

ADD REPLYlink written 2.6 years ago by SomeoneElse0

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

ADD REPLYlink written 2.2 years 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 23 months ago • written 23 months ago by mirza80

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

ADD REPLYlink written 15 months ago by Wenhu_Cao50

Great! :D It worked perfectly well. Thanks

ADD REPLYlink written 11 months ago by marianocollantes0
gravatar for AGS
6.3 years ago by
Brooklyn, ny
AGS230 wrote:

I'd use faSimplify

ADD COMMENTlink written 6.3 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: 1804 users visited in the last hour