This function is driving me insane!
def CCAD1 (tree)
leaves = []
for otu in tree:
if tree[otu][2]== None and tree[otu][1]== None:
leaves += [otu]
ccad = {}
for leaf in leaves:
otuX = leaf
otu1 = leaf
for leaf2 in leaves:
otuY = leaf2
otu2 = leaf2
while tree[otu1][0] is not None and tree[otu2][0] is not None and tree[otu1][0][0] != tree[otu2][0][0]:
otu1,otu2,tree = tree[otu1][0][0],tree[otu2][0][0],tree
if tree[otu1][0] is not None:
ccad[otuX] = {otuY:tree[otu1][0]}
return ccad
This is the input to the function
{'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}
The output should be in the structure of something like {"A":{"B":("AB",4)}}, in the code above this is the dictionary ‘CCAD’. I’ve literally been trying all night to do it but it’s not working and I have no idea why.
Basically what I’m trying to do is to build a function that will output a dictionary of dictionaries where for each distinct pair of elements in the list leaves it will calculate an ancestor (I got some great help calculating that previously here) along with the distance, which in that link there would be a matter of keeping a running total each time the function iterates.
It’s outputting a dictionary of dictionaries like I need, but it’s not doing it for every pair, only certain ones. The ‘tree’ data structure is in that link too if you need to see it.
Any help is appreciated, I’m getting pretty desperate at this stage :/
Okay, I think you want to calculate the distances between every leaf node. So I’m having a go at solving your problem, not answering your question.
Your commonAncestor algorithm is flawed as it assumes that the leaf nodes are all at the same depth. They are not.
The first solution that springs to mind is to determine all of the leaf nodes and calculate the path to the root node for each of them. Determine the closest common ancestor by comparing both paths in reverse.
The output from this is a dictionary of pairs of nodes and the number of hops between them.
This prints out for clarity: