Phylogenetic Tree
In [1]:
Copied!
# %pip install pycirclize
# %pip install pycirclize
1. Simple Tree¶
User can plot the phylogenetic tree with Circos.initialize_from_tree()
or track.tree()
methods.
Both methods return a TreeViz instance, which can be manipulated to set phylogenetic tree
annotations such as marker and highlight.
1-1. Default Style¶
In [2]:
Copied!
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
tree_file = load_example_tree_file("alphabet.nwk")
circos, tv = Circos.initialize_from_tree(tree_file)
fig = circos.plotfig()
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
tree_file = load_example_tree_file("alphabet.nwk")
circos, tv = Circos.initialize_from_tree(tree_file)
fig = circos.plotfig()
1-2. Change Style 1¶
In [3]:
Copied!
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
tree_file = load_example_tree_file("alphabet.nwk")
circos, tv = Circos.initialize_from_tree(
tree_file,
start=20, # Default: 0
end=340, # Default: 360
r_lim=(10, 100), # Default: (50, 100)
line_kws=dict(color="red", lw=2), # Default: {}, Change color & linewidth
align_line_kws=dict(ls="dashdot", lw=1), # Default: {}, Change linestyle & linewidth
)
fig = circos.plotfig()
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
tree_file = load_example_tree_file("alphabet.nwk")
circos, tv = Circos.initialize_from_tree(
tree_file,
start=20, # Default: 0
end=340, # Default: 360
r_lim=(10, 100), # Default: (50, 100)
line_kws=dict(color="red", lw=2), # Default: {}, Change color & linewidth
align_line_kws=dict(ls="dashdot", lw=1), # Default: {}, Change linestyle & linewidth
)
fig = circos.plotfig()
1-3. Change Style 2¶
In [4]:
Copied!
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
import matplotlib.pyplot as plt
# Create 2x2 polar subplots
fig = plt.figure(figsize=(16, 16))
fig.subplots_adjust(wspace=0.05, hspace=0.05)
ax_list = fig.subplots(2, 2, subplot_kw=dict(polar=True)).flatten()
# Define 4 types kwargs for `Circos.initialize_from_tree()` method
kwargs_list = [
dict(outer=True, align_leaf_label=True, ignore_branch_length=False),
dict(outer=True, align_leaf_label=False, ignore_branch_length=False),
dict(outer=False, align_leaf_label=True, ignore_branch_length=False),
dict(outer=True, align_leaf_label=True, ignore_branch_length=True),
]
# Plot trees with different kwargs
tree_file = load_example_tree_file("alphabet.nwk")
for ax, kwargs in zip(ax_list, kwargs_list):
circos, tv = Circos.initialize_from_tree(tree_file, r_lim=(60, 100), **kwargs)
kwargs_text = "\n".join([f"{k}: {v}" for k, v in kwargs.items()])
circos.text(kwargs_text, size=14)
circos.plotfig(ax=ax)
from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
import matplotlib.pyplot as plt
# Create 2x2 polar subplots
fig = plt.figure(figsize=(16, 16))
fig.subplots_adjust(wspace=0.05, hspace=0.05)
ax_list = fig.subplots(2, 2, subplot_kw=dict(polar=True)).flatten()
# Define 4 types kwargs for `Circos.initialize_from_tree()` method
kwargs_list = [
dict(outer=True, align_leaf_label=True, ignore_branch_length=False),
dict(outer=True, align_leaf_label=False, ignore_branch_length=False),
dict(outer=False, align_leaf_label=True, ignore_branch_length=False),
dict(outer=True, align_leaf_label=True, ignore_branch_length=True),
]
# Plot trees with different kwargs
tree_file = load_example_tree_file("alphabet.nwk")
for ax, kwargs in zip(ax_list, kwargs_list):
circos, tv = Circos.initialize_from_tree(tree_file, r_lim=(60, 100), **kwargs)
kwargs_text = "\n".join([f"{k}: {v}" for k, v in kwargs.items()])
circos.text(kwargs_text, size=14)
circos.plotfig(ax=ax)