-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandom_setup_local.py
186 lines (169 loc) · 7.12 KB
/
random_setup_local.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
"""
To set up a simplified system with randomly diffusion insulin secretory granules.
"""
from __future__ import print_function, division
import IMP.atom
import IMP.algebra
import IMP.rmf
import IMP.core
import RMF
import IMP.container
import IMP.display
import sys
import math
import random
import numpy
from random import choice
from random import uniform
import GranuleFactory
# record the simulation and ISG distance
f1=open('random_ISG_record.txt', 'w')
f2=open('random_ISG-NE_distance.xvg', 'w')
def convert_time_ns_to_frames(time_ns, step_size_fs):
'''
Given time in nanoseconds time_ns and step size in femtosecond
step_size_fs, return an integer number of frames greater or equal
to 1, such that time_ns*step_size_fs is as close as possible to
time_ns.
'''
FS_PER_NS= 1E6
time_fs= time_ns * FS_PER_NS
n_frames_float= (time_fs+0.0) / step_size_fs
n_frames= int(round(n_frames_float))
return max(n_frames, 1)
def create_nucleus(m, R):
'''
Generate a coarse-grained spherical nuclear envelope
of radius R in model m
'''
p= IMP.Particle(m, "nucleus")
xyzr = IMP.core.XYZR.setup_particle(p)
xyzr.set_coordinates_are_optimized(True)
xyzr.set_coordinates([0,0,0])
xyzr.set_radius(R)
IMP.display.Colored.setup_particle(p,
IMP.display.get_display_color(2))
IMP.atom.Hierarchy.setup_particle(p)
return p
def do_stats_after_optimize(granules):
'''write out ISG coordinates long time'''
distance = []
for g in granules:
distance.append(round(IMP.core.get_distance(IMP.core.XYZ(h_nucleus),IMP.core.XYZ(g)),4))
return distance
#---------- Simulation parameters ----------
# I. Parts parameters
L = 50000 # Length of our bounding box, A
R = 20000 # PBC radius, A
R_NUCLEUS = 10000 # NE radius, A
N_GRANULES = 50 # Number of granules
R_GRANULES = 1500 # Radius of granules, A
# II. Interaction parameters
K_BB = 0.1 # Strength of the harmonic boundary box in kcal/mol/A^2
K_EXCLUDED=0.1 # Strength of lower-harmonic excluded volume score in kcal/mol/A^2
# III. Time parameters
BD_STEP_SIZE_SEC= 10E-8 # a time step of 10 ns
SIM_TIME_SEC= 0.050 # a total simulation time of 5 ms
bd_step_size_fs= BD_STEP_SIZE_SEC * 1E+15
sim_time_ns= SIM_TIME_SEC * 1E+9
RMF_DUMP_INTERVAL_NS= sim_time_ns / 1000.0
sim_time_frames= convert_time_ns_to_frames(sim_time_ns, bd_step_size_fs)
rmf_dump_interval_frames= convert_time_ns_to_frames(RMF_DUMP_INTERVAL_NS, bd_step_size_fs)
print("Simulation time {:.1e} ns / {} frames; "
"RMF dump interval {:.1e} ns / {} frames".format(sim_time_ns,
sim_time_frames,
RMF_DUMP_INTERVAL_NS,
rmf_dump_interval_frames), file = f1)
# -------- I. System parts --------
# Model:
m = IMP.Model()
# Root of parts hierarchy:
p_root= IMP.Particle(m, "root")
h_root = IMP.atom.Hierarchy.setup_particle(p_root)
# Outer bounding box for simulation:
bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(-L/2, -L/2, -L/2), IMP.algebra.Vector3D(L/2, L/2, L/2))
# PBC cytoplasm bounding sphere:
pbc_sphere= IMP.algebra.Sphere3D([0,0,0], R)
# Nucleus:
p_nucleus= create_nucleus(m, R_NUCLEUS)
IMP.atom.Mass.setup_particle(p_nucleus, 1.0) # fake mass
h_nucleus= IMP.atom.Hierarchy(p_nucleus)
h_root.add_child(h_nucleus)
# Granules hierarchy root:
p_granules_root= IMP.Particle(m, "Granules")
IMP.atom.Mass.setup_particle(p_granules_root, 1.0) # fake mass
h_granules_root= IMP.atom.Hierarchy.setup_particle(p_granules_root)
h_root.add_child(h_granules_root)
# Actual granules:
nucleus_sphere= IMP.core.XYZR(p_nucleus).get_sphere()
gf=GranuleFactory.GranuleFactory\
(model= m,
default_R= R_GRANULES,
cell_sphere= pbc_sphere,
nucleus_sphere= nucleus_sphere)
for i in range(N_GRANULES):
granule= gf.create_simple_granule("Granule_{}".format(i))
h_granule= IMP.atom.Hierarchy(granule)
h_granules_root.add_child(h_granule)
IMP.atom.Diffusion(granule).set_diffusion_coefficient(1.5E-10) # A^2/fs
#print(IMP.atom.Diffusion(granule).get_diffusion_coefficient())
#print(IMP.core.XYZ(granule))
#print(h_root.get_children())
# ----- II. System interactions: -----
# Add enclosing spheres for pbc and outer simulation box
bb_harmonic= IMP.core.HarmonicUpperBound(0, K_BB)
pbc_bsss = IMP.core.BoundingSphere3DSingletonScore(bb_harmonic,
pbc_sphere)
outer_bbss = IMP.core.BoundingBox3DSingletonScore(bb_harmonic,
bb)
# Restraints - match score with particles:
rs = []
rs.append(IMP.container.SingletonsRestraint(pbc_bsss,
h_granules_root.get_children()))
rs.append(IMP.container.SingletonsRestraint(outer_bbss,
h_granules_root.get_children()))
# Add excluded volume restraints among all (close pairs of) particles:
ev = IMP.core.ExcludedVolumeRestraint(IMP.atom.get_leaves(h_root),
K_EXCLUDED,
10, # slack affects speed only
# (slack of close pairs finder)
"EV")
rs.append(ev)
# Scoring Function from restraints
sf = IMP.core.RestraintsScoringFunction(rs, "SF")
#print(h_root.get_children())
# -------- III. System dynamic: --------
bd = IMP.atom.BrownianDynamics(m)
bd.set_log_level(IMP.SILENT)
bd.set_scoring_function(sf)
bd.set_maximum_time_step(bd_step_size_fs) # in femtoseconds
bd.set_temperature(310.15)
# -------- Add RMF visualization --------
rmf = RMF.create_rmf_file("random.rmf")
rmf.set_description("Brownian dynamics trajectory with {}fs timestep.\n"\
.format(bd_step_size_fs))
IMP.rmf.add_hierarchy(rmf, h_root)
IMP.rmf.add_restraints(rmf, rs)
IMP.rmf.add_geometry(rmf, IMP.display.BoundingBoxGeometry(bb))
IMP.rmf.add_geometry(rmf, IMP.display.SphereGeometry(pbc_sphere))
# Pair RMF with model using an OptimizerState ("listener")
sos = IMP.rmf.SaveOptimizerState(m, rmf)
sos.set_log_level(IMP.SILENT)
sos.set_simulator(bd)
sos.set_period(rmf_dump_interval_frames)
bd.add_optimizer_state(sos)
# Dump initial frame to RMF
sos.update_always("initial conformation")
# -------- Run simulation ---------
print("Running simulation", file = f1)
print("Score before: {:f}".format(sf.evaluate(True)), file = f1)
n_frames_left=sim_time_frames
frames_per_cycle=1000 # can be 10, or 100, dependeing on the frequency of the optimizer state and how much ISG moves.
while n_frames_left>0:
cur_n_frames=min(frames_per_cycle, n_frames_left)
bd.optimize(cur_n_frames)
print(*do_stats_after_optimize(h_granules_root.get_children()), sep=" ", file = f2)
print(sim_time_frames - n_frames_left, file = f1)
n_frames_left = n_frames_left - cur_n_frames
print("Run finished succesfully", file = f1)
print("Score ater: {:f}".format(sf.evaluate(True)), file = f1)