PyiTOL API Usage Guide¶
中文 | English
PyiTOL wraps iTOL's three batch API endpoints, supporting automatic retry, error translation, and multi-format export.
API Endpoints¶
| Endpoint | Function | PyiTOL Command |
|---|---|---|
https://itol.embl.de/batch_uploader.cgi |
Upload tree files + templates | pyitol task upload |
https://itol.embl.de/batch_downloader.cgi |
Export rendered images | pyitol task export |
https://itol.embl.de/batch_delete.cgi |
Delete uploaded trees | pyitol task delete |
API Key Configuration¶
Three methods are supported (in descending order of priority):
Method 1: Command-line parameter¶
Method 2: Environment variable¶
Method 3: Key file¶
Create a .itolapi.key file in the project directory and write the plain-text API Key into it:
Or specify a custom path:
Security note: PyiTOL checks the key file permissions and issues a warning if group or other have any read, write, or execute permission. It is recommended to use
chmod 600 .itolapi.key.
task Command Overview¶
| Command | Description |
|---|---|
pyitol task upload |
Upload trees and templates to iTOL |
pyitol task export |
Export rendered images from iTOL |
pyitol task upload-and-export |
Upload and export (in one step) |
pyitol task delete |
Delete uploaded trees |
pyitol task status |
Query the status of a tree |
pyitol task run |
Run a configuration file (single config or batch list) |
Upload Workflow¶
Basic Upload¶
Multi-template Upload¶
pyitol task upload \
--tree tree.nwk \
--config colors.txt \
--config bars.txt \
--config heatmap.txt \
--dataset-name MyTree
PyiTOL automatically packages multiple template files into a zip for upload.
Auto-export After Upload¶
pyitol task upload-and-export \
--tree tree.nwk \
--config colors.txt \
--output result.svg --format svg --wait 60
--wait is a fixed delay (in seconds, default 60) applied before exporting: once the upload finishes the command sleeps for that many seconds and then issues a single export. It is not a deadline, so the elapse of the wait does not make the command fail.
Export Control¶
Supported Formats¶
PyiTOL supports 8 export formats:
| Format | Description | Extra Parameters |
|---|---|---|
svg |
Vector graphic (recommended) | |
png |
Bitmap image | dpi |
pdf |
Document | dpi |
tiff |
TIFF image | dpi |
eps |
Encapsulated PostScript | dpi |
newick |
Newick tree format | |
nexus |
Nexus tree format | |
phyloxml |
PhyloXML format |
How you pass the format and dimension parameters depends on the command:
pyitol task exporthas no standalone--format/--dpioptions, so they must be passed via--parameter key=value; onlypyitol task upload-and-exportprovides the--format(svg/png/pdf),--dpi(PNG/PDF only),--widthand--heightoptions directly.
Export Dimensions¶
pyitol task export \
--upload-id TREE_ID \
--download ./output \
--parameter format=png \
--parameter dpi=300 \
--parameter width=2400 \
--parameter height=1600
task export only accepts the --upload-id/-u, --download/-d, --api-key, --api-key-file, --parameters-file, --parameter, --wait and --retry options, so the export format (format=, default pdf) and the dimensions (dpi=, width=, height=) must all be passed through --parameter. Use pyitol task upload-and-export if you want to pass --format, --dpi, --width and --height directly.
Delete Operation¶
# Delete a single tree
pyitol task delete --upload-id TREE_ID
# Batch delete
pyitol task delete --upload-id ID1 --upload-id ID2
Status Query¶
task statustakes the uploaded tree name via--tree-name/-n(not--upload-id); it probes the rendering status internally.
Batch Operations¶
Batch Upload/Export (Configuration File)¶
pyitol task run supports executing batch tasks via YAML/JSON configuration files. The configuration file is in list format, with each item specifying the operation to perform via the command field.
Supported command values: upload, export, extract, monophyly, convert-binary, convert-connect, template.
Create batch.yaml:
- command: upload
tree: tree1.nwk
configs: [colors1.txt, bars1.txt]
api_key: YOUR_KEY
name: Tree1
- command: upload
tree: tree2.nwk
configs: [colors2.txt]
api_key: YOUR_KEY
name: Tree2
- command: export
upload_id: TREE_ID_1
download: ./output
parameters:
format: svg
dpi: 300
Execute:
# Batch execute a list
pyitol task run --config-list batch.yaml
# Single config execution
pyitol task run --config single_workflow.yaml
Single configuration file example single_workflow.yaml:
Error Handling¶
PyiTOL has a built-in iTOL API error translation table that converts the English error messages returned by iTOL into user-friendly suggestions. These suggestions are always printed in Chinese at runtime — they are hardcoded in src/pyitol/utils/reporter.py and there is no locale switch — so the English below is an abridged reference translation of that output.
| iTOL Error Keyword | Meaning of the suggestion PyiTOL prints (abridged) |
|---|---|
invalid api key |
Invalid API key: check the --api-key parameter or the key stored in .itolapi.key |
file too large |
File too large: iTOL limits the total upload size to 2 MB |
tree format |
Tree file format error: make sure the file is valid Newick or Nexus |
rate limit |
Too many requests: retry later |
rendering |
Rendering failed: the iTOL server timed out while rendering |
For the complete error code table, see Error Codes Reference or src/pyitol/utils/reporter.py.
Network Retry¶
The API client has a built-in exponential backoff retry strategy:
- Automatically retries HTTP 429, 500, 502, 503, 504
- Default retry count: 3
- Adjustable via the --retry parameter
Python API Reference¶
Beyond the command line, every PyiTOL capability is callable directly from Python. The most commonly used public classes and functions are listed below by module, each with a minimal runnable example. Signatures follow the source; use help(function) at runtime for full parameters.
pyitol.api.client.ITOLAPIClient — iTOL Batch API Client¶
Wraps upload, export, delete, and status queries, with built-in exponential-backoff retry and error translation.
Constructor: ITOLAPIClient(api_key=None, api_key_file='.itolapi.key', max_retries=3, backoff_factor=1.0, timeout=120.0, strict_permissions=False). The API key can also be supplied via the ITOL_API_KEY environment variable.
Key methods:
| Method | Description |
|---|---|
upload(tree_file, template_files, force=False, datasets_visible=None, project_name=None, tree_description=None) |
Package a zip and upload; returns the tree ID (string) |
export(tree_ids, fmt='pdf', output_dir=None, datasets_visible=None, dpi=None, width=None, height=None, extra_params=None) |
Export; returns {tree_id: local_path} |
delete(tree_id) / batch_delete(tree_ids) |
Delete a single / batch of trees |
delete_project(project_name) / batch_delete_projects(project_names) |
Delete projects |
upload_and_export(tree_file, template_files, fmt='pdf', output_dir='.', wait_time=120, poll_interval=10, ...) |
Upload, sleep once for the fixed min(poll_interval, wait_time) seconds, then call export exactly once; iTOL offers no readiness endpoint, so there is no polling. Returns the local path |
query_status(tree_name, timeout=10) |
Indirectly probe render status; returns a dict |
close() |
Close the session (also usable as a context manager) |
from pyitol.api.client import ITOLAPIClient
# Option 1: explicit management
client = ITOLAPIClient(api_key_file='.itolapi.key')
tree_id = client.upload(
"tree.nwk",
["colors.txt", "bars.txt"],
project_name="MyTree",
tree_description="Example tree",
)
print("Uploaded:", tree_id)
paths = client.export([tree_id], fmt="png", output_dir="./out", dpi=300)
print("Exported:", paths)
client.delete(tree_id)
client.close()
# Option 2: context manager (recommended)
with ITOLAPIClient(api_key="YOUR_API_KEY") as client:
out = client.upload_and_export(
tree_file="tree.nwk",
template_files=["colors.txt"],
fmt="svg",
output_dir="./output",
wait_time=120,
)
print("Upload and export:", out)
pyitol.templates.generator.TemplateGenerator — Template Generation Engine¶
Programmatically assemble multiple dataset schemas and write iTOL v7 template files.
Key methods:
| Method | Description |
|---|---|
add_schema(schema) |
Add a prebuilt TemplateSchema |
add_schema_from_data(type_name, label, data, parameters=None, separator='TAB', color='#ff0000') |
Quickly add a dataset from a list of row dicts |
set_style(params) / set_name(name) |
Set tree style params / template display name |
write(output_path, mode='write' \| 'append') |
Write a single template file; returns Path |
write_multiple(output_dir, prefix='') |
Write each schema as a separate file; returns list[Path] |
from pyitol.templates.generator import TemplateGenerator
gen = TemplateGenerator()
gen.set_name("MyTemplate")
gen.add_schema_from_data(
type_name="color_strip",
label="Phylum",
data=[
{"id": "TaxonA", "label": "Proteobacteria"},
{"id": "TaxonB", "label": "Firmicutes"},
],
separator="TAB",
)
gen.add_schema_from_data(
type_name="simple_bar",
label="Abundance",
data=[
{"id": "TaxonA", "value": "12.3"},
{"id": "TaxonB", "value": "8.1"},
],
)
path = gen.write("my_template.txt") # merged template
paths = gen.write_multiple("./templates", prefix="ds_") # separate files
type_namevalues: seepyitol.templates.presets.list_presets()(it returns 30 preset names, e.g.color_strip,simple_bar,heatmap,tree_colors; note this is a different list from the 33 template types supported by the unified entrypyitol template create).
pyitol.templates.learner — Learning from Existing Templates¶
| Function | Description |
|---|---|
learn_template(template_path) |
Parse an iTOL template file; returns a dict with template_name, datasets, etc. |
train_theme(template_dir, min_freq=0.5) |
Aggregate all *.txt templates in a directory; produce recommended default params per dataset type |
from pyitol.templates.learner import learn_template, train_theme
info = learn_template("colors.txt")
print(info["template_name"], info["datasets"])
theme = train_theme("./templates_dir", min_freq=0.5)
print(theme)
Equivalent CLI: pyitol learn template --template colors.txt --output learned.json and pyitol learn train-theme --dir ./templates_dir --output theme.json.
pyitol.templates.presets — Palette & Template Presets¶
| Function | Description |
|---|---|
list_presets() |
List all 30 dataset template preset names |
get_preset(name) |
Get a template schema class instance by name |
get_palette(preset_name, palette_name='primary', n=None) |
Get a color list (preset_name is nature/cell/colorblind) |
list_all_palettes() |
List all available palette names grouped by preset |
from pyitol.templates.presets import get_palette, list_all_palettes, list_presets
print(list_presets()) # 30 preset names
print(list_all_palettes()) # palette names grouped by nature/cell/colorblind
colors = get_palette("nature") # default primary
colors = get_palette("nature", "vibrant") # specific sub-palette
Equivalent CLI: pyitol learn palette --preset-name nature.
pyitol.core.taxonomy — Taxonomy Analysis¶
| Function | Description |
|---|---|
get_ranks(taxonomy_path, id_column='id') |
Return the list of taxonomic ranks |
extract_taxonomy(taxonomy_path, tree_path, rank, id_column='id', multi_tree='error') |
Extract taxonomy at a rank; returns list[dict] |
extract_taxonomy_with_stats(...) |
Extract with member counts, LCA stats, etc. |
extract_taxonomy_from_tip_names(tree_path, output_path=None, name_format='auto', delimiter='_', taxonomy_levels=None, delimiter_mode='segment') |
Infer a taxonomy table from tip names |
from pyitol.core.taxonomy import get_ranks, extract_taxonomy
ranks = get_ranks("taxonomy.csv")
print(ranks) # ['Domain', 'Phylum', 'Class', ...]
rows = extract_taxonomy("taxonomy.csv", "tree.nwk", rank="Phylum")
for r in rows:
print(r["id"], r["name"])
pyitol.core.monophyly — Monophyly Checking¶
| Function | Description |
|---|---|
check_monophyly(taxonomy_path, tree_path, rank, id_column='id', polytomy_mode='strict') |
Check monophyly of all groups at a rank; returns list[dict] |
check_monophyly_with_taxa_list(taxonomy_path, tree_path, rank=None, taxa_list=None, taxa_file=None, id_column='id', polytomy_mode='strict') |
Check a given taxon list (supports LUCA/LACA/LBCA/ROOT) |
from pyitol.core.monophyly import check_monophyly_with_taxa_list
results = check_monophyly_with_taxa_list(
taxonomy_path="taxonomy.csv",
tree_path="tree.nwk",
rank="Phylum",
taxa_list=["Proteobacteria", "Firmicutes"],
)
for r in results:
print(r["group"], r["status"]) # monophyletic / paraphyletic / polyphyletic / incomplete_sampling / data_insufficient / unknown
pyitol.core.parser — Tree Parsing¶
| Function | Description |
|---|---|
detect_tree_schema(tree_path) |
Return 'newick' or 'nexus' |
get_single_tree(tree_path, ...) |
Read and return a single tree (dendropy Tree) |
format_support_values(...) |
Format support values (IQ-TREE / RAxML / ALRT, etc.) |
from pyitol.core.parser import detect_tree_schema
schema = detect_tree_schema("tree.nwk")
print(schema) # 'newick'
pyitol.utils.data_converters — Data Format Conversion¶
| Function | Description |
|---|---|
wide_to_long(input_file, output_file, id_column='id', value_name='value', var_name='variable') |
Wide to long |
long_to_wide(input_file, output_file, id_column='id', variable_column='variable', value_column='value') |
Long to wide |
df_tree(df_or_path, columns=None, main='col', order='none', metric='euclidean', linkage_method='average') |
Build a Newick tree from a dataframe |
convert_to_binary_matrix(...) / category_to_connect(...) |
Numeric ranges to a binary matrix (-1/0/1) / same-category nodes to connection pairs |
from pyitol.utils.data_converters import wide_to_long, long_to_wide, df_tree
wide_to_long("wide.csv", "long.csv", id_column="id")
long_to_wide("long.csv", "wide.csv", id_column="id")
tree_path = df_tree("data.csv", columns=["A", "B", "C"], main="col", order="hclust")
pyitol.utils.io — File Search¶
| Function | Description |
|---|---|
search_tree_file(directory, recursive=True, extensions=None) |
Recursively search tree files; returns list[Path] |
find_first_tree_file(directory, recursive=True, extensions=None) |
Return the first tree file path or None |
from pyitol.utils.io import search_tree_file, find_first_tree_file
files = search_tree_file("./data", recursive=True)
first = find_first_tree_file("./data")
print(files, first)
pyitol.utils.tree_info — Tree Information¶
| Function | Description |
|---|---|
get_tree_info(tree_path) |
Return a summary dict (tip count, internal node count, total nodes, whether branch lengths are present, max depth, sample tip names); it only reports the presence of branch lengths, a tree with branch lengths is not required |
get_tip_order(tree_path) |
Return the ordered list of tip names |
extract_tree_info(...) |
Low-level extraction |
from pyitol.utils.tree_info import get_tree_info
info = get_tree_info("tree.nwk")
print(info) # {'tip_count': ..., 'internal_node_count': ..., 'total_nodes': ..., 'has_branch_lengths': ..., 'max_depth': ..., 'sample_tips': ...}
pyitol.utils.color_tools — Color Utilities¶
| Function | Description |
|---|---|
assign_colors_to_categories(categories, palette=None) |
Assign colors to a category list; returns {category: color} |
generate_gradient_colors(start, end, n) |
Generate n gradient colors |
hex_to_rgb / rgb_to_hex / darken_color / lighten_color / validate_color |
Color conversion and adjustment |