-
Notifications
You must be signed in to change notification settings - Fork 0
/
villus_clustering.py
199 lines (159 loc) · 6.54 KB
/
villus_clustering.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
"""SPV simulation of intestinal cell clustering.
A 2D cross-sectional view of the intestinal tissue with three cell/material types:
Pdgrfa- (dark green), Pdgfra+ (light green) and epithelium (red).
Run by calling this script and providing a parameters file as an arguments.
Writes output as npy data files.
Notes:
- If multithreading is not working in Ubuntu, try installing
libopenblas0, libopenblas0-pthread
"""
from collections import defaultdict
import numpy as np
from scipy.stats import qmc
import time, os, shutil, sys
# Local modules:
from tissue import *
import spv_stats, spv_mesh
import energy_heatmap as hm
def random_cell_states(rows, cols, ngfp):
"""
Generates a 2D array of randomly distributed cell states, 0 or 1, with given
total number of states 1. Positions of 1s are drawn from low discrepancy
Halton sequence.
Parameters:
rows: number of array rows
cols: number of array columns
ngfp: number of cells with state 1
Returns
Array of cell states, size rows x cols
"""
# Array of cell identities with 0 = non-gfp, 1 = gfp.
cells = np.zeros([rows,cols])
if (ngfp == 0):
return cells
if (ngfp == rows*cols):
return np.ones([rows,cols])
# Generate low discrepancy distribution of gpf cells from the Halton sequence.
sampler = qmc.Halton(d=2, scramble=False)
sample = sampler.random(n=ngfp)
# Assign coordinates to cell nodes/bins, then set those cells to 1.
# Note: Some coordinates may overlap; taking care of those below.
i2d = np.floor(sample*[rows,cols])
for i in i2d:
cells[int(i[0]), int(i[1])] = 1
# Inject the cells missing due to bin overlaps using the ordinary random numbers.
n = ngfp - int(sum(sum(cells)))
iz = np.where(cells == 0)
inject = np.random.randint(0, np.shape(iz)[1], size=n)
for i in inject:
cells[iz[0][i], iz[1][i]] = 1
return cells
#
# Handle input; read parameters into dictionary P.
#
if (len(sys.argv) < 2):
print("Usage: python " + sys.argv[0] + " [parameters].py")
exit(0)
sys.path.insert(0, os.path.dirname(sys.argv[1]))
s = os.path.basename(sys.argv[1])
par = __import__(s.split(".")[0])
P = defaultdict(lambda: [])
for p in dir(par):
if (p[0:2] != "_"):
P[p] = getattr(par, p)
# Some quick sanity testing.
if (len(P['domain_size']) != 2):
print("Error: Parameter file has missing parameteres.")
exit(1)
#
# Initialize tissue; assign cell types.
#
m = int(P["domain_size"][1]) # initial number of cell rows
n = int(P["domain_size"][0]) # initial number of columns
m0 = int(P["lumen_thickness"]) # thickness of epithlium
m1 = int(P["mes_thickness"][0]) # Pdgfra+
m2 = int(P["mes_thickness"][1]) # Pdgfra- / shallow mesenchyme
m3 = int(P["mes_thickness"][2]) # Pdgfra- / deep mesenchyme
m4 = m - m0 - m1 - m2 - m3 # Pure Pdgfra- / bottom gap
x0 = spv_mesh.init_square_lattice(rows=m, cols=n, noise=P["init_noise"], rng_seed=P["rng_seed"])
vor = Tissue(x0, P)
# Construct GFP layers in top to bottom order, with top layer(s) having the
# highest density.
gfp = []
ngfp = m1*(1.0-P["mes_density"][0])
for i in range(0,m1):
ngfp = np.max([0.0, ngfp])
p = np.min([ngfp, 1.0])
arr = np.array( [1]*int(p*n) + [0]*(n-int(p*n)) )
np.random.shuffle(arr)
gfp = np.concatenate([gfp, arr])
ngfp = ngfp - 1.0
gfp = np.flip(gfp)
# If Pdgfra- mesenchyme density (mes_density) is less than 1, the mensenchyme is
# injected with Pdgfra+ cells to simulate SAG treatment effects resulting in deep
# clustering. Following ptch1 data, moving deeper into the mesenchyme there is
# first Pdgfra+ cells at lower density, then Pdgfra+ cells increasing in density.
# Finally, to avoid periodicity effects (clustering cells adhering to the top of
# the epithelium) in the current simulations, a thin gap with no Pdgfra+ cells is
# set as the final layer.
# l2 = round(0.5*(m2-m3)) # nunber of rows, top layer
# l1 = (m2-m3) - l2 # number of rows, bottom layer
# First layer (bottom)
mes1 = round(m3 * n * (1.0-P["mes_density"][2])) # number of Pdgfra+ cells
mes0 = m3*n - mes1 # number of Pdgfra- cells
mm1 = random_cell_states(m3, n, mes1)
print("Bottom mesenchymal layer, number of Pdgfra low and high cells: " \
+ str(mes0) + ", " + str(mes1))
# Second layer (the one touching epithelium)
mes1 = round(m2 * n * (1.0-P["mes_density"][1]))
mm2 = random_cell_states(m2, n, mes1)
print("Top mesenchymal layer, number of Pdgfra low and high cells: " \
+ str(mes0) + ", " + str(mes1))
# Combine layers 1 and 2, add bottom gap.
mes = np.concatenate([np.zeros(int(m4*n)), mm1.ravel(), mm2.ravel()])
# Set the final cell type array.
c_types = [ mes, \
gfp, \
np.full(m0*n, 2) ]
c_types = np.concatenate(c_types)
c_types = c_types.astype(int)
#
# Set simulation settings, execute simulation.
#
kE = par.W[0][2] + par.W[1][2] - par.W[0][1]
kG = par.W[0][1] + par.W[1][2] - par.W[0][2]
kM = par.W[0][1] + par.W[0][2] - par.W[1][2]
print("Effective tissue cohesions: kE=%f, kG=%f, kM=%f" %(kE, kG, kM))
vor.set_interaction( W=np.asarray(par.W), c_types=c_types, pE=[], randomize=False )
# Output folder for writing results.
s = os.path.basename(sys.argv[1])
prefix = s.split("_")[1] if len(s.split("_"))>2 else s
outputDir = "%s_%d_size-%d_activ-%s" %(prefix, time.time(), par.domain_size[0], \
str(vor.v0).replace(" ", ""))
if not os.path.exists(outputDir):
os.makedirs(outputDir)
# Save this script, parameters to results.
shutil.copy2(__file__, outputDir)
shutil.copy2(sys.argv[1], outputDir)
# Set simulation time step, end time.
vor.set_t_span(par.dt, par.tMax)
# Print_every sets the number of iterations for which to print the percentage completed.
t_start = time.time()
x_save, tri_save = vor.simulate( print_every=100, output_dir=outputDir, rng_seed=par.rng_seed )
print('Simulation took %f seconds' %(time.time() - t_start))
#
# Write additional statistics, plots.
#
print('Writing additional data/statistics...')
t_start = time.time()
spv_stats.write_velocity_stats( x_save, c_types, par.dt, output_file=outputDir+"/stats_velocity.txt" )
spv_stats.write_connection_matrix( tri_save[-1], c_types, output_folder=outputDir )
# Plot cell-cell interaction energies matrix as a heatmap.
'''
fig, ax = plt.subplots(dpi=200)
im = hm.heatmap(np.asarray(par.W), vor.colors, vor.colors, ax=ax, cmap="YlGn")
texts = annotate_heatmap(im)
fig.tight_layout()
plt.savefig(outputDir + "/cell_interaction_energies.png")
'''
print('done after %f seconds' %(time.time() - t_start))