-
Notifications
You must be signed in to change notification settings - Fork 3
/
compartmentTree.py
55 lines (51 loc) · 2.07 KB
/
compartmentTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import treelib3
def getCompartmentHierarchy(compartmentList):
'''
constructs a tree structure containing the
compartment hierarchy excluding 2D compartments
@param:compartmentList
'''
def removeMembranes(tree,nodeID):
node = tree.get_node(nodeID)
if node.data == 2:
parent = tree.get_node(nodeID).bpointer
if parent == None:
#accounting for the case where the topmost node is a membrane
newRoot = tree.get_node(nodeID).fpointer[0]
tree.root = newRoot
node = tree.get_node(newRoot)
else:
tree.link_past_node(nodeID)
nodeID = parent
node = tree.get_node(nodeID)
for element in node.fpointer:
removeMembranes(tree,element)
from copy import deepcopy
tree = treelib3.Tree()
c2 = deepcopy(compartmentList)
while len(c2) > 0:
removeList = []
for element in c2:
if c2[element][2] in ['', None]:
try:
tree.create_node(element,element,data=c2[element][0])
except treelib3.tree.MultipleRootError:
#there's more than one top level element
tree2 = treelib3.Tree()
tree2.create_node('dummyRoot','dummyRoot',data=3)
tree2.paste('dummyRoot',tree)
tree2.create_node(element,element,parent='dummyRoot',data=c2[element][0])
tree = tree2
removeList.append(element)
elif tree.contains(c2[element][2]):
tree.create_node(element,element,parent=c2[element][2],data=c2[element][0])
removeList.append(element)
for element in removeList:
c2.pop(element)
removeMembranes(tree,tree.root)
return tree
def getOutsideInsideCompartment(compartmentList,compartment):
outside = compartmentList[compartment][2]
for comp in compartmentList:
if compartmentList[comp][2] == compartment:
return outside, comp