<p>Hi all</p>

<p>I am building a pipeline for taxonomically identifying a blast result at each taxa level, with a bespoke reference dataset. I want it to be easily reproduceable, which it is, other than an ugly step where I have to take a list of taxonIDs and put it into the site:</p>

<p><a href="http://www.ncbi.nlm.nih.gov/Taxonomy/TaxIdentifier/tax_identifier.cgi">http://www.ncbi.nlm.nih.gov/Taxonomy/TaxIdentifier/tax_identifier.cgi</a></p>

<p>retrieve the output and carry on. </p>

<p>Does anyone know if I can get the code that is used? Clearly its calling a cgi from somewhere but I cant find it in the ncbi ftp. If not, is there an equivalent? Technically, I could pull apart the names.dmp and nodes.dmp but there is already a ncbi tool so I'm loathed to do that.</p>

<p>Thanks</p>

<hr/>

<p>N.B.
The function is turning:</p>

<pre><code>515482
515474
</code></pre>

<p>into</p>

<pre><code>515482  |       Nitzschia dubiiformis   |       515482 2857 33852 33851 33850 33849 2836 33634 2759 131567
515474  |       Cocconeis stauroneiformis       |       515474 216715 216714 186023 33850 33849 2836 33634 2759 131567
</code></pre>

<p><strong>EDIT:</strong> For the record, I have ~30,000 taxIDs that I am retrieving and gawbul's script, while elegant, won't complete. Im looking at Frederic's now for its multi-coring ability. </p>

<p>You can use BioPython to access any of NCBI's Entrez databases, specifically the taxonomy database in your case - <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc116">http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc116</a></p>
<p>Or you can do the same sort of thing with BioPerl's Bio::DB::Taxonomy package - <a href="http://doc.bioperl.org/bioperl-live/Bio/DB/Taxonomy.html">http://doc.bioperl.org/bioperl-live/Bio/DB/Taxonomy.html</a> and Bio::Tree::Tree package - <a href="http://doc.bioperl.org/bioperl-live/Bio/Tree/Tree.html">http://doc.bioperl.org/bioperl-live/Bio/Tree/Tree.html</a></p>
<p>Perl example:</p>
<pre><code>use strict;
use warnings;
use Bio::DB::Taxonomy;
use Bio::Tree::Tree;

my @taxonids = ("515482", "515474");
my @lineages = ();

my $db = Bio::DB::Taxonomy-&gt;new(-source =&gt; 'entrez');

foreach my $taxonid (@taxonids) {
    my $taxon = $db-&gt;get_taxon(-taxonid =&gt; $taxonid);
    my $tree = Bio::Tree::Tree-&gt;new(-node =&gt; $taxon);
    my @taxa = $tree-&gt;get_nodes;
    my @tids = ();
    foreach my $t (@taxa) {
        unshift(@tids, $t-&gt;id());
    }
    push(@lineages, $taxonid . "\t|\t" . $taxon-&gt;scientific_name() . "\t|\t" . "@tids");
}

foreach my $lineage (@lineages) {
    print "$lineage\n";
}
</code></pre>
<p>Outputs:</p>
<pre><code>515482  |       Nitzschia dubiiformis   |       515482 2857 33852 33851 33850 33849 2836 33634 2759 131567
515474  |       Cocconeis stauroneiformis       |       515474 216715 216714 186023 33850 33849 2836 33634 2759 131567
</code></pre>
<p>Python example:</p>
<pre><code>#import entrez module
from Bio import Entrez

# set variables
taxids = [515482, 515474]

# set email
Entrez.email = "youremail@gmail.com"

# traverse ids
for taxid in taxids:
    handle = Entrez.efetch(db="taxonomy", id=taxid, mode="text", rettype="xml")
    records = Entrez.read(handle)
    for taxon in records:
        taxid = taxon["TaxId"]
        name = taxon["ScientificName"]
        tids = []
        for t in taxon["LineageEx"]:
            tids.insert(0, t["TaxId"])
        tids.insert(0, taxid)
        print "%s\t|\t%s\t|\t%s" % (taxid, name, " ".join(tids))
</code></pre>
<p>Outputs:</p>
<pre><code>515482  |       Nitzschia dubiiformis   |       515482 2857 33852 33851 33850 33849 2836 33634 2759 131567
515474  |       Cocconeis stauroneiformis       |       515474 216715 216714 186023 33850 33849 2836 33634 2759 131567
</code></pre>
<p>These essentially just call <a href="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi">http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi</a>, which you could call and parse yourself? Perhaps iterate over the ids in the list, call the url for each and output as you need?</p>
<p>Calling the following for example:</p>
<p><a href="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&amp;id=515482&amp;mode=text&amp;report=xml">http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&amp;id=515482&amp;mode=text&amp;report=xml</a></p>
<p>Give us the following XML output:</p>

https://gist.github.com/RamRS/bfa2314828cf918be83f0de36dc13d15

<p>Which you can then parse the lineage tax ids from quite simply. Pierre gives a great example in his post, using an XSLT stylesheet.</p>