-
Notifications
You must be signed in to change notification settings - Fork 1
/
MC.py
275 lines (244 loc) · 10.3 KB
/
MC.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import random
class Membrane(object):
""" creates a membrane (width x height) of hexagons"""
def __init__(self, width, height):
self.width = width
self.height = height #must be even for correct coordinates!!!!
self.room = []
for x in range(self.width):
room_x=[]
for y in range(self.height):
room_x.append('-')
self.room.append(room_x)
def __str__(self):
for y in range(self.height-1, -1, -1):
if y%2 == 1:
print ' ',
for x in range(self.width):
if self.room[x][y] == '-':
print ' ' ,self.room[x][y], ' ',
else:
print ' ' ,self.room[x][y].a_or_d, ' ',
print ' '
print ' '
return ''
def getRandomPos(self):
"""returns a random position of the membrane"""
x = random.choice(range(self.width))
y = random.choice(range(self.height))
return Position(x,y)
class Position(object):
def __init__(self,x,y):
self.x = x
self.y = y
def __str__(self):
return str(self.getX()) + ' ' + str(self.getY())
def getX(self):
return self.x
def getY(self):
return self.y
def convert_pos(self, membrane):
""" applys the concept of periodic boundry condition:
if the position is out of the membrane, creates a new position at the oposite side of membrane"""
if self.x<0:
self.x = self.x + membrane.width
if self.x>membrane.width-1:
self.x = self.x - membrane.width
if self.y<0:
self.y = self.y + membrane.height
if self.y>membrane.height-1:
self.y = self.y - membrane.height
def get_neighbour_pos(self, membrane):
""" returns the coordinates of the neighbourous positions as a list"""
neighbour_pos = []
if self.getY()%2 == 0:
for i in [(0,1),(1,0),(0,-1),(-1,-1),(-1,0),(-1,1)]:
x = self.getX() + i[0]
y = self.getY() + i[1]
new_pos = Position(x,y)
new_pos.convert_pos(membrane)
neighbour_pos.append(new_pos)
else:
for i in [(1,1),(1,0),(1,-1),(0,-1),(-1,0),(0,1)]:
x = self.getX() + i[0]
y = self.getY() + i[1]
new_pos = Position(x,y)
new_pos.convert_pos(membrane)
neighbour_pos.append(new_pos)
return neighbour_pos
def are_neighbours(self, pos, membrane):
""" returns True if the two positions are neighbours, else return False"""
pos_list = self.get_neighbour_pos(membrane)
for i in pos_list:
if i.getX() == pos.getX() and i.getY() == pos.getY():
return True
else:
return False
class Receptor(object):
""" creates a receptor, in a membrane. The receptor can be 'D'(onor) or 'A'(cceptor)
paon, pdon, paoff, pdoff represents the probability of dimerization (on) or
monomeization (off) with an acceptor (a) or donor(d). Donor receptors have
an rluc luminsecence (rluc) and yfp luminescence vaue. Receptor is positioned
into a random, not occupied hexa of the membrane"""
def __init__(self, membrane, a_or_d, paon, pdon, paoff, pdoff):
self.membrane = membrane
self.a_or_d = a_or_d
self.paon = paon
self.pdon = pdon
self.paoff = paoff
self.pdoff = pdoff
self.dimer = '-'
self.has_moved = False #hasn't moved in this iteration
self.has_changed_dimer_status = False #hasn't changed dimer status in this iteration
if self.a_or_d == 'D':
self.rluc = 1.0
self.yfp = 0.0
self.bret = self.yfp / self.rluc
occupied = True
while occupied:
self.pos = self.membrane.getRandomPos()
if self.membrane.room[self.pos.getX()][self.pos.getY()] == '-':
occupied = False
self.membrane.room[self.pos.getX()][self.pos.getY()] = self
def get_free_neighbour_pos(self):
"""returns a free neighbour position in a list"""
neighbour_pos = self.pos.get_neighbour_pos(self.membrane)
free_neighbour_pos = []
for i in neighbour_pos:
if self.membrane.room[i.getX()][i.getY()] == '-':
free_neighbour_pos.append(i)
return free_neighbour_pos
def get_neighbours(self):
"""return the neighbouring receptors in a list"""
neighbour_pos = self.pos.get_neighbour_pos(self.membrane)
neighbours = []
for i in neighbour_pos:
if self.membrane.room[i.getX()][i.getY()] != '-':
neighbours.append(self.membrane.room[i.getX()][i.getY()])
return neighbours
def get_free_neighbour_pos_dimer(self):
"""return the free neighbouring positions of a dimer"""
neighbour_pos_self = self.pos.get_neighbour_pos(self.membrane)
neighbour_pos_pair = self.dimer.pos.get_neighbour_pos(self.dimer.membrane)
free_neighbour_pos = []
for i in neighbour_pos_self:
if self.membrane.room[i.getX()][i.getY()] == '-' or self.membrane.room[i.getX()][i.getY()] == self.dimer:
for j in neighbour_pos_pair:
if self.dimer.membrane.room[j.getX()][j.getY()] == '-' or self.dimer.membrane.room[j.getX()][j.getY()] == self:
if i.are_neighbours(j,self.membrane):
free_neighbour_pos.append([i,j])
return free_neighbour_pos
def move(self):
if self.has_moved == False:
if self.dimer == '-':
free_neighbour_pos=self.get_free_neighbour_pos()
self.membrane.room[self.pos.getX()][self.pos.getY()] = '-'
if len(free_neighbour_pos)>0:
self.pos=random.choice(free_neighbour_pos)
self.membrane.room[self.pos.getX()][self.pos.getY()] = self
self.has_moved = True
elif self.dimer.has_moved == False:
free_neighbour_pos = self.get_free_neighbour_pos_dimer()
self.membrane.room[self.pos.getX()][self.pos.getY()] = '-'
self.dimer.membrane.room[self.dimer.pos.getX()][self.dimer.pos.getY()] = '-'
if len(free_neighbour_pos)>0:
new_pos = random.choice(free_neighbour_pos)
self.pos = new_pos[0]
self.dimer.pos = new_pos[1]
self.membrane.room[self.pos.getX()][self.pos.getY()] = self
self.dimer.membrane.room[self.dimer.pos.getX()][self.dimer.pos.getY()] = self.dimer
self.has_moved = True
self.dimer.has_moved = True
def change_dimer_status(self):
if self.has_changed_dimer_status == False:
if self.dimer == '-':
neighbours = self.get_neighbours()
for i in neighbours:
if i.has_changed_dimer_status == False:
x = random.random()
if i.a_or_d == 'A':
p=self.paon
if i.a_or_d == 'D':
p=self.pdon
if x<p:
self.dimer = i
self.dimer.dimer = self
self.has_changed_dimer_status = True
self.dimer.has_changed_dimer_status = True
break
else:
if self.dimer.has_changed_dimer_status == False:
x=random.random()
if self.dimer.a_or_d == 'A':
p=self.paoff
if self.dimer.a_or_d == 'D':
p=self.pdoff
if x<p:
self.has_changed_dimer_status = True
self.dimer.has_changed_dimer_status = True
self.dimer.dimer = '-'
self.dimer = '-'
def get_bret(self):
if self.a_or_d == 'D':
self.rluc = 1.0
self.yfp = 0.4
neighbours = self.get_neighbours()
for i in neighbours:
if i.a_or_d == 'A':
self.yfp = self.yfp + 0.2
self.rluc = self.rluc - 0.05
self.bret=self.yfp/self.rluc
def calculate_bret(rlist):
bret = 0
d = 0
for i in rlist:
i.get_bret()
for i in rlist:
if i.a_or_d == 'D':
bret+=i.bret
d+=1
return bret/d
def graphics_demo():
""" graphical demo with one donor and one acceptor, with pdimerization=1.0
ends with 's' key"""
mem = Membrane(6,6)
rlist = []
print mem
for i in range(1):
rlist.append(Receptor(mem,'A',0.0,1.0,0.0,0.0))
rlist.append(Receptor(mem,'D',1.0,0.0,0.0,0.0))
print mem
while raw_input()!='s':
print mem
for i in rlist:
i.has_moved=False
i.has_changed_dimer_status=False
for i in rlist:
i.move()
for i in rlist:
i.change_dimer_status()
def simulate(d,a,pddon,pddoff,pdaon,pdaoff,paaon,paaoff):
""" d: number of donors
a: number of acceptors
pddon: donor-donor association probability
pddoff: donor-donor dissociation probability
pdaon: donor-acceptor association probability
pdaoff: donor-acceptor dissociation probability
paaon: acceptor-acceptor association probability
paaoff: acceptor-acceptor dissociation probability
performs simulation for 1000 iterations, returns bret"""
mem = Membrane(100,100)
rlist = []
for j in range(d):
rlist.append(Receptor(mem, 'D', pdaon, pddon, pdaoff, pddoff))
for j in range(a):
rlist.append(Receptor(mem, 'A', paaon, pdaon, paaoff, pdaoff))
for j in range(1000):
for k in rlist:
k.has_moved = False
k.has_changed_dimer_status = False
for k in rlist:
k.move()
for k in rlist:
k.change_dimer_status()
return calculate_bret(rlist)