javascript
// DNA Replication Simulation in p5.js (global mode)
// Constants for DNA structure
const BASE_PAIR_SPACING = 10; // pixels between base pairs along the helix axis
const HELIX_RADIUS = 40; // radius of the DNA helix
const HELIX_TWIST = 36; // degrees per base pair (10 bp per turn => 360/10=36)
const MAX_BASE_PAIRS = 40; // length of the DNA strand
// Physics parameters (will be controlled by sliders)
let helicaseSpeedSlider, forkStabilitySlider, polymeraseSpeedSlider, frictionSlider;
let resetBtn, pauseBtn, stepBtn;
let paused = false;
let simulationTime = 0; // seconds
// DNA strands data
// Each base pair has two nucleotides: top and bottom strands
// We'll represent each nucleotide as a point on the helix
// The replication fork moves along the strand, separating the two strands
// State variables:
let forkPosition = 0; // position along base pairs where fork currently is (0 to MAX_BASE_PAIRS)
let forkVelocity = 0; // speed of fork movement (bp/s)
let forkAcceleration = 0; // acceleration (bp/s^2)
let polymeraseTopPos = 0; // polymerase progress on top strand (bp)
let polymeraseBottomPos = 0; // polymerase progress on bottom strand (bp)
let polymeraseSpeed = 0; // polymerase speed (bp/s)
let friction; // friction coefficient for damping
// Time tracking for smooth animation
let lastMillis = 0;
function setup() {
createCanvas(400, 400);
// Sliders
// Helicase speed: 0.5 to 5 bp/s, default 2 bp/s
helicaseSpeedSlider = createSlider(0.5, 5, 2, 0.1);
helicaseSpeedSlider.position(10, 420);
helicaseSpeedSlider.style('width', '180px');
// Fork stability (damping/friction): 0 to 1, default 0.3
forkStabilitySlider = createSlider(0, 1, 0.3, 0.01);
forkStabilitySlider.position(210, 420);
forkStabilitySlider.style('width', '180px');
// Polymerase speed: 0.5 to 5 bp/s, default 1.5 bp/s
polymeraseSpeedSlider = createSlider(0.5, 5, 1.5, 0.1);
polymeraseSpeedSlider.position(10, 450);
polymeraseSpeedSlider.style('width', '180px');
// Friction (damping for polymerase): 0 to 1, default 0.2
frictionSlider = createSlider(0, 1, 0.2, 0.01);
frictionSlider.position(210, 450);
frictionSlider.style('width', '180px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(10, 480);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause');
pauseBtn.position(80, 480);
pauseBtn.mousePressed(togglePause);
stepBtn = createButton('Step');
stepBtn.position(150, 480);
stepBtn.mousePressed(stepSim);
resetSim();
frameRate(60);
lastMillis = millis();
}
function draw() {
background(245);
// Calculate delta time in seconds, capped for stability
let now = millis();
let dt = (now - lastMillis) / 1000;
lastMillis = now;
dt = constrain(dt, 0, 0.05); // max 50ms step to avoid instability
// Read slider values
let helicaseSpeed = helicaseSpeedSlider.value(); // bp/s
let forkStability = forkStabilitySlider.value(); // damping/friction coefficient (0-1)
polymeraseSpeed = polymeraseSpeedSlider.value(); // bp/s
friction = frictionSlider.value(); // damping for polymerase (0-1)
if (!paused) {
simulationTime += dt;
// --- Update fork physics ---
// Fork tries to move forward at helicaseSpeed, but friction slows it down
// Simple physics: acceleration = (targetSpeed - currentSpeed) * stiffness - friction * velocity
// forkAcceleration units: bp/s^2
let targetSpeed = helicaseSpeed;
let stiffness = 10; // how strongly fork tries to reach helicaseSpeed
forkAcceleration = stiffness * (targetSpeed - forkVelocity) - forkStability * forkVelocity;
forkVelocity += forkAcceleration * dt;
// Update fork position
forkPosition += forkVelocity * dt;
// Clamp fork position to max base pairs
if (forkPosition > MAX_BASE_PAIRS) {
forkPosition = MAX_BASE_PAIRS;
forkVelocity = 0;
}
if (forkPosition < 0) {
forkPosition = 0;
forkVelocity = 0;
}
// --- Update polymerase progress ---
// Polymerase moves behind the fork, copying the separated strands
// Polymerase speed is controlled by slider, slowed by friction
// Polymerase cannot go beyond forkPosition (can't replicate ahead of fork)
// Polymerase velocity with friction damping
let polymeraseTargetSpeed = polymeraseSpeed;
// Simple velocity approach: polymerase tries to reach polymeraseSpeed, slowed by friction
// We'll just move polymerase forward smoothly but never beyond forkPosition
let polymeraseMaxPos = forkPosition;
// Move polymeraseTopPos towards polymeraseMaxPos at polymeraseSpeed with friction damping
let deltaTop = polymeraseMaxPos - polymeraseTopPos;
if (deltaTop > 0) {
// Move polymerase forward
let stepTop = polymeraseTargetSpeed * dt;
stepTop *= (1 - friction); // friction slows progress
polymeraseTopPos += min(stepTop, deltaTop);
}
// Same for bottom strand polymerase
let deltaBottom = polymeraseMaxPos - polymeraseBottomPos;
if (deltaBottom > 0) {
let stepBottom = polymeraseTargetSpeed * dt;
stepBottom *= (1 - friction);
polymeraseBottomPos += min(stepBottom, deltaBottom);
}
}
// --- Draw DNA helix and replication fork ---
push();
translate(width / 2, height / 2 + 30);
// Draw backbone strands and base pairs
strokeWeight(2);
// Draw full DNA strand (double helix)
for (let i = 0; i < MAX_BASE_PAIRS; i++) {
let angle = radians(i * HELIX_TWIST);
let y = -i * BASE_PAIR_SPACING;
// Positions of nucleotides on top and bottom strands
let xTop = HELIX_RADIUS * cos(angle);
let zTop = HELIX_RADIUS * sin(angle);
let xBottom = -xTop;
let zBottom = -zTop;
// Map z to brightness for 3D effect
let brightnessTop = map(zTop, -HELIX_RADIUS, HELIX_RADIUS, 100, 255);
let brightnessBottom = map(zBottom, -HELIX_RADIUS, HELIX_RADIUS, 100, 255);
// Backbone strands
stroke(0, brightnessTop, 150);
point(xTop, y);
stroke(150, brightnessBottom, 0);
point(xBottom, y);
// Base pairs as lines connecting top and bottom nucleotides
stroke(100, 100, 100, 150);
line(xTop, y, xBottom, y);
}
// Draw replication fork as a "Y" shape at forkPosition
// Fork separates the strands starting at forkPosition
// Calculate fork Y position
let forkY = -forkPosition * BASE_PAIR_SPACING;
// Draw fork arms separating strands
strokeWeight(4);
stroke(255, 100, 100);
// Left arm (top strand)
line(0, forkY, -HELIX_RADIUS * 1.5, forkY + BASE_PAIR_SPACING * 3);
// Right arm (bottom strand)
line(0, forkY, HELIX_RADIUS * 1.5, forkY + BASE_PAIR_SPACING * 3);
// Draw fork point
fill(255, 50, 50);
noStroke();
ellipse(0, forkY, 10, 10);
// Draw polymerase enzymes as circles moving behind fork on each strand
// Polymerase on top strand
let polyTopY = -polymeraseTopPos * BASE_PAIR_SPACING;
let polyTopAngle = radians(polymeraseTopPos * HELIX_TWIST);
let polyTopX = HELIX_RADIUS * cos(polyTopAngle);
fill(0, 200, 255);
stroke(0, 100, 200);
strokeWeight(2);
ellipse(polyTopX, polyTopY, 14, 14);
// Polymerase on bottom strand
let polyBottomY = -polymeraseBottomPos * BASE_PAIR_SPACING;
let polyBottomAngle = radians(polymeraseBottomPos * HELIX_TWIST);
let polyBottomX = -HELIX_RADIUS * cos(polyBottomAngle);
fill(0, 255, 150);
stroke(0, 150, 100);
ellipse(polyBottomX, polyBottomY, 14, 14);
// Labels for polymerases
noStroke();
fill(0);
textSize(10);
textAlign(CENTER, CENTER);
text('Pol', polyTopX, polyTopY - 12);
text('Pol', polyBottomX, polyBottomY - 12);
pop();
// --- Draw UI text ---
fill(0);
noStroke();
textSize(14);
textAlign(LEFT, CENTER);
text(`Helicase Speed (bp/s): ${helicaseSpeed.toFixed(2)}`, 10, 410);
text(`Fork Stability (damping): ${forkStability.toFixed(2)}`, 210, 410);
text(`Polymerase Speed (bp/s): ${polymeraseSpeed.toFixed(2)}`, 10, 440);
text(`Polymerase Friction: ${friction.toFixed(2)}`, 210, 440);
text(`Fork Position: ${forkPosition.toFixed(2)} bp`, 10, 380);
text(`Fork Velocity: ${forkVelocity.toFixed(2)} bp/s`, 10, 360);
text(`Polymerase Top Pos: ${polymeraseTopPos.toFixed(2)} bp`, 210, 380);
text(`Polymerase Bottom Pos: ${polymeraseBottomPos.toFixed(2)} bp`, 210, 360);
// Show simulation time
text(`Time: ${simulationTime.toFixed(2)} s`, 320, 20);
// Show pause/play button label
pauseBtn.html(paused ? 'Play' : 'Pause');
}
function resetSim() {
simulationTime = 0;
forkPosition = 0;
forkVelocity = 0;
forkAcceleration = 0;
polymeraseTopPos = 0;
polymeraseBottomPos = 0;
paused = false;
}
function togglePause() {
paused = !paused;
}
function stepSim() {
if (paused) {
// Advance simulation by fixed small timestep (e.g. 1/60 s)
let dt = 1 / 60;
// Use slider values
let helicaseSpeed = helicaseSpeedSlider.value();
let forkStability = forkStabilitySlider.value();
polymeraseSpeed = polymeraseSpeedSlider.value();
friction = frictionSlider.value();
simulationTime += dt;
// Update fork physics
let targetSpeed = helicaseSpeed;
let stiffness = 10;
forkAcceleration = stiffness * (targetSpeed - forkVelocity) - forkStability * forkVelocity;
forkVelocity += forkAcceleration * dt;
forkPosition += forkVelocity * dt;
forkPosition = constrain(forkPosition, 0, MAX_BASE_PAIRS);
if (forkPosition === MAX_BASE_PAIRS) forkVelocity = 0;
if (forkPosition === 0) forkVelocity = 0;
// Update polymerase progress
let polymeraseMaxPos = forkPosition;
let deltaTop = polymeraseMaxPos - polymeraseTopPos;
if (deltaTop > 0) {
let stepTop = polymeraseSpeed * dt * (1 - friction);
polymeraseTopPos += min(stepTop, deltaTop);
}
let deltaBottom = polymeraseMaxPos - polymeraseBottomPos;
if (deltaBottom > 0) {
let stepBottom = polymeraseSpeed * dt * (1 - friction);
polymeraseBottomPos += min(stepBottom, deltaBottom);
}
}
}