This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Renaming fasta headers according to a matching name list

Hi all,

Can anyone advise me on how to rename the header in my fasta file? Say I have a seq.fa file with transcript sequences:

>TR1|c0_g1_i1
GTCGAGCATGGTCTTGGTCATCTTCCTTTCAAAGAA
>TR6|c0_g1_i1
GTGGAATATCGCCAGTGACCATCACTGATTAACCTG

I also have a file with contigs matching the transcripts - names.txt:

TR1|c0_g1_i1    scaf0432344_50037.734_wgs
TR6|c0_g1_i1    scaf0159424_10142.072_wgs

How to I add contig names to the fasta file headers so that the "scaf0..." identifier comes before the "TR..."?

Desired output:

>scaf0432344_50037.734_wgs|TR1|c0_g1_i1
GTCGAGCATGGTCTTGGTCATCTTCCTTTCAAAGAA
>scaf0159424_10142.072_wgs|TR6|c0_g1_i1
GTGGAATATCGCCAGTGACCATCACTGATTAACCTG

Cheers!

text-processing command-line sequence

Did you try anything? If yes post what you've tried which is not working?

No, sorry, I have no clue how to go about it.

Your fasta headers look like Trinity output. Did you run Trinity on linux? Or were you given the fasta?

I have looked for a couple of hours now but didn't manage to find anything matching exactly what I need to do. I'm not trained so things that are not exactly what I need, unfortunately, were not a help to me. Since I don't need to replace the header but rather modify it, this particular post wasn't helpful to me.

I am trying to use above mentioned perl script and modified it and trying to get a desirable output, but I am unable to get it. My input fasta file looks like-

>V091201769_1 H648IQM04I6TDD orig_bc=AGCACTGTAG
GACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAGAGGAGAAGGAAGCTTGCCT
>V091209099_2 H648IQM04JTWVX orig_bc=ACGAGTGCGT
GATAAACGCTGGCGGCGCACATAAGACATGCAAGTCGAACGAACTTAACCATTAGTTTAC
>V091201769_3 H648IQM04IRZLN orig_bc=AGCACTGTAG
GATGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAAGCACTTTATTTGATTTCCTTCGGGACT

and the text file-

V091200036 TR01555
V091205377 TR01556
V091201769 TR01557
V091209099 TR01558

The expected outcome which i need is-

>TR01557_1 H648IQM04I6TDD orig_bc=AGCACTGTAG
GACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAGAGGAGAAGGAAGCTTGCCT
>TR01558_2 H648IQM04JTWVX orig_bc=ACGAGTGCGT
GATAAACGCTGGCGGCGCACATAAGACATGCAAGTCGAACGAACTTAACCATTAGTTTAC
>TR01557_3 H648IQM04IRZLN orig_bc=AGCACTGTAG
GATGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAAGCACTTTATTTGATTTCCTTCGGGACT

I need to rename the header name before first underscore, and keep the rest of the header name as such.

Any suggestions?

Use SeqKit:

$ seqkit replace -p "^(.+?)_(\d+)" -r '{kv}_$2' -k names.txt seq.fa

seqkit is a life saver

CAN YOU PLZ EXPLAIN THIS COMMAND, giving explanation with parts of it's functions so that i can use it according to my task , bcz i need to just add unique set of strings that i created right after the accession number with an #. for better understanding i eed my files like this-

V091201769_1#TR01555 H648IQM04I6TDD orig_bc=AGCACTGTAG GACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAGAGGAGAAGGAAGCTTGCCT V091209099_2#TR01556 H648IQM04JTWVX orig_bc=ACGAGTGCGT GATAAACGCTGGCGGCGCACATAAGACATGCAAGTCGAACGAACTTAACCATTAGTTTAC V091201769_3#TR01557 H648IQM04IRZLN orig_bc=AGCACTGTAG GATGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAAGCACTTTATTTGATTTCCTTCGGGACT

Dear Shenwei, As I'm a biologist who is totally new to any programming task would you please tell me what would be the Seqkit's command if I want to do the following.

My input fasta is like

