Skip to content

Commit cacfda1

Browse files
benjefferymergify[bot]
authored andcommitted
Remove remains of the lowlevel VariantGenerator
1 parent 55393cd commit cacfda1

File tree

2 files changed

+0
-223
lines changed

2 files changed

+0
-223
lines changed

python/_tskitmodule.c

-168
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,6 @@ typedef struct {
148148
tsk_diff_iter_t *tree_diff_iterator;
149149
} TreeDiffIterator;
150150

151-
typedef struct {
152-
PyObject_HEAD
153-
TreeSequence *tree_sequence;
154-
tsk_vargen_t *variant_generator;
155-
} VariantGenerator;
156-
157151
typedef struct {
158152
PyObject_HEAD
159153
TreeSequence *tree_sequence;
@@ -584,27 +578,6 @@ make_alleles(tsk_variant_t *variant)
584578
return ret;
585579
}
586580

587-
static PyObject *
588-
make_variant(tsk_variant_t *variant, tsk_size_t num_samples)
589-
{
590-
PyObject *ret = NULL;
591-
npy_intp dims = num_samples;
592-
PyObject *alleles = make_alleles(variant);
593-
PyArrayObject *genotypes = (PyArrayObject *) PyArray_SimpleNew(1, &dims, NPY_INT32);
594-
595-
/* TODO update this to account for 16 bit variants when we provide the
596-
* high-level interface. */
597-
if (genotypes == NULL || alleles == NULL) {
598-
goto out;
599-
}
600-
memcpy(PyArray_DATA(genotypes), variant->genotypes, num_samples * sizeof(int32_t));
601-
ret = Py_BuildValue("iOO", variant->site.id, genotypes, alleles);
602-
out:
603-
Py_XDECREF(genotypes);
604-
Py_XDECREF(alleles);
605-
return ret;
606-
}
607-
608581
static PyObject *
609582
convert_sites(const tsk_site_t *sites, tsk_size_t num_sites)
610583
{
@@ -11167,140 +11140,6 @@ static PyTypeObject TreeDiffIteratorType = {
1116711140
// clang-format on
1116811141
};
1116911142

11170-
/*===================================================================
11171-
* VariantGenerator
11172-
*===================================================================
11173-
*/
11174-
11175-
static int
11176-
VariantGenerator_check_state(VariantGenerator *self)
11177-
{
11178-
int ret = 0;
11179-
if (self->variant_generator == NULL) {
11180-
PyErr_SetString(PyExc_SystemError, "converter not initialised");
11181-
ret = -1;
11182-
}
11183-
return ret;
11184-
}
11185-
11186-
static void
11187-
VariantGenerator_dealloc(VariantGenerator *self)
11188-
{
11189-
if (self->variant_generator != NULL) {
11190-
tsk_vargen_free(self->variant_generator);
11191-
PyMem_Free(self->variant_generator);
11192-
self->variant_generator = NULL;
11193-
}
11194-
Py_XDECREF(self->tree_sequence);
11195-
Py_TYPE(self)->tp_free((PyObject *) self);
11196-
}
11197-
11198-
static int
11199-
VariantGenerator_init(VariantGenerator *self, PyObject *args, PyObject *kwds)
11200-
{
11201-
int ret = -1;
11202-
int err;
11203-
static char *kwlist[]
11204-
= { "tree_sequence", "samples", "isolated_as_missing", "alleles", NULL };
11205-
TreeSequence *tree_sequence = NULL;
11206-
PyObject *samples_input = Py_None;
11207-
PyObject *py_alleles = Py_None;
11208-
PyArrayObject *samples_array = NULL;
11209-
tsk_id_t *samples = NULL;
11210-
tsk_size_t num_samples = 0;
11211-
int isolated_as_missing = 1;
11212-
const char **alleles = NULL;
11213-
npy_intp *shape;
11214-
tsk_flags_t options = 0;
11215-
11216-
self->variant_generator = NULL;
11217-
self->tree_sequence = NULL;
11218-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|OiO", kwlist, &TreeSequenceType,
11219-
&tree_sequence, &samples_input, &isolated_as_missing, &py_alleles)) {
11220-
goto out;
11221-
}
11222-
if (!isolated_as_missing) {
11223-
options |= TSK_ISOLATED_NOT_MISSING;
11224-
}
11225-
self->tree_sequence = tree_sequence;
11226-
Py_INCREF(self->tree_sequence);
11227-
if (TreeSequence_check_state(self->tree_sequence) != 0) {
11228-
goto out;
11229-
}
11230-
if (samples_input != Py_None) {
11231-
samples_array = (PyArrayObject *) PyArray_FROMANY(
11232-
samples_input, NPY_INT32, 1, 1, NPY_ARRAY_IN_ARRAY);
11233-
if (samples_array == NULL) {
11234-
goto out;
11235-
}
11236-
shape = PyArray_DIMS(samples_array);
11237-
num_samples = (tsk_size_t) shape[0];
11238-
samples = PyArray_DATA(samples_array);
11239-
}
11240-
if (py_alleles != Py_None) {
11241-
alleles = parse_allele_list(py_alleles);
11242-
if (alleles == NULL) {
11243-
goto out;
11244-
}
11245-
}
11246-
self->variant_generator = PyMem_Malloc(sizeof(tsk_vargen_t));
11247-
if (self->variant_generator == NULL) {
11248-
PyErr_NoMemory();
11249-
goto out;
11250-
}
11251-
/* Note: the vargen currently takes a copy of the samples list. If we wanted
11252-
* to avoid this we would INCREF the samples array above and keep a reference
11253-
* to in the object struct */
11254-
err = tsk_vargen_init(self->variant_generator, self->tree_sequence->tree_sequence,
11255-
samples, num_samples, alleles, options);
11256-
if (err != 0) {
11257-
handle_library_error(err);
11258-
goto out;
11259-
}
11260-
ret = 0;
11261-
out:
11262-
PyMem_Free(alleles);
11263-
Py_XDECREF(samples_array);
11264-
return ret;
11265-
}
11266-
11267-
static PyObject *
11268-
VariantGenerator_next(VariantGenerator *self)
11269-
{
11270-
PyObject *ret = NULL;
11271-
tsk_variant_t *var;
11272-
int err;
11273-
11274-
if (VariantGenerator_check_state(self) != 0) {
11275-
goto out;
11276-
}
11277-
err = tsk_vargen_next(self->variant_generator, &var);
11278-
if (err < 0) {
11279-
handle_library_error(err);
11280-
goto out;
11281-
}
11282-
if (err == 1) {
11283-
ret = make_variant(var, var->num_samples);
11284-
}
11285-
out:
11286-
return ret;
11287-
}
11288-
11289-
static PyTypeObject VariantGeneratorType = {
11290-
// clang-format off
11291-
PyVarObject_HEAD_INIT(NULL, 0)
11292-
.tp_name = "_tskit.VariantGenerator",
11293-
.tp_basicsize = sizeof(VariantGenerator),
11294-
.tp_dealloc = (destructor) VariantGenerator_dealloc,
11295-
.tp_flags = Py_TPFLAGS_DEFAULT,
11296-
.tp_doc = "VariantGenerator objects",
11297-
.tp_iter = PyObject_SelfIter,
11298-
.tp_iternext = (iternextfunc) VariantGenerator_next,
11299-
.tp_init = (initproc) VariantGenerator_init,
11300-
.tp_new = PyType_GenericNew,
11301-
// clang-format on
11302-
};
11303-
1130411143
/*===================================================================
1130511144
* Variant
1130611145
*===================================================================
@@ -12530,13 +12369,6 @@ PyInit__tskit(void)
1253012369
Py_INCREF(&TreeDiffIteratorType);
1253112370
PyModule_AddObject(module, "TreeDiffIterator", (PyObject *) &TreeDiffIteratorType);
1253212371

12533-
/* VariantGenerator type */
12534-
if (PyType_Ready(&VariantGeneratorType) < 0) {
12535-
return NULL;
12536-
}
12537-
Py_INCREF(&VariantGeneratorType);
12538-
PyModule_AddObject(module, "VariantGenerator", (PyObject *) &VariantGeneratorType);
12539-
1254012372
/* Variant type */
1254112373
if (PyType_Ready(&VariantType) < 0) {
1254212374
return NULL;

python/tests/test_lowlevel.py

-55
Original file line numberDiff line numberDiff line change
@@ -2278,61 +2278,6 @@ def test_iterator(self):
22782278
self.verify_iterator(_tskit.TreeDiffIterator(ts))
22792279

22802280

2281-
class TestVariantGenerator(LowLevelTestCase):
2282-
"""
2283-
Tests for the VariantGenerator class.
2284-
"""
2285-
2286-
def test_uninitialised_tree_sequence(self):
2287-
ts = _tskit.TreeSequence()
2288-
with pytest.raises(ValueError):
2289-
_tskit.VariantGenerator(ts)
2290-
2291-
def test_constructor(self):
2292-
with pytest.raises(TypeError):
2293-
_tskit.VariantGenerator()
2294-
with pytest.raises(TypeError):
2295-
_tskit.VariantGenerator(None)
2296-
ts = self.get_example_tree_sequence()
2297-
with pytest.raises(ValueError):
2298-
_tskit.VariantGenerator(ts, samples={})
2299-
with pytest.raises(TypeError):
2300-
_tskit.VariantGenerator(ts, impute_missing_data=None)
2301-
with pytest.raises(_tskit.LibraryError):
2302-
_tskit.VariantGenerator(ts, samples=[-1, 2])
2303-
with pytest.raises(TypeError):
2304-
_tskit.VariantGenerator(ts, alleles=1234)
2305-
2306-
def test_alleles(self):
2307-
ts = self.get_example_tree_sequence()
2308-
for bad_type in [["a", "b"], "sdf", 234]:
2309-
with pytest.raises(TypeError):
2310-
_tskit.VariantGenerator(ts, samples=[1, 2], alleles=bad_type)
2311-
with pytest.raises(ValueError):
2312-
_tskit.VariantGenerator(ts, samples=[1, 2], alleles=tuple())
2313-
2314-
for bad_allele_type in [None, 0, b"x", []]:
2315-
with pytest.raises(TypeError):
2316-
_tskit.VariantGenerator(ts, samples=[1, 2], alleles=(bad_allele_type,))
2317-
2318-
def test_iterator(self):
2319-
ts = self.get_example_tree_sequence()
2320-
self.verify_iterator(_tskit.VariantGenerator(ts))
2321-
2322-
def test_missing_data(self):
2323-
tables = _tskit.TableCollection(1)
2324-
tables.nodes.add_row(flags=1, time=0)
2325-
tables.nodes.add_row(flags=1, time=0)
2326-
tables.sites.add_row(0.1, "A")
2327-
tables.build_index()
2328-
ts = _tskit.TreeSequence(0)
2329-
ts.load_tables(tables)
2330-
variant = list(_tskit.VariantGenerator(ts))[0]
2331-
_, genotypes, alleles = variant
2332-
assert np.all(genotypes == -1)
2333-
assert alleles == ("A", None)
2334-
2335-
23362281
class TestVariant(LowLevelTestCase):
23372282
"""
23382283
Tests for the Variant class.

0 commit comments

Comments
 (0)