Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to plot shapes on tracks? #48

Closed
dongzhang0725 opened this issue Jan 7, 2024 · 5 comments
Closed

Is there a way to plot shapes on tracks? #48

dongzhang0725 opened this issue Jan 7, 2024 · 5 comments
Labels
question Further information is requested

Comments

@dongzhang0725
Copy link

dongzhang0725 commented Jan 7, 2024

Can we plot shapes of matplotlib (https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers) on a track of the tree figure? If so, can anyone give me an example? Thank you!
For example:
image

@moshi4
Copy link
Owner

moshi4 commented Jan 8, 2024

Hi @dongzhang0725,

I think track.scatter() method can do roughly the same thing.

Code Example

from pycirclize import Circos
from pycirclize.utils import load_example_tree_file, ColorCycler
import random

tree_file = load_example_tree_file("large_example.nwk")
circos, tv = Circos.initialize_from_tree(
    tree_file,
    r_lim=(40, 90),
    leaf_label_size=5,
    leaf_label_rmargin=10,
    label_formatter=lambda t: t.replace("_", " "),
)

# Add track for marker plot
tree_sector = circos.sectors[0]
track = tree_sector.add_track((90, 100))

# Plot 3 layer markers for leaf labels
for leaf_label in tv.leaf_labels:
    x = tv.name2xr[leaf_label][0]
    track.scatter(x=[x], y=[0.2], vmin=0, vmax=1, color="salmon")
    track.scatter(x=[x], y=[0.5], vmin=0, vmax=1, color="skyblue", marker="s")
    if random.random() > 0.5:
        track.scatter(x=[x], y=[0.8], vmin=0, vmax=1, color=ColorCycler(), marker="*", s=25)

circos.savefig("example.png", dpi=300)

example.png

example

@dongzhang0725
Copy link
Author

Hi @dongzhang0725,

I think track.scatter() method can do roughly the same thing.

Code Example

from pycirclize import Circos
from pycirclize.utils import load_example_tree_file, ColorCycler
import random

tree_file = load_example_tree_file("large_example.nwk")
circos, tv = Circos.initialize_from_tree(
    tree_file,
    r_lim=(40, 90),
    leaf_label_size=5,
    leaf_label_rmargin=10,
    label_formatter=lambda t: t.replace("_", " "),
)

# Add track for marker plot
tree_sector = circos.sectors[0]
track = tree_sector.add_track((90, 100))

# Plot 3 layer markers for leaf labels
for leaf_label in tv.leaf_labels:
    x = tv.name2xr[leaf_label][0]
    track.scatter(x=[x], y=[0.2], vmin=0, vmax=1, color="salmon")
    track.scatter(x=[x], y=[0.5], vmin=0, vmax=1, color="skyblue", marker="s")
    if random.random() > 0.5:
        track.scatter(x=[x], y=[0.8], vmin=0, vmax=1, color=ColorCycler(), marker="*", s=25)

circos.savefig("example.png", dpi=300)

example.png

example

Dear @moshi4 ,

Thank you for your prompt response; it works like a charm. Pycirclize proves to be very powerful and convenient. I am planning to utilize it for tree annotations. I have an additional question regarding the display of support values on the nodes, as this is crucial for phylogenetic tree visualization, and I did not find relevant information on the homepage. Specifically, I am interested in displaying support value text on the branches of MCA, similar to this example:
image

On another note, is there a way to convert the circular tree to a rectangular mode? If so, I believe it could be a promising tool to replace iTOL, an online tree annotation tool that requires a subscription.

@moshi4
Copy link
Owner

moshi4 commented Jan 8, 2024

I have an additional question regarding the display of support values on the nodes, as this is crucial for phylogenetic tree visualization, and I did not find relevant information on the homepage. Specifically, I am interested in displaying support value text on the branches of MCA

Example Code

Plot bootstrap values on each internal node in tree.

from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
import random

tree_file = load_example_tree_file("large_example.nwk")
circos, tv = Circos.initialize_from_tree(
    tree_file,
    r_lim=(20, 100),
    leaf_label_size=5,
    label_formatter=lambda t: t.replace("_", " "),
    line_kws=dict(color="grey"),
)

# Set tentative bootstrap value to internal nodes of tree
# 'large_example.nwk' has no bootstrap value
for innode in tv.tree.get_nonterminals():
    innode.confidence = random.randint(90, 100)

# Plot bootstrap value on each internal node in tree
sector = circos.sectors[0]
for innode in tv.tree.get_nonterminals():
    x, r =tv.name2xr[innode.name]
    if innode.confidence is not None:
        bootstrap_value = str(innode.confidence)
        sector.text(bootstrap_value, x, r + 0.5, size=5, color="black", orientation="vertical")

