def test_read_hapmap_nonzero_end(self): with open(self.temp_file, "w+") as f: print("HEADER", file=f) print("chr1 0 5 x", file=f) print("s 2 1 x x x", file=f) with pytest.raises(ValueError): msprime.read_hapmap(self.temp_file)
def test_read_hapmap_nonzero_start(self): with open(self.temp_file, "w+") as f: print("HEADER", file=f) print("chr1 1 5 x", file=f) print("s 2 0 x x x", file=f) rm = msprime.read_hapmap(self.temp_file) np.testing.assert_array_equal(rm.position, [0, 1, 2]) np.testing.assert_array_equal(rm.rate, [0, 5e-8])
def test_read_hapmap_gzipped(self): try: filename = self.temp_file + ".gz" with gzip.open(filename, "w+") as f: f.write(b"HEADER\n") f.write(b"chr1 0 1\n") f.write(b"chr1 1 5.5\n") f.write(b"s 2 0\n") rm = msprime.read_hapmap(filename) np.testing.assert_array_equal(rm.position, [0, 1, 2]) np.testing.assert_array_equal(rm.rate, [1e-8, 5.5e-8]) finally: os.unlink(filename)
def test_mean_recombination_rate(self): # Some quick sanity checks. recomb_map = msprime.RecombinationMap([0, 1], [1, 0]) mean_rr = recomb_map.mean_recombination_rate assert mean_rr == 1.0 recomb_map = msprime.RecombinationMap([0, 1, 2], [1, 0, 0]) mean_rr = recomb_map.mean_recombination_rate assert mean_rr == 0.5 recomb_map = msprime.RecombinationMap([0, 1, 2], [0, 0, 0]) mean_rr = recomb_map.mean_recombination_rate assert mean_rr == 0.0 # Test mean_recombination_rate is correct after reading from # a hapmap file. read_hapmap() ignores the cM # field, so here we test against using the cM field directly. def hapmap_rr(hapmap_file): first_pos = 0 with open(hapmap_file) as f: next(f) # skip header for line in f: pos, rate, cM = map(float, line.split()[1:4]) if cM == 0: first_pos = pos return cM / 100 / (pos - first_pos) hapmap = """chr pos rate cM 1 4283592 3.79115663174456 0 1 4361401 0.0664276817058413 0.294986106359414 1 7979763 10.9082897515584 0.535345505591925 1 8007051 0.0976780648822495 0.833010916332456 1 8762788 0.0899929572085616 0.906829844052373 1 9477943 0.0864382908650907 0.971188757364862 1 9696341 4.76495005895746 0.990066707213216 1 9752154 0.0864316558730679 1.25601286485381 1 9881751 0.0 1.26721414815999""" with tempfile.TemporaryDirectory() as temp_dir: hapfile = os.path.join(temp_dir, "hapmap.txt") with open(hapfile, "w") as f: f.write(hapmap) recomb_map = msprime.read_hapmap(f.name) mean_rr = recomb_map.mean_rate mean_rr2 = hapmap_rr(hapfile) self.assertAlmostEqual(mean_rr, mean_rr2, places=15)