0% found this document useful (0 votes)
16 views7 pages

BBL736 Assignment

The document contains code for simulating a mutual repressor system using both deterministic and stochastic methods. It implements ordinary differential equations (ODEs) to model protein concentrations over time and compares results from Euler and RK4 integration methods, as well as Gillespie stochastic simulations. The visualizations illustrate the dynamics of the system under different initial conditions.

Uploaded by

Ravinder Kuhad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

BBL736 Assignment

The document contains code for simulating a mutual repressor system using both deterministic and stochastic methods. It implements ordinary differential equations (ODEs) to model protein concentrations over time and compares results from Euler and RK4 integration methods, as well as Gillespie stochastic simulations. The visualizations illustrate the dynamics of the system under different initial conditions.

Uploaded by

Ravinder Kuhad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BBL736 Assignment

Ravinder 2021BB10008
Shashank 2021BB10362
Pankaj Kumar 2021BB10350

Question 1:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

# Parameters
alpha = 10
n = 2

# Define the ODE system


def mutual_repressor(t, p):
p1, p2 = p
dp1_dt = alpha / (1 + p2**n) - p1
dp2_dt = alpha / (1 + p1**n) - p2
return [dp1_dt, dp2_dt]

# Time span for the simulation


t_span = (0, 20)
t_eval = np.linspace(t_span[0], t_span[1], 400)

# Case 1: p1(0) > p2(0)


initial_conditions1 = [5, 1]
sol1 = solve_ivp(mutual_repressor, t_span, initial_conditions1, t_eval=t_eval)

# Case 2: p1(0) < p2(0)


initial_conditions2 = [1, 5]
sol2 = solve_ivp(mutual_repressor, t_span, initial_conditions2, t_eval=t_eval)

# Plotting results
plt.figure(figsize=(10, 5))

# Plot case 1
plt.plot(sol1.t, sol1.y[0], label='p1 (IC: 5,1)', color='blue')
plt.plot(sol1.t, sol1.y[1], label='p2 (IC: 5,1)', color='red', linestyle='--')

# Plot case 2
plt.plot(sol2.t, sol2.y[0], label='p1 (IC: 1,5)', color='green')
plt.plot(sol2.t, sol2.y[1], label='p2 (IC: 1,5)', color='orange', linestyle='--')

plt.xlabel('Time')
plt.ylabel('Protein concentration')
plt.title('Time series for the mutual repressor system')
plt.legend()
plt.show()

def euler_integration(f, p0, t, dt):


p = np.zeros((len(t), len(p0)))
p[0] = p0
for i in range(1, len(t)):
p[i] = p[i-1] + dt * np.array(f(t[i-1], p[i-1]))
return p

dt = 0.05
t_euler = np.arange(t_span[0], t_span[1] + dt, dt)

# Euler for case 1


p_euler1 = euler_integration(mutual_repressor, initial_conditions1, t_euler, dt)
# Euler for case 2
p_euler2 = euler_integration(mutual_repressor, initial_conditions2, t_euler, dt)

plt.figure(figsize=(10, 5))

# Case 1 Euler
plt.plot(t_euler, p_euler1[:, 0], label='p1 Euler (IC: 5,1)', color='blue')
plt.plot(t_euler, p_euler1[:, 1], label='p2 Euler (IC: 5,1)', color='red',
linestyle='--')

# Case 2 Euler
plt.plot(t_euler, p_euler2[:, 0], label='p1 Euler (IC: 1,5)', color='green')
plt.plot(t_euler, p_euler2[:, 1], label='p2 Euler (IC: 1,5)', color='orange',
linestyle='--')

plt.xlabel('Time')
plt.ylabel('Protein concentration')
plt.title('Time series using Euler method')
plt.legend()
plt.show()

def rk4_integration(f, p0, t, dt):


p = np.zeros((len(t), len(p0)))
p[0] = p0
for i in range(1, len(t)):
k1 = np.array(f(t[i-1], p[i-1]))
k2 = np.array(f(t[i-1] + dt/2, p[i-1] + dt*k1/2))
k3 = np.array(f(t[i-1] + dt/2, p[i-1] + dt*k2/2))
k4 = np.array(f(t[i-1] + dt, p[i-1] + dt*k3))
p[i] = p[i-1] + dt*(k1 + 2*k2 + 2*k3 + k4)/6
return p

# RK4 for case 1


p_rk4_1 = rk4_integration(mutual_repressor, initial_conditions1, t_euler, dt)
# RK4 for case 2
p_rk4_2 = rk4_integration(mutual_repressor, initial_conditions2, t_euler, dt)

plt.figure(figsize=(10, 5))

