-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrusion-sim-graphs2.jl
329 lines (300 loc) · 12.3 KB
/
extrusion-sim-graphs2.jl
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import Base.show
using DataFrames
using Random
import Pkg
#Pkg.add("Graphs")
using Graphs
#Pkg.add("Plots")
using Plots
#Pkg.add("ColorSchemes")
using ColorSchemes
using Statistics
mutable struct Cohesin
processivity::Int64
alive::Int64
left_pos::Int64
right_pos::Int64
left_blocked::Bool
right_blocked::Bool
left_ctcf_captured::Bool
right_ctcf_captured::Bool
extruded_beads::Array{Bool}
function Cohesin(processivity, polymer_length, alive=0)
loading_pos = rand(1:polymer_length)
new(processivity, alive, loading_pos, loading_pos, false, false, false, false, fill(false, polymer_length))
end
end
function Base.show(io::IO, object::Cohesin)
left_blocked_str = string((object.left_blocked) ? " (Cohesin blocked)" : "",
(object.left_ctcf_captured) ? " (CTCF captured)" : "")
right_blocked_str = string((object.right_blocked) ? " (Cohesin blocked)" : "",
(object.right_ctcf_captured) ? " (CTCF captured)" : "")
print(io, string("Lived ", object.alive, " of ", object.processivity, " - "),
string("Anchors: ", object.left_pos, left_blocked_str, ", ", object.right_pos, right_blocked_str, " - "),
string("Extruded beads:", findall(object.extruded_beads), "\n"))
end
mutable struct Sim
polymer_length::Int64
bead_size::Int64
cohesins::Array{Cohesin}
plus_ctcfs::DataFrame
minus_ctcfs::DataFrame
occupied_positions::Array{Bool}
processivity::Float64
ctcf_capture_rate::Float64
ctcf_stabilization_factor::Int64
extruded_beads::Array{Bool}
bead_graph::SimpleGraph
function Sim(n, polymer_length_bp=1e6, processivity_bp=150e3, bead_size = 1e3,
ctcf_capture_rate=0.25, ctcf_stabilization_factor=4, plus_ctcfs=missing,
minus_ctcfs=missing)
# Define polymer_length and processity in bead units
polymer_length = Int(polymer_length_bp/bead_size)
processivity = processivity_bp / bead_size
occupied_positions = fill(false, polymer_length)
alive = 0
cohesins = []
bead_graph = path_graph(polymer_length)
for i in 1:n
collision = true
while (collision)
global coh = Cohesin(processivity, polymer_length, alive)
if (!occupied_positions[coh.left_pos])
collision = false
end
end
occupied_positions[coh.left_pos] = true
alive = round(alive + processivity/n)
push!(cohesins, coh)
add_edge!(bead_graph, coh.left_pos, copy(coh.left_pos))
end
empty_ctcf_df = DataFrame(pos_bp=Float64[], pos=Int64[], occupancy_rate=Float64[], bound_cohesin=Bool[])
if ismissing(plus_ctcfs)
plus_ctcfs = deepcopy(empty_ctcf_df)
end
if ismissing(minus_ctcfs)
minus_ctcfs = deepcopy(empty_ctcf_df)
end
new(polymer_length, bead_size, cohesins, plus_ctcfs, minus_ctcfs, occupied_positions,
processivity, ctcf_capture_rate, ctcf_stabilization_factor, fill(false, polymer_length), bead_graph)
end
end
function Base.show(io::IO, object::Sim)
print(io, string("Cohesin positions: ", findall(object.occupied_positions), "\n"),
string("Extruded beads: ", sum(object.extruded_beads), " of ", object.polymer_length, "\n"),
string("Plus CTCF positions (bound fraction): ", object.plus_ctcfs.pos_bp,
"(", object.plus_ctcfs.occupancy_rate, ") ", "\n"),
string("Minus CTCF positions (bound fraction): ", object.minus_ctcfs.pos_bp,
"(", object.minus_ctcfs.occupancy_rate, ") ", "\n"), "Individual cohesins:")
for coh in object.cohesins
show(coh)
end
end
function add_ctcf(object::Sim, orientation, pos_bp, occupancy_rate=1)
if orientation=="+"
push!(object.plus_ctcfs, (pos_bp, ceil(pos_bp/object.bead_size), occupancy_rate, false))
sort!(object.plus_ctcfs, :pos_bp, rev=true)
end
if orientation=="-"
push!(object.minus_ctcfs, (pos_bp, ceil(pos_bp/object.bead_size), occupancy_rate, false))
sort!(object.minus_ctcfs, :pos_bp, rev=false)
end
object
end
function delete_ctcf(object::Sim, pos_bp)
idx = indexin(pos_bp, object.plus_ctcfs.pos_bp)[1]
if idx != nothing
delete!(object.plus_ctcfs, [idx])
end
idx = indexin(pos_bp, object.minus_ctcfs.pos_bp)[1]
if idx != nothing
delete!(object.minus_ctcfs, [idx])
end
object
end
function advance(object::Sim)
occupied_positions = object.occupied_positions
for i in 1:length(object.cohesins)
coh = object.cohesins[i]
coh.alive += 1
if coh.alive >= coh.processivity
# println("Cohesin fell off!")
if coh.left_pos+1 != coh.right_pos
rem_edge!(object.bead_graph, coh.left_pos, coh.right_pos)
end
occupied_positions[coh.left_pos] = false
occupied_positions[coh.right_pos] = false
# Release CTCF (if any)
idx = indexin(coh.left_pos, object.plus_ctcfs.pos)[1]
if idx != nothing
object.plus_ctcfs.bound_cohesin[idx] = false
end
idx = indexin(coh.right_pos, object.minus_ctcfs.pos)[1]
if idx != nothing
object.minus_ctcfs.bound_cohesin[idx] = false
end
# Generate a new one (making sure it doesn't overlap an existing one)
collision = true
while (collision)
coh = Cohesin(object.processivity, object.polymer_length)
if (!occupied_positions[coh.left_pos])
collision = false
end
end
occupied_positions[coh.left_pos] = true
add_edge!(object.bead_graph, coh.left_pos, copy(coh.left_pos))
else
# LEFT
new_left = coh.left_pos - 1
if (!coh.left_ctcf_captured)
# Left side captured by a CTCF?
for idx in findall((object.plus_ctcfs.pos .== new_left) .& (object.plus_ctcfs.bound_cohesin .== false))
if rand(Float64) < object.plus_ctcfs.occupancy_rate[idx] * object.ctcf_capture_rate
# println("Captured!")
object.plus_ctcfs.bound_cohesin[idx] = true
coh.left_ctcf_captured = true
coh.processivity = object.processivity * object.ctcf_stabilization_factor
end
end
if (new_left>=1)
if (occupied_positions[new_left]==false)
coh.left_blocked = false
if coh.left_pos != coh.right_pos
occupied_positions[coh.left_pos] = false
end
if coh.left_pos+1 != coh.right_pos
rem_edge!(object.bead_graph, coh.left_pos, coh.right_pos)
end
add_edge!(object.bead_graph, new_left, coh.right_pos)
coh.left_pos = new_left
occupied_positions[new_left] = true
end
else
coh.left_blocked = true
end
end
# RIGHT
new_right = coh.right_pos + 1
if (!coh.right_ctcf_captured)
# Right side captured by a CTCF?
for idx in findall((object.minus_ctcfs.pos .== new_right) .& (object.minus_ctcfs.bound_cohesin .== false))
if rand(Float64) < object.minus_ctcfs.occupancy_rate[idx] * object.ctcf_capture_rate
# println("Captured!")
object.minus_ctcfs.bound_cohesin[idx] = true
coh.right_ctcf_captured = true
coh.processivity = object.processivity * object.ctcf_stabilization_factor
end
end
if (new_right<=object.polymer_length)
if (occupied_positions[new_right]==false)
coh.right_blocked = false
if coh.left_pos != coh.right_pos
occupied_positions[coh.right_pos] = false
end
if coh.left_pos+1 != coh.right_pos
rem_edge!(object.bead_graph, coh.left_pos, coh.right_pos)
end
add_edge!(object.bead_graph, coh.left_pos, new_right)
coh.right_pos = new_right
occupied_positions[new_right] = true
end
else
coh.right_blocked = true
end
end
if coh.right_pos - coh.left_pos >= 2
coh.extruded_beads[((coh.left_pos+1):(coh.right_pos-1))] .= true
end
end
object.cohesins[i] = coh
end
object.occupied_positions = occupied_positions
extruded_beads_sum = fill(false, object.polymer_length)
for coh in object.cohesins
extruded_beads_sum = extruded_beads_sum .| coh.extruded_beads
end
object.extruded_beads = extruded_beads_sum
object
end
function effective_dist(object::Sim)
mat = transpose(dijkstra_shortest_paths(object.bead_graph, 1).dists)
for i in 2:object.polymer_length
mat = vcat(mat, transpose(dijkstra_shortest_paths(object.bead_graph, i).dists))
end
mat
end
function plot_fiber(object::Sim, xmin=nothing, xmax=nothing, main=nothing)
x_list = [i for i in 1:object.polymer_length]
y_list = map(x -> (x ? 2 : 1), object.extruded_beads)
palette = ColorSchemes.Set1_9
color_list = [ColorSchemes.grays[1] for i in 1:object.polymer_length]
shape_list = [:circle for i in 1:object.polymer_length]
for i in 1:length(object.cohesins)
col = palette[i]
coh = object.cohesins[i]
color_list[coh.left_pos] = col
color_list[coh.right_pos] = col
if coh.left_blocked
shape_list[coh.left_pos] = :square
end
if coh.left_ctcf_captured
shape_list[coh.left_pos] = :rtriangle
end
if coh.right_blocked
shape_list[coh.right_pos] = :square
end
if coh.right_ctcf_captured
shape_list[coh.right_pos] = :ltriangle
end
end
if isnothing(xmin)
xmin = (-2)
end
if isnothing(xmax)
xmax = length(object.extruded_beads)+2
end
p = scatter(x_list, y_list, color=color_list, markershape=shape_list, label="", markersize=10, markerstrokewidth=0,
xlimits=(xmin,xmax), ylimits=(0,5))
for i in 1:length(object.cohesins)
col = palette[i]
coh = object.cohesins[i]
annotate!(mean([coh.left_pos, coh.right_pos]), 2.5+i*0.7,
text(string("Cohesin ", i, "\n(", coh.alive, " of ", coh.processivity, ")"), col, :center, 9))
end
if ((nrow(object.plus_ctcfs) + nrow(object.minus_ctcfs)) > 0 )
ctcf_plot_df = [object.plus_ctcfs; object.minus_ctcfs]
ctcf_plot_df.pch = [[">" for i in 1:nrow(object.plus_ctcfs)]; ["<" for i in 1:nrow(object.minus_ctcfs)]]
ctcf_plot_df = groupby(ctcf_plot_df, :pos)
ctcf_plot_df = combine(ctcf_plot_df, :pos, eachindex => :y, :pch)
for i in 1:nrow(ctcf_plot_df)
annotate!(ctcf_plot_df.pos[i], 0.25*ctcf_plot_df.y[i], text(ctcf_plot_df.pch[i], :center, 12))
end
end
p
end
default(size = (900, 300))
Random.seed!(1234)
object = Sim(2, 50e3, 10e3, 1000, 0.9, 4)
object = add_ctcf(object, "+", 15000, 1)
object = add_ctcf(object, "+", 15005, 1)
object = add_ctcf(object, "+", 15010, 1)
object = add_ctcf(object, "-", 30000, 1)
object = add_ctcf(object, "-", 30005, 1)
object = add_ctcf(object, "-", 30010, 1)
object
dists = effective_dist(object)
for i in 1:20000
# plot the first 20 steps in the simulation
if i<=20
display(plot_fiber(object))
end
advance(object)
dists = (dists + effective_dist(object))
end
dists = dists/20000
# Average effective distances between points over the duration of the simulation
dists
contacts = (dists.+10).^(-1)
plot(heatmap(dists[10:40,10:40]))
plot(dists[15,:])
plot(heatmap(log.(contacts[10:40,10:40])))