-
Notifications
You must be signed in to change notification settings - Fork 0
/
dEploid-py.cpp
583 lines (491 loc) · 15.5 KB
/
dEploid-py.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <Python.h>
#include "structmember.h" // fix something array have incomplete type
#include <string> /* string */
#include <exception>
#include <stdexcept>
#include "lib/src/vcfReader.hpp"
#include "lib/src/mcmc.hpp"
#include "lib/src/dEploidIO.hpp"
#define MODULE_DOC \
"Low level interface for dEploid"
//typedef struct {
//PyObject_HEAD
//tree_sequence_t *tree_sequence;
//} TreeSequence;
static void
handle_library_error(int err)
{
//if (msp_is_kas_error(err)) {
//PyErr_SetString(MsprimeFileFormatError, msp_strerror(err));
//} else {
//switch (err) {
//case MSP_ERR_FILE_VERSION_TOO_NEW:
//PyErr_SetString(MsprimeVersionTooNewError, msp_strerror(err));
//break;
//case MSP_ERR_FILE_VERSION_TOO_OLD:
//PyErr_SetString(MsprimeVersionTooOldError, msp_strerror(err));
//break;
//case MSP_ERR_FILE_FORMAT:
//PyErr_SetString(MsprimeFileFormatError, msp_strerror(err));
//break;
//case MSP_ERR_OUT_OF_BOUNDS:
//PyErr_SetString(PyExc_IndexError, msp_strerror(err));
//break;
//default:
//PyErr_SetString(MsprimeLibraryError, msp_strerror(err));
//}
//}
//PyErr_SetString(MsprimeFileFormatError, msp_strerror(err));
}
static PyObject *
dEploid_add(PyObject *self, PyObject *args)
{
PyObject *ret = NULL;
double x, y, result;
if (!PyArg_ParseTuple(args, "dd", &x, &y)) {
goto out;
}
result = x + y;
ret = Py_BuildValue("d", result);
out:
return ret;
}
static PyObject *
extractCoverageFromVcf(PyObject *args)
{
PyObject *ret = NULL;
//size_t num_tracked_samples;
int err;
err = 0;
//if (SparseTree_get_node_argument(self, args, &node) != 0) {
//goto out;
//}
//err = sparse_tree_get_num_tracked_samples(self->sparse_tree, (node_id_t) node,
//&num_tracked_samples);
//if (err != 0) {
//handle_library_error(err);
//goto out;
//}
////ret = Py_BuildValue("I", (unsigned int) num_tracked_samples);
//out:
if ( err != 0 ){
handle_library_error(err);
}
return ret;
}
PyObject* matrixToListList_Double(const vector < vector<double> > &data) {
PyObject* listListObj = PyList_New( data.size() );
if (!listListObj) throw std::logic_error("Unable to allocate memory for Python list");
for (size_t i = 0; i < data.size(); i++) {
PyObject* listObj = PyList_New(data[i].size());
for (size_t ii = 0; ii < data[i].size(); ii++) {
PyObject *num = PyFloat_FromDouble( (double) data[i][ii]);
if (!num) {
Py_DECREF(listObj);
throw std::logic_error("Unable to allocate memory for Python list");
}
PyList_SET_ITEM(listObj, ii, num);
}
PyList_SET_ITEM(listListObj, i, listObj);
}
return listListObj;
}
PyObject* vectorToList_Double(const vector<double> &data) {
PyObject* listObj = PyList_New( data.size() );
if (!listObj) throw std::logic_error("Unable to allocate memory for Python list");
for (unsigned int i = 0; i < data.size(); i++) {
PyObject *num = PyFloat_FromDouble( (double) data[i]);
if (!num) {
Py_DECREF(listObj);
throw std::logic_error("Unable to allocate memory for Python list");
}
PyList_SET_ITEM(listObj, i, num);
}
return listObj;
}
PyObject* vectorToList_Str(const vector<string> &data) {
PyObject* listObj = PyList_New( data.size() );
if (!listObj) throw std::logic_error("Unable to allocate memory for Python list");
for (unsigned int i = 0; i < data.size(); i++) {
PyObject *s = Py_BuildValue("s", data[i].c_str());
PyList_SET_ITEM(listObj, i, s);
}
return listObj;
}
/*===================================================================
* VcfStruct
*===================================================================
*/
typedef struct {
PyObject_HEAD
bool locked;
VcfReader *vcfreader;
} VcfStruct;
static int
VcfStruct_check_state(VcfStruct *self)
{
int ret = 0;
if (self->vcfreader == NULL) {
PyErr_SetString(PyExc_SystemError, "VcfStruct not initialised");
ret = -1;
}
return ret;
}
static void
VcfStruct_dealloc(VcfStruct* self)
{
if (self->vcfreader != NULL) {
//delete self->vcfreader;
PyMem_Free(self->vcfreader);
self->vcfreader = NULL;
}
Py_XDECREF(self->vcfreader);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static int
VcfStruct_init(VcfStruct *self, PyObject *args)
{
int ret = -1;
int err;
int ok;
char *s;
ok = PyArg_ParseTuple(args, "s", &s);
if (ok == 0){ // 0 is false/fail, 1 is success
PyErr_SetString(PyExc_SystemError, "Bad name");
return ret;
}
std::string filename(s);
try {
self->vcfreader = new VcfReader(filename);
self->vcfreader->finalize();
} catch (const exception &e) {
PyErr_SetString(PyExc_SystemError, e.what());
ret = -1;
return ret;
}
ret = 0;
return ret;
}
static PyObject *
VcfStruct_get_vcfheader(VcfStruct *self)
{
PyObject *ret = NULL;
if (VcfStruct_check_state(self) != 0) {
goto out;
}
ret = vectorToList_Str(self->vcfreader->headerLines);
out:
return ret;
}
static PyObject *
VcfStruct_get_vqslod(VcfStruct *self)
{
PyObject *ret = NULL;
if (VcfStruct_check_state(self) != 0) {
goto out;
}
ret = vectorToList_Double(self->vcfreader->vqslod);
out:
return ret;
}
static PyObject *
VcfStruct_get_refcount(VcfStruct *self)
{
PyObject *ret = NULL;
if (VcfStruct_check_state(self) != 0) {
goto out;
}
ret = vectorToList_Double(self->vcfreader->refCount);
out:
return ret;
}
static PyObject *
VcfStruct_get_altcount(VcfStruct *self)
{
PyObject *ret = NULL;
if (VcfStruct_check_state(self) != 0) {
goto out;
}
ret = vectorToList_Double(self->vcfreader->altCount);
out:
return ret;
}
static PyMemberDef VcfStruct_members[] = {
{NULL} /* Sentinel */
};
static PyMethodDef VcfStruct_methods[] = {
{"get_vcfheader", (PyCFunction) VcfStruct_get_vcfheader, METH_NOARGS,
"Returns the VCF header as plain text." },
{"get_refCount", (PyCFunction) VcfStruct_get_refcount, METH_NOARGS,
"Returns VCF reference allele count." },
{"get_altCount", (PyCFunction) VcfStruct_get_altcount, METH_NOARGS,
"Returns VCF alternative allele count." },
{"get_vqslod", (PyCFunction) VcfStruct_get_vqslod, METH_NOARGS,
"Returns VCF SNP VQSLOD scores." },
{NULL} /* Sentinel */
};
static PyTypeObject Vcf = {
PyVarObject_HEAD_INIT(NULL, 0)
"_dEploid.Vcf", /* tp_name */
sizeof(VcfStruct), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)VcfStruct_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"VcfStruct objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
VcfStruct_methods, /* tp_methods */
VcfStruct_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)VcfStruct_init, /* tp_init */
//0, /* tp_alloc */
//Noddy_new, /* tp_new */
};
/*
* mcmcChain
* This will be equivalent to the main function in dEploid
*/
typedef struct {
PyObject_HEAD
bool locked;
McmcSample * mcmcSample; // = new McmcSample();
} McmcSampleStruct;
static int
mcmcChain_check_state(McmcSampleStruct *self)
{
int ret = 0;
if (self->mcmcSample == NULL) {
PyErr_SetString(PyExc_SystemError, "McmcSampleStruct not initialised");
ret = -1;
}
return ret;
}
static void
mcmcChain_dealloc(McmcSampleStruct* self)
{
if (self->mcmcSample != NULL) {
//delete self->vcfreader;
PyMem_Free(self->mcmcSample);
self->mcmcSample = NULL;
}
Py_XDECREF(self->mcmcSample);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static int
mcmcChain_init(McmcSampleStruct* self, PyObject *args)
{
int ret = -1;
int err;
int ok;
char *s;
ok = PyArg_ParseTuple(args, "s", &s);
std::string cmd(s);
if (cmd.size() > 0){
try {
DEploidIO dEploidIO(cmd);
self->mcmcSample = new McmcSample();
MersenneTwister rg(dEploidIO.randomSeed());
McmcMachinery mcmcMachinery(&dEploidIO.plaf_,
&dEploidIO.refCount_,
&dEploidIO.altCount_,
dEploidIO.panel,
&dEploidIO,
self->mcmcSample,
&rg,
false); // use IBD
mcmcMachinery.runMcmcChain(true, // show progress
false); // use IBD
} catch (const exception &e) {
PyErr_SetString(PyExc_SystemError, e.what());
ret = -1;
return ret;
}
}
ret = 0;
return ret;
}
static PyObject *
mcmcChain_get_proportions(McmcSampleStruct* self)
{
PyObject *ret = NULL;
if (mcmcChain_check_state(self) != 0) {
goto out;
}
ret = matrixToListList_Double(self->mcmcSample->proportion);
out:
return ret;
}
static PyObject *
mcmcChain_get_hap(McmcSampleStruct* self)
{
PyObject *ret = NULL;
if (mcmcChain_check_state(self) != 0) {
goto out;
}
ret = matrixToListList_Double(self->mcmcSample->hap);
out:
return ret;
}
static PyObject *
mcmcChain_get_llk(McmcSampleStruct* self)
{
PyObject *ret = NULL;
if (mcmcChain_check_state(self) != 0) {
goto out;
}
ret = vectorToList_Double(self->mcmcSample->sumLLKs);
out:
return ret;
}
static PyMemberDef mcmcChain_members[] = {
{NULL} /* Sentinel */
};
static PyMethodDef mcmcChain_methods[] = {
{"get_proportions", (PyCFunction) mcmcChain_get_proportions, METH_NOARGS,
"Returns proportion mcmc samples." },
{"get_hap", (PyCFunction) mcmcChain_get_hap, METH_NOARGS,
"Returns haplotypes of last iteration of mcmc samples." },
{"get_llk", (PyCFunction) mcmcChain_get_llk, METH_NOARGS,
"Returns likelihood chain of mcmc samples." },
{NULL} /* Sentinel */
};
static PyTypeObject mcmcChain = {
PyVarObject_HEAD_INIT(NULL, 0)
"_dEploid.mcmcChain", /* tp_name */
sizeof(McmcSampleStruct), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)mcmcChain_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
"McmcSampleStruct objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
mcmcChain_methods, /* tp_methods */
mcmcChain_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)mcmcChain_init, /* tp_init */
//0, /* tp_alloc */
//Noddy_new, /* tp_new */
};
/*
* dEploid_methods initialization
*/
static PyObject *
dEploid_getProgramVertionStr(PyObject *self)
{
return Py_BuildValue("s", VERSION);
}
static PyObject *
dEploid_getLibraryVertionStr(PyObject *self)
{
return Py_BuildValue("s", DEPLOIDVERSION);
}
static PyObject *
dEploid_getLassoLibraryVertionStr(PyObject *self)
{
return Py_BuildValue("s", LASSOVERSION);
}
static PyObject *
dEploid_getCompileTimeStr(PyObject *self)
{
return Py_BuildValue("s", COMPILEDATE);
}
static PyMethodDef dEploid_methods[] = {
{"add", (PyCFunction) dEploid_add, METH_VARARGS,
"Print a lovely skit to standard output."},
{"getLibraryVertionStr", (PyCFunction) dEploid_getLibraryVertionStr,
METH_NOARGS, "Returns the version of the dEploid git version." },
{"getLassoLibraryVertionStr", (PyCFunction) dEploid_getLassoLibraryVertionStr,
METH_NOARGS, "Returns the version of the lasso git version." },
{"getProgramVertionStr", (PyCFunction) dEploid_getProgramVertionStr,
METH_NOARGS, "Returns the version of the dEploid C++ library." },
{"getCompileTimeStr", (PyCFunction) dEploid_getCompileTimeStr,
METH_NOARGS, "Returns the compilation time." },
{NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef dEploidmodule = {
PyModuleDef_HEAD_INIT,
"_dEploid",
MODULE_DOC,
-1,
dEploid_methods,
NULL, NULL, NULL, NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit__dEploid(void)
#else
#define INITERROR return
void
init__dEploid(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&dEploidmodule);
#else
PyObject *module = Py_InitModule3("_dEploid", dEploid_methods, MODULE_DOC);
#endif
/* Vcf type */
Vcf.tp_new = PyType_GenericNew;
if (PyType_Ready(&Vcf) < 0) {
INITERROR;
}
Py_INCREF(&Vcf);
PyModule_AddObject(module, "Vcf", (PyObject *) &Vcf);
/* mcmcChain type */
mcmcChain.tp_new = PyType_GenericNew;
if (PyType_Ready(&mcmcChain) < 0) {
INITERROR;
}
Py_INCREF(&mcmcChain);
PyModule_AddObject(module, "mcmcChain", (PyObject *) &mcmcChain);
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}