-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.py
executable file
·161 lines (150 loc) · 5.31 KB
/
cell.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
''' Authors : Huy Nguyen, Nhi
Program : Cell Tracking
class of cell
Start : 08/04/2018
End : /2018
'''
# a class of cell, each cell object will be defined with a name, od, and volume
# and a dictionary that stores info so we can dump it later into json file to
# keep update and write.
# The way we add link to children is a bit unconventional, since we want to calculate
# the volume of the children, the getVolumeChildren will calculate how much media
# will be added\
import collections
class Cell(object):
def __init__(self,name,od,volume=None,date = 0,parent= None,media = ""):
self.name = name # name of the culture
self.od = [od] # list of ods value, this will getting updated until dilution
self.volume = volume
self.date = [date]
self.children = []
self.parent = parent
self.add = 0
self.media = media
"""
function : giving the cell / culture, and the new od, and the date since last update, we update our Cell object
input : od, date
output : N/A
"""
def update(self,od,date,volume) :
self.od.append(od)
self.date.append(date)
self.volume = volume
"""
function : Giving arrays of volume and ods for the children, calculate how much media volumn to be added
and how much volumn after in each dilution
input : ods(array of float), volumes(array of float, these volumes have to add up to the self.volume)
output : names
"""
def setChildren(self,ods, volumes,dateObserve):
numberChildren = len(ods)
names = []
for i in range(numberChildren):
name = self.name+"_"+str(i)
v = volumes[i]
od = float(ods[i])
newV = round(self.od[-1]*v/od,2)
# create new Cell object
child = Cell(name=name,od=od,volume=newV,date=dateObserve,parent=self)
child.add = newV-v
self.children.append(child)
names.append(name)
return names
"""
function : giving the cell object, saving all the info in a dictionary and dump into a json file
input : Cell object
output : dictionary
"""
def toDictionary(self):
dictionary = collections.OrderedDict()
def dfs(node,d):
if node:
d["name"] = node.name
d["od"] = node.od
d["date"] = ["{}-{}-{}".format(k.year,k.month,k.day) for k in node.date]
d["volume"] = node.volume
d["add"] = node.add
d["media"] = node.media
if node.parent:
d["parent"] = node.parent.name
else:
d["parent"] = None
d["children"] = []
for child in node.children:
childD = collections.OrderedDict()
dfs(child,childD)
d["children"].append(childD)
dfs(self,dictionary)
return dictionary
"""
function : giving the cell object, get back to the root
input : Cell object
output : root (Cell)
"""
def getRoot(self):
parent = self.parent
while parent:
self = parent
parent = self.parent
return self
"""
function : giving the cell object, and a node name, return the node with the name
input : Cell object
output : node
"""
def getNode(self,nodeName):
current_name = self.name.split("_")
target_name = nodeName.split("_")
for i in range(min(len(current_name),len(target_name))):
if current_name[i]!=target_name[i]:
i = i-1
break
for j in range(len(current_name)-i-1):
self=self.parent
if self.name == nodeName:
return self
else:
for child in self.children:
if child.name == nodeName:
return child
"""
function : giving the root, and a node name, return the node with the name
input : Cell object
output : node
"""
def getNodeFromRoot(self,nodeName):
target_name = nodeName.split("_")
current_node = self
for i in target_name[1:]:
current_node = current_node.children[int(i)]
return current_node
"""
function : giving the root, return all the leaf names
input : Cell object
output : names(list)
"""
def getLeafNames(self):
names = []
def dfs(node):
if node:
if not node.children:
names.append(node.name)
for child in node.children:
dfs(child)
dfs(self)
return names
"""
function : giving the cell object, get all the names
input : Cell object
output : names (list)
"""
def getNames(self):
names = []
def dfs(node):
if node:
names.append(node.name)
for child in node.children:
dfs(child)
dfs(self)
return names