Example #1
0
    def test_sampling_output(self):
        """ Check that output generated by the sampling postprocessing goes to the correct path."""

        # Setup parameters.
        parameters = CancerSimulatorParameters(
                                            matrix_size=1000,
                                            number_of_generations=20,
                                            division_probability=1,
                                            adv_mutant_division_probability=1,
                                            death_probability=0.1,
                                            adv_mutant_death_probability=0.0,
                                            mutation_probability=1,
                                            adv_mutant_mutation_probability=1,
                                            adv_mutation_wait_time=10,
                                            number_of_initial_mutations=150,
                                            number_of_mutations_per_division=1,
                                            tumour_multiplicity=None,
                                            sampling_fraction=0.5,
                                            plot_tumour_growth=True,
                                            export_tumour=True,
                                            )

        simulator = CancerSimulator(parameters=parameters, seed=1, outdir="casim_out")
        # Cleanup (remove test output).
        self._test_files.append(simulator.outdir)

        # Run the simulation.
        simulator.run()

        # Check sampling file was written.
        self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sample_out_[0-9]{3}_[0-9]{3}.txt"))
        self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sampleHistogram_[0-9]{3}_[0-9]{3}.pdf"))
        self.assertIn('growthCurve.pdf', os.listdir(simulator._CancerSimulator__simdir))
Example #2
0
    def test_high_sampling_fraction(self):
        """ Test run with sampling_fraction=0.9 """

        # Setup parameters.
        parameters = CancerSimulatorParameters(
                                            matrix_size=1000,
                                            number_of_generations=20,
                                            division_probability=1,
                                            adv_mutant_division_probability=1,
                                            death_probability=0.1,
                                            adv_mutant_death_probability=0.0,
                                            mutation_probability=1,
                                            adv_mutant_mutation_probability=1,
                                            adv_mutation_wait_time=10,
                                            number_of_initial_mutations=150,
                                            number_of_mutations_per_division=50,
                                            tumour_multiplicity=None,
                                            read_depth=100,
                                            sampling_fraction=0.9,
                                            )

        # Setup the simulator.
        simulator = CancerSimulator(parameters=parameters, seed=1, outdir=mkdtemp())
        # Cleanup (remove test output).
        self._test_files.append(simulator.outdir)

        # Check run returns sanely.
        self.assertEqual(simulator.run(), 0)
Example #3
0
    def test_setup_io(self):
        """ Test the IO handling. """

        default_parameters = CancerSimulatorParameters()

        # Setup the simulator without outdir.
        cancer_sim = CancerSimulator(default_parameters, seed=1)
        self._test_files.append("casim_out")

        # Test it is set to the default path in CWD.
        self.assertEqual(cancer_sim.outdir, "casim_out")

        # Get seed dir.
        seeddir = os.path.join("casim_out", 'cancer_%d' % cancer_sim._CancerSimulator__seed)
        # Check all subdirectories are correctly named and exist.
        self.assertEqual(cancer_sim._CancerSimulator__logdir, os.path.join(seeddir, 'log'))
        self.assertTrue(os.path.isdir(cancer_sim._CancerSimulator__logdir))
        self.assertEqual(cancer_sim._CancerSimulator__simdir, os.path.join(seeddir, 'simOutput'))
        self.assertTrue(os.path.isdir(cancer_sim._CancerSimulator__simdir))

        # Create an empty dir.
        tmpdir = mkdtemp()
        self._test_files.append(tmpdir)

        # Set to a different dir.
        cancer_sim.outdir = tmpdir

        # Check export_tumour flag.
        self.assertTrue(cancer_sim.parameters.export_tumour)

        # Check exception is thrown if same O dir is used twice.
        with self.assertRaises(IOError) as exc:
            cancer_sim.outdir = tmpdir