>AFA46815.1 chromosomal replication initiator protein DnaA [Acetobacterium woodii DSM 1030]
MDTSFKKIWDAALEIIKPDISPTSFNTWFLKIKPINYVNNTYYFLSENEFEKGILESRHIPLITNALAEVTGKTGQVKIV
LKEEDAMIAQNADSPNVSITNVEEKISNYQNNSMNPKYTFDSFVIGENNRFAHAACVAVSEAPSERYNPLFIYGGVGLGK
THLMQAIGHYILSYAPHKKVVYVSCEKFTNDFIDAIQNKTNISFRNKYRSADILLIDDIQFLAKKEGTQEEFFHTFNTLH
QENKQIVISSDRPPKEIPTLEERLRSRFESGLITDIYAPNFETRIAIIRKKAESFSDEIPNEVLSFIADSIHSNIRELEG
ALTTVFAYSKLHNAPINLDSAKNALKDIFRKKDDIVITSEYIKEVTAKYFNITVEDMNSKKRTRSISLPRQVAMYITREI
TDLSLPKIGDEFGGRDHSTVIHACQKITEEMTINTDFKNLILRIEREINGK
>AFA46816.1 DNA polymerase III subunit beta [Acetobacterium woodii DSM 1030]
MKFNCSKESLMAALTIAQKAVSSKTTLTILEGILFSAADNKLLLRSTDLEIGVEVTIPAEVEKEGEIVLTASIIGELIRK
MSGSDIFFESNENHQIKIECLLSNFTLKGLSSEEFPAFPEIIDDHIFSIDAGVLKELIKGTLFSVATNENIPVLTGVKIE
LHADDINLIALDGYRLALKSGKIKNNLTEDLSVIIPSKSLSEINKVLSNYSGDVTVKFSKNQIFFEMDNTQFTSRLLEGD
FINYKQIIPVEKATQVKINRRLLLESSERAALLAREGKNNLIKMDFNQDQLILTSNADIGDVFEVIPIENSGESLKIAFN
SKFLIEALRVIEGDELMIDMTTSVGPGVLLPIEGNNFIYLILPVRIAEEN
>AFA46817.1 RNA-binding protein containing S4 domain [Acetobacterium woodii DSM 1030]
MQIIEINTEFIKIDQLLKYAGIVGNGSDVKFMILDGLIRVNGELCTQRNKKIRDGDLVEIEDYEPLQVKGITTTCLSKR

and the text file is:

222528058
222528059
222528060

The expected output is:

>gi|222528058|ref|AFA46815.1| chromosomal replication initiator protein DnaA [Acetobacterium woodii DSM 1030]
MDTSFKKIWDAALEIIKPDISPTSFNTWFLKIKPINYVNNTYYFLSENEFEKGILESRHIPLITNALAEVTGKTGQVKIV
LKEEDAMIAQNADSPNVSITNVEEKISNYQNNSMNPKYTFDSFVIGENNRFAHAACVAVSEAPSERYNPLFIYGGVGLGK
THLMQAIGHYILSYAPHKKVVYVSCEKFTNDFIDAIQNKTNISFRNKYRSADILLIDDIQFLAKKEGTQEEFFHTFNTLH
QENKQIVISSDRPPKEIPTLEERLRSRFESGLITDIYAPNFETRIAIIRKKAESFSDEIPNEVLSFIADSIHSNIRELEG
ALTTVFAYSKLHNAPINLDSAKNALKDIFRKKDDIVITSEYIKEVTAKYFNITVEDMNSKKRTRSISLPRQVAMYITREI
TDLSLPKIGDEFGGRDHSTVIHACQKITEEMTINTDFKNLILRIEREINGK
>gi|222528059|ref|AFA46816.1| chromosomal replication initiator protein DnaA [Acetobacterium woodii DSM 1030]
MKFNCSKESLMAALTIAQKAVSSKTTLTILEGILFSAADNKLLLRSTDLEIGVEVTIPAEVEKEGEIVLTASIIGELIRK
MSGSDIFFESNENHQIKIECLLSNFTLKGLSSEEFPAFPEIIDDHIFSIDAGVLKELIKGTLFSVATNENIPVLTGVKIE
LHADDINLIALDGYRLALKSGKIKNNLTEDLSVIIPSKSLSEINKVLSNYSGDVTVKFSKNQIFFEMDNTQFTSRLLEGD
FINYKQIIPVEKATQVKINRRLLLESSERAALLAREGKNNLIKMDFNQDQLILTSNADIGDVFEVIPIENSGESLKIAFN
SKFLIEALRVIEGDELMIDMTTSVGPGVLLPIEGNNFIYLILPVRIAEEN
>gi|222528060|ref|AFA46817.1| chromosomal replication initiator protein DnaA [Acetobacterium woodii DSM 1030]
MQIIEINTEFIKIDQLLKYAGIVGNGSDVKFMILDGLIRVNGELCTQRNKKIRDGDLVEIEDYEPLQVKGITTTCLSKR

I will greatly appreciate if you could help

Firstly preparing the mapping file of accession and GI:

If you have Unix/Linux, it's simple:

paste <(seqkit seq -n -i seqs.fasta) gi.txt
AFA46815.1      222528058
AFA46816.1      222528059
AFA46817.1      222528060

If not, you may need help of csvtk which has windows version:

seqkit seq -n -i seqs.fasta | csvtk sample -H -t -p 1 -n > n-acc.tsv

csvtk sample -H -t -p 1 -n gi.txt > n-gi.tsv

csvtk join -H -t n-acc.tsv n-gi.tsv | csvtk -H -t cut -f 2,3 > acc2gi.tsv

Then you can replace AFA46815.1 with gi|222528058|ref|AFA46815.1|:

