Skip to content

Commit d467261

Browse files
Test cleaning
1 parent 56754f2 commit d467261

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

tests/test_tskit.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,6 @@ def insert_branch_sites(ts, m=1):
9494
return tables.tree_sequence()
9595

9696

97-
@pytest.fixture()
98-
def fx_ts_isolated_samples():
99-
tables = tskit.Tree.generate_balanced(2, span=10).tree_sequence.dump_tables()
100-
# This also tests sample nodes that are not a single block at
101-
# the start of the nodes table.
102-
tables.nodes.add_row(time=0, flags=tskit.NODE_IS_SAMPLE)
103-
return insert_branch_sites(tables.tree_sequence())
104-
105-
10697
class TestSimpleTs:
10798
@pytest.fixture()
10899
def conversion(self, tmp_path):
@@ -199,17 +190,28 @@ class TestTskitFormat:
199190
"""Unit tests for TskitFormat without using full conversion."""
200191

201192
@pytest.fixture()
202-
def fx_simple_ts(self, tmp_path):
193+
def fx_simple_ts(self):
203194
return simple_ts(add_individuals=True)
204195

205196
@pytest.fixture()
206-
def fx_ts_2_diploids(self, tmp_path):
197+
def fx_ts_2_diploids(self):
207198
ts = msprime.sim_ancestry(2, sequence_length=10, random_seed=42)
208199
return add_mutations(ts)
209200

210201
@pytest.fixture()
211-
def fx_no_individuals_ts(self, tmp_path):
212-
return simple_ts(add_individuals=False)
202+
def fx_ts_isolated_samples(self):
203+
tables = tskit.Tree.generate_balanced(2, span=10).tree_sequence.dump_tables()
204+
# This also tests sample nodes that are not a single block at
205+
# the start of the nodes table.
206+
tables.nodes.add_row(time=0, flags=tskit.NODE_IS_SAMPLE)
207+
return insert_branch_sites(tables.tree_sequence())
208+
209+
def test_path_or_ts_input(self, tmp_path, fx_simple_ts):
210+
f1 = tsk.TskitFormat(fx_simple_ts)
211+
ts_path = tmp_path / "trees.ts"
212+
fx_simple_ts.dump(ts_path)
213+
f2 = tsk.TskitFormat(ts_path)
214+
f1.ts.tables.assert_equals(f2.ts.tables)
213215

214216
def test_small_position_dtype(self):
215217
tables = tskit.TableCollection(sequence_length=100)
@@ -443,7 +445,7 @@ def test_isolated_as_missing(self, fx_ts_isolated_samples):
443445
expected_gt_missing = np.array([[1], [0], [-1]])
444446
nt.assert_array_equal(variant_data_missing.genotypes, expected_gt_missing)
445447

446-
def test_genotype_dtype_i1(self, tmp_path):
448+
def test_genotype_dtype_i1(self):
447449
tables = tskit.TableCollection(sequence_length=100)
448450
for _ in range(4):
449451
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
@@ -454,15 +456,13 @@ def test_genotype_dtype_i1(self, tmp_path):
454456
tables.mutations.add_row(site=site_id, node=0, derived_state="T")
455457
tables.sort()
456458
tree_sequence = tables.tree_sequence()
457-
ts_path = tmp_path / "small_alleles.trees"
458-
tree_sequence.dump(ts_path)
459459

460-
format_obj = tsk.TskitFormat(ts_path)
460+
format_obj = tsk.TskitFormat(tree_sequence)
461461
schema = format_obj.generate_schema()
462462
call_genotype_spec = next(s for s in schema.fields if s.name == "call_genotype")
463463
assert call_genotype_spec.dtype == "i1"
464464

465-
def test_genotype_dtype_i4(self, tmp_path):
465+
def test_genotype_dtype_i4(self):
466466
tables = tskit.TableCollection(sequence_length=100)
467467
for _ in range(4):
468468
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
@@ -475,10 +475,8 @@ def test_genotype_dtype_i4(self, tmp_path):
475475

476476
tables.sort()
477477
tree_sequence = tables.tree_sequence()
478-
ts_path = tmp_path / "large_alleles.trees"
479-
tree_sequence.dump(ts_path)
480478

481-
format_obj = tsk.TskitFormat(ts_path)
479+
format_obj = tsk.TskitFormat(tree_sequence)
482480
schema = format_obj.generate_schema()
483481
call_genotype_spec = next(s for s in schema.fields if s.name == "call_genotype")
484482
assert call_genotype_spec.dtype == "i4"
@@ -487,6 +485,7 @@ def test_genotype_dtype_i4(self, tmp_path):
487485
@pytest.mark.parametrize(
488486
"ts",
489487
[
488+
# Standard individuals-with-a-given-ploidy situation
490489
add_mutations(
491490
msprime.sim_ancestry(4, ploidy=1, sequence_length=10, random_seed=42)
492491
),
@@ -496,20 +495,20 @@ def test_genotype_dtype_i4(self, tmp_path):
496495
add_mutations(
497496
msprime.sim_ancestry(3, ploidy=12, sequence_length=10, random_seed=142)
498497
),
498+
# No individuals, ploidy1
499+
add_mutations(msprime.simulate(4, length=10, random_seed=412)),
499500
],
500501
)
501502
def test_against_tskit_vcf_output(ts, tmp_path):
502503
vcf_path = tmp_path / "ts.vcf"
503-
ts_path = tmp_path / "ts.trees"
504-
ts.dump(ts_path)
505504
with open(vcf_path, "w") as f:
506505
ts.write_vcf(f)
507506

508507
tskit_zarr = tmp_path / "tskit.zarr"
509508
vcf_zarr = tmp_path / "vcf.zarr"
510-
tsk.convert(ts_path, tskit_zarr)
509+
tsk.convert(ts, tskit_zarr, worker_processes=0)
511510

512-
vcf.convert([vcf_path], vcf_zarr)
511+
vcf.convert([vcf_path], vcf_zarr, worker_processes=0)
513512
ds1 = sg.load_dataset(tskit_zarr)
514513
ds2 = (
515514
sg.load_dataset(vcf_zarr)

0 commit comments

Comments
 (0)