Question: Renaming Entries In A Fasta File
gravatar for thiago84naka
7.0 years ago by
thiago84naka100 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 • 33k views
ADD COMMENTlink modified 3.4 years ago by noirot.celine50 • written 7.0 years ago by thiago84naka100
gravatar for Pierre Lindenbaum
7.0 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum122k wrote:
 awk '/^>/{print ">" ++i; next}{print}' < file.fasta
ADD COMMENTlink written 7.0 years ago by Pierre Lindenbaum122k

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 5.3 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 5.1 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 3.2 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 3.1 years ago • written 3.1 years ago by cdiaza0

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

ADD REPLYlink written 2.9 years ago by Picasa470

@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.7 years ago • written 2.7 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 2.0 years 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 13 months ago • written 13 months ago by Audrey0

the changes are made but not saved.

ADD REPLYlink written 13 months ago by Pierre Lindenbaum122k

Hello, i want to change the fasta name of this input file :

M04631:312:000000000-C6V6K:1:2107:11495:1734 1:N:0:ACTGAGCG+TTATGCGA

M04631:312:000000000-C6V6K:1:2107:13059:1785 1:N:0:ACTGAGCG+TTATGCGA

In this fasta name:






what script do I have to use??

ADD REPLYlink modified 7 months ago • written 7 months ago by kari_vo30

With respect, if you are already stuck at this most simple task, better spend some quality time on Unix and NGS basics before diving into any analysis. In the end, you as the analyst have to stand up for your analysis.

ADD REPLYlink written 7 months ago by ATpoint22k

A similar question+answer was posted above. What are you missing ?

ADD REPLYlink written 7 months ago by Pierre Lindenbaum122k

my question was resolved, thank you. I needed it in this manner because i have other script that only works with these fasta names . thanks.

ADD REPLYlink modified 7 months ago • written 7 months ago by kari_vo30
gravatar for Istvan Albert
7.0 years ago by
Istvan Albert ♦♦ 81k
University Park, USA
Istvan Albert ♦♦ 81k 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 7.0 years ago by Istvan Albert ♦♦ 81k
gravatar for David Langenberger
7.0 years ago by
David Langenberger9.0k wrote:

Try this:

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

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

ADD REPLYlink modified 7.0 years ago • written 7.0 years ago by Matt Shirley9.1k
gravatar for noirot.celine
3.4 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 3.4 years ago by noirot.celine50

@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 2.6 years ago • written 2.6 years ago by mirza80
gravatar for AGS
7.0 years ago by
Brooklyn, ny
AGS230 wrote:

I'd use faSimplify

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