Even though Neil's answer put me on the right track and is therefore marked as the correct answer, I would like to show you my modified solution built on Neil's solution. There is in fact one additional step required, and that is to generate an id-to-taxon mapping file.
Note that it is not possible to add the taxon id to the FASTA id-line in the as e.g. gnl|taxon|9606 as described here, because that will yield an error on duplicate ids.
The following code generates a .fa file and a .map file which contains one mapping of sequence id to taxid per line.
The fasta headers look like this:
>ref|NC_003038 taxon=176652, Invertebrate iridescent virus 6, complete genome.
and include the taxon id in the description part.
use strict;
use warnings;
use Bio::SeqIO;
my $file = $ARGV[0];
my $seqio = Bio::SeqIO->new(-file => $file, -format => "genbank");
my $fasta = Bio::SeqIO->new(-file => ">$file.fa", -format => "fasta");
open TAXMAP, ">$file.map" || die "couldn't open mapping file: $!\n";
while(my $seq = $seqio->next_seq) {
my $taxid = "";
for my $feat($seq->get_SeqFeatures) {
if($feat->has_tag("db_xref")) {
for my $id($feat->get_tag_values("db_xref")) {
if($id =~/taxon:(\d+)/) {
$taxid = $1;
}
}
}
}
my $id = "ref|".$seq->id;
my $fa = Bio::Seq->new(-id => $id,
-desc => " taxon=$taxid, ".$seq->description,
-seq => $seq->seq);
$fasta->write_seq($fa);
print TAXMAP "$id $taxid\n" if $taxid;
}
Using the output of this script, the database can be built using the following command:
makeblastdb -in viral.1.genomic.gbff.fa -parse_seqids -dbtype nucl \
-taxid_map viral.1.genomic.gbff.map
Note that according to this question in versions before 2.2.25+ this doesn't work.
The presence of the taxonids in the DB can be checked using the following command:
blastdbcmd -db viral.1.genomic.gbff.fa -entry all -outfmt "%T"
176652
130760
...
I really hope this is of great help for everybody trying something similar.
Just notice that it is mentioned nowhere in the manual, that makeblastdb supports anything else than FASTA.
Isn't it similar (without going through all your text) to this question making a BLAST DB alias based on gi's? http://biostar.stackexchange.com/questions/15047/make-a-custom-blast-library-using-the-output-of-another-blast-result/15050#15050
Not the same question, no - they want the taxon ID in the fasta file before formatting the database.