Giter Club home page Giter Club logo

Comments (5)

moshi4 avatar moshi4 commented on May 21, 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

from pycirclize.

dongzhang0725 avatar dongzhang0725 commented on May 21, 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

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.

from pycirclize.

moshi4 avatar moshi4 commented on May 21, 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.

from pycirclize.

dongzhang0725 avatar dongzhang0725 commented on May 21, 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.

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

from pycirclize.

moshi4 avatar moshi4 commented on May 21, 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.

from pycirclize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.