...
 
Commits (8)
......@@ -9,5 +9,5 @@ pyvenv.cfg
pip-selfcheck.json
descriptionextractor.egg-info/
description_extractor.egg-info/
extractor-core/
# description-extractor
HGVS variant description extractor
HGVS variant description extractor
Extract a list of differences between two sequences.
## Installation
`git clone git@github.com:mutalyzer/description-extractor.git`
`git checkout mutalyzer3`
`python setup.py install` or `pip install .`
## Use
`import extractor`
`variants = extractor.describe_dna('AAATAA', 'AAAGAAA')`
......@@ -10,7 +10,112 @@
static PyObject*
extractor_extract_dna(PyObject*, PyObject* args)
point_location(size_t const point)
{
return Py_BuildValue("{s:s,s:i}", "type", "point", "position", point);
} // point_location
static PyObject*
range_location(size_t const start, size_t const end)
{
PyObject* start_object = point_location(start);
if (start_object == NULL)
{
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
PyObject* end_object = point_location(end);
if (end_object == NULL)
{
Py_DECREF(start_object);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
return Py_BuildValue("{s:s,s:O,s:O}", "type", "range", "start", start_object, "end", end_object);
} // range_location
static PyObject*
insertion_dict(mutalyzer::Variant const &variant)
{
PyObject* reference_range = range_location(variant.reference_start, variant.reference_end);
if (reference_range == NULL)
{
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
return Py_BuildValue("{s:s,s:O}", "source", "observed", "location", reference_range);
} // insertion_dict
static PyObject*
variant_dict(std::vector<mutalyzer::Variant>::const_iterator &it)
{
PyObject* sample_range = range_location(it->sample_start, it->sample_end);
if (sample_range == NULL)
{
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
PyObject* inserted = NULL;
if ((it->type & mutalyzer::TRANSPOSITION_OPEN) == mutalyzer::TRANSPOSITION_OPEN)
{
inserted = PyList_New(0);
if (inserted == NULL)
{
Py_DECREF(sample_range);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for PyList_New");
return NULL;
} // if
while ((it->type & mutalyzer::TRANSPOSITION_CLOSE) != mutalyzer::TRANSPOSITION_CLOSE)
{
PyObject* item = insertion_dict(*it);
if (item == NULL)
{
Py_DECREF(sample_range);
Py_DECREF(inserted);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
if (PyList_Append(inserted, item) != 0)
{
Py_DECREF(sample_range);
Py_DECREF(inserted);
Py_DECREF(item);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for PyList_Append");
return NULL;
} // if
++it;
} // while
} // if
else if (it->type == mutalyzer::IDENTITY)
{
return Py_BuildValue("{s:s,s:O}", "type", "equal", "location", sample_range);
} // if
else if (it->type == mutalyzer::REVERSE_COMPLEMENT)
{
return Py_BuildValue("{s:s,s:O}", "type", "inv", "location", sample_range);
} // if
if (inserted == NULL)
{
inserted = insertion_dict(*it);
if (inserted == NULL)
{
Py_DECREF(sample_range);
Py_DECREF(inserted);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
} // if
return Py_BuildValue("{s:s,s:O,s:[O]}", "type", "delins", "location", sample_range, "insertions", inserted);
} // variant_dict
static PyObject*
extractor_describe_dna(PyObject*, PyObject* args)
{
char* reference;
char* sample;
......@@ -34,33 +139,33 @@ extractor_extract_dna(PyObject*, PyObject* args)
for (std::vector<mutalyzer::Variant>::const_iterator it = variants.begin(); it != variants.end(); ++it)
{
PyObject* item = Py_BuildValue("{s:i, s:i, s:i, s:i, s:i, s:i, s:i}",
"reference_start", it->reference_start,
"reference_end", it->reference_end,
"sample_start", it->sample_start,
"sample_end", it->sample_end,
"type", it->type,
"transposition_start", it->transposition_start,
"transposition_end", it->transposition_end);
PyObject* item = variant_dict(it);
if (item == NULL)
{
Py_DECREF(result);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for Py_BuildValue");
return NULL;
} // if
if (PyList_Append(result, item) != 0)
{
Py_DECREF(item);
Py_DECREF(result);
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for PyList_Append");
return NULL;
} // if
} // for
return result;
} // extractor_extract
} // extractor_describe_dna
static PyMethodDef ExtractorMethods[] =
{
{"extract_dna", extractor_extract_dna, METH_VARARGS, "Run the extract function."},
{"describe_dna", extractor_describe_dna, METH_VARARGS,
"Give an allele description of the change from {reference} to {observed}.\n"
" :arg ascii reference: Reference sequence.\n"
" :arg ascii observed: Observed sequence.\n"
" :returns list({}): A list of dictionaries representing the obsereved allele in terms of the reference sequence."
},
{NULL, NULL, 0, NULL} // sentinel
}; // ExtractorMethods
......@@ -69,22 +174,23 @@ static PyMethodDef ExtractorMethods[] =
static struct PyModuleDef extractormodule =
{
PyModuleDef_HEAD_INIT,
"descriptionextractor",
"HGVS Variant Description Extractor",
"description-extractor",
"HGVS variant description extractor\n"
"Extract a list of differences between two sequences.",
-1,
ExtractorMethods
}; // extractormodule
PyMODINIT_FUNC
PyInit_descriptionextractor(void)
PyInit_extractor(void)
{
PyObject* module = PyModule_Create(&extractormodule);
PyModule_AddStringConstant(module, "version", mutalyzer::VERSION);
PyModule_AddStringConstant(module, "core_version", mutalyzer::VERSION);
return module;
} // PyInit_descriptionextractor
} // PyInit_extractor
int
......@@ -97,7 +203,7 @@ main(int, char* argv[])
return EXIT_FAILURE;
} // if
PyImport_AppendInittab("descriptionextractor", PyInit_descriptionextractor);
PyImport_AppendInittab("extractor", PyInit_extractor);
Py_SetProgramName(program);
......
......@@ -2,16 +2,26 @@ from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import subprocess
RELEASE = False
VERSION_MAJOR = 3
VERSION_MINOR = 0
VERSION_PATCH = 0
VERSION = '.'.join(map(str, [VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH]))
if not RELEASE:
VERSION += '-dev'
class git_clone_external(build_ext):
def run(self):
subprocess.check_call(['rm', '-rf', 'extractor-core'])
subprocess.check_call(['git', 'clone', 'git@github.com:mutalyzer/extractor-core.git'])
build_ext.run(self)
extractor = Extension('descriptionextractor', sources = ['extractor-module.cc',
'extractor-core/src/extractor.cc'])
setup(name = 'descriptionextractor',
version = '3.0.0',
extractor = Extension('extractor', sources = ['extractor-module.cc',
'extractor-core/src/extractor.cc'])
setup(name = 'description-extractor',
version = VERSION,
cmdclass = {'build_ext': git_clone_external},
description = 'HGVS variant description extractor',
ext_modules = [extractor]
......