I have some gene sequencing data like below:
data = [{'sequence': 'gene1__gene2__gene3', 'occurrence': 10},
{'sequence': 'gene2__gene3', 'occurrence': 5},
{'sequence': 'gene2', 'occurrence': 2},
{'sequence': 'gene4', 'occurrence': 4}
]
I want to transform this into following (tree-like)dictionary
data structure, where any sub-path tells me the co-occurrence count of that set of genes:
tree_dict = {
'gene1': {'occurrence': 10, 'self': 0, 'children': {'gene2': {'occurrence': 10, 'self': 0, 'children': {'gene3': {'occurrence': 10, 'self': 10, 'children': {}}}},
'gene3': {'occurrence': 10, 'self': 0, 'children': {'gene2': {'occurrence': 10, 'self': 10, 'children': {}}}},
}
},
'gene2': {'occurrence': 17, 'self': 2, 'children': {'gene1': {'occurrence': 10, 'self': 0, 'children': {'gene3': {'occurrence': 10, 'self': 10, 'children': {}}}},
'gene3': {'occurrence': 15, 'self': 5, 'children': {'gene1': {'occurrence': 10, 'self': 10, 'children': {}}}},
}
},
'gene3': {'occurrence': 15, 'self': 0, 'children': {'gene1': {'occurrence': 10, 'self': 0, 'children': {'gene2': {'occurrence': 10, 'self': 10, 'children': {}}}},
'gene2': {'occurrence': 15, 'self': 5, 'children': {'gene1': {'occurrence': 10, 'self': 10, 'children': {}}}},
}
},
'gene4': {'occurrence': 4, 'self': 4, 'children': {}}
}
In the tree_dict
above:
self
refers to occurrence of just the nodes in the (sub)path. For ex:gene3
never exists all by itself and thus haveself
value of 0; whilegene2
exists all by itself2
times and thus have theself
value of 2.occurrence
refers to occurrence of the nodes in the (sub)path both as substrings and whole.
Code that I tried?
I was trying with failure iterative approaches, when I know that the solution of this have to be a recursive function. Something similar to this question: How to transform a list into a hierarchy dict. But I was not able to make any progress in that direction.