-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTanglesModel.jl
More file actions
executable file
·1523 lines (1363 loc) · 55.5 KB
/
TanglesModel.jl
File metadata and controls
executable file
·1523 lines (1363 loc) · 55.5 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__precompile__()
module TanglesModel
using Base: UInt32
using MultiScaleArrays: length
using DifferentialEquations
using MultiScaleArrays
using HDF5
using Interpolations
import FiniteDiff
module StochasticToggle
include("StochasticToggleModel.jl")
export generate_jump_problem
end
export mRNA_Parameters, DEFAULT_mRNA_PARAMS
export RNAP_Parameters, DEFAULT_RNAP_PARAMS, DNA_Parameters, DEFAULT_DNA_PARAMS
export SimulationParameters, DEFAULT_SIM_PARAMS
export LinearBoundaryParameters, CircularBoundaryParameters, UncoupledGene, MultiUncoupledGene, CoupledGene, MultiCoupledGene, DiscreteConfig
export simulate_full_examples, simulate_summarized_runs, simulate_discrete_runs, simulate_sc_rnap_dynamics
export OriginalTopoisomerase, IntragenicTopoisomerase, IntergenicTopoisomerase, NoTopoisomerase
export NoTorqueFunctionPerturbation, PositiveSupercoilingBuffering
export NoRNAPInitPerturbation, RNAPInitEnergyWell
function check_nonnegative(x, name)
if x < 0
error(name + " must be non-negative!")
end
end
function check_positive(x, name)
if x <= 0
error(name + " must be positive!")
end
end
struct mRNA_Parameters
drag_coeff::Float64 # (pN nm^(drag_exponent - 1)) The base drag (e.g. α in α (nascent length)^exp)
drag_exponent::Float64 # (unitless) The exponent in the drag equation.
function mRNA_Parameters(drag_coeff,drag_exponent)
check_positive(drag_coeff, "mRNA drag coefficient")
new(drag_coeff, drag_exponent)
end
end
DEFAULT_mRNA_PARAMS = mRNA_Parameters(1/20, 1)
struct RNAP_Parameters
radius::Float64 # (nm) The width of a RNA polymerase.
max_velocity::Float64 # (nm / s) The maximum speed of a RNA polymerase.
stall_torque::Float64 # (pN nm) The applied torque at which the RNA polymerase slows down and stops.
stall_width::Float64 # (pN nm) The width over which the polymerase stops, smoothing out the stall behavior.
function RNAP_Parameters(radius, max_velocity, stall_torque, stall_width)
check_nonnegative(radius, "RNAP radius")
check_nonnegative(max_velocity, "RNAP max velocity")
check_positive(stall_width, "Stall torque width")
new(radius, max_velocity, stall_torque, stall_width)
end
end
DEFAULT_RNAP_PARAMS = RNAP_Parameters(15, 20, 12, 3)
struct DNA_Parameters
applied_force::Float64 # (pN) The assumed constant applied force to the DNA.
twist_mobility::Float64 # (s pN nm) The DNA twist mobility.
bend_persistance_length::Float64 # (pN) The stat-mech distance over which bend motion is correlated.
twist_persistance_length::Float64 # (pN) The stat-mech distance over which twist motion is correlated.
plectoneme_twist_persistance_length::Float64 # (pN) The stat-mech distance over which plectonemic twist motion is correlated.
function DNA_Parameters(applied_force, twist_mobility, bend_plength, twist_plength, plectoneme_twist_plength)
check_positive(applied_force, "Applied force")
check_positive(twist_mobility, "Twist mobility")
check_positive(bend_plength, "Bend persistance length")
check_positive(twist_plength, "Twist persistance length")
check_positive(plectoneme_twist_plength, "Plectoneme twist persistance length")
new(applied_force, twist_mobility, bend_plength, twist_plength, plectoneme_twist_plength)
end
end
DEFAULT_DNA_PARAMS = DNA_Parameters(1, 10, 50, 95, 24)
abstract type TopoisomeraseType end
struct OriginalTopoisomerase <: TopoisomeraseType end
struct IntragenicTopoisomerase <: TopoisomeraseType end
struct IntergenicTopoisomerase <: TopoisomeraseType end
struct NoTopoisomerase <: TopoisomeraseType end
abstract type TorqueFunctionPerturbation end
struct NoTorqueFunctionPerturbation <: TorqueFunctionPerturbation end
struct PositiveSupercoilingBuffering <: TorqueFunctionPerturbation
# Adds a "buffer" against positive supercoiling, by extending
# the function in the positive domain bny a certain amount.
# Potentially useful for simulating chromatin fibers.
σ_buffer::Float64
end
abstract type RNAPInitPerturbation end
struct NoRNAPInitPerturbation <: RNAPInitPerturbation end
struct RNAPInitEnergyWell <: RNAPInitPerturbation
# Represents the case where there is a left and a right
# boundary, beyond which polymerases are incapable of
# initiating. Useful for representing R-loops.
left_boundary::Float64
right_boundary::Float64
end
struct SimulationParameters
mRNA_params::mRNA_Parameters
RNAP_params::RNAP_Parameters
DNA_params::DNA_Parameters
temperature::Float64 # (K) The temperature over which to run the simulation.
topoisomerase_rate::Float64 # (1 / sec) The base rate of topoisomerase activity.
mRNA_degradation_rate::Float64 # (1 / sec) The base rate of mRNA degradation.
sc_dependent::Bool # If supercoiling-dependent initiation is used
σ2_coeff::Float64 # The leading coefficient on the σ^2 term
topo_type::TopoisomeraseType
torque_perturbation::TorqueFunctionPerturbation # Any perturbations to the torque function
rnap_init_perturbation::RNAPInitPerturbation # Any perturbations to the RNAP initiation rate function
end
# Helper outer constructors that take none of the above
SimulationParameters(
mRNA_params::mRNA_Parameters, RNAP_params::RNAP_Parameters, DNA_params::DNA_Parameters,
temperature, topoisomerase_rate::Float64, mRNA_degradation_rate::Float64,
sc_dependent::Bool, σ2_coeff::Float64
) = SimulationParameters(mRNA_params, RNAP_params, DNA_params, temperature, topoisomerase_rate,
mRNA_degradation_rate, sc_dependent, σ2_coeff, OriginalTopoisomerase(),
NoTorqueFunctionPerturbation(), NoRNAPInitPerturbation()
)
# and that take one of the perturbations
SimulationParameters(
mRNA_params::mRNA_Parameters, RNAP_params::RNAP_Parameters, DNA_params::DNA_Parameters,
temperature, topoisomerase_rate::Float64, mRNA_degradation_rate::Float64,
sc_dependent::Bool, σ2_coeff::Float64, topo_type::TopoisomeraseType
) = SimulationParameters(mRNA_params, RNAP_params, DNA_params, temperature, topoisomerase_rate,
mRNA_degradation_rate, sc_dependent, σ2_coeff, topo_type,
NoTorqueFunctionPerturbation(), NoRNAPInitPerturbation()
)
SimulationParameters(
mRNA_params::mRNA_Parameters, RNAP_params::RNAP_Parameters, DNA_params::DNA_Parameters,
temperature, topoisomerase_rate::Float64, mRNA_degradation_rate::Float64,
sc_dependent::Bool, σ2_coeff::Float64, torque_perturbation::TorqueFunctionPerturbation
) = SimulationParameters(mRNA_params, RNAP_params, DNA_params, temperature, topoisomerase_rate,
mRNA_degradation_rate, sc_dependent, σ2_coeff, OriginalTopoisomerase(),
torque_perturbation, NoRNAPInitPerturbation()
)
SimulationParameters(
mRNA_params::mRNA_Parameters, RNAP_params::RNAP_Parameters, DNA_params::DNA_Parameters,
temperature, topoisomerase_rate::Float64, mRNA_degradation_rate::Float64,
sc_dependent::Bool, σ2_coeff::Float64, rnap_init_perturbation::RNAPInitPerturbation
) = SimulationParameters(mRNA_params, RNAP_params, DNA_params, temperature, topoisomerase_rate,
mRNA_degradation_rate, sc_dependent, σ2_coeff, OriginalTopoisomerase(),
NoTorqueFunctionPerturbation(), rnap_init_perturbation
)
DEFAULT_SIM_PARAMS = SimulationParameters(
DEFAULT_mRNA_PARAMS, DEFAULT_RNAP_PARAMS, DEFAULT_DNA_PARAMS,
298, 1 / 1200, 1 / 1200, false, 0.0
)
struct InternalParameters
k_b::Float64 # (pN nm / K) Boltzmann constant.
ω_0::Float64 # (1 / nm) Relaxed twist frequency.
A::Float64 # (nm) DNA bend persistance length.
C::Float64 # (nm) DNA twist persistance length.
P::Float64 # (nm) DNA plectoneme twist persistence length.
T::Float64 # (nm) Temperature.
f::Float64 # (pN) Constant force applied.
v_0::Float64 # (nm / s) Max speed of RNAP.
τ_c::Float64 # (pN nm) Stall torque.
τ_w::Float64 # (pN nm) Stall torque distribution width.
r_rnap::Float64 # (nm) RNAP radius.
χ::Float64 # (s pN nm) DNA twist mobility.
η::Float64 # (pN nm^(α - 1)) mRNA drag coefficient.
α::Float64 # (unitless) mRNA drag power-law exponent.
τ_0::Float64 # (pN nm) Intermediate torque value.
τ_s::Float64 # (pN nm) Critical stretched-phase torque.
τ_p::Float64 # (pN nm) Critical plectonemic torque.
σ_s::Float64 # (unitless) Critical stretched-phase supercoiling density.
σ_p::Float64 # (unitless) Critical plectonemic-phase supercoiling density.
topo_rate::Float64 # (1 / s) base topoisomerase activity rate
mRNA_deg_rate::Float64 # (1 / s) base mRNA degradation rate
sc_dependent::Bool # If supercoiling dependent initiation is used
σ2_coeff::Float64 # The leading coefficient on the σ^2 term
topo_type::TopoisomeraseType # the type of topoisomerase activity we want
torque_perturbation::TorqueFunctionPerturbation # Modifications to the torque function
rnap_init_perturbation::RNAPInitPerturbation # Modifications to the RNAP-initiation function
end
abstract type BoundaryParameters end
struct CircularBoundaryParameters <: BoundaryParameters
length::Float64
end
struct LinearBoundaryParameters <: BoundaryParameters
length::Float64
left_is_free::Bool
right_is_free::Bool
end
struct Promoter
position::Float64
base_rate::Float64
supercoiling_dependent::Bool
end
abstract type Gene end
struct UncoupledGene <: Gene
base_rate::Float64
idx::UInt32
start::Float64
terminate::Float64
end
struct MultiUncoupledGene <: Gene
base_rate::Float64
idxes::Array{UInt32,1}
start::Float64
terminate::Float64
end
struct CoupledGene <: Gene
base_rate::Float64
idx::UInt32
start::Float64
terminate::Float64
coupling_function
end
struct MultiCoupledGene <: Gene
base_rate::Float64
idxes::Array{UInt32,1}
start::Float64
terminate::Float64
coupling_function
end
struct DiscreteConfig
genes::Array{<:Gene}
n_other_discrete::Int64
discrete_reactions::Vector{Pair{Function, Vector{Pair{Int64, Int64}}}} # Pairs of the form (propensity_func(discrete, t) => Array{Pair{Int64, Int64}})
# where the second array of pairs is the stoichiometry
function DiscreteConfig(genes::Array{<:Gene})
new(genes, 0, [])
end
function DiscreteConfig(genes::Array{<:Gene}, n_other_discrete::Int64, discrete_reactions::Vector{Pair{Function, Vector{Pair{Int64,Int64}}}})
n_genes = length(genes)
for (_, stoich_coeffs) in discrete_reactions
for (species_id, stoich_coeff) in stoich_coeffs
if species_id < 1 || species_id > (n_genes + n_other_discrete)
error("Invalid reaction passed in discrete_reactions!\n\tid:" + species_id + "\n\tstoich coeff:", stoich_coeff)
end
end
end
new(genes, n_other_discrete, discrete_reactions)
end
end
function InternalParameters(sim_params::SimulationParameters)
# Computes intermediate values, initializing a InternalParameters struct
# from the better-documented SimulationParameters object.
k_b = 1380649 / 100000000 # pN nm / K
ω_0 = 1.85 # 1 / nm
T = sim_params.temperature
f = sim_params.DNA_params.applied_force
A = sim_params.DNA_params.bend_persistance_length
C = sim_params.DNA_params.twist_persistance_length
P = sim_params.DNA_params.plectoneme_twist_persistance_length
# Compute intermediates
c = k_b * T * C * ω_0^2
p = k_b * T * P * ω_0^2
g = f - sqrt(k_b * T * f / A)
cs = c * (1 - C / (4 * A) * sqrt(k_b * T / (A * f)))
# Compute critical values
τ_s = cs / ω_0
τ_0 = sqrt(2 * p * g / (ω_0^2 * (1 - p / cs)))
τ_p = p / ω_0
σ_s = 1 / cs * sqrt(2 * p * g / (1 - p / cs))
σ_p = 1 / p * sqrt(2 * p * g / (1 - p / cs))
InternalParameters(
1380649 / 100000000, # k_b
1.85, # ω_0
sim_params.DNA_params.bend_persistance_length, # A
sim_params.DNA_params.twist_persistance_length, # C
sim_params.DNA_params.plectoneme_twist_persistance_length, # P
sim_params.temperature, # T
sim_params.DNA_params.applied_force, # f
sim_params.RNAP_params.max_velocity, # v_0
sim_params.RNAP_params.stall_torque, # τ_c
sim_params.RNAP_params.stall_width, # τ_w
sim_params.RNAP_params.radius, # r_rnap
sim_params.DNA_params.twist_mobility, # χ
sim_params.mRNA_params.drag_coeff, # η
sim_params.mRNA_params.drag_exponent, # α
τ_0,
τ_s,
τ_p,
σ_s,
σ_p,
sim_params.topoisomerase_rate,
sim_params.mRNA_degradation_rate,
sim_params.sc_dependent,
sim_params.σ2_coeff,
sim_params.topo_type,
sim_params.torque_perturbation,
sim_params.rnap_init_perturbation
)
end
mutable struct TanglesArray <: DEDataArray{Float64,1}
x::Array{Float64,1}
discrete_components::Array{Int32,1}
polymerase_direction::Array{Int64, 1}
polymerase_stop::Array{Float64,1}
polymerase_gene::Array{Array{UInt32,1}}
end
function get_sc_region(position::Float64, u::TanglesArray)::Int64
loc::Int64 = 1
num_polymerases::Int64 = convert(UInt32, (length(u) - 1) / 3)
x = @view u.x[1:3:(end-1)]
while loc <= num_polymerases && x[loc] < position
loc += 1
end
return loc
end
function interp_twist(position::Float64, u::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, bcs::BoundaryParameters, ω0::Float64)
return interp_twist(position, u.u, bcs, ω0)
end
function interp_twist(position::Float64, u::TanglesArray, bcs::LinearBoundaryParameters, ω0::Float64)
# Returns (insert_idx, insert_twist)
num_polymerases::Int64 = convert(UInt32, (length(u) - 1) / 3)
if num_polymerases == 0
return (1, 0.0)
end
x = @view u.x[1:3:(end-1)]
ϕ = @view u.x[2:3:(end-1)]
insert_location::Int64 = get_sc_region(position, u)
σ_target = supercoiling_density(u, insert_location, bcs, ω0)
# σ = Δϕ / (Δx (-ω0)), so Δϕ = -ω0 σ Δx
if insert_location == 1
# Special case, depends on BCs
if bcs.left_is_free
return (insert_location, ϕ[insert_location])
end
return (insert_location, σ_target * -ω0 * position)
end
if insert_location == num_polymerases && bcs.right_is_free
# Special case, no twist needed on free right end
return (insert_location, ϕ[insert_location])
end
# Calculate distance to previous polymerase and use that to set twist.
return (insert_location,
σ_target * -ω0 * (position - x[insert_location - 1]) + ϕ[insert_location - 1])
end
function interp_twist(position::Float64, u::TanglesArray, bcs::CircularBoundaryParameters, ω0::Float64)
# Returns (insert_idx, insert_twist)
num_polymerases::Int64 = convert(UInt32, (length(u) - 1) / 3)
if num_polymerases == 0
return (1, 0.0)
end
x = @view u.x[1:3:(end-1)]
ϕ = @view u.x[2:3:(end-1)]
insert_location::Int64 = get_sc_region(position, u)
σ_target = supercoiling_density(u, insert_location, bcs, ω0)
# σ = Δϕ / (Δx (-ω0)), so Δϕ = -ω0 σ Δx
if insert_location == 1
# Here, calculate this by the distance to the last polymerase.
return (insert_location,
ϕ[num_polymerases] + -ω0 * σ_target * (position + bcs.length - x[num_polymerases]))
end
return (insert_location,
σ_target * -ω0 * (position - x[insert_location - 1]) + ϕ[insert_location - 1])
end
function supercoiling_density(
u::TanglesArray,
i::Int64,
bcs::CircularBoundaryParameters,
ω0::Float64)::Float64
if length(u.x) == 1
return 0
end
# Computes supercoiling density in N regions, accounting for the circular behavior
# of the system by duplicating the first region density into the N + 1 slot used later.
num_rnap = convert(Int32, (length(u.x) - 1) / 3)
x = @view u[1:3:(end-1)]
ϕ = @view u[2:3:(end-1)]
if i == 1 || i == num_rnap + 1
return (ϕ[1] - ϕ[end]) / (x[1] + bcs.length - x[end]) / -ω0
end
return (ϕ[i] - ϕ[i - 1]) / (x[i] - x[i - 1]) / -ω0
end
function supercoiling_density(
u::TanglesArray,
i::Int64,
bcs::LinearBoundaryParameters,
ω0::Float64)::Float64
# Computes supercoiling density in N+1 regions, allowing for free or fixed left/right
# boundary conditions
if length(u.x) == 1
return 0
end
num_rnap = convert(Int32, (length(u.x) - 1) / 3)
x = @view u[1:3:(end-1)]
ϕ = @view u[2:3:(end-1)]
if i == 1
if bcs.left_is_free
return 0
else
return ϕ[1] / (x[1] * -ω0)
end
elseif i == num_rnap + 1
if bcs.right_is_free
return 0
else
return -ϕ[end] / (bcs.length - x[end]) / -ω0
end
end
return (ϕ[i] - ϕ[i - 1]) / (x[i] - x[i - 1]) / -ω0
end
struct TanglesParams
sim_params::InternalParameters
bc_params::BoundaryParameters
end
function internal_torque_response(σ::Float64, params::TanglesParams)
abs_σ = abs(σ)
# Three regime torque region, from:
# 0 <= |σ| < σ_s => σ τ_s
# σ_s <= |σ| < σ_p => sgn(σ) τ_0 -- phase transition regime
# σ_p <= |σ| => σ τ_p
if abs_σ < params.sim_params.σ_s
return params.sim_params.τ_s * σ
elseif abs_σ < params.sim_params.σ_p
return params.sim_params.τ_0 * sign(σ)
end
return params.sim_params.τ_p * σ
end
function internal_torque_response(σ::Float64, params::TanglesParams,
_::NoTorqueFunctionPerturbation
)
return internal_torque_response(σ, params)
end
function internal_torque_response(σ::Float64, params::TanglesParams,
torque_perturb::PositiveSupercoilingBuffering
)
# Extend the "sigma = 0" range to the right, until we "consume"
# all of the σ buffer.
if σ >= 0
if σ <=torque_perturb.σ_buffer
return internal_torque_response(0.0, params)
end
return internal_torque_response(σ - torque_perturb.σ_buffer, params)
end
return internal_torque_response(σ, params)
end
function torque_response(σ::Float64, params::TanglesParams)::Float64
internal_torque_response(σ, params, params.sim_params.torque_perturbation)
end
function polymerase_velocity(σ_b::Float64, σ_f::Float64, params::TanglesParams)
# Compute the polymerase velocity given neighboring supercoiling densities.
# τ_s = stall torque, τ_w = stall torque width
stall_behind = (abs(torque_response(σ_b, params)) - params.sim_params.τ_s) / params.sim_params.τ_w
stall_ahead = (abs(torque_response(σ_f, params)) - params.sim_params.τ_s) / params.sim_params.τ_w
# Restrict exponential argument to 10 (e.g. truncate the stall) to prevent overflows.
return params.sim_params.v_0 / (
(1 + exp(min(10.0, stall_behind)))
* (1 + exp(min(10.0, stall_ahead)))
)
end
function polymerase_termination_check(u::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, t, integrator)
return polymerase_termination_check(u.u, t, integrator)
end
function polymerase_termination_check(u::TanglesArray, t, integrator)
if length(u) > 1
return min(((u.polymerase_stop - u[1:3:(end-1)]) .* u.polymerase_direction)...)
end
# Never trigger polymerase termination
return 1
end
function terminate!(c::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, idx::UInt32)
terminate!(c.u, idx)
end
function terminate!(c::TanglesArray, idx::UInt32)
remove_idx = (1 + ((idx - 1) * 3)):(idx * 3)
if remove_idx[end] > length(c)
@warn "Data array is too small!"
else
for i in c.polymerase_gene[idx]
c.discrete_components[i] += 1
end
deleteat!(c.x, remove_idx)
deleteat!(c.polymerase_direction, idx)
deleteat!(c.polymerase_stop, idx)
deleteat!(c.polymerase_gene, idx)
end
end
function _destroy_mRNA!(c::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, idx::UInt32)
_destroy_mRNA!(c.u, idx)
end
function _destroy_mRNA!(c::TanglesArray, idx::UInt32)
c.discrete_components[idx] -= 1
end
function degrade_mRNA!(integrator, idx::UInt32)
for c in full_cache(integrator)
_destroy_mRNA!(c, idx)
end
end
function _update_discrete!(c::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, updates::Array{Pair{Int64, Int64}})
_update_discrete!(c.u, updates)
end
function _update_discrete!(c::TanglesArray, updates::Array{Pair{Int64, Int64}})
for (species_id, update_amount) in updates
c.discrete_components[species_id] += update_amount
end
end
function update_discrete!(integrator, updates::Array{Pair{Int64, Int64}})
for c in full_cache(integrator)
_update_discrete!(c, updates)
end
end
function FiniteDiff.resize!(c::TanglesArray, i::Int64)
resize!(c.x, i)
end
function extend_rnap!(c::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, position::Float64, insert_idx::UInt32, twist::Float64, genes::Array{UInt32,1}, terminate_end::Float64)
extend_rnap!(c.u, position, insert_idx, twist, genes, terminate_end)
end
function extend_rnap!(c::TanglesArray, position::Float64, insert_idx::UInt32, twist::Float64, genes::Array{UInt32,1}, terminate_end::Float64)
# Insert items in reverse order
insert!(c.x,1 + 3 * (insert_idx - 1), 0.0)
insert!(c.x,1 + 3 * (insert_idx - 1), twist)
insert!(c.x,1 + 3 * (insert_idx - 1), position)
insert!(c.polymerase_direction, insert_idx, terminate_end > position ? 1 : -1)
insert!(c.polymerase_stop, insert_idx, terminate_end)
insert!(c.polymerase_gene, insert_idx, genes)
end
function add_polymerase!(integrator, position::Float64, genes::Array{UInt32,1}, terminate_end::Float64)
#print("Attempting to add polymerase. Current u:")
#println(integrator.u)
insert_idx::UInt32, twist::Float64 = interp_twist(position, integrator.u, integrator.p.bc_params, integrator.p.sim_params.ω_0)
#println("Adding to full_cache vars")
for c in full_cache(integrator)
extend_rnap!(c, position, insert_idx, twist, genes, terminate_end)
end
#println("Printing new cache...")
#for c in full_cache(integrator)
# println(c)
#end
#println("Done adding!")
end
function mRNA_degradation_rate(u::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, p::TanglesParams, t, idx::UInt32)
mRNA_degradation_rate(u.u, p, t, idx)
end
function mRNA_degradation_rate(u::TanglesArray, p::TanglesParams, t, idx::UInt32)
return p.sim_params.mRNA_deg_rate * u.discrete_components[idx]
end
function polymerase_initiation_rate(u::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, p::TanglesParams, t, promoter::Promoter, coupling_func)::Float64
return polymerase_initiation_rate(u.u, p, t, promoter, coupling_func)
end
function base_polymerase_init_energy(σ::Float64, p::TanglesParams)
energy::Float64 = (
torque_response(σ, p)
+ p.sim_params.σ2_coeff * (σ / p.sim_params.σ_s)^2 * p.sim_params.τ_0) * 1.2 * 2.0 * π
return energy
end
function polymerase_init_energy(σ::Float64, p::TanglesParams, _::NoRNAPInitPerturbation)
base_polymerase_init_energy(σ, p)
end
function polymerase_init_energy(σ::Float64, p::TanglesParams, rnap_perturb::RNAPInitEnergyWell)
if σ < rnap_perturb.left_boundary || σ > rnap_perturb.right_boundary
# Return a very high (but still finite, to prevent exponential explosions) well boundary
# to approximate infinite walls
return p.sim_params.k_b * p.sim_params.T * 1000.0
end
return base_polymerase_init_energy(σ, p)
end
function polymerase_initiation_rate(u::TanglesArray, p::TanglesParams, t, promoter::Promoter, coupling_func)::Float64
# Initiation rate is zero if initiation site is occupied
if length(u.x) == 1
return promoter.base_rate * coupling_func(u.discrete_components, t)
end
if minimum(abs,u.x[1:3:end-1] .- promoter.position) < p.sim_params.r_rnap * 2
return 0.0
end
σ::Float64 = supercoiling_density(u, get_sc_region(promoter.position, u), p.bc_params, p.sim_params.ω_0)
energy::Float64 = polymerase_init_energy(σ, p, p.sim_params.rnap_init_perturbation)
sc_rate_factor::Float64 = min(50.0,exp(-energy / (p.sim_params.k_b * p.sim_params.T)))
return promoter.base_rate * coupling_func(u.discrete_components, t) * (promoter.supercoiling_dependent ? sc_rate_factor : 1.0)
end
function generate_jump(gene::UncoupledGene, sc_dependent::Bool)::VariableRateJump
return VariableRateJump(
(u,p,t) -> polymerase_initiation_rate(
u,p,t,
Promoter(gene.start,gene.base_rate,sc_dependent),(mRNA, t)->1.0),
(int) -> add_polymerase!(int, gene.start, [gene.idx], gene.terminate))
end
function generate_jump(gene::MultiUncoupledGene, sc_dependent::Bool)::VariableRateJump
return VariableRateJump(
(u,p,t) -> polymerase_initiation_rate(
u,p,t,
Promoter(gene.start,gene.base_rate,sc_dependent),(mRNA, t)->1.0),
(int) -> add_polymerase!(int, gene.start, gene.idxes, gene.terminate))
end
function generate_jump(gene::CoupledGene, sc_dependent::Bool)::VariableRateJump
return VariableRateJump(
(u,p,t) -> polymerase_initiation_rate(
u,p,t,
Promoter(gene.start,gene.base_rate,sc_dependent),gene.coupling_function),
(int) -> add_polymerase!(int, gene.start, [gene.idx], gene.terminate))
end
function generate_jump(gene::MultiCoupledGene, sc_dependent::Bool)::VariableRateJump
return VariableRateJump(
(u,p,t) -> polymerase_initiation_rate(
u,p,t,
Promoter(gene.start,gene.base_rate,sc_dependent),gene.coupling_function),
(int) -> add_polymerase!(int, gene.start, gene.idxes, gene.terminate))
end
function terminate_polymerase!(integrator)
# Identify polymerase to be terminated
#print("Attempting to terminate. Current u:")
#println(integrator.u)
p_idx::UInt32 = findmin(abs.(integrator.u.u[1:3:(end-1)] - integrator.u.u.polymerase_stop))[2]
# Update gene lists
remove_idx = (1 + ((p_idx - 1) * 3)):(p_idx * 3)
#println("Deleting full_cache vars")
# Update all internal caches
for c in full_cache(integrator)
terminate!(c, p_idx)
end
#println("Deleting non-user cache")
deleteat_non_user_cache!(integrator,remove_idx)
#println("Printing remaining cache...")
#for c in full_cache(integrator)
# println(c)
#end
#println("Done terminating")
end
function internal_relax_supercoiling!(u::TanglesArray, n_rnap::Int64, start_idx::Int64, end_idx::Int64, bcs::CircularBoundaryParameters)
x = @view u.x[1:3:end-1]
ϕ = @view u.x[2:3:end-1]
# If we cover all polymerases, relax all:
if start_idx == 1 && end_idx == n_rnap
ϕ[start_idx:end_idx] .= 0
return
end
# Handle the edge cases properly by setting the x "boundaries"
if start_idx == 1
x_lower = x[end] - bcs.length
ϕ_lower = ϕ[end]
x_upper = x[end_idx + 1]
ϕ_upper = ϕ[end_idx + 1]
elseif end_idx == n_rnap
x_lower = x[start_idx - 1]
ϕ_lower = ϕ[start_idx - 1]
x_upper = bcs.length + x[1]
ϕ_upper = ϕ[1]
else
x_lower = x[start_idx - 1]
ϕ_lower = ϕ[start_idx - 1]
x_upper = x[end_idx + 1]
ϕ_upper = ϕ[end_idx + 1]
end
for i in start_idx:end_idx
α = (x_upper - x[i]) / (x_upper - x_lower)
ϕ[i] = (ϕ_lower * α) + (ϕ_upper * (1 - α))
end
end
function internal_relax_supercoiling!(u::TanglesArray, n_rnap::Int64, start_idx::Int64, end_idx::Int64, bcs::LinearBoundaryParameters)
x = @view u.x[1:3:end-1]
ϕ = @view u.x[2:3:end-1]
# Handle special cases
# Relax everything if either we cover all polymerases...
if start_idx == 1 && end_idx == n_rnap
ϕ[start_idx:end_idx] .= 0
return
end
# or if we are at the edges and that edge is free
if (start_idx == 1 && bcs.left_is_free) || (start_idx == n_rnap && bcs.right_is_free)
ϕ[start_idx:end_idx] .= 0
return
end
if start_idx == 1
# We have at least one polymerase to the right to interpolate with
for i in start_idx:end_idx
ϕ[i] = ϕ[end_idx + 1] * x[i] / x[end_idx + 1]
end
return
end
if end_idx == n_rnap
# Interpolate from the left
for i in start_idx:end_idx
ϕ[i] = ϕ[start_idx - 1] * (bcs.length - x[i]) / (bcs.length - x[start_idx - 1])
end
return
end
# Otherwise, interpolate between left and right values
for i in start_idx:end_idx
α = (x[end_idx + 1] - x[i]) / (x[end_idx + 1] - x[start_idx - 1])
ϕ[i] = (ϕ[start_idx - 1] * α) + (ϕ[end_idx + 1] * (1 - α))
end
end
function relax_supercoiling!(u::ExtendedJumpArray{Float64, 1, TanglesArray, Vector{Float64}}, left::Float64, right::Float64, bcs::BoundaryParameters)
relax_supercoiling!(u.u, left, right, bcs)
end
function relax_supercoiling!(u::TanglesArray, left::Float64, right::Float64, bcs::BoundaryParameters)
num_polymerases::Int64 = convert(UInt32, (length(u) - 1) / 3)
if num_polymerases == 0
return
end
x = @view u.x[1:3:end-1]
ϕ = @view u.x[2:3:end-1]
start_idx = 1
end_idx = 1
for i in 1:num_polymerases
rnap_pos::Float64 = u.x[1 + 3 * (start_idx - 1)]
if rnap_pos < left
start_idx += 1
end
if rnap_pos < right
end_idx += 1
end
end
# Correct for the off-by-one error
end_idx -= 1
internal_relax_supercoiling!(u, num_polymerases, start_idx,end_idx, bcs)
end
function relax_supercoiling!(integrator, left::Float64, right::Float64)
for c in full_cache(integrator)
relax_supercoiling!(c, left, right, integrator.p.bc_params)
end
end
function tangles_derivatives!(du, u::TanglesArray, params::TanglesParams, t)
# Arguments
# ---------
# du: preallocated, mutable derivative array.
# u: A (3N + 1,) vector encoding (location, phi, mRNA_length)
# for each polymerase. The first variable is a dummy variable that does not change,
# but is needed to support the zero polymerase case.
# p: named_tuple
# A tuple containing simulation parameters. In particular, it contains:
# sim_params: a SimulationParameters object
# bc_params: a BoundaryParameters object
ns = 3 # number of states
num_polymerases::Int64 = convert(Int64, (length(u) - 1) / ns)
# Unpack useful constants
ω0 = params.sim_params.ω_0
α = params.sim_params.α
η = params.sim_params.η
χ = params.sim_params.χ
for i = 1:num_polymerases
x = u[1 + (ns * (i - 1))]
ϕ = u[2 + (ns * (i - 1))]
z = u[3 + (ns * (i - 1))]
σ_b = supercoiling_density(u, i, params.bc_params, params.sim_params.ω_0)
σ_f = supercoiling_density(u, i + 1, params.bc_params, params.sim_params.ω_0)
# Compute polymerase direction and velocity
v = u.polymerase_direction[i] * polymerase_velocity(σ_b, σ_f, params)
τ = torque_response(σ_f, params) - torque_response(σ_b,params)
drag = η * z^α
dϕ = drag * v * ω0 / (χ + drag) .- τ / (χ + drag)
du[1 + (ns * (i - 1))] = v
du[2 + (ns * (i - 1))] = dϕ
du[3 + (ns * (i - 1))] = abs(v)
end
du[end] = 0.0
end
function out_of_domain(u, p, t)
if length(u.u) == 1
return false
end
return any(diff(u.u[1:3:end-1]) .< 0)
end
function calculate_topo_jumps(_::Array{Gene}, _::SimulationParameters, _::NoTopoisomerase)
return []
end
function calculate_topo_jumps(sorted_genes::Array{Gene}, sim_params::SimulationParameters, _::OriginalTopoisomerase)
# Duplicate gene if there is only one
if length(sorted_genes) == 1
push!(sorted_genes, sorted_genes[1])
end
topo_jumps = [
# Foreach pair of adjacent genes...
ConstantRateJump(
# each has an equal probability of relaxing
(u,p,t)->sim_params.topoisomerase_rate / (length(sorted_genes) - 1.0),
# when a pair is selected, relax all polymerases that are located between...
(int)->relax_supercoiling!(int,
# the first gene's left-most extent
min(sorted_genes[i].start, sorted_genes[i].terminate),
# and the second gene's left-most extent (!)
min(sorted_genes[i+1].start, sorted_genes[i+1].terminate)))
for i in 1:(length(sorted_genes)-1)]
# ex: A/B pair selected
# ------AAAAA----BBBBBB-----CCCCCCC------
# ^ ^
# |--------|
return topo_jumps
end
function calculate_topo_jumps(sorted_genes::Array{Gene}, sim_params::SimulationParameters, _::IntragenicTopoisomerase)
topo_jumps = [
# Foreach gene..
ConstantRateJump(
# each has an equal probability of relaxing
(u,p,t)->sim_params.topoisomerase_rate / length(sorted_genes),
# when a gene is selected, relax all polymerases that are located between...
(int)->relax_supercoiling!(int,
# the gene's left-most extent
min(sorted_genes[i].start, sorted_genes[i].terminate),
# and the gene's right-most extent
max(sorted_genes[i].start, sorted_genes[i].terminate)))
for i in 1:length(sorted_genes)]
# ex: B selected
# ------AAAAA----BBBBBB-----CCCCCCC------
# ^ ^
# |----|
return topo_jumps
end
function calculate_topo_jumps(sorted_genes::Array{Gene}, sim_params::SimulationParameters, _::IntergenicTopoisomerase)
# Duplicate gene if there is only one
if length(sorted_genes) == 1
push!(sorted_genes, sorted_genes[1])
end
topo_jumps = [
# Foreach pair of adjacent genes...
ConstantRateJump(
# each has an equal probability of relaxing
(u,p,t)->sim_params.topoisomerase_rate / (length(sorted_genes) - 1.0),
# when a pair is selected, relax all polymerases that are located between...
(int)->relax_supercoiling!(int,
# the first gene's left-most extent
min(sorted_genes[i].start, sorted_genes[i].terminate),
# and the second gene's right-most extent
max(sorted_genes[i+1].start, sorted_genes[i+1].terminate)))
for i in 1:(length(sorted_genes)-1)]
# ex: A/B pair selected
# ------AAAAA----BBBBBB-----CCCCCCC------
# ^ ^
# |-------------|
return topo_jumps
end
function build_problem(
sim_params::SimulationParameters,
bcs::BoundaryParameters,
dconfig::DiscreteConfig,
t_end::Float64;
tsteps::Int64=-1)
build_problem(sim_params, bcs, dconfig, t_end, zeros(Int32,length(dconfig.genes)+ dconfig.n_other_discrete), tsteps=tsteps)
end
function build_problem(
sim_params::SimulationParameters,
bcs::BoundaryParameters,
dconfig::DiscreteConfig,
t_end::Float64,
ics_discrete::Array{Int32,1};
tsteps::Int64=-1)
extra_save_positions = (tsteps == -1 ? (true,true) : (false,false))
extra_tstops = (tsteps == -1 ? [] : range(0.0, t_end, length=tsteps))
u0 = TanglesArray([0.0], ics_discrete, [], [], [])
problem = ODEProblem(tangles_derivatives!, u0, [0.0, t_end], TanglesParams(
InternalParameters(sim_params), bcs))
termination_callback = ContinuousCallback(polymerase_termination_check, terminate_polymerase!,
save_positions=extra_save_positions)
# Calculate intergenic regions
sorted_genes::Array{Gene} = sort(dconfig.genes, by=(g::Gene)->min(g.start, g.terminate))
topo_jumps = calculate_topo_jumps(sorted_genes, sim_params, sim_params.topo_type)
jump_problem = JumpProblem(problem, Direct(),
generate_jump.(dconfig.genes, sim_params.sc_dependent)...,
topo_jumps...,
[VariableRateJump(
(u,p,t)->mRNA_degradation_rate(u,p,t,convert(UInt32,i)),
(int)->degrade_mRNA!(int, convert(UInt32,i))) for i in 1:length(dconfig.genes)]...,
[VariableRateJump(
(u,_,t)-> convert(Float64, propensity(u.u.discrete_components, t)),
(int)->update_discrete!(int, updates)
)
for (propensity, updates) in dconfig.discrete_reactions]...
,save_positions=extra_save_positions)
return () -> solve(jump_problem, Tsit5(), callback=termination_callback, maxiters=1e6, dtmax=10, isoutofdomain=out_of_domain,
saveat=extra_tstops, tstops=extra_tstops)
end
function write_bcs(group::HDF5.Group, bcs::LinearBoundaryParameters)
attributes(group)["bcs.is_circular"] = 0
attributes(group)["bcs.length"] = bcs.length
attributes(group)["bcs.left_free"] = bcs.left_is_free
attributes(group)["bcs.right_free"] = bcs.right_is_free
end
function write_bcs(group::HDF5.Group, bcs::CircularBoundaryParameters)
attributes(group)["bcs.is_circular"] = 1
attributes(group)["bcs.length"] = bcs.length
end
function write_topo_type(
group::HDF5.Group,
_::NoTopoisomerase
)
attributes(group)["topoisomerase_type"] = "none"
end
function write_topo_type(
group::HDF5.Group,