1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include <Python.h>
static void
calculate(const char sequence[], int s, Py_ssize_t m, double* matrix,
Py_ssize_t n, float* scores)
{
Py_ssize_t i, j;
char c;
double score;
int ok;
float* p = scores;
float nan = 0.0;
nan /= nan;
for (i = 0; i < n; i++)
{
score = 0.0;
ok = 1;
for (j = 0; j < m; j++)
{
c = sequence[i+j];
switch (c)
{
/* Handling mixed case input here rather than converting it to
uppercase in Python code first, since doing so could use too
much memory if sequence is too long (e.g. chromosome or
plasmid). */
case 'A':
case 'a':
score += matrix[j*4+0]; break;
case 'C':
case 'c':
score += matrix[j*4+1]; break;
case 'G':
case 'g':
score += matrix[j*4+2]; break;
case 'T':
case 't':
score += matrix[j*4+3]; break;
default:
ok = 0;
}
}
if (ok) *p = (float)score;
else *p = nan;
p++;
}
}
static int
matrix_converter(PyObject* object, void* address)
{
const int flags = PyBUF_C_CONTIGUOUS | PyBUF_FORMAT;
char datatype;
Py_buffer* view = address;
if (PyObject_GetBuffer(object, view, flags) == -1) {
PyErr_SetString(PyExc_RuntimeError,
"position-weight matrix is not an array");
return 0;
}
datatype = view->format[0];
switch (datatype) {
case '@':
case '=':
case '<':
case '>':
case '!': datatype = view->format[1]; break;
default: break;
}
if (datatype != 'd') {
PyErr_Format(PyExc_RuntimeError,
"position-weight matrix data format incorrect ('%c', expected 'd')",
datatype);
return 0;
}
if (view->ndim != 2) {
PyErr_Format(PyExc_RuntimeError,
"position-weight matrix has incorrect rank (%d expected 2)",
view->ndim);
return 0;
}
if (view->shape[1] != 4) {
PyErr_Format(PyExc_RuntimeError,
"position-weight matrix should have four columns "
"(%zd columns found)", view->shape[1]);
return 0;
}
return 1;
}
static int
scores_converter(PyObject* object, void* address)
{
const int flags = PyBUF_C_CONTIGUOUS | PyBUF_FORMAT;
char datatype;
Py_buffer* view = address;
if (PyObject_GetBuffer(object, view, flags) == -1)
return 0;
datatype = view->format[0];
switch (datatype) {
case '@':
case '=':
case '<':
case '>':
case '!': datatype = view->format[1]; break;
default: break;
}
if (datatype != 'f') {
PyErr_Format(PyExc_RuntimeError,
"scores array has incorrect data format ('%c', expected 'f')",
datatype);
return 0;
}
if (view->ndim != 1) {
PyErr_Format(PyExc_ValueError,
"scores array has incorrect rank (%d expected 1)",
view->ndim);
return 0;
}
return 1;
}
static char calculate__doc__[] =
" calculate(sequence, pwm) -> array of score values\n"
"\n"
"This function calculates the position-weight matrix scores for all\n"
"positions along the sequence, and returns them as a Numerical Python\n"
"array.\n";
static PyObject*
py_calculate(PyObject* self, PyObject* args, PyObject* keywords)
{
const char* sequence;
static char* kwlist[] = {"sequence", "matrix", "scores", NULL};
Py_ssize_t m;
Py_ssize_t n;
int s;
PyObject* result = NULL;
Py_buffer scores;
Py_buffer matrix;
matrix.obj = NULL;
scores.obj = NULL;
if(!PyArg_ParseTupleAndKeywords(args, keywords, "s#O&O&", kwlist,
&sequence,
&s,
matrix_converter, &matrix,
scores_converter, &scores)) goto exit;
m = matrix.shape[0];
n = scores.shape[0];
if (n != s - m + 1) {
PyErr_SetString(PyExc_RuntimeError,
"size of scores array is inconsistent");
goto exit;
}
calculate(sequence, s, m, matrix.buf, n, scores.buf);
Py_INCREF(Py_None);
result = Py_None;
exit:
if (matrix.obj) PyBuffer_Release(&matrix);
if (scores.obj) PyBuffer_Release(&scores);
return result;
}
static struct PyMethodDef methods[] = {
{"calculate", (PyCFunction)py_calculate, METH_VARARGS | METH_KEYWORDS, calculate__doc__},
{NULL, NULL, 0, NULL} /* sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_pwm",
"Fast calculations involving position-weight matrices",
-1,
methods,
NULL,
NULL,
NULL,
NULL
};
PyObject*
PyInit__pwm(void)
#else
void init_pwm(void)
#endif
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
if (m==NULL) return NULL;
#else
m = Py_InitModule4("_pwm",
methods,
"Fast calculations involving position-weight matrices",
NULL,
PYTHON_API_VERSION);
if (m==NULL) return;
#endif
if (PyErr_Occurred()) Py_FatalError("can't initialize module _pwm");
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}
|