seqkit replace -k acc2gi.tsv -p "^(.+?) (.+)$" -r "gi|{kv}|ref|\$1| \$2" seqs.fasta > seqs.renamed.fasta

seqkit seq -n seqs.renamed.fasta 
gi|222528058|ref|AFA46815.1| chromosomal replication initiator protein DnaA [Acetobacterium woodii DSM 1030]
gi|222528059|ref|AFA46816.1| DNA polymerase III subunit beta [Acetobacterium woodii DSM 1030]
gi|222528060|ref|AFA46817.1| RNA-binding protein containing S4 domain [Acetobacterium woodii DSM 1030]

Hi Shenwei

seqkit replace -k acc2gi.tsv -p "^(.+?) (.+)$" -r "gi|{kv}|ref|\$1| \$2" seqs.fasta > seqs.renamed.fasta

Can you modify this command for me?

This is my fasta header

>orf05188   draft|P. aeruginosa 908|Pseudomonas|orf05188  Scaffold_1  5479915 5478419  len=1497

My output header should look like

>geneA

Thanks alot

What is the relationship between current fasta header and expected one?

I want to replace orf05188 with geneA

3 answers

Using the solution proposed in replace fasta headers with another name in a text file by @Kenosis just with a little modification, you'll get the desired output:

  use strict;
  use warnings;

    my @arr;

    while (<>) {
        chomp;
        my @a = split /\t/;
        push @arr, $a[1] . "|" . $a[0] if length;
        last if eof; }

    while (<>) {
        print /^>/ ? ">" . shift(@arr) . "\n" : $_; }

Call the script in the following way: perl script.pl textFile fastaFile [>outFile]

I'd strongly suggest you to try to reach the solution yourself, otherwise you'll never learn. And it is recommended to show up your unsuccessful attempts to get the solution, otherwise people can think that you are asking just to avoid the effort of doing it yourself. Even if your attempts are not the best ones (all of us have been through that), I'd suggest you to explain to the community what you've tried.

Thank you so much. Unfortunately learning in my own time isn't an option since there's pressure for me to produce results asap. Which really isn't helping anyone in the long run, but I don't have a say in it. But I get your point and next time will explain that I'm not just trying to bum off someone.

I am trying to use above mentioned perl script and modified it and trying to get a desirable output, but I am unable to get it. My input fasta file looks like-

V091201769_1 H648IQM04I6TDD orig_bc=AGCACTGTAG GACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAGAGGAGAAGGAAGCTTGCCT V091209099_2 H648IQM04JTWVX orig_bc=ACGAGTGCGT GATAAACGCTGGCGGCGCACATAAGACATGCAAGTCGAACGAACTTAACCATTAGTTTAC V091201769_3 H648IQM04IRZLN orig_bc=AGCACTGTAG GATGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAAGCACTTTATTTGATTTCCTTCGGGACT

and the text file-

V091200036 TR01555 V091205377 TR01556 V091201769 TR01557 V091209099 TR01558

The expected outcome which i need is-

TR01557_1 H648IQM04I6TDD orig_bc=AGCACTGTAG GACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAGAGGAGAAGGAAGCTTGCCT TR01558_2 H648IQM04JTWVX orig_bc=ACGAGTGCGT GATAAACGCTGGCGGCGCACATAAGACATGCAAGTCGAACGAACTTAACCATTAGTTTAC TR01557_3 H648IQM04IRZLN orig_bc=AGCACTGTAG GATGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGAAGCACTTTATTTGATTTCCTTCGGGACT

I need to rename the header name before first underscore, and keep the rest of the header name as such.

Any suggestions?

This is another solution that was proposed to me, if anyone finds it helpful:

awk 'FNR==NR{
  a[">"$1]=$2;next
}
$1 in a{
  sub(/>/,">"a[$1]"|",$1)
}1' names.txt seq.fa

Good one-liner. Thanks

SeqKit supports this case now by subcommand replace, usage, download .

seqs:

$ cat seq.fa 
>TR1|c0_g1_i1
GTCGAGCATGGTCTTGGTCATCTTCCTTTCAAAGAA
>TR6|c0_g1_i1
GTGGAATATCGCCAGTGACCATCACTGATTAACCTG

names.txt should be tab-delimited:

$ cat names.txt 
TR1|c0_g1_i1    scaf0432344_50037.734_wgs
TR6|c0_g1_i1    scaf0159424_10142.072_wgs

replace

$ seqkit replace -p "(.+)" -r '{kv}|$1' -k names.txt seq.fa 
[INFO] read key-value file: names.txt
[INFO] 2 pairs of key-value loaded
>scaf0432344_50037.734_wgs|TR1|c0_g1_i1
GTCGAGCATGGTCTTGGTCATCTTCCTTTCAAAGAA
>scaf0159424_10142.072_wgs|TR6|c0_g1_i1
GTGGAATATCGCCAGTGACCATCACTGATTAACCTG

Log in to answer this question.