Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 69 additions & 34 deletions _msprimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4791,6 +4791,39 @@ TableCollection_compute_mutation_parents(TableCollection *self)
return ret;
}

static PyObject *
TableCollection_deduplicate_sites(TableCollection *self)
{
int err;
PyObject *ret = NULL;

err = table_collection_deduplicate_sites(self->tables, 0);
if (err != 0) {
handle_library_error(err);
goto out;
}
ret = Py_BuildValue("");
out:
return ret;
}

/* Forward declaration */
static PyTypeObject TableCollectionType;

static PyObject *
TableCollection_equals(TableCollection *self, PyObject *args)
{
PyObject *ret = NULL;
TableCollection *other = NULL;

if (!PyArg_ParseTuple(args, "O!", &TableCollectionType, &other)) {
goto out;
}
ret = Py_BuildValue("i", table_collection_equals(self->tables, other->tables));
out:
return ret;
}

static PyGetSetDef TableCollection_getsetters[] = {
{"individuals", (getter) TableCollection_get_individuals, NULL, "The individual table."},
{"nodes", (getter) TableCollection_get_nodes, NULL, "The node table."},
Expand All @@ -4810,8 +4843,12 @@ static PyMethodDef TableCollection_methods[] = {
"Simplifies for a given sample subset." },
{"sort", (PyCFunction) TableCollection_sort, METH_VARARGS|METH_KEYWORDS,
"Sorts the tables to satisfy tree sequence requirements." },
{"equals", (PyCFunction) TableCollection_equals, METH_VARARGS,
"Returns True if the parameter table collection is equal to this one." },
{"compute_mutation_parents", (PyCFunction) TableCollection_compute_mutation_parents,
METH_NOARGS, "Computes the mutation parents for a the tables." },
{"deduplicate_sites", (PyCFunction) TableCollection_deduplicate_sites,
METH_NOARGS, "Removes sites with duplicate positions." },
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -4891,22 +4928,26 @@ MutationGenerator_init(MutationGenerator *self, PyObject *args, PyObject *kwds)
{
int ret = -1;
int err;
size_t block_size = 1024 * 1024;
static char *kwlist[] = {"random_generator", "mutation_rate", NULL};
int alphabet = 0;
static char *kwlist[] = {"random_generator", "mutation_rate", "alphabet", NULL};
double mutation_rate = 0;
RandomGenerator *random_generator = NULL;

self->mutgen = NULL;
self->random_generator = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!d", kwlist,
&RandomGeneratorType, &random_generator, &mutation_rate)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!d|i", kwlist,
&RandomGeneratorType, &random_generator, &mutation_rate, &alphabet)) {
goto out;
}
self->random_generator = random_generator;
Py_INCREF(self->random_generator);
if (RandomGenerator_check_state(self->random_generator) != 0) {
goto out;
}
if (alphabet != MSP_ALPHABET_BINARY && alphabet != MSP_ALPHABET_NUCLEOTIDE) {
PyErr_Format(PyExc_ValueError, "Bad mutation alphabet");
goto out;
}
if (mutation_rate < 0) {
PyErr_Format(PyExc_ValueError, "mutation_rate must be >= 0");
goto out;
Expand All @@ -4917,7 +4958,7 @@ MutationGenerator_init(MutationGenerator *self, PyObject *args, PyObject *kwds)
goto out;
}
err = mutgen_alloc(self->mutgen, mutation_rate, random_generator->rng,
0, block_size);
alphabet, 0);
if (err != 0) {
handle_library_error(err);
goto out;
Expand All @@ -4940,48 +4981,40 @@ MutationGenerator_get_mutation_rate(MutationGenerator *self)
return ret;
}

static PyObject *
MutationGenerator_get_alphabet(MutationGenerator *self)
{
PyObject *ret = NULL;

if (MutationGenerator_check_state(self) != 0) {
goto out;
}
ret = Py_BuildValue("i", self->mutgen->alphabet);
out:
return ret;
}

