Example #1
0
    def test_pedigree_sanity_checks(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])
        is_sample = np.array([1, 1, 0, 0])

        self.assertRaises(
            NotImplementedError,
            msprime.Pedigree,
            individual=individual,
            parents=parents,
            time=time,
            ploidy=1,
        )
        bad_parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 4)
        self.assertRaises(
            ValueError,
            msprime.Pedigree,
            individual=individual,
            parents=bad_parents,
            time=time,
        )
        bad_individual = np.array([-1, 2, 3, 4])
        self.assertRaises(
            ValueError,
            msprime.Pedigree,
            individual=bad_individual,
            parents=parents,
            time=time,
        )

        bad_times = np.array([1, 1, 1, 1])
        ped = msprime.Pedigree(individual,
                               parents,
                               bad_times,
                               is_sample,
                               sex=None,
                               ploidy=2)
        self.assertRaises(
            ValueError,
            ped.check_times,
            individual=ped.individual,
            parents=ped.parents,
            time=ped.time,
        )

        ped = msprime.Pedigree(individual, parents, time, sex=None, ploidy=2)
        self.assertRaises(ValueError, ped.set_samples)
        self.assertRaises(ValueError, ped.set_samples, num_samples=10)
        self.assertRaises(ValueError,
                          ped.set_samples,
                          num_samples=2,
                          sample_IDs=[1, 2])

        self.assertRaises(ValueError, ped.get_times, individual=ped.individual)
Example #2
0
    def test_ped_wf_recombination(self):
        inds = np.array([1, 2, 3, 4, 5, 6])
        parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1,
                                   -1]).reshape(-1, 2)
        times = np.array([0, 0, 0, 0, 1, 1])
        is_sample = np.array([1, 1, 1, 1, 0, 0])
        t = max(times)

        model = msprime.WrightFisherPedigree()
        ped = msprime.Pedigree(inds,
                               parent_indices,
                               times,
                               is_sample,
                               sex=None,
                               ploidy=2)
        ts = msprime.simulate(
            sample_size=4,
            pedigree=ped,
            recombination_rate=0.1,
            model=[model, (1, "dtwf")],
        )
        tree = ts.first()
        self.assertEqual(tree.num_roots, 1)
        all_times = ts.tables.nodes.time
        ped_times = all_times[np.logical_and(all_times > 0, all_times <= t)]
        self.assertGreater(ped_times.shape[0], 0)
        self.assertTrue(np.all(ped_times == np.floor(ped_times)))
        wf_times = all_times[all_times > t]
        self.assertGreater(wf_times.shape[0], 0)
Example #3
0
    def test_pedigree_unsupported_events(self):
        inds = np.array([1, 2, 3, 4, 5, 6])
        parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1,
                                   -1]).reshape(-1, 2)
        times = np.array([0, 0, 0, 0, 1, 1])
        is_sample = np.array([1, 1, 1, 1, 0, 0])
        t = max(times)

        ped = msprime.Pedigree(inds,
                               parent_indices,
                               times,
                               is_sample,
                               sex=None,
                               ploidy=2)

        bad_model_change = msprime.SimulationModelChange(
            0.5, msprime.DiscreteTimeWrightFisher())
        self.assertRaises(
            NotImplementedError,
            msprime.simulate,
            4,
            pedigree=ped,
            demographic_events=[bad_model_change],
            model="wf_ped",
        )
        bad_demographic_event = msprime.PopulationParametersChange(
            t, initial_size=2)
        self.assertRaises(
            NotImplementedError,
            msprime.simulate,
            4,
            pedigree=ped,
            demographic_events=[bad_demographic_event],
            model="wf_ped",
        )
Example #4
0
    def test_ped_wf_single_locus(self):
        inds = np.array([1, 2, 3, 4, 5, 6])
        parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1,
                                   -1]).reshape(-1, 2)
        times = np.array([0, 0, 0, 0, 1, 1])
        is_sample = np.array([1, 1, 1, 1, 0, 0])
        t = max(times)

        model = msprime.WrightFisherPedigree()
        ped = msprime.Pedigree(inds,
                               parent_indices,
                               times,
                               is_sample,
                               sex=None,
                               ploidy=2)
        ts = msprime.simulate(sample_size=4,
                              Ne=2,
                              pedigree=ped,
                              model=(model, (t, "dtwf")))
        tree = ts.first()
        assert tree.num_roots == 1
        all_times = ts.tables.nodes.time
        ped_times = all_times[np.logical_and(all_times > 0, all_times <= t)]
        assert ped_times.shape[0] > 0
        assert np.all(ped_times == np.floor(ped_times))
        wf_times = all_times[all_times > t]
        assert wf_times.shape[0] > 0