# Case 1 RK4
plt.plot(t_euler, p_rk4_1[:, 0], label='p1 RK4 (IC: 5,1)', color='blue')
plt.plot(t_euler, p_rk4_1[:, 1], label='p2 RK4 (IC: 5,1)', color='red',
linestyle='--')

# Case 2 RK4
plt.plot(t_euler, p_rk4_2[:, 0], label='p1 RK4 (IC: 1,5)', color='green')
plt.plot(t_euler, p_rk4_2[:, 1], label='p2 RK4 (IC: 1,5)', color='orange',
linestyle='--')

plt.xlabel('Time')
plt.ylabel('Protein concentration')
plt.title('Time series using RK4 method')
plt.legend()
plt.show()
Question 2:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

# PARAMETERS
alpha = 10
n = 2
max_time = 50

def mutual_repressor(t, p):


p1, p2 = p
dp1_dt = alpha / (1 + p2**n) - p1
dp2_dt = alpha / (1 + p1**n) - p2
return [dp1_dt, dp2_dt]

t_span = (0, max_time)


t_eval = np.linspace(t_span[0], t_span[1], 400)

# Initial conditions for the two cases:


initial_conditions_det_case1 = [5, 1] # NP1 > NP2 initially
initial_conditions_det_case2 = [1, 5] # NP1 < NP2 initially

sol_det1 = solve_ivp(mutual_repressor, t_span, initial_conditions_det_case1,


t_eval=t_eval)
sol_det2 = solve_ivp(mutual_repressor, t_span, initial_conditions_det_case2,
t_eval=t_eval)

def gillespie_simulation(NP1_0, NP2_0, alpha, n, max_time):


t = 0.0
times = [t]
NP1 = NP1_0
NP2 = NP2_0
NP1_list = [NP1]
NP2_list = [NP2]
while t < max_time:
# Compute propensities for each reaction:
a1 = alpha / (1 + (NP2)**n) # production of NP1
a2 = NP1 # degradation of NP1
a3 = alpha / (1 + (NP1)**n) # production of NP2
a4 = NP2 # degradation of NP2
a0 = a1 + a2 + a3 + a4

if a0 == 0:
break

# Time step: sample dt from an exponential distribution


r1 = np.random.rand()
dt = -np.log(r1) / a0
t += dt

# Determine which reaction occurs:


r2 = np.random.rand() * a0
if r2 < a1:
NP1 += 1 # Production of NP1
elif r2 < a1 + a2:
NP1 = max(0, NP1 - 1) # Degradation of NP1
elif r2 < a1 + a2 + a3:
NP2 += 1 # Production of NP2
else:
NP2 = max(0, NP2 - 1) # Degradation of NP2

times.append(t)
NP1_list.append(NP1)
NP2_list.append(NP2)

return np.array(times), np.array(NP1_list), np.array(NP2_list)

times_stoch1, NP1_stoch1, NP2_stoch1 = gillespie_simulation(5, 1, alpha, n, max_time)


# Case 2: NP1(0) < NP2(0)
times_stoch2, NP1_stoch2, NP2_stoch2 = gillespie_simulation(1, 5, alpha, n, max_time)

plt.figure(figsize=(12, 6))
plt.plot(sol_det1.t, sol_det1.y[0], label='Deterministic p1 (IC: 5,1)', color='blue',
linewidth=2)
plt.plot(sol_det1.t, sol_det1.y[1], label='Deterministic p2 (IC: 5,1)', color='red',
linewidth=2, linestyle='--')
# Case 2: Deterministic
plt.plot(sol_det2.t, sol_det2.y[0], label='Deterministic p1 (IC: 1,5)', color='green',
linewidth=2)
plt.plot(sol_det2.t, sol_det2.y[1], label='Deterministic p2 (IC: 1,5)',
color='orange', linewidth=2, linestyle='--')

plt.step(times_stoch1, NP1_stoch1, where='post', label='Stochastic NP1 (IC: 5,1)',


color='blue', linestyle=':')
plt.step(times_stoch1, NP2_stoch1, where='post', label='Stochastic NP2 (IC: 5,1)',
color='red', linestyle=':')
plt.step(times_stoch2, NP1_stoch2, where='post', label='Stochastic NP1 (IC: 1,5)',
color='green', linestyle=':')
plt.step(times_stoch2, NP2_stoch2, where='post', label='Stochastic NP2 (IC: 1,5)',
color='orange', linestyle=':')

plt.xlabel('Time')
plt.ylabel('Protein number / concentration')
plt.title('Deterministic vs Stochastic (Gillespie) Simulations of the Mutual Repressor
System')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy