Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created April 22, 2020 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfsone/cb4c41a58f89e84db76debac9fab8239 to your computer and use it in GitHub Desktop.
Save kfsone/cb4c41a58f89e84db76debac9fab8239 to your computer and use it in GitHub Desktop.
import random
# time
t = 0
# how likely is replication to be successful
replication_success_ratio = 0.7
# estimate time for rna in a cell to reach a replication center,
# we'll also repurpose it as how long it takes a viable strand to
# start itself replicating (which is generous),
# and how long the virions in a dead cell take to find new cells.
replication_cycle = 3 * 60
# how many virions from a dead cell are viable and find another
viable_escape_ratio = .2
# how many virions from a dead cell find a unique cell:
unique_cell_arrivals = .3
# how many virions in a cell causes it to die?
lethal_population = 400
class Cell:
def __init__(self, live_virions):
self.live_virions = live_virions
self.dead = False
def cycle(self):
if random.random() <= replication_success_ratio:
self.live_virions += max(int(self.live_virions * replication_success_ratio), 1)
self.dead = self.live_virions >= lethal_population
if self.dead:
self.live_virions = int(self.live_virions * viable_escape_ratio)
return not self.dead
class Body:
# track live cells that are infected
infected_cells = None
# track cells that have died whose virions need to find new homes
dead_cells = []
# counters
cells_died = 0
active_virions = 0
transit_virions = 0
def cycle(self):
# infect new cells from the died cells
if self.infected_cells is None:
self.infected_cells = [Cell(1)]
else:
new_infections = []
new_cells = []
for cell in self.dead_cells:
escapees = cell.live_virions
for i in range(1, 5):
infecting = max(int(escapees * unique_cell_arrivals), 1)
new_cells.extend([Cell(i) for _ in range(infecting)])
escapees -= infecting
self.dead_cells = [c for c in self.infected_cells if not c.cycle()]
self.cells_died += len(self.dead_cells)
self.infected_cells[:] = [c for c in self.infected_cells if not c.dead] + new_cells
self.active_virions = sum(c.live_virions for c in self.infected_cells)
self.transit_virions = sum(c.live_virions for c in self.dead_cells)
body = Body()
for i in range(60):
body.cycle()
print(
"T+%02dh:%02dm:%02ds infected cells: %9d, total virions: %9d, active virions: %9d, cells killed:%9d"
% (
int(t/3600), int((t/60)%60), t % 60,
len(body.infected_cells),
body.active_virions + body.transit_virions,
body.active_virions,
body.cells_died
)
)
t += replication_cycle
print("Fin")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment