<jupyter_start><jupyter_text>Quickstart example for cancer simulation This notebook illustrates how to run a 2D cancer simulation using CancerSim. It's a rather contrieved example with no actual scientific background. It's main purpose is to give a simple example to be used as a template for more complex simulation runs. It also demonstrates how to parametrize the simulation through the `CancerSimulationParameters` API instead of the `params.py` module.The simulation treats a 2D tumour on a 20x20 grid for 10 generations. Healthy cells and cancer cells differ by their division probabilities (0.5 vs 0.8), death probabilities (0.1 vs. 0.4), and mutation probabilities (0.2 vs. 0.8). The first cancer cell carries 2 mutations, new mutations occur with a 20% probability and 10 fold abundance. The first advantageous mutation occurs in the 3rd generation. We sample 10% of the tumour at a read depth of 100.After the run, we plot the tumour growth curve and the mutation frequency histograms for the whole tumour and the sampled tumour.  Import modules<jupyter_code># The cancer simulation module.
from casim import casim
import logging
#  3rd party modules.
import os, shutil
from wand.image import Image as WImage # To render pdf images in nb.
from glob import glob<jupyter_output><empty_output><jupyter_text>Setup parameters<jupyter_code>parameters=casim.CancerSimulatorParameters(
                                matrix_size=20,
                                number_of_generations=10,
                                division_probability=0.5,
                                adv_mutant_division_probability=0.8,
                                death_probability=0.1,
                                adv_mutant_death_probability=0.4,
                                mutation_probability=0.2,
                                adv_mutant_mutation_probability=0.8,
                                number_of_mutations_per_division=10,
                                adv_mutation_wait_time=3,
                                number_of_initial_mutations=2,
                                sampling_fraction=0.1,
                                plot_tumour_growth=True,
                                export_tumour=True
                               )<jupyter_output><empty_output><jupyter_text>Setup the simulation engine.<jupyter_code>if os.path.isdir('out'):
    shutil.rmtree('out')
cancer_sim = casim.CancerSimulator(parameters, seed=1, outdir='out/')
# Get more verbose logging.
logger = logging.getLogger().setLevel("INFO")<jupyter_output><empty_output><jupyter_text>Run the simulation<jupyter_code>cancer_sim.run()<jupyter_output>2020-08-10 16:14:45,538 INFO: Running in single tumour mode.
2020-08-10 16:14:45,540 INFO: First cell at (10, 10).
2020-08-10 16:14:45,542 INFO: Ready to start CancerSim run with these parameters:
2020-08-10 16:14:45,543 INFO: matrix_size = 20
2020-08-10 16:14:45,544 INFO: number_of_generations = 10
2020-08-10 16:14:45,545 INFO: division_probability = 0.5
2020-08-10 16:14:45,546 INFO: adv_mutant_division_probability = 0.8
2020-08-10 16:14:45,547 INFO: death_probability = 0.1
2020-08-10 16:14:45,547 INFO: adv_mutant_death_probability = 0.4
2020-08-10 16:14:45,548 INFO: mutation_probability = 0.2
2020-08-10 16:14:45,549 INFO: adv_mutant_mutation_probability = 0.8
2020-08-10 16:14:45,550 INFO: number_of_mutations_per_division = 10
2020-08-10 16:14:45,551 INFO: adv_mutation_wait_time = 3
2020-08-10 16:14:45,551 INFO: number_of_initial_mutations = 2
2020-08-10 16:14:45,552 INFO: tumour_multiplicity = single
2020-08-10 16:14:45,553 INFO: read_depth = 100
2020-08-10 16:14:45,554 INFO: samplin[...]<jupyter_text>OutputAfter the run has finished, you should find the results in`out/cancer_1/simOutput`.<jupyter_code>!ls out/cancer_1/simOutput<jupyter_output>death_list.p	 mtx_VAF.txt		    sample_out_12_11.txt
growthCurve.pdf  mut_container.p	    wholeTumourVAFHistogram.pdf
mtx.p		 sampleHistogram_12_11.pdf<jupyter_text>Files with the extension `.p` are binary files (python pickles) needed torestart a simulation. Let's take a look at the `.txt`  files. They contain the simulation output:`mtx_VAF.txt` is a datafile with three columns: `mutation_id` lists the index ofeach primary mutation, `additional_mut_id` indexes the subsequent mutations that occur in a cell ofa given `mutation_id`; `frequency` is the frequency at which a given mutation occurs.The file `sample_out_502_488.txt` lists all mutations of the artificial sampletaken from the whole tumour. Columns are identical to `mtx_VAF.txt`. The two `.pdf` files are plots of the whole tumour histogram and the sampledtumour histogram, respectively. You should see figures similar to these:  Display results. The mutation frequency histogram for the whole tumour and the for sampled part of the tumour, as well as the tumour growth curve (number of tumour cells vs. time) are stored as pdf images in the simulation output directory.<jupyter_code>image_path=os.path.join(cancer_sim.outdir,'cancer_1', 'simOutput')
pdfs = glob(os.path.join(image_path,"*.pdf"))
pdfs
for pdf in pdfs:
    print(pdf)
    display(WImage(filename=pdf))<jupyter_output>out/cancer_1/simOutput/growthCurve.pdf<jupyter_text>Load binary files (pickles)The remaining output files are serialized versions ("pickles") of the tumourgeometry as a 2D matrix (`mtx.p`), the death list (`death_list.p`), and themutation list (list of tuples listing the parent and the mutation ID of eachtumour cell, `mut_container.p`).To read the pickled data, we define a utility function<jupyter_code>import pickle
from matplotlib import pyplot
def unpickle(file):

    with open(os.path.join(cancer_sim._CancerSimulator__simdir, file),'rb') as fp:
        return pickle.load(fp)
# Load the mutation matrix
mtx=unpickle('mtx.p').toarray()
# Plot the tumour as a 2D map color coding the mutation ID
cmap = pyplot.cm.get_cmap('PRGn', mtx.max()+1)

pyplot.matshow(mtx, cmap=cmap)
pyplot.colorbar()
unpickle('mut_container.p')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup