-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlinearpartitionmodule.cc
More file actions
333 lines (287 loc) · 9.77 KB
/
linearpartitionmodule.cc
File metadata and controls
333 lines (287 loc) · 9.77 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Python interface for LinearPartition
*
* Copyright 2024 Hyeshik Chang
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* “Software”), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include <fstream>
int
trap_fprintf(FILE *fp, const char *fmt, ...)
{
/* Block outputs to stderr */
return 0;
}
int
trap_printf(const char *fmt, ...)
{
/* Block outputs to stderr */
return 0;
}
#define private protected
#undef fprintf
#define fprintf trap_fprintf
#undef printf
#define printf trap_printf
// Intercept the MEA structure output
#define __mea_hook__ (1) { threshknot_file_index = structure; } if (1)
// Monkey patch LinearPartition symbols to allow double-linking of E and V
/* EternaFold model */
#define main _linearpartition_e_main
#define BeamCKYParser LPE_BeamCKYParser
#define multi_base LPE_multi_base
#define multi_unpaired LPE_multi_unpaired
#define multi_paired LPE_multi_paired
#define external_unpaired LPE_external_unpaired
#define external_paired LPE_external_paired
#define base_pair LPE_base_pair
#define internal_1x1_nucleotides LPE_internal_1x1_nucleotides
#define helix_stacking LPE_helix_stacking
#define terminal_mismatch LPE_terminal_mismatch
#define bulge_0x1_nucleotides LPE_bulge_0x1_nucleotides
#define helix_closing LPE_helix_closing
#define dangle_left LPE_dangle_left
#define dangle_right LPE_dangle_right
#define internal_explicit LPE_internal_explicit
#define hairpin_length LPE_hairpin_length
#define bulge_length LPE_bulge_length
#define internal_length LPE_internal_length
#define internal_symmetric_length LPE_internal_symmetric_length
#define internal_asymmetry LPE_internal_asymmetry
#define hairpin_length_at_least LPE_hairpin_length_at_least
#define bulge_length_at_least LPE_bulge_length_at_least
#define internal_length_at_least LPE_internal_length_at_least
#define internal_symmetric_length_at_least LPE_internal_symmetric_length_at_least
#define internal_asymmetry_at_least LPE_internal_asymmetry_at_least
#include "contrib/feature_weight_e.h"
#include "LinearPartition.cpp"
#undef multi_base
#undef multi_unpaired
#undef multi_paired
#undef external_unpaired
#undef external_paired
#undef base_pair
#undef internal_1x1_nucleotides
#undef helix_stacking
#undef terminal_mismatch
#undef bulge_0x1_nucleotides
#undef helix_closing
#undef dangle_left
#undef dangle_right
#undef internal_explicit
#undef hairpin_length
#undef bulge_length
#undef internal_length
#undef internal_symmetric_length
#undef internal_asymmetry
#undef hairpin_length_at_least
#undef bulge_length_at_least
#undef internal_length_at_least
#undef internal_symmetric_length_at_least
#undef internal_asymmetry_at_least
#define lpv /* Vienna model */
#undef FASTCKY_BEAMCKYPAR_H
#undef FASTCKY_W
#undef main
#undef BeamCKYParser
#define main _linearpartition_v_main
#define BeamCKYParser LPV_BeamCKYParser
#define hash_pair LPV_hash_pair
#define comp LPV_comp
#define State LPV_State
#define Fast_Exp LPV_Fast_Exp
#define Fast_LogExpPlusOne LPV_Fast_LogExpPlusOne
#define Fast_LogPlusEquals LPV_Fast_LogPlusEquals
#define quickselect LPV_quickselect
#define quickselect_partition LPV_quickselect_partition
#define rtrim LPV_rtrim
#define pf_type LPV_pf_type
#define value_type LPV_value_type
#include "LinearPartition.cpp"
#undef State
#undef main
#undef private
#undef fprintf
#undef printf
struct basepair_prob {
int32_t i;
int32_t j;
double prob;
};
static PyArray_Descr *partition_return_descr;
template<typename BaseParser>
class BeamCKYParserTemplate : public BaseParser {
public:
using BaseParser::BaseParser; // Inherit constructors
PyObject* get_basepair_prob(void) {
PyArrayObject* res;
npy_intp dim;
struct basepair_prob* bpp;
dim = this->Pij.size();
res = (PyArrayObject*)PyArray_SimpleNewFromDescr(1, &dim, partition_return_descr);
if (res == NULL)
return NULL;
Py_INCREF(partition_return_descr);
assert(partition_return_descr->elsize == sizeof(struct basepair_prob));
bpp = (struct basepair_prob*)PyArray_DATA(res);
for (auto it = this->Pij.begin(); it != this->Pij.end(); ++it) {
bpp->i = it->first.first - 1;
bpp->j = it->first.second - 1;
bpp->prob = it->second;
bpp++;
}
return (PyObject*)res;
}
};
class EternaBeamCKYParser : public BeamCKYParserTemplate<LPE_BeamCKYParser> {
public:
using BeamCKYParserTemplate::BeamCKYParserTemplate; // Inherit constructors
};
class ViennaBeamCKYParser : public BeamCKYParserTemplate<LPV_BeamCKYParser> {
public:
using BeamCKYParserTemplate::BeamCKYParserTemplate; // Inherit constructors
};
PyDoc_STRVAR(linearpartition_partition_doc,
"partition(seq, mode='vienna', beamsize=100, sharpturn=False, cutoff=0.0,\n"
" gamma=3.0, dangles=2)\n"
"\n"
"Returns the base-pairing probability matrix, ensemble free energy and the\n"
"Maximum Expected Accuracy (MEA) structure for the given RNA sequence\n"
"`seq` predicted by LinearPartition.");
static PyObject *
linearpartition_partition(PyObject *self, PyObject *args, PyObject *kwds)
{
const char *seq, *mode="vienna";
int beamsize=100, dangles=2, sharpturn=0;
float gamma=3.0, bpp_cutoff=0.0;
static const char *kwlist[] = {
"seq", "mode", "beamsize", "sharpturn", "cutoff", "gamma", "dangles", NULL};
enum { ETERNA, VIENNA } mode_enum;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|sipffi:partition",
(char**)kwlist, &seq, &mode, &beamsize,
&sharpturn, &bpp_cutoff, &gamma, &dangles))
return NULL;
if (strcmp(mode, "eterna") == 0)
mode_enum = ETERNA;
else if (strcmp(mode, "vienna") == 0)
mode_enum = VIENNA;
else {
PyErr_SetString(PyExc_ValueError,
"mode must be either 'eterna' or 'vienna'");
return NULL;
}
string rna_seq(seq);
PyObject *probmtx;
double free_energy;
string mea_structure;
/* Call LinearPartition */
switch (mode_enum) {
case ETERNA: {
EternaBeamCKYParser parser(beamsize, !sharpturn, false, "", "", false, bpp_cutoff,
"", true, gamma, "", false, false, 0.3, "", "", false, dangles);
Py_BEGIN_ALLOW_THREADS
free_energy = parser.parse(rna_seq);
Py_END_ALLOW_THREADS
probmtx = parser.get_basepair_prob();
if (probmtx == NULL)
return NULL;
mea_structure = parser.threshknot_file_index;
break;
}
case VIENNA: {
ViennaBeamCKYParser parser(beamsize, !sharpturn, false, "", "", false, bpp_cutoff,
"", true, gamma, "", false, false, 0.3, "", "", false, dangles);
Py_BEGIN_ALLOW_THREADS
free_energy = parser.parse(rna_seq);
Py_END_ALLOW_THREADS
probmtx = parser.get_basepair_prob();
if (probmtx == NULL)
return NULL;
mea_structure = parser.threshknot_file_index;
break;
}
default:
PyErr_SetString(PyExc_RuntimeError, "unknown mode");
return NULL;
}
PyObject *ret=NULL, *py_free_energy=NULL, *py_mea_structure=NULL;
bool failed=true;
do {
ret = PyDict_New();
if (ret == NULL)
break;
py_free_energy = PyFloat_FromDouble(free_energy);
if (py_free_energy == NULL)
break;
py_mea_structure = PyUnicode_FromString(mea_structure.c_str());
if (py_mea_structure == NULL)
break;
PyDict_SetItemString(ret, "structure", py_mea_structure);
PyDict_SetItemString(ret, "free_energy", py_free_energy);
PyDict_SetItemString(ret, "bpp", probmtx);
failed = false;
} while (0);
Py_XDECREF(py_free_energy);
Py_XDECREF(py_mea_structure);
Py_DECREF(probmtx);
if (failed) {
Py_XDECREF(ret);
return NULL;
}
else
return ret;
}
static PyMethodDef linearpartition_methods[] = {
{"partition", (PyCFunction)linearpartition_partition,
METH_VARARGS | METH_KEYWORDS, linearpartition_partition_doc},
{NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc,
"CPython interface to LinearPartition");
static struct PyModuleDef linearpartitionmodule = {
PyModuleDef_HEAD_INIT,
"linearpartition",
module_doc,
0,
linearpartition_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_linearpartition(void)
{
import_array();
PyObject *op=Py_BuildValue("[(s, s), (s, s), (s, s)]",
"i", "i4", "j", "i4", "prob", "f8");
if (op == NULL)
return NULL;
if (PyArray_DescrConverter(op, &partition_return_descr) == NPY_FAIL) {
Py_DECREF(op);
return NULL;
}
Py_DECREF(op);
return PyModuleDef_Init(&linearpartitionmodule);
}