Example #4
0
    def test_serialize(self):
        """ The the serialization of the entire object. """
        parameters = CancerSimulatorParameters()
        cancer_sim = CancerSimulator(parameters, seed=2, outdir=mkdtemp())

        # Cleanup.
        self._test_files.append(cancer_sim.outdir)

        # Dump.
        cancer_sim.dump()

        # Reload
        loaded_simulation = load_cancer_simulation(cancer_sim.dumpfile)
        self.assertIsInstance(loaded_simulation, CancerSimulator)

        # Check parameters.
        loaded_parameters = loaded_simulation.parameters

        self.assertEqual(loaded_parameters.number_of_generations, parameters.number_of_generations)
        self.assertEqual(loaded_parameters.matrix_size, parameters.matrix_size)
        self.assertEqual(loaded_parameters.number_of_generations, parameters.number_of_generations)
        self.assertEqual(loaded_parameters.division_probability, parameters.division_probability)
        self.assertEqual(loaded_parameters.adv_mutant_division_probability, parameters.adv_mutant_division_probability)
        self.assertEqual(loaded_parameters.death_probability, parameters.death_probability)
        self.assertEqual(loaded_parameters.adv_mutant_death_probability, parameters.adv_mutant_death_probability)
        self.assertEqual(loaded_parameters.mutation_probability, parameters.mutation_probability)
        self.assertEqual(loaded_parameters.adv_mutant_mutation_probability, parameters.adv_mutant_mutation_probability)
        self.assertEqual(loaded_parameters.number_of_mutations_per_division, parameters.number_of_mutations_per_division)
        self.assertEqual(loaded_parameters.adv_mutation_wait_time, parameters.adv_mutation_wait_time)
        self.assertEqual(loaded_parameters.number_of_initial_mutations, parameters.number_of_initial_mutations)
        self.assertEqual(loaded_parameters.tumour_multiplicity, parameters.tumour_multiplicity)
        self.assertEqual(loaded_parameters.read_depth, parameters.read_depth)
        self.assertEqual(loaded_parameters.sampling_fraction, parameters.sampling_fraction)

        # Check we can run.
        loaded_simulation.run()

        # dump again.
        loaded_simulation.dump()

        # Load again
        loaded_again_simulation = load_cancer_simulation(loaded_simulation.dumpfile)
        
        # Check that the internal state is the same as in the dump.
        self.assertAlmostEqual(numpy.linalg.norm(loaded_simulation._CancerSimulator__mtx.toarray()
            - loaded_again_simulation._CancerSimulator__mtx.toarray()), 0.0) 
        self.assertEqual(loaded_simulation._CancerSimulator__mut_container, loaded_again_simulation._CancerSimulator__mut_container) 
        self.assertEqual(loaded_simulation._CancerSimulator__xaxis_histogram, loaded_again_simulation._CancerSimulator__xaxis_histogram) 
        self.assertEqual(loaded_simulation._CancerSimulator__biopsy_timing, loaded_again_simulation._CancerSimulator__biopsy_timing) 
        self.assertEqual(loaded_simulation._CancerSimulator__beneficial_mutation, loaded_again_simulation._CancerSimulator__beneficial_mutation) 
        self.assertEqual(loaded_simulation._CancerSimulator__growth_plot_data, loaded_again_simulation._CancerSimulator__growth_plot_data) 
        self.assertEqual(loaded_simulation._CancerSimulator__mutation_counter, loaded_again_simulation._CancerSimulator__mutation_counter) 
        self.assertEqual(loaded_simulation._CancerSimulator__s, loaded_again_simulation._CancerSimulator__s) 
        self.assertEqual(loaded_simulation._CancerSimulator__ploidy, loaded_again_simulation._CancerSimulator__ploidy) 
        self.assertEqual(loaded_simulation._CancerSimulator__mut_multiplier, loaded_again_simulation._CancerSimulator__mut_multiplier) 
        self.assertEqual(loaded_simulation._CancerSimulator__pool, loaded_again_simulation._CancerSimulator__pool) 

        # Run once more.
        loaded_again_simulation.run()
Example #5
0
    def test_init_step_increases(self):
        """ Check the the internal step counter is propely updated. """

        parameters = CancerSimulatorParameters()
        cancer_sim = CancerSimulator(parameters, seed=1, outdir = mkdtemp())

        self.assertEqual(cancer_sim._CancerSimulator__init_step, 0)
        cancer_sim.run()
        self.assertEqual(cancer_sim._CancerSimulator__init_step, 2)
Example #6
0
    def test_export_tumour_matrix(self):
        """ Test exporting the tumour matrix. """

        parameters = CancerSimulatorParameters()
        cancer_sim = CancerSimulator(parameters, seed=1, outdir = mkdtemp())
        cancer_sim.run()

        # Check files where created.
        listing = os.listdir(cancer_sim._CancerSimulator__simdir)
        for f in ['mtx.p', 'mut_container.p', 'mtx_VAF.txt']:
            self.assertIn(f, listing)
Example #7
0
    def test_reference_data_1mut(self):
        """ Run a reference test and compare against reference data."""
        """ 1 mutation per division."""

        # Setup parameters. Values taken from casim/params.py.
        parameters = CancerSimulatorParameters(
                                            matrix_size=1000,
                                            number_of_generations=20,
                                            division_probability=1,
                                            adv_mutant_division_probability=1,
                                            death_probability=0.1,
                                            adv_mutant_death_probability=0.0,
                                            mutation_probability=1,
                                            adv_mutant_mutation_probability=1,
                                            adv_mutation_wait_time=10,
                                            number_of_initial_mutations=150,
                                            number_of_mutations_per_division=1,
                                            tumour_multiplicity=None,
                                            sampling_fraction=0.1,
                                            sampling_positions=[(501,502)],
                                            )

        simulator = CancerSimulator(parameters=parameters, seed=1, outdir="reference_test_out")
        self._test_files.append('reference_test_out')

        simulator.run()

        ### Load results and reference data.
        # Reference data.
        ref_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'reference_test_data_1mut', 'cancer_1', 'simOutput')
        with open(os.path.join(ref_dir, 'mtx.p'), 'rb') as fp:
            ref_mtx = pickle.load(fp)
        with open(os.path.join(ref_dir, 'mut_container.p'), 'rb') as fp:
            ref_mutations = pickle.load(fp)

        run_dir = os.path.join('reference_test_out', 'cancer_1', 'simOutput')
        # Run data.
        with open(os.path.join(run_dir, 'mtx.p'), 'rb') as fp:
            run_mtx = pickle.load(fp)
        with open(os.path.join(run_dir, 'mut_container.p'), 'rb') as fp:
            run_mutations = pickle.load(fp)

        # Check data is equal.
        self.assertAlmostEqual(numpy.linalg.norm((ref_mtx - run_mtx).toarray()), 0.0)
        self.assertEqual(ref_mutations, run_mutations)
Example #8
0
    def test_default_constructor (self):
        """ Test the construction of the Simulator without arguments. """

        # Cleanup.
        self._test_files.append("casim_out")

        # Test that the Simulator cannot be constructed without parameters.
        with self.assertRaises(ValueError):
            casim = CancerSimulator()
Example #9
0
    def test_sampling_positions(self):
        """ Check setting the sample positions generates the expected output."""

        # Setup parameters.
        parameters = CancerSimulatorParameters(
                                            matrix_size=1000,
                                            number_of_generations=20,
                                            division_probability=1,
                                            adv_mutant_division_probability=1,
                                            death_probability=0.1,
                                            adv_mutant_death_probability=0.0,
                                            mutation_probability=1,
                                            adv_mutant_mutation_probability=1,
                                            adv_mutation_wait_time=10,
                                            number_of_initial_mutations=150,
                                            number_of_mutations_per_division=1,
                                            tumour_multiplicity=None,
                                            sampling_fraction=0.1,
                                            sampling_positions=[
                                                [500,500],
                                                [450,550],
                                                [450,550],
                                                [550,450],
                                                [550,550],
                                                ],
                                            plot_tumour_growth=True,
                                            export_tumour=True,
                                            )

        simulator = CancerSimulator(parameters=parameters, seed=1, outdir="casim_out")
        # Cleanup (remove test output).
        self._test_files.append(simulator.outdir)

        # Run the simulation.
        simulator.run()

        # Check sampling file was written.
        self.assertIn("sample_out_500_500.txt", os.listdir(simulator._CancerSimulator__simdir))
        self.assertIn("sampleHistogram_500_500.pdf", os.listdir(simulator._CancerSimulator__simdir))
        self.assertRegex(",".join(os.listdir(simulator._CancerSimulator__simdir)), re.compile(r"sampleHistogram_[0-9]{3}_[0-9]{3}.pdf"))
Example #10
0
        parameters = CancerSimulatorParameters(
            matrix_size=4096,
            number_of_generations=g,  # vary
            division_probability=dp,  # 1 => exp. growth
            adv_mutant_division_probability=0.3,
            death_probability=0.0,
            adv_mutant_death_probabilityability=0.0,
            mutation_rate=1.0,  # 1 =>
            adv_mutant_mutation_probability=0.8,
            mutations_per_division=
            10,  # if mutation event occurs, have this number of mutation
            adv_mutation_interval=30000,  # large to not kick in
            number_of_initital_mutations=
            2,  # initial number of mutations in first cancer cell
            tumour_multiplicity='single',
        )

        simulator = CancerSimulator(parameters,
                                    seed=None,
                                    outdir="%s" % (n_run))

        t0 = time()
        simulator.run()
        T = time() - t0

        with open(os.path.join(simulator.outdir, "log.txt"), 'a') as fp:
            fp.write("%s\t%f\t%d\t%e\n" % (simulator.seed, dp, g, T))

        simulator.dump()
        sleep(1)