circos.savefig("example.png", dpi=300)

example.png

example

It may be good idea to add a method to the TreeViz class to more easily perform bootstrap value plots. I will consider this in the next release.

On another note, is there a way to convert the circular tree to a rectangular mode?

pyCirclize does not provide a way to convert a circular tree to a rectangular tree. Since pyCirclize is a "Circular visualization in Python" library, it is not intended to implement tree plotting in rectangular mode.
If you want to plot trees in rectangular mode as well as pyCirclize, you may be interested in looking at the phyTreeViz library.

@dongzhang0725
Copy link
Author

I have an additional question regarding the display of support values on the nodes, as this is crucial for phylogenetic tree visualization, and I did not find relevant information on the homepage. Specifically, I am interested in displaying support value text on the branches of MCA

Example Code

Plot bootstrap values on each internal node in tree.

from pycirclize import Circos
from pycirclize.utils import load_example_tree_file
import random

tree_file = load_example_tree_file("large_example.nwk")
circos, tv = Circos.initialize_from_tree(
    tree_file,
    r_lim=(20, 100),
    leaf_label_size=5,
    label_formatter=lambda t: t.replace("_", " "),
    line_kws=dict(color="grey"),
)

# Set tentative bootstrap value to internal nodes of tree
# 'large_example.nwk' has no bootstrap value
for innode in tv.tree.get_nonterminals():
    innode.confidence = random.randint(90, 100)

# Plot bootstrap value on each internal node in tree
sector = circos.sectors[0]
for innode in tv.tree.get_nonterminals():
    x, r =tv.name2xr[innode.name]
    if innode.confidence is not None:
        bootstrap_value = str(innode.confidence)
        sector.text(bootstrap_value, x, r + 0.5, size=5, color="black", orientation="vertical")

circos.savefig("example.png", dpi=300)

example.png

example

It may be good idea to add a method to the TreeViz class to more easily perform bootstrap value plots. I will consider this in the next release.

On another note, is there a way to convert the circular tree to a rectangular mode?

pyCirclize does not provide a way to convert a circular tree to a rectangular tree. Since pyCirclize is a "Circular visualization in Python" library, it is not intended to implement tree plotting in rectangular mode. If you want to plot trees in rectangular mode as well as pyCirclize, you may be interested in looking at the phyTreeViz library.

Dear @moshi4 ,

Thank you for providing the code; it has been very helpful. I am also experimenting with integrating a text() function into the TreeViz class, which is derived from the marker() function, and I have achieved some initial success with it.

def text(
          self,
          query: str | list[str] | tuple[str],
          text,
          *,
          size: int = 6,
          **kwargs,
  ) -> None:
      target_node_name = self.search_target_node_name(query)

      # Set markers (x, r) coordinates (include descendent nodes)
      x: list[float] = []
      r: list[float] = []
      rmin, rmax = self.track.r_plot_lim
      clade: Clade = next(self.tree.find_clades(target_node_name))
      descendent_nodes: list[Clade] = list(clade.find_clades())
      for descendent_node in descendent_nodes:
          node_x, node_r = self.name2xr[str(descendent_node.name)]
          if descendent_node.is_terminal() and self._align_leaf_label:
              node_r = rmax if self._outer else rmin
          x.append(node_x)
          r.append(node_r)

      # If `descendent=False`, remove descendent nodes (x, r) coordinate
      # if not descendent:
      x, r = x[0], r[0]

      self.track.text(text, x, r, **kwargs)

If you are planning to add a method to draw text to the TreeViz class, I have an alternative suggestion concerning the text's position (which can also be applied to markers). Specifically, consider finding a way to obtain the position (x and r) of the center of the branch of MCA. This would allow the text to be positioned at the center of the internal branches. Following this, we may also need some adjustments to shift the x position and avoid overlaps between text and branches.

For example:
image

If you want to plot trees in rectangular mode as well as pyCirclize, you may be interested in looking at the [phyTreeViz](https://github.com/moshi4/phyTreeViz) library.
It's great to learn that you've developed a separate package for handling rectangular trees. I will explore the source code to attempt combining various plots (barplot, pieplot, scatter, etc.) with it.

Thank you for your helps again,

Dong

@moshi4
Copy link
Owner

moshi4 commented Jan 18, 2024

I close this issue because the issue of the title has been resolved.

Other feature requests raised in the discussion will be considered separately.

@moshi4 moshi4 closed this as completed Jan 18, 2024
@moshi4 moshi4 mentioned this issue Feb 18, 2024
@moshi4 moshi4 added the question Further information is requested label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants