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

Fix buildNewick Formula and Update Dendrogram Branch Representation #15

Merged
merged 4 commits into from
Dec 19, 2024

Conversation

kylacochrane
Copy link
Collaborator

@kylacochrane kylacochrane commented Nov 19, 2024

This PR (currently indirectly) addresses ISSUE13.

Changes:

  • Fixed the buildNewick formula to adjust branch lengths (both internal and leaf branches) to represent cophenetic distances rather than patristic distances.
  • Branch lengths now reflect the distance from all samples to their most recent common ancestor.

Benefits:

  • This approach mirrors how BioNumerics currently displays dendrograms.
  • It aligns with how GAS's fcluster generates flat clusters based on user-provided thresholds, ensuring the dendrogram allows users to visually interpret cluster levels by assessing branch heights between clusters.

Key Considerations:
This method deviates from the traditional representation of distances in dendrograms. As noted in ISSUE13, branch lengths are typically represented by patristic distances along a tree typically correspond to the values in the distance matrix (i.e., the sum of branch lengths from Sample A to Sample B should equal the distance provided in the matrix).

To align buildNewick with this traditional approach, the formula can be updated with the following adjustments:

def buildNewick(self,node, newick, parentdist, leaf_names):
        if node.is_leaf():
            return "%s:%f%s" % (leaf_names[node.id], (parentdist - node.dist / 2), newick)
        else:
            if len(newick) > 0:
                newick = f"):{(parentdist - node.dist/2)}{newick}"
            else:
                newick = ");"
            newick = self.buildNewick(node.get_left(), newick, node.dist, leaf_names)
            newick = self.buildNewick(node.get_right(), ",%s" % (newick), node.dist, leaf_names)
            newick = "(%s" % (newick)
            return newick

@sgsutcliffe
Copy link
Contributor

sgsutcliffe commented Dec 19, 2024

Running the changes on the example file we see the change on the left panel compared to the orginal code right panel @kylacochrane you got it!
Screenshot 2024-12-19 143840

Copy link
Member

@apetkau apetkau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much @kylacochrane for all your great work on this. And thanks so much @sgsutcliffe for testing this out on data to confirm it's working 😄

By the way, here is the tree (from Steven's comparison) with distances and internal node heights labeled:

image

This can be compared to distances in this example file to confirm that the cophenetic distances in the tree match distances in the distance matrix (other than differences due to average linkage distances between clusters):

dists A B C D E F G H I J
A 0 1 2 2 5 5 6 6 9 9
B 1 0 3 3 6 6 7 7 9 9
C 2 3 0 0 3 3 6 6 9 9
D 2 3 0 0 3 3 6 6 9 9
E 5 6 3 3 0 0 6 6 9 9
F 5 6 3 3 0 0 6 6 9 9
G 6 7 6 6 6 6 0 0 3 3
H 6 7 6 6 6 6 0 0 3 3
I 9 9 9 9 9 9 3 3 0 1
J 9 9 9 9 9 9 3 3 1 0

@sgsutcliffe sgsutcliffe merged commit a8db44d into dev Dec 19, 2024
2 checks passed
@sgsutcliffe sgsutcliffe deleted the newick_updates branch December 19, 2024 20:38
@sgsutcliffe sgsutcliffe mentioned this pull request Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants