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]) with pytest.raises(NotImplementedError): pedigrees.Pedigree( individual=individual, parents=parents, time=time, ploidy=1, ) bad_parents = np.array([2, 3, 2, 3, -1, -1, -1, -1]).reshape(-1, 4) with pytest.raises(ValueError): pedigrees.Pedigree( individual=individual, parents=bad_parents, time=time, ) bad_individual = np.array([-1, 2, 3, 4]) with pytest.raises(ValueError): pedigrees.Pedigree( individual=bad_individual, parents=parents, time=time, ) bad_times = np.array([1, 1, 1, 1]) ped = pedigrees.Pedigree(individual, parents, bad_times, is_sample, sex=None, ploidy=2) with pytest.raises(ValueError): ped.check_times( individual=ped.individual, parents=ped.parents, time=ped.time, ) ped = pedigrees.Pedigree(individual, parents, time, sex=None, ploidy=2) with pytest.raises(ValueError): ped.set_samples() with pytest.raises(ValueError): ped.set_samples(num_samples=10) with pytest.raises(ValueError): ped.set_samples(num_samples=2, sample_IDs=[1, 2]) with pytest.raises(ValueError): ped.get_times(individual=ped.individual)
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 = pedigrees.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") assert ts.num_edges > 0 ped.set_samples(sample_IDs=[1, 2]) ts = msprime.simulate(2, pedigree=ped, model="wf_ped") assert ts is not None with pytest.raises(ValueError): ped.set_samples(sample_IDs=[1, 1]) with pytest.raises(ValueError): ped.set_samples(sample_IDs=[1, 3]) with pytest.raises(NotImplementedError): ped.set_samples(sample_IDs=[1, 3], probands_only=False) ped.set_samples(sample_IDs=[1, 2]) assert ped.get_proband_indices() == [0, 1]
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 = pedigrees.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, demographic_events=[ msprime.SimulationModelChange(time=1, model="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
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 = pedigrees.Pedigree(inds, parent_indices, times, is_sample, sex=None, ploidy=2) bad_model_change = msprime.SimulationModelChange( 0.5, msprime.DiscreteTimeWrightFisher()) with pytest.raises(RuntimeError, match="not support interruption"): msprime.simulate( 4, pedigree=ped, demographic_events=[bad_model_change], model="wf_ped", ) bad_demographic_event = msprime.PopulationParametersChange( t, initial_size=2) with pytest.raises(_msprime.LibraryError): msprime.simulate( 4, pedigree=ped, demographic_events=[bad_demographic_event], model="wf_ped", )
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 = pedigrees.Pedigree(inds, parent_indices, times, is_sample, sex=None, ploidy=2) ts = msprime.simulate(2, pedigree=ped, model=model) assert ts is not None
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 = pedigrees.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: assert ts is not None
def test_pedigree(self): with tempfile.TemporaryDirectory() as tmpdir: ped_path = pathlib.Path(tmpdir) / "tmp.ped" individual = np.array([1, 2, 3, 4], dtype=int) parents = np.array([2, 3, 2, 3, -1, -1, -1, -1], dtype=int).reshape(-1, 2) time = np.array([0, 0, 1, 1]) is_sample = np.array([1, 1, 0, 0], dtype=int) ped = pedigrees.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(): assert tree.num_roots > 1 assert ts.num_individuals == len(individual) assert ts.num_samples == 4
def test_simple_case(self): # Simple test to check that the current code is still roughly working. # Should be replace by better tests. individual = np.array([1, 2, 3, 4]) parents = np.array([-1, -1, -1, -1, 0, 1, 1, 0]).reshape(-1, 2) time = np.array([1, 1, 0, 0]) is_sample = np.array([0, 0, 1, 1]) model = msprime.WrightFisherPedigree() ped = pedigrees.Pedigree(individual, parents, time, is_sample, sex=None, ploidy=2) ts = msprime.simulate(2, pedigree=ped, model=model, recombination_rate=1) table = ts.tables.individuals assert np.all(table[0].parents == [-1, -1]) assert np.all(table[1].parents == [-1, -1])
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 = pedigrees.Pedigree(individual, parents, time, is_sample, sex=None, ploidy=2) ped.save_txt(self.temp_pedigree_text_file) with pytest.raises(NotImplementedError): pedigrees.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 = pedigrees.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 = pedigrees.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 = pedigrees.Pedigree.read_npy( self.temp_pedigree_array_file) # TODO compre this to the file above. assert isinstance(ped_from_npy, pedigrees.Pedigree)
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 = pedigrees.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)