Skip to main content

Simulation for cell metabolic and transduction pathway evolution

Project description

MagicSoup


Documentation: https://magic-soup.readthedocs.io/

Source Code: https://github.com/mRcSchwering/magic-soup

PyPI: https://pypi.org/project/magicsoup/


This game simulates cell metabolic and transduction pathway evolution. Define a 2D world with certain molecules and reactions. Add a few cells and create evolutionary pressure by selectively replicating and killing them. Then run and see what random mutations can do.

co2 fixing In this simulation cells were made to fix carbon from an artificial CO2 source in the middle of the map. (A) Total molecule counts of some molecule species over time. (B) Cell map with cells in white at chosen time points.

Installation

For CPU alone you can just do:

pip install magicsoup

This simulation relies on PyTorch. To increase performance you can move calculations to a GPU. In this case you should setup PyTorch first before installing MagicSoup. To setup pytorch correctly for your GPU see Get Started (pytorch.org).

Example

The basic building blocks of what a cell can do are defined by the world's chemistry. There are molecules and reactions that can convert these molecules. Cells can develop proteins with domains that can transport these molecules, catalyze the reactions, and be regulated by molecules. Any reaction or transport happens only if energetically favourable. Below, I am defining the reaction $CO2 + NADPH \rightleftharpoons formiat + NADP$. Each molecule species is defined with a energy.

import torch
import magicsoup as ms

NADPH = ms.Molecule("NADPH", 200 * 1e3)
NADP = ms.Molecule("NADP", 100 * 1e3)
formiat = ms.Molecule("formiat", 20 * 1e3)
co2 = ms.Molecule("CO2", 10 * 1e3)

molecules = [NADPH, NADP, formiat, co2]
reactions = [([co2, NADPH], [formiat, NADP])]

chemistry = ms.Chemistry(reactions=reactions, molecules=molecules)
world = ms.World(chemistry=chemistry)

By coupling multiple domains within the same protein, energetically unfavourable actions can be powered with the energy of energetically favourable ones. These domains, their specifications, and how they are coupled in proteins, is all encoded in the cell's genome. Here, I am generating 100 cells with random genomes of 500 basepairs each and place them in random places on the 2D world map.

genomes = [ms.random_genome(s=500) for _ in range(100)]
world.add_cells(genomes=genomes)

Cells discover new proteins by chance through mutations. In the function below all cells experience 1E-3 random point mutations per nucleotide. 10% of them will be indels.

def mutate_cells():
    mutated = ms.point_mutations(seqs=world.genomes)
    world.update_cells(genome_idx_pairs=mutated)

Evolutionary pressure can be applied by selectively killing or replicating cells. Here, cells have an increased chance of dying when formiat gets too low and an increased chance of replicating when formiat gets high.

def sample(p: torch.Tensor) -> list[int]:
    idxs = torch.argwhere(torch.bernoulli(p))
    return idxs.flatten().tolist()

def kill_cells():
    x = world.cell_molecules[:, 2]
    idxs = sample(.01 / (.01 + x))
    world.kill_cells(cell_idxs=idxs)

def replicate_cells():
    x = world.cell_molecules[:, 2]
    idxs = sample(x ** 3 / (x ** 3 + 20.0 ** 3))
    world.divide_cells(cell_idxs=idxs)

Finally, the simulation itself is run in a python loop by repetitively calling the different steps. With world.enzymatic_activity() chemical reactions and molecule transport in cells advance by one time step. world.diffuse_molecules() lets molecules on the world map diffuse and permeate through cell membranes (if they can) by one time step.

for _ in range(1000):
    world.enzymatic_activity()
    kill_cells()
    replicate_cells()
    mutate_cells()
    world.diffuse_molecules()
    world.increment_cell_lifetimes()

See the Docs for more examples and a description of all the mechanics of this simulation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magicsoup-0.4.1.tar.gz (69.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

magicsoup-0.4.1-py3-none-any.whl (57.7 kB view details)

Uploaded Python 3

File details

Details for the file magicsoup-0.4.1.tar.gz.

File metadata

  • Download URL: magicsoup-0.4.1.tar.gz
  • Upload date:
  • Size: 69.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/6.0.0 pkginfo/1.9.6 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.10.10

File hashes

Hashes for magicsoup-0.4.1.tar.gz
Algorithm Hash digest
SHA256 358e7d2d5c9e18540347461c5ce4d480371690badabf061d3db322e74f6f7a3f
MD5 433d2aeef1f21ee8b6e2238d3ddca027
BLAKE2b-256 f56d1540cb1c3b22deae2de6455d284a060ff8cf519b8d5e3ef3b5e8b6cc22df

See more details on using hashes here.

File details

Details for the file magicsoup-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: magicsoup-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 57.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/6.0.0 pkginfo/1.9.6 requests/2.29.0 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.10.10

File hashes

Hashes for magicsoup-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6935dcf6cc3a45eb5b3d23538a33595c4ae36c99095e57e5e79ecec446bee4a7
MD5 513c9a39b41831f58d742b881e70e0a1
BLAKE2b-256 4abc9dbe8fef3da3b8a712e111549ba0bc8369be082639201b7765f42104ba86

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page