Example #5
0
    def test_pedigree_samples(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])

        ped = msprime.Pedigree(individual, parents, time)
        # This interface is pretty poor - what we want is for the
        # samples argument to simulate to be interpreted as individual
        # IDs. For now, let's just leave it as it is, though.
        ped.set_samples(2)
        ts = msprime.simulate(2, pedigree=ped, model="wf_ped")
        self.assertGreater(ts.num_edges, 0)

        ped.set_samples(sample_IDs=[1, 2])
        ts = msprime.simulate(2, pedigree=ped, model="wf_ped")
        self.assertTrue(ts is not None)
        self.assertRaises(ValueError, ped.set_samples, sample_IDs=[1, 1])
        self.assertRaises(ValueError, ped.set_samples, sample_IDs=[1, 3])
        self.assertRaises(NotImplementedError,
                          ped.set_samples,
                          sample_IDs=[1, 3],
                          probands_only=False)

        ped.set_samples(sample_IDs=[1, 2])
        self.assertEqual(ped.get_proband_indices(), [0, 1])
Example #6
0
    def test_pedigree_read_write(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])
        is_sample = np.array([1, 1, 0, 0])

        ped = msprime.Pedigree(individual,
                               parents,
                               time,
                               is_sample,
                               sex=None,
                               ploidy=2)

        ped.save_txt(self.temp_pedigree_text_file)
        self.assertRaises(NotImplementedError,
                          msprime.Pedigree.read_txt,
                          self.temp_pedigree_text_file,
                          sex_col=4)
        ped_from_txt = msprime.Pedigree.read_txt(self.temp_pedigree_text_file,
                                                 time_col=None)
        ts = msprime.simulate(2, pedigree=ped_from_txt, model="wf_ped")
        self.assertTrue(ts is not None)
        ped_from_txt = msprime.Pedigree.read_txt(self.temp_pedigree_text_file,
                                                 time_col=3)
        ts = msprime.simulate(2, pedigree=ped_from_txt, model="wf_ped")
        self.assertTrue(ts is not None)

        ped.save_npy(self.temp_pedigree_array_file)
        ped_from_npy = msprime.Pedigree.read_npy(self.temp_pedigree_array_file)
        ts = msprime.simulate(2, pedigree=ped_from_npy, model="wf_ped")
        self.assertTrue(ts is not None)
Example #7
0
    def test_pedigree_samples(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])

        ped = msprime.Pedigree(individual, parents, time)
        ped.set_samples(2)
        ts = msprime.simulate(2, pedigree=ped, model="wf_ped")
        self.assertTrue(ts is not None)
        ped.set_samples(1)
        self.assertRaises(ValueError,
                          msprime.simulate,
                          2,
                          pedigree=ped,
                          model="wf_ped")

        ped.set_samples(sample_IDs=[1, 2])
        ts = msprime.simulate(2, pedigree=ped, model="wf_ped")
        self.assertTrue(ts is not None)
        self.assertRaises(ValueError, ped.set_samples, sample_IDs=[1, 1])
        self.assertRaises(ValueError, ped.set_samples, sample_IDs=[1, 3])
        self.assertRaises(NotImplementedError,
                          ped.set_samples,
                          sample_IDs=[1, 3],
                          probands_only=False)
        ped.set_samples(sample_IDs=[1])
        self.assertRaises(ValueError,
                          msprime.simulate,
                          2,
                          pedigree=ped,
                          model="wf_ped")

        ped.set_samples(sample_IDs=[1, 2])
        self.assertEqual(ped.get_proband_indices(), [0, 1])
Example #8
0
    def test_ped_wf_single_locus(self):
        inds = np.array([1, 2, 3, 4, 5, 6])
        parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1,
                                   -1]).reshape(-1, 2)
        times = np.array([0, 0, 0, 0, 1, 1])
        is_sample = np.array([1, 1, 1, 1, 0, 0])
        t = max(times)

        model = msprime.WrightFisherPedigree()
        ped = msprime.Pedigree(inds,
                               parent_indices,
                               times,
                               is_sample,
                               sex=None,
                               ploidy=2)
        ts = msprime.simulate(
            sample_size=4,
            pedigree=ped,
            demographic_events=[
                msprime.SimulationModelChange(
                    t, msprime.DiscreteTimeWrightFisher(2))
            ],
            model=model,
        )
        tree = ts.first()
        self.assertEqual(tree.num_roots, 1)
        all_times = ts.tables.nodes.time
        ped_times = all_times[np.logical_and(all_times > 0, all_times <= t)]
        self.assertGreater(ped_times.shape[0], 0)
        self.assertTrue(np.all(ped_times == np.floor(ped_times)))
        wf_times = all_times[all_times > t]
        self.assertGreater(wf_times.shape[0], 0)
