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.
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!
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.
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
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.
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?
There are a lot of posts regarding this issue. I'd suggest you to have a look before asking because I think that this topic has been widely covered:
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-
and the text file-
The expected outcome which i need is-
I need to rename the header name before first underscore, and keep the rest of the header name as such.
Any suggestions?
Use SeqKit:
That works. Thanks!
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-
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
and the text file is:
The expected output is:
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:
If not, you may need help of csvtk which has windows version:
Then you can replace
AFA46815.1withgi|222528058|ref|AFA46815.1|:Hi Shenwei
Can you modify this command for me?
This is my fasta header
My output header should look like
Thanks alot
What is the relationship between current fasta header and expected one?
I want to replace orf05188 with geneA