Skip to content

Commit 56754f2

Browse files
Bump up test coverage
1 parent 6527ccf commit 56754f2

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

bio2zarr/tskit.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def __init__(
4545
if self._num_samples < 1:
4646
raise ValueError("individuals_nodes must have at least one sample")
4747
self.max_ploidy = individuals_nodes.shape[1]
48-
if sample_ids is None:
49-
sample_ids = [f"tsk_{j}" for j in range(self._num_samples)]
50-
elif len(sample_ids) != self._num_samples:
48+
if len(sample_ids) != self._num_samples:
5149
raise ValueError(
5250
f"Length of sample_ids ({len(sample_ids)}) does not match "
5351
f"number of samples ({self._num_samples})"

tests/test_tskit.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def test_missing_dependency():
2929
)
3030

3131

32+
def tskit_model_mapping(ind_nodes, ind_names=None):
33+
if ind_names is None:
34+
ind_names = ["tsk{j}" for j in range(len(ind_nodes))]
35+
return tskit.VcfModelMapping(ind_nodes, ind_names)
36+
37+
3238
def add_mutations(ts):
3339
# Add some mutation to the tree sequence. This guarantees that
3440
# we have variation at all sites > 0.
@@ -311,6 +317,23 @@ def test_iter_field(self, fx_simple_ts):
311317
with pytest.raises(ValueError, match="Unknown field"):
312318
list(format_obj.iter_field("unknown_field", None, 0, 3))
313319

320+
def test_zero_samples(self, fx_simple_ts):
321+
model_mapping = tskit_model_mapping(np.array([]))
322+
with pytest.raises(ValueError, match="at least one sample"):
323+
tsk.TskitFormat(fx_simple_ts, model_mapping=model_mapping)
324+
325+
def test_no_valid_samples(self, fx_simple_ts):
326+
model_mapping = fx_simple_ts.map_to_vcf_model()
327+
model_mapping.individuals_nodes[:] = -1
328+
with pytest.raises(ValueError, match="at least one valid sample"):
329+
tsk.TskitFormat(fx_simple_ts, model_mapping=model_mapping)
330+
331+
def test_model_size_mismatch(self, fx_simple_ts):
332+
model_mapping = fx_simple_ts.map_to_vcf_model()
333+
model_mapping.individuals_name = ["x"]
334+
with pytest.raises(ValueError, match="match number of samples"):
335+
tsk.TskitFormat(fx_simple_ts, model_mapping=model_mapping)
336+
314337
@pytest.mark.parametrize(
315338
("ind_nodes", "expected_gts"),
316339
[
@@ -347,10 +370,7 @@ def test_iter_field(self, fx_simple_ts):
347370
],
348371
)
349372
def test_iter_alleles_and_genotypes(self, fx_simple_ts, ind_nodes, expected_gts):
350-
model_mapping = tskit.VcfModelMapping(
351-
ind_nodes, ["tsk{j}" for j in range(len(ind_nodes))]
352-
)
353-
373+
model_mapping = tskit_model_mapping(ind_nodes)
354374
format_obj = tsk.TskitFormat(fx_simple_ts, model_mapping=model_mapping)
355375

356376
shape = (2, 2) # (num_samples, max_ploidy)
@@ -375,9 +395,7 @@ def test_iter_alleles_and_genotypes(self, fx_simple_ts, ind_nodes, expected_gts)
375395
def test_iter_alleles_and_genotypes_missing_node(self, fx_ts_2_diploids):
376396
# Test with node ID that doesn't exist in tree sequence (out of range)
377397
ind_nodes = np.array([[10, 11], [12, 13]], dtype=np.int32)
378-
model_mapping = tskit.VcfModelMapping(
379-
ind_nodes, ["tsk{j}" for j in range(len(ind_nodes))]
380-
)
398+
model_mapping = tskit_model_mapping(ind_nodes)
381399
format_obj = tsk.TskitFormat(fx_ts_2_diploids, model_mapping=model_mapping)
382400
shape = (2, 2)
383401
with pytest.raises(
@@ -387,9 +405,7 @@ def test_iter_alleles_and_genotypes_missing_node(self, fx_ts_2_diploids):
387405

388406
def test_isolated_as_missing(self, fx_ts_isolated_samples):
389407
ind_nodes = np.array([[0], [1], [3]])
390-
model_mapping = tskit.VcfModelMapping(
391-
ind_nodes, ["tsk{j}" for j in range(len(ind_nodes))]
392-
)
408+
model_mapping = tskit_model_mapping(ind_nodes)
393409

394410
format_obj_default = tsk.TskitFormat(
395411
fx_ts_isolated_samples,

0 commit comments

Comments
 (0)