Skip to content

Commit

Permalink
deploy: a88743b
Browse files Browse the repository at this point in the history
  • Loading branch information
jordibc committed May 31, 2024
1 parent 191431d commit 93515ed
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
35 changes: 17 additions & 18 deletions _modules/ete4/gtdb_taxonomy/gtdbquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre

<span class="k">if</span> <span class="n">dbfile</span> <span class="o">!=</span> <span class="n">DEFAULT_GTDBTAXADB</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dbfile</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;GTDB database not present yet (first time used?)&#39;</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="n">urlbase</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;https://github.com/etetoolkit/ete-data/raw/main&#39;</span>
<span class="s1">&#39;/gtdb_taxonomy/gtdblatest&#39;</span><span class="p">)</span>

<span class="n">update_ete_data</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">DEFAULT_GTDBTAXADUMP</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">urlbase</span><span class="si">}</span><span class="s1">/gtdb_latest_dump.tar.gz&#39;</span><span class="p">)</span>

<span class="n">update_ete_data</span><span class="p">(</span><span class="n">DEFAULT_GTDBTAXADUMP</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="s1">&#39;gtdb_taxonomy/gtdblatest/gtdb_latest_dump.tar.gz&#39;</span><span class="p">)</span>

<span class="bp">self</span><span class="o">.</span><span class="n">update_taxonomy_database</span><span class="p">(</span><span class="n">taxdump_file</span><span class="o">=</span><span class="n">DEFAULT_GTDBTAXADUMP</span><span class="p">)</span>

Expand Down Expand Up @@ -190,7 +189,7 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre

<span class="k">def</span> <span class="nf">_get_id2rank</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">internal_taxids</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Given a list of numeric ids (each one representing a taxa in GTDB), return a dictionary with their corresponding ranks.</span>
<span class="sd"> Examples: </span>
<span class="sd"> Examples:</span>
<span class="sd"> &gt; gtdb.get_rank([2174, 205487, 610])</span>
<span class="sd"> {2174: &#39;family&#39;, 205487: &#39;order&#39;, 610: &#39;phylum&#39;}</span>

Expand All @@ -199,13 +198,13 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="n">ids</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;&quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="n">internal_taxids</span><span class="p">)</span> <span class="o">-</span> <span class="p">{</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">})</span>
<span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&#39;SELECT taxid, rank FROM species WHERE taxid IN (</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="n">ids</span><span class="p">)</span>
<span class="k">return</span> <span class="p">{</span><span class="n">tax</span><span class="p">:</span> <span class="n">spname</span> <span class="k">for</span> <span class="n">tax</span><span class="p">,</span> <span class="n">spname</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()}</span>

<div class="viewcode-block" id="GTDBTaxa.get_rank">
<a class="viewcode-back" href="../../../reference/reference_taxonomy.html#ete4.GTDBTaxa.get_rank">[docs]</a>
<span class="k">def</span> <span class="nf">get_rank</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">taxids</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Give a list of GTDB string taxids, return a dictionary with their corresponding ranks.</span>
<span class="sd"> Examples: </span>
<span class="sd"> </span>
<span class="sd"> Examples:</span>

<span class="sd"> &gt; gtdb.get_rank([&#39;c__Thorarchaeia&#39;, &#39;RS_GCF_001477695.1&#39;])</span>
<span class="sd"> {&#39;c__Thorarchaeia&#39;: &#39;class&#39;, &#39;RS_GCF_001477695.1&#39;: &#39;subspecies&#39;}</span>
<span class="sd"> &quot;&quot;&quot;</span>
Expand All @@ -218,7 +217,7 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&#39;SELECT taxid, rank FROM species WHERE taxid IN (</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="n">ids</span><span class="p">)</span>
<span class="k">for</span> <span class="n">tax</span><span class="p">,</span> <span class="n">rank</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">fetchall</span><span class="p">():</span>
<span class="n">taxid2rank</span><span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_taxid_translator</span><span class="p">([</span><span class="n">tax</span><span class="p">])</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">rank</span>

<span class="k">return</span> <span class="n">taxid2rank</span></div>


Expand Down Expand Up @@ -539,7 +538,7 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre

<div class="viewcode-block" id="GTDBTaxa.annotate_tree">
<a class="viewcode-back" href="../../../reference/reference_taxonomy.html#ete4.GTDBTaxa.annotate_tree">[docs]</a>
<span class="k">def</span> <span class="nf">annotate_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">taxid_attr</span><span class="o">=</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">tax2name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="k">def</span> <span class="nf">annotate_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">taxid_attr</span><span class="o">=</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">tax2name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">tax2track</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tax2rank</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ignore_unclassified</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Annotate a tree containing taxids as leaf names.</span>

Expand Down Expand Up @@ -576,7 +575,7 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="n">tax2name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_taxid_translator</span><span class="p">(</span><span class="n">taxids</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">tax2track</span> <span class="ow">or</span> <span class="n">taxids</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">tax2track</span><span class="o">.</span><span class="n">keys</span><span class="p">()))):</span>
<span class="n">tax2track</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_lineage_translator</span><span class="p">(</span><span class="n">taxids</span><span class="p">)</span>

<span class="n">all_taxid_codes</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">_tax</span> <span class="k">for</span> <span class="n">_lin</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">tax2track</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="k">for</span> <span class="n">_tax</span> <span class="ow">in</span> <span class="n">_lin</span><span class="p">])</span>
<span class="n">extra_tax2name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_taxid_translator</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">all_taxid_codes</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">tax2name</span><span class="o">.</span><span class="n">keys</span><span class="p">())))</span>
<span class="n">tax2name</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_tax2name</span><span class="p">)</span>
Expand Down Expand Up @@ -620,15 +619,15 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="n">rank</span> <span class="o">=</span> <span class="s1">&#39;Unknown&#39;</span><span class="p">,</span>
<span class="n">named_lineage</span> <span class="o">=</span> <span class="p">[])</span>
<span class="k">else</span><span class="p">:</span>

