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 any way to integrate between heatmap data and tree leaves? #50

Closed
Zymeth0211 opened this issue Jan 17, 2024 · 3 comments
Closed
Labels
question Further information is requested

Comments

@Zymeth0211
Copy link

Zymeth0211 commented Jan 17, 2024

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
Copy link
Owner

moshi4 commented Jan 17, 2024

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

@Zymeth0211
Copy link
Author

Zymeth0211 commented Jan 17, 2024

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
Copy link
Owner

moshi4 commented Jan 18, 2024

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

@moshi4 moshi4 closed this as completed Jan 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