moshi4 / pyCirclize

Circular visualization in Python (Circos Plot, Chord Diagram, Radar Chart)
https://moshi4.github.io/pyCirclize/
MIT License
758 stars 47 forks source link

Is there any way to integrate between heatmap data and tree leaves? #50

Closed Zymeth0211 closed 9 months ago

Zymeth0211 commented 9 months ago

Dear Moshi04.

Thank you for your perfect system to create a beautiful combination of tree and heatmap.

I have a question about data integration between heatmap data and tree leaves. How do you integrate between them?

from pycirclize import Circos
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#Import the tree
tree_file = "\\tree_test\\abs alignment_tree.nwk"

# Sample DataFrame
df= pd.read_csv("\\tree_test\\abs_abundance.csv", index_col= 0)
print(df)

#make a tree with pycirclize
circos, tv = Circos.initialize

_from_tree(
    tree_file,
    start=5,
    end=355,
    leaf_label_size=8,
    align_leaf_label=True,
    ladderize=True,
    r_lim=(0, 60),
    leaf_label_rmargin=10,
    ignore_branch_length=True,
    )

#making heatmap
sector = circos.sectors[0]
track1 = sector.add_track((60, 65))
track1.heatmap(df["4R"], cmap="Reds", show_value=False)
track1.text("4R",adjust_rotation=False,color="black", r=20)

track2 = sector.add_track((65, 70))
track2.heatmap(df["3R"], cmap="Reds", show_value=False)
circos.colorbar(bounds=(1.0, 0.6, 0.02, 0.3),cmap="Reds", )
fig = circos.plotfig(), 

output 6d3fc1f1-21ff-49c5-af81-de31babf63ea

Here is zip containing nwk and abundance data for heat map.

I apologize to ask you basic things. tree_test.zip

Thank you Best regards.

moshi4 commented 9 months ago

I am not sure what "integrate" means. Could you please clarify what you mean?

Zymeth0211 commented 9 months ago

Dear Moshi4

Thank you for your rapid response.

In my case, the tree data and heat map data frame are not correctly synchronized. For example, Cl_1995 has to be lighter in terms of color due to its small value. Is there any way to fix by synchronizing between newick and matrix data?

moshi4 commented 9 months ago

I am not going to explain the code in detail, so please try to understand the content yourself.

Code Example

from pycirclize import Circos
import pandas as pd

# Initialize circos tree
tree_file = "./tree_test/abs alignment_tree.nwk"
circos, tv = Circos.initialize_from_tree(
    tree_file,
    start=5,
    end=355,
    leaf_label_size=10,
    align_leaf_label=True,
    ladderize=True,
    r_lim=(0, 60),
    leaf_label_rmargin=12,
    ignore_branch_length=True,
)

# Read CSV & sort by order of leaf labels
df = pd.read_csv("./tree_test/abs_abundance.csv", index_col=0)
sorted_df = df.loc[tv.leaf_labels]
print(sorted_df)

# Set colorbar
vmin, vmax = 0, sorted_df.to_numpy().max()
circos.colorbar(bounds=(1.0, 0.35, 0.02, 0.3), cmap="Reds", vmin=vmin, vmax=vmax)

# 4R heatmap
sector = circos.sectors[0]
track1 = sector.add_track((60, 65))
track1.heatmap(sorted_df["4R"].to_numpy(), cmap="Reds", vmin=vmin, vmax=vmax, show_value=True)
circos.text("4R", r=track1.r_center)

# 3R heatmap
track2 = sector.add_track((65, 70))
track2.heatmap(sorted_df["3R"].to_numpy(), cmap="Reds", vmin=vmin, vmax=vmax, show_value=True)
circos.text("3R", r=track2.r_center)

circos.savefig("example.png")

example.png example