ete icon indicating copy to clipboard operation
ete copied to clipboard

Is there an example how use ete with scipy.cluster.hierarchical?

Open sorenwacker opened this issue 2 years ago • 2 comments

Is there an example how use ete with scipy.cluster.hierarchical? I would like to plot a dendrogram, but I am not sure how to convert the scipy output into ete format.


def plot_dendrogram(
    df,
    labels=None,
    orientation="left",
    metric=None,
    color_threshold=0,
    above_threshold_color="k",
    **kwargs,
):

    Z = hierarchy.linkage(df, metric=metric)
    T = hierarchy.to_tree(Z)
    data = hierarchy.dendrogram(
        Z,
        labels=labels,
        orientation=orientation,
        color_threshold=color_threshold,
        above_threshold_color=above_threshold_color,
    )
    ndx = data["leaves"]
    if orientation in ["left", "right"]:
        pl.xticks([])
    if orientation in ["top", "bottom"]:
        pl.xticks([])
    pl.gca().set(frame_on=False)

    return Z, T

sorenwacker avatar Dec 04 '21 01:12 sorenwacker

@sorenwacker could you convert to newick and then convert to an ete3 object that way ete3.ClusterNode(newick=newick_str)?

Here's a bit of code I have used before:

linkage_matrix = scipy.cluster.hierarchy.linkage(...)
# labels = names of indices in linkage_matrix as a list (e.g., ['label1', 'label2', ...] )

skb_tree = skbio.tree.TreeNode.from_linkage_matrix(linkage_matrix, id_list=labels)
skb_newick = str(skb_tree).strip().replace("'", "")
ete_tree = ete3.ClusterNode(newick=skb_newick)

Here I'm using scikit-bio to convert the linkage matrix into a scikit-bio tree which lets me convert to Newick representation but maybe there's a better way of doing this.

apetkau avatar Jan 14 '22 21:01 apetkau

Thanks. Such a converter would be a great add on. #featurerequest

sorenwacker avatar Feb 01 '22 02:02 sorenwacker