forked from Prosimio/Cellular_Growth_CA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celular_growth_ca.py
193 lines (136 loc) · 6.19 KB
/
celular_growth_ca.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 10:52:30 2018
@author: Prosimios
"""
import numpy as np
import matplotlib.pyplot as plt
import time
import sys
import pickle as pkl
import cgca_functions as cgca
def load_obj(name, folder ):
with open(folder+'/' + name + '.pkl', 'rb') as f:
return pkl.load(f)
def save_obj(obj, name, folder ):
with open(folder+'/'+ name + '.pkl', 'wb') as f:
pkl.dump(obj, f, pkl.HIGHEST_PROTOCOL)
def main():
#sys.argv[1] = grid size value e.g. 200
#sys.argv[2] = number of simulation e.g. 10
#sys.argv[3] = steps per simulation e.g. 50000
#initialize input variables
#(in the future will be better to use a file with them as input)
grid_size = 150
sim_num = 2
steps = 15000
g_ratio= [1,1]
#g_ratio= [1,1]
# g_ratio = [2,1] --> plasmid 1 divide twice fast than plasmid 2
if len(sys.argv) > 1:
grid_size = int(sys.argv[1])
else:
grid_size = int(input("Please enter value of grid size (it will be size [n,n]): n="))
if len(sys.argv) > 2:
sim_num = int(sys.argv[2])
else:
sim_num = int(input("Please enter the number of simulations: "))
if len(sys.argv) > 3:
steps = int(sys.argv[3])
else:
steps = int(input("Please enter the number of steps per simulation: "))
if len(sys.argv) > 4:
input_ratios = sys.argv[4].split(':')
for i in range(len(input_ratios)):
g_ratio[i] = int(input_ratios[i])
######################################
### OTHER THINGS SET BY THE USER ###
prob_dist = 'contact_exp' # grid selection probability, used on nb_prob()
#to save the last figure
# filename = 'Segregation\\ratios\\image_%03d.jpg'
save_im_final = False #put True to save them
filename_j = 'finalSatate_%04d.jpg'
#to save images of a sequence
every = 100 #every how many save images and/or computations
save_im_step = False #put True to save them
filename_i = 'im_%04d.jpg'
#####################################
all_ratios = []
#save_obj(all_ratios, 'ratios', 'data') #data: folder , ratios = filename
if save_im_final == True or save_im_step == True:
plt.figure()
##########################
## Run the simulations ##
for j in range(sim_num):
time1 = time.clock()
######################################
### OTHER THINGS SET BY THE USER ###
initial_pattern_choise = 1
#####################################
# create a initial empty grid
grid = np.zeros((grid_size,grid_size))
# define the initial grid and plasmid pattern
grid = cgca.initial_pattern(grid, initial_pattern_choise)
#plasm_grid = cgca.initial_plasmids(grid)
plasm_grid = cgca.initial_plasmids(grid, pattern_num = 0, num_plas = 2, max_copy = 4)
# Uncoment Show the initial state
# plt.figure(2)
# im_grid = create_image(grid, plasm_grid)
# plt.title('step 0')
# plt.imshow(im_grid)
#counter of step saved images
count = 0
#determine the growth probability of each genotype
plas_probs = cgca.plasmid_gProb(g_ratio)
ratios = [] #to store the cell type ratios
for i in range(steps):
#select a random growing cell
cell_pos = cgca.select_cell(grid)
free_nb = cgca.check_nbhd(grid, cell_pos)
if free_nb[0].shape[0] > 0: # go if there is a place in the neighborhood
plasmids = plasm_grid[cell_pos[0], cell_pos[1],:] #get its plasmids
c_growth = cgca.plasm_g_test(plasmids, plas_probs)
if c_growth == True:
#update its plasmids and cell state, n:new
n_plasmids, n_state = cgca.plasmid_update(plasmids, cell_pos)
plasm_grid[cell_pos[0], cell_pos[1],:] = n_plasmids
grid[cell_pos[0], cell_pos[1]] = n_state
#state will not be evaluated before role_divide
#role_divide function shouldn´t allow divition of that cell
divide_flag = cgca.role_divideFlag(n_plasmids)
#perform the divition if flag changed
if divide_flag != 0:
#assign a cell to a new position
free_proba = cgca.nb_prob(grid, cell_pos, prob_dist)
grid, nCell_pos = cgca.cell_divition(grid, cell_pos, free_nb, free_proba)
#split the mother plasmids
m_plasmids, c_plasmids = cgca.divide_plasmids(n_plasmids)
#assign mother and child plasmids
plasm_grid[cell_pos[0], cell_pos[1],:] = m_plasmids
plasm_grid[nCell_pos[0], nCell_pos[1],:] = c_plasmids
else:
grid[cell_pos[0],cell_pos[1]] = 1
#store values and images
if i%every == 0 or i == steps-1:
ratios.append(cgca.cell_ratio(plasm_grid))
if save_im_step == True:
plt.title('step '+ str(i+1))
im_grid = cgca.create_image2(grid, plasm_grid)
plt.imshow(im_grid)
plt.savefig(filename_i%(count), transparent=True)
count += 1
#Plot the result
if i == steps-1 and save_im_final == True:
im_grid = cgca.create_image2(grid, plasm_grid)
plt.imshow(im_grid)
plt.title('step '+ str(i+1))
plt.savefig(filename_j%(j), transparent=True)
all_ratios.append(np.asarray(ratios))
elapsed = time.clock() - time1
print('elapsed time: ' + str(elapsed)+ ' sec')
#print(all_ratios)
#return(all_ratios)
save_obj(all_ratios, 'ratios', 'data')
return()
if __name__== "__main__":
main()