-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_model.py
310 lines (247 loc) · 11.7 KB
/
full_model.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# TODO: "THE GAP"
# new model, set Kc = 0 and set other K to 1, change Kf accordingly
# change end time, i.e. t = 10, 100, ...
# Ki is constant, others are linear
import numpy as np
import random
import math
from collections import defaultdict
def calculate_intensity(reaction_count, reactants_list, species_counts, rates, species_list):
intensities = []
for i in range(reaction_count):
rate = rates[i]
for reactant in reactants_list[i]:
species, coefficient = reactant
if coefficient == 1:
index = species_list.index(species)
rate *= species_counts[index]
elif coefficient == 2:
index = species_list.index(species)
rate *= 0.5 * species_counts[index] * (species_counts[index] - 1)
# elif coefficient == 0:
# index = species_list.index(species)
# rate *= rates[index]
intensities.append(rate)
return intensities
def reaction_to_changes(reactants, products):
changes = defaultdict(int) # Initialize a dictionary to store changes for each species
# Process reactants
for reactant in reactants:
species, coefficient = reactant
changes[species] -= coefficient
# Process products
for product in products:
species, coefficient = product
changes[species] += coefficient
# Convert the dictionary to a list
changes_list = [(species, change) for species, change in changes.items()]
result = [x[1] for x in changes_list]
print(f'The jumps of reaction: {result}')
return result
def simGillespie(species_counts, jumps, rates, end_time, reaction_count, reactants_list, species_list):
# Define the initial species counts, reaction rates, and intensity functions
# species_counts = [10, 10, 50, 10] # Initial counts for four species
# rates = [200, 10, 25, 1, 0.01, 1] # Reaction rates for six reactions
# Define the changes in species counts for each reaction
# jumps = [
# [0, 1, 0, 0], # Transcription
# [0, 0, 1, 0], # Translation
# [0, -1, 0, 0], # Degradation of mRNA
# [0, 0, -1, 0], # Degradation of protein
# [0, 0, -2, 1], # Protein dimerization
# [0, 0, 0, -1], # Degradation of dimer
# ]
# Define the time and simulation end time
time = 0
break_early = 0
# end_time = 8
# Initialize lists to store time points and species counts
time_points = [time]
species_counts_history = [species_counts]
# Main simulation loop
while time < end_time:
intensity_functions = calculate_intensity(reaction_count, reactants_list, species_counts, rates, species_list)
# print(f'Intensities function: {intensity_functions}')
# Calculate the intensity functions based on species_counts
# intensity_functions = [rates[0],
# rates[1] * species_counts[1],
# rates[2] * species_counts[1],
# rates[3] * species_counts[2],
# rates[4] * species_counts[2] * (species_counts[2] - 1),
# rates[5] * species_counts[3]]
# Calculate the total intensity
total_intensity = sum(intensity_functions)
# Generate two independent uniform(0,1) random numbers
r1 = random.uniform(0, 1)
r2 = random.uniform(0, 1)
# Calculate the time increment
delta_t = -np.log(r1) / total_intensity
# TODO: what to do if total_intensity is 0 in Gillespie's algorithm?
if delta_t <= 0 or total_intensity == 0:
break_early = 1
break
# print(f'Total_intensity is : {total_intensity}, Delta_t for reaction is: {delta_t}')
# Find index
i = 0
p_sum = 0.0
while p_sum < r2 * total_intensity:
p_sum += intensity_functions[i]
i += 1
reaction = i - 1
# Update time
time += delta_t
# Update species counts based on the chosen reaction
species_counts = [count + change for count, change in zip(species_counts, jumps[reaction])]
# Append the current time and species counts to the history
time_points.append(time)
species_counts_history.append(list(species_counts))
# return species_counts
if break_early == 1:
return species_counts
else:
return species_counts_history[len(species_counts_history) - 2]
def full_model(init_comp_count, num_species, init_species_counts, network_jumps, network_rates, end_time, reaction_count, reactants_list, species_list):
comp_counts = init_comp_count
comp_rates = [1, 1, 1, 1] # Reaction rates for four compartment reactions
curr_state = init_species_counts
# Define the changes in species counts for each reaction
# Reaction 0: 0 -> C
# Reaction 1: C -> 0
# Reaction 2: 2C -> C
# Reaction 3: C -> 2C
comp_jumps = [1, -1, -1, 1]
# Define the time and simulation end time
time = 0
end_time = 100
# Initialize lists to store time points and species counts
time_points = [time]
# species_counts_history = [comp_counts]
# Main simulation loop
while time < end_time:
# Calculate the intensity functions based on species_counts
intensity_functions = [comp_rates[0],
comp_rates[1] * comp_counts,
comp_rates[2] * comp_counts * (comp_counts - 1) / 2,
comp_rates[3] * comp_counts]
# Calculate the total intensity
total_intensity = sum(intensity_functions)
# Generate two independent uniform(0,1) random numbers
r1 = random.uniform(0, 1)
r2 = random.uniform(0, 1)
# Calculate the time increment
delta_t = -np.log(r1) / total_intensity
# Update time
time += delta_t
# Decide which reaction occurred
i = 0
p_sum = 0.0
while p_sum < r2 * total_intensity:
p_sum += intensity_functions[i]
i += 1
reaction = i - 1
# Update species counts based on the chosen reaction
comp_counts = comp_counts + comp_jumps[reaction]
# Append the current time and species counts to the history
time_points.append(time)
# species_counts_history.append(comp_counts)
# 1. if 0 -> C is chosen
if reaction == 0:
print("0 -> C is chosen")
for i in range(comp_counts - 1):
newState = simGillespie(curr_state[i], network_jumps, network_rates, delta_t, reaction_count, reactants_list, species_list)
curr_state[i] = newState
# the initial state of the new compartment
# TODO: should be determined by miu
new_comp = [random.randint(0, 10) for _ in range(num_species)]
curr_state.append(new_comp)
# 2. if C -> 0 is chosen
elif reaction == 1:
print("C -> 0 is chosen")
del_comp = random.randint(0, comp_counts)
curr_state.pop(del_comp)
for i in range(comp_counts):
newState = simGillespie(curr_state[i], network_jumps, network_rates, delta_t, reaction_count, reactants_list, species_list)
curr_state[i] = newState
# 3. if 2C -> C is chosen
elif reaction == 2:
print("2C -> C is chosen")
for i in range(comp_counts + 1):
newState = simGillespie(curr_state[i], network_jumps, network_rates, delta_t, reaction_count, reactants_list, species_list)
curr_state[i] = newState
# choose two different compartments uniformly at random
two_random_comp = []
while len(two_random_comp) < 2:
random_int = random.randint(0, comp_counts)
if random_int not in two_random_comp:
two_random_comp.append(random_int)
curr_state[min(two_random_comp)] = [x + y for x, y in zip(curr_state[two_random_comp[0]], curr_state[two_random_comp[1]])]
curr_state.pop(max(two_random_comp))
# 4. if C -> 2C is chosen
else:
print("C -> 2C is chosen")
for i in range(comp_counts - 1):
newState = simGillespie(curr_state[i], network_jumps, network_rates, delta_t, reaction_count, reactants_list, species_list)
curr_state[i] = newState
# choose a compartment to split uniformly at random
split_comp = random.randint(0, comp_counts - 2)
new_comp_1 = list(curr_state[split_comp])
new_comp_2 = list(curr_state[split_comp])
for j in range(len(curr_state[split_comp])):
new_comp_1[j] = random.randint(0, curr_state[split_comp][j])
new_comp_2[j] = new_comp_2[j] - new_comp_1[j]
curr_state.pop(split_comp)
curr_state.append(new_comp_1)
curr_state.append(new_comp_2)
print(f'Current state is {curr_state}')
return curr_state
def main():
# Ask for user input and parse it
init_comp_count = int(input("Enter the initial compartment count: "))
species_input = input("Enter all species in the reaction network (e.g., 'G M P'): ")
species_list = species_input.split()
num_species = len(species_list)
# num_species = int(input("Enter the number of species in the reaction network: "))
# Parse a list of initial species counts
if init_comp_count == 0:
init_species_counts = []
else:
init_species_counts = []
for i in range(init_comp_count):
input_species_counts = input(f"Enter initial species counts of compartment {i + 1} separated by spaces: ")
init_species_counts.append([int(x) for x in input_species_counts.split()])
reaction_count = int(input("Enter the number of reactions in reaction network: "))
# Parse reaction jumps
network_jumps = []
reactants_list = []
for i in range(reaction_count):
# Ask for user input for reactants
reactants = []
reactants_input = input("Enter reactants separated by '+' (Ex. 2 A + 1 B): ")
if reactants_input:
reactant_tokens = reactants_input.split('+')
for token in reactant_tokens:
coefficient, element = token.strip().split()
reactants.append((element, int(coefficient)))
reactants_list.append(reactants)
# Ask for user input for products
products = []
products_input = input("Enter products separated by '+' (Ex. 2 A + 1 B): ")
if products_input:
product_tokens = products_input.split('+')
for token in product_tokens:
coefficient, element = token.strip().split()
products.append((element, int(coefficient)))
network_jumps.append(reaction_to_changes(reactants, products))
# # Parse a list of network jumps
# network_jumps = []
# for i in range(reaction_count):
# input_jumps = input(f"Enter jump of reaction {i + 1} separated by spaces: ")
# network_jumps.append([int(x) for x in input_jumps.split()])
# Parse a list of network rates
network_rates = input("Enter network rates separated by spaces: ")
network_rates = [float(x) for x in network_rates.split()]
end_time = float(input("Enter the end time: "))
output = full_model(init_comp_count, num_species, init_species_counts, network_jumps, network_rates, end_time, reaction_count, reactants_list, species_list)
print(f"Final state is {output}")
if __name__ == '__main__':
main()