static PyObject *
MutationGenerator_generate(MutationGenerator *self, PyObject *args, PyObject *kwds)
{
int err;
PyObject *ret = NULL;
NodeTable *nodes = NULL;
EdgeTable *edges = NULL;
MutationTable *mutations = NULL;
SiteTable *sites = NULL;
static char *kwlist[] = {"nodes", "edges", "sites", "mutations", NULL};
TableCollection *tables = NULL;
int flags = 0;
int keep = 0;
static char *kwlist[] = {"tables", "keep", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!O!", kwlist,
&NodeTableType, &nodes,
&EdgeTableType, &edges,
&SiteTableType, &sites,
&MutationTableType, &mutations)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|i", kwlist,
&TableCollectionType, &tables, &keep)) {
goto out;
}
if (MutationGenerator_check_state(self) != 0) {
goto out;
}
if (NodeTable_check_state(nodes) != 0) {
goto out;
}
if (EdgeTable_check_state(edges) != 0) {
goto out;
}
if (SiteTable_check_state(sites) != 0) {
goto out;
}
if (MutationTable_check_state(mutations) != 0) {
goto out;
}
err = mutgen_generate_tables_tmp(self->mutgen, nodes->table,
edges->table);
if (err != 0) {
handle_library_error(err);
goto out;
if (keep) {
flags = MSP_KEEP_SITES;
}
err = mutgen_populate_tables(self->mutgen, sites->table,
mutations->table);
err = mutgen_generate(self->mutgen, tables->tables, flags);
if (err != 0) {
handle_library_error(err);
goto out;
Expand All @@ -4998,6 +5031,8 @@ static PyMemberDef MutationGenerator_members[] = {
static PyMethodDef MutationGenerator_methods[] = {
{"get_mutation_rate", (PyCFunction) MutationGenerator_get_mutation_rate,
METH_NOARGS, "Returns the mutation rate for this mutation generator."},
{"get_alphabet", (PyCFunction) MutationGenerator_get_alphabet,
METH_NOARGS, "Returns the alphabet for this mutation generator."},
{"generate", (PyCFunction) MutationGenerator_generate,
METH_VARARGS|METH_KEYWORDS,
"Generate mutations and write to the specified table."},
Expand Down
26 changes: 26 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ Variable recombination rates
.. autoclass:: msprime.RecombinationMap
:members:

++++++++++++++++++++
Simulating mutations
++++++++++++++++++++

When running coalescent simulations it's usually most convenient to use the
``mutation_rate`` argument to the :func:`.simulate` function to throw neutral
mutations down on the trees. However, sometimes we wish to throw mutations
down on an existing tree sequence: for example, if we want to see the outcome
of different random mutational processes on top of a single simulated topology,
or if we have obtained the tree sequence from another program and wish to
overlay neutral mutations on this tree sequence.

.. data:: msprime.BINARY == 0

The binary mutation alphabet where ancestral states are always "0" and
derived states "1".

.. data:: msprime.NUCLEOTIDES == 1

The nucleotides mutation alphabet in which ancestral and derived states are
chosen from the characters "A", "C", "G" and "T".

.. autoclass:: msprime.InfiniteSites

.. autofunction:: msprime.mutate


************************
Using simulation results
Expand Down
6 changes: 1 addition & 5 deletions lib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,11 +894,7 @@ run_simulate(const char *conf_file, const char *output_file, int verbose, int nu
if (ret != 0) {
goto out;
}
ret = mutgen_generate_tables_tmp(mutgen, tables.nodes, tables.edges);
if (ret != 0) {
goto out;
}
ret = mutgen_populate_tables(mutgen, tables.sites, tables.mutations);
ret = mutgen_generate(mutgen, &tables, 0);
if (ret != 0) {
goto out;
}
Expand Down
21 changes: 10 additions & 11 deletions lib/msprime.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
#define MSP_MODEL_DIRAC 4
#define MSP_MODEL_DTWF 5

/* Alphabets for mutation generator */
#define MSP_ALPHABET_BINARY 0
#define MSP_ALPHABET_NUCLEOTIDE 1

/* Flags for mutgen */
#define MSP_KEEP_SITES 1


typedef struct segment_t_t {
population_id_t population_id;
Expand Down Expand Up @@ -236,12 +243,8 @@ typedef struct {
int alphabet;
gsl_rng *rng;
double mutation_rate;
size_t num_mutations;
size_t max_num_mutations;
size_t mutation_block_size;
site_table_t *sites;
infinite_sites_mutation_t *mutations;
object_heap_t avl_node_heap;
avl_tree_t sites;
block_allocator_t allocator;
} mutgen_t;

int msp_alloc(msp_t *self, size_t num_samples, sample_t *samples, gsl_rng *rng);
Expand Down Expand Up @@ -352,11 +355,7 @@ void recomb_map_print_state(recomb_map_t *self, FILE *out);
int mutgen_alloc(mutgen_t *self, double mutation_rate, gsl_rng *rng,
int alphabet, size_t mutation_block_size);
int mutgen_free(mutgen_t *self);
/* TODO finalise this interface */
int mutgen_generate_tables_tmp(mutgen_t *self, node_table_t *nodes,
edge_table_t *edges);
int mutgen_populate_tables(mutgen_t *self, site_table_t *sites,
mutation_table_t *mutations);
int mutgen_generate(mutgen_t *self, table_collection_t *tables, int flags);
void mutgen_print_state(mutgen_t *self, FILE *out);

double compute_falling_factorial_log(unsigned int m);
Expand Down
Loading