Question: Change header of a Fasta file according to the file name
0
gravatar for akhilvbioinfo
3.1 years ago by
akhilvbioinfo140
India, chennai
akhilvbioinfo140 wrote:

Hai

I have a Fasta file like GCA_001609185.1_ASM160918v1_genomic.fsa and i want to change header of this fasta file like this >GCA_001609185.1_ASM160918v1_genomic
I am looking for solutions

Thank you

awk fasta • 3.8k views
ADD COMMENTlink modified 3.1 years ago by Pierre Lindenbaum122k • written 3.1 years ago by akhilvbioinfo140
2

There is only one record in the file?

ADD REPLYlink written 3.1 years ago by WouterDeCoster40k
2

This is kind of off topic, so I won't be overly surprised if someone closes it. Having said that, note that awk has a FILENAME variable that you could use for this purpose.

ADD REPLYlink written 3.1 years ago by Devon Ryan91k
8
gravatar for Pierre Lindenbaum
3.1 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum122k wrote:
 awk '/^>/ {gsub(/.fa(sta)?$/,"",FILENAME);printf(">%s\n",FILENAME);next;} {print}' input.fa
ADD COMMENTlink modified 3.1 years ago • written 3.1 years ago by Pierre Lindenbaum122k

Is there a way to do this for multiple (i.e. several hundred) files at once and output as individual files (with the original names) to a subdirectory? Thanks!

ADD REPLYlink modified 2.4 years ago • written 2.4 years ago by theclubstyle0

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

ADD REPLYlink written 2.4 years ago by Pierre Lindenbaum122k

Dear Pierre Lindenbaum. I'm a molecular biologist and I have the same question as theclubstyle "

Is there a way to do this for multiple (i.e. several hundred) files at once and output as individual files (with the original names) to a subdirectory?"

I have read the information on the page shared by you, but I am not able to solve the problem with my limited knowledge in bioinformatics.

I'd appreciate your help.

ADD REPLYlink written 8 months ago by Gonzalo0
1

Assuming all your fasta files are in the current directory and you want to write to outdir

Using a bash loop

for FILE in *.fa;
do
 awk '/^>/ {gsub(/.fa(sta)?$/,"",FILENAME);printf(">%s\n",FILENAME);next;} {print}' $FILE > outdir/changed_${FILE}
done

Using GNU parallel

ls *.fa | parallel 'awk '/^>/ {gsub(/.fa(sta)?$/,"",FILENAME);printf(">%s\n",FILENAME);next;} {print}' {} > outdir/changed_{}'
ADD REPLYlink modified 8 months ago • written 8 months ago by WouterDeCoster40k

It works! :) . Deeply grateful for your help,

Regards

ADD REPLYlink written 8 months ago by Gonzalo0
Please log in to add an answer.

Help
Access

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