-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondensin.py
210 lines (154 loc) · 5.43 KB
/
condensin.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
import os
import sys
import numpy as np
import h5py
from copy import deepcopy
from collections import defaultdict
import extrusionlib as el
# from extrusionlib.lef_factory import *
def unbind_other(leg, other, context, lifetime):
walker = other['walker']
if not other['unloadable']:
leg['stall'] = True
return
if np.random.random() < (1/lifetime):
context[walker['pos',:]] = 0
walker.reset()
else:
leg['stall'] = True
return
def swap_bypass(leg, other, context, lifetime):
step_dir = other['pos'] - leg['pos']
active_dir = leg['dir']
if step_dir != active_dir:
return
if not other['passable']:
leg['stall'] = True
return
if np.random.random() < (1/lifetime):
p1 = leg['pos']
p2 = other['pos']
leg['pos'] = p2
other['pos'] = p1
context[p1] = other
context[p2] = leg
else:
leg['stall'] = True
return
def hop_bypass(leg, other, context, lifetime):
step_dir = other['pos'] - leg['pos']
active_dir = leg['dir']
if step_dir != active_dir:
return
if (not other['passable']):
leg['stall'] = True
return
if np.random.random() < (1/lifetime):
last_leg = other
while True:
new = context[last_leg['pos']+active_dir]
if isinstance(new, Leg):
if not new['passable']:
leg['stall'] = True
return
last_leg = new
elif not new:
break
else:
leg['stall'] = True
return
p1 = leg['pos']
p2 = last_leg['pos'] + leg['dir']
leg['pos'] = p2
context[p1] = 0
context[p2] = leg
else:
leg['stall'] = True
return
def push_thru_CTCF(leg, other, context, lifetime):
"""
Encounter action. Pushes other legs if they are pushable.
Is only performed when moving in the direction of motion, and with probability 1/lifetime.
(set lifetime=1 to be performed always).
"""
step_dir = other['pos'] - leg['pos']
active_dir = leg['dir']
if step_dir != active_dir:
return # The step is diffusive and I don't want to push on a diffusive step
if np.random.random() < (1/lifetime):
if other['pushable']:
leg_list = [other]
while True:
new = context[leg_list[-1]['pos']+active_dir]
if isinstance(new, Leg):
if not new['pushable']:
leg['stall'] = True
return
leg_list.append(new)
elif not new:
break
else:
leg['stall'] = True
return
for l in leg_list[::-1]:
context[l['pos']] = 0
l['pos'] = l['pos']+active_dir
context[l['pos']] = l
context[leg['pos']] = 0
leg['pos'] = leg['pos']+active_dir
context[leg['pos']] = leg
else:
leg['stall'] = True
else:
leg['stall'] = True
return
def add_condensins(context, condensin_inputs, lattice_params):
chroms, L = lattice_params
lam = condensin_inputs['lambda']
d = condensin_inputs['d']
sol_lt = condensin_inputs['solution_lifetime']
v = condensin_inputs['velocity']
if np.isfinite(lam):
lattice_lt = lam/2
f = lattice_lt/(lattice_lt + sol_lt)
N_condensin = int(chroms*L/(d * f))
else:
N_condensin = int(chroms*L/d)
## Determining collision rule
collision_type = condensin_inputs.get('interaction_info', 'stall')
if collision_type == 'unload':
coll_fn = partial(unbind_other, lifetime=1)
elif collision_type == 'push':
coll_fn = partial(push_thru_CTCF, lifetime=1)
elif collision_type == 'hop bypass':
coll_fn = partial(hop_bypass, lifetime=1)
elif collision_type == 'swap bypass':
coll_fn = partial(swap_bypass, lifetime=1)
elif collision_type == 'stall':
coll_fn = stall_any
else:
raise
## Creating Condensin Extruders
base_template = {'v':v, 'D':0, 'max_v':v, 'max_D':0, 'pos':np.nan, 'stalled':False, 'halted':False}
reset_args = ['stalled','pos','halted']
for _ in range(N_condensin):
legs = []
for direc in [-1, 1]:
leg_template = deepcopy(base_template)
leg_template['dir'] = direc
l = Leg(leg_template=leg_template, reset_args=reset_args)
l.add_encounter_check(collision_type, coll_fn)
legs.append(l)
walker = Walker(legs=legs, walker_attrs={'solution':True, 'name':'condensin'})
walker.add_step_check('bind', partial(bind_walker, lifetime=sol_lt))
if np.isfinite(lam):
walker.add_step_check('unbind', partial(unbind, lifetime=lattice_lt))
context.add_walker('condensin', walker)
condensin_params = {
'N':N_condensin,
'lambda':lam,
'd':d,
'solution_lifetime':sol_lt,
'velocity':v,
}
return context, condensin_params