<span class="k">if</span> <span class="n">ignore_unclassified</span><span class="p">:</span>
<span class="n">vectors</span> <span class="o">=</span> <span class="p">[</span><span class="n">lf</span><span class="o">.</span><span class="n">props</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;lineage&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">lf</span> <span class="ow">in</span> <span class="n">n2leaves</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="k">if</span> <span class="n">lf</span><span class="o">.</span><span class="n">props</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;lineage&#39;</span><span class="p">)]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">vectors</span> <span class="o">=</span> <span class="p">[</span><span class="n">lf</span><span class="o">.</span><span class="n">props</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;lineage&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">lf</span> <span class="ow">in</span> <span class="n">n2leaves</span><span class="p">[</span><span class="n">node</span><span class="p">]]</span>
<span class="n">lineage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_common_lineage</span><span class="p">(</span><span class="n">vectors</span><span class="p">)</span>

<span class="n">rank</span> <span class="o">=</span> <span class="n">tax2rank</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">lineage</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="s1">&#39;Unknown&#39;</span><span class="p">)</span>

<span class="k">if</span> <span class="n">lineage</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">rank</span> <span class="o">!=</span> <span class="s1">&#39;subspecies&#39;</span><span class="p">:</span>
<span class="n">ancestor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_taxid_translator</span><span class="p">([</span><span class="n">lineage</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]])[</span><span class="n">lineage</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span>
Expand Down Expand Up @@ -844,12 +843,12 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="n">basepath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">dbfile</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">if</span> <span class="n">basepath</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">basepath</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">basepath</span><span class="p">)</span>
<span class="c1"># if users don&#39;t provie targz_file, update the latest version from ete-data </span>

<span class="c1"># if users don&#39;t provie targz_file, update the latest version from ete-data</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">targz_file</span><span class="p">:</span>
<span class="n">update_local_taxdump</span><span class="p">(</span><span class="n">DEFAULT_GTDBTAXADUMP</span><span class="p">)</span>
<span class="n">targz_file</span> <span class="o">=</span> <span class="n">DEFAULT_GTDBTAXADUMP</span>

<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">targz_file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="n">t</span><span class="p">,</span> <span class="n">synonyms</span> <span class="o">=</span> <span class="n">load_gtdb_tree_from_dump</span><span class="p">(</span><span class="n">tar</span><span class="p">)</span>

Expand All @@ -868,7 +867,7 @@ <h1>Source code for ete4.gtdb_taxonomy.gtdbquery</h1><div class="highlight"><pre
<span class="k">def</span> <span class="nf">update_local_taxdump</span><span class="p">(</span><span class="n">fname</span><span class="o">=</span><span class="n">DEFAULT_GTDBTAXADUMP</span><span class="p">):</span>
<span class="c1"># latest version of gtdb taxonomy dump</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;https://github.com/etetoolkit/ete-data/raw/main/gtdb_taxonomy/gtdblatest/gtdb_latest_dump.tar.gz&quot;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Downloading </span><span class="si">{</span><span class="n">fname</span><span class="si">}</span><span class="s1"> from </span><span class="si">{</span><span class="n">url</span><span class="si">}</span><span class="s1"> ...&#39;</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ <h1>Source code for ete4.smartview.renderer.layouts.ncbi_taxonomy_layouts</h1><d
<span class="c1"># Make sure we have the big file with all the colors.</span>
<span class="n">taxid2color_file</span> <span class="o">=</span> <span class="n">ETE_DATA_HOME</span> <span class="o">+</span> <span class="s1">&#39;/taxid2color.json&#39;</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">taxid2color_file</span><span class="p">):</span>
<span class="n">url</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;https://github.com/etetoolkit/ete-data/raw/main&#39;</span>
<span class="s1">&#39;/layouts/taxid2color.json&#39;</span><span class="p">)</span>
<span class="n">update_ete_data</span><span class="p">(</span><span class="n">taxid2color_file</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>
<span class="n">update_ete_data</span><span class="p">(</span><span class="n">taxid2color_file</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="s1">&#39;layouts/taxid2color.json&#39;</span><span class="p">)</span>

<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">taxid2color_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">handle</span><span class="p">:</span>
<span class="n">_taxid2color</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">handle</span><span class="p">)</span>
Expand Down
Loading

0 comments on commit 93515ed

Please sign in to comment.