Example #9
0
    def test_wf_ped(self):
        inds = np.array([1, 2, 3, 4])
        parent_indices = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        times = np.array([0, 0, 1, 1])
        is_sample = np.array([1, 1, 0, 0])

        model = msprime.WrightFisherPedigree()
        ped = msprime.Pedigree(
            inds, parent_indices, times, is_sample, sex=None, ploidy=2
        )
        ts = msprime.simulate(2, pedigree=ped, model=model)
        assert ts is not None
Example #10
0
 def test_pedigree(self):
     with tempfile.TemporaryDirectory() as tmpdir:
         ped_path = pathlib.Path(tmpdir) / "tmp.ped"
         individual = np.array([1, 2, 3, 4])
         parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
         time = np.array([0, 0, 1, 1])
         is_sample = np.array([1, 1, 0, 0])
         ped = msprime.Pedigree(individual,
                                parents,
                                time,
                                is_sample,
                                sex=None,
                                ploidy=2)
         ped.save_txt(ped_path)
         ts = self.run_script(
             f"2 --pedigree-file {ped_path} --model=wf_ped")
         for tree in ts.trees():
             self.assertGreater(tree.num_roots, 1)
    def to_msprime_pedigree(self):
        """Create an ``msprime`` representation of the pedigree

        This converts a :class:`Pedigree` into an ``msprime.Pedigree``

        Returns:
            msprime.Pedigree: an msprime Pedigree
        """

        tbl = self.to_table()

        individual = tbl.individual.values
        parent_IDs = np.vstack((tbl.father, tbl.mother)).transpose()
        parent_idx = msp.Pedigree.parent_ID_to_index(individual, parent_IDs)
        
        msp_ped = msp.Pedigree(individual, parent_idx, tbl.time.values)
        msp_ped.set_samples(sample_IDs=self.probands(), probands_only=True)

        return msp_ped
Example #12
0
    def test_pedigree_replicates(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])
        is_sample = np.array([1, 1, 0, 0])

        model = msprime.WrightFisherPedigree()
        ped = msprime.Pedigree(individual,
                               parents,
                               time,
                               is_sample,
                               sex=None,
                               ploidy=2)
        replicates = msprime.simulate(2,
                                      pedigree=ped,
                                      model=model,
                                      recombination_rate=1,
                                      num_replicates=100)
        for ts in replicates:
            self.assertTrue(ts is not None)
Example #13
0
    def test_pedigree_read_write(self):
        individual = np.array([1, 2, 3, 4])
        parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 2)
        time = np.array([0, 0, 1, 1])
        is_sample = np.array([1, 1, 0, 0])

        ped = msprime.Pedigree(individual,
                               parents,
                               time,
                               is_sample,
                               sex=None,
                               ploidy=2)

        ped.save_txt(self.temp_pedigree_text_file)
        self.assertRaises(
            NotImplementedError,
            msprime.Pedigree.read_txt,
            self.temp_pedigree_text_file,
            sex_col=4,
        )
        # FIXME
        # The compute_times should be done automatically in this case .
        # ped_from_txt = msprime.Pedigree.read_txt(
        #     self.temp_pedigree_text_file, time_col=None
        # )
        # ts = msprime.simulate(2, pedigree=ped_from_txt, model="wf_ped")
        # self.assertTrue(ts is not None)
        # ped_from_txt = msprime.Pedigree.read_txt(
        #     self.temp_pedigree_text_file, time_col=3
        # )
        # ts = msprime.simulate(2, pedigree=ped_from_txt, model="wf_ped")
        # self.assertTrue(ts is not None)

        ped.save_npy(self.temp_pedigree_array_file)
        ped_from_npy = msprime.Pedigree.read_npy(self.temp_pedigree_array_file)
        # TODO compre this to the file above.
        self.assertIsInstance(ped_from_npy, msprime.Pedigree)
Example #14
0
 def test_pedigree(self):
     inds = np.array([1, 2, 3, 4, 5, 6])
     parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1,
                                -1]).reshape(-1, 2)
     times = np.array([0, 0, 0, 0, 1, 1])
     is_sample = np.array([1, 1, 1, 1, 0, 0])
     t = max(times)
     model = msprime.WrightFisherPedigree()
     ped = msprime.Pedigree(inds,
                            parent_indices,
                            times,
                            is_sample,
                            sex=None,
                            ploidy=2)
     ts = msprime.simulate(
         sample_size=4,
         pedigree=ped,
         demographic_events=[
             msprime.SimulationModelChange(
                 t, msprime.DiscreteTimeWrightFisher())
         ],
         model=model,
     )
     self.verify(ts)