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
|
#include <Python.h>
#include <assert.h>
#include <inttypes.h>
#include "tree.h"
#include <assert.h>
#include <float.h>
static void pyGTFDealloc(pyGTFtree_t *self) {
if(self->t) destroyGTFtree(self->t);
PyObject_DEL(self);
}
#if PY_MAJOR_VERSION >= 3
//Return 1 iff obj is a ready unicode type
int PyString_Check(PyObject *obj) {
if(PyUnicode_Check(obj)) {
return PyUnicode_READY(obj)+1;
}
return 0;
}
char *PyString_AsString(PyObject *obj) {
return PyBytes_AsString(PyUnicode_AsASCIIString(obj));
}
PyObject *PyString_FromString(char *s) {
return PyUnicode_FromString(s);
}
#endif
//Will return 1 for long or int types currently
int isNumeric(PyObject *obj) {
#if PY_MAJOR_VERSION < 3
if(PyInt_Check(obj)) return 1;
#endif
return PyLong_Check(obj);
}
//On error, throws a runtime error, so use PyErr_Occurred() after this
uint32_t Numeric2Uint(PyObject *obj) {
long l;
#if PY_MAJOR_VERSION < 3
if(PyInt_Check(obj)) {
return (uint32_t) PyInt_AsLong(obj);
}
#endif
l = PyLong_AsLong(obj);
//Check bounds
if(l > 0xFFFFFFFF) {
PyErr_SetString(PyExc_RuntimeError, "Length out of bounds for a bigWig file!");
return (uint32_t) -1;
}
return (uint32_t) l;
}
static PyObject *pyGTFinit(PyObject *self, PyObject *args) {
GTFtree *t = NULL;
pyGTFtree_t *pt;
t = initGTFtree();
if(!t) return NULL;
pt = PyObject_New(pyGTFtree_t, &pyGTFtree);
if(!pt) goto error;
pt->t = t;
return (PyObject*) pt;
error:
if(t) destroyGTFtree(t);
PyErr_SetString(PyExc_RuntimeError, "Received an error during tree initialization!");
return NULL;
}
static PyObject *pyAddEntry(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
char *chrom = NULL, *name = NULL, *sscore = NULL;
uint32_t start, end, labelIdx;
double score;
uint8_t strand;
unsigned long lstrand, lstart, lend, llabelIdx;
if(!(PyArg_ParseTuple(args, "skkskks", &chrom, &lstart, &lend, &name, &lstrand, &llabelIdx, &sscore))) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEntry received an invalid or missing argument!");
return NULL;
}
//Convert all of the longs
if(lstart >= (uint32_t) -1 || lend >= (uint32_t) -1 || lend <= lstart) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEntry received invalid bounds!");
return NULL;
}
start = (uint32_t) lstart;
end = (uint32_t) lend;
if(lstrand != 0 && lstrand != 1 && lstrand != 3) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEntry received an invalid strand!");
return NULL;
}
strand = (uint8_t) lstrand;
if(llabelIdx >= (uint32_t) -1) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEntry received an invalid label idx (too large)!");
return NULL;
}
labelIdx = (uint32_t) llabelIdx;
//Handle the score
if(strcmp(sscore, ".") == 0) {
score = DBL_MAX;
} else {
score = strtod(sscore, NULL);
}
//Actually add the entry
if(addGTFentry(t, chrom, start, end, strand, name, labelIdx, score)) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEntry received an error while inserting an entry!");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyAddEnrichmentEntry(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
char *chrom = NULL, *sscore = NULL, *feature = NULL;
uint32_t start, end;
double score;
uint8_t strand;
unsigned long lstrand, lstart, lend;
if(!(PyArg_ParseTuple(args, "skkkss", &chrom, &lstart, &lend, &lstrand, &sscore, &feature))) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEnrichmentEntry received an invalid or missing argument!");
return NULL;
}
//Convert all of the longs
if(lstart >= (uint32_t) -1 || lend >= (uint32_t) -1 || lend <= lstart) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEnrichmentEntry received invalid bounds!");
return NULL;
}
start = (uint32_t) lstart;
end = (uint32_t) lend;
if(lstrand != 0 && lstrand != 1 && lstrand != 3) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEnrichmentEntry received an invalid strand!");
return NULL;
}
strand = (uint8_t) lstrand;
//Handle the score
if(strcmp(sscore, ".") == 0) {
score = DBL_MAX;
} else {
score = strtod(sscore, NULL);
}
//Actually add the entry
if(addEnrichmententry(t, chrom, start, end, strand, score, feature)) {
PyErr_SetString(PyExc_RuntimeError, "pyAddEnrichmentEntry received an error while inserting an entry!");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyVine2Tree(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
sortGTF(t);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyPrintGTFtree(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
printGTFtree(t);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pyCountEntries(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
uint32_t nEntries = 0;
unsigned long lnEntries = 0;
int32_t i;
PyObject *out = NULL;
for(i=0; i<t->n_targets; i++) {
nEntries += t->chroms[i]->n_entries;
}
lnEntries = (unsigned long) nEntries;
out = PyLong_FromUnsignedLong(lnEntries);
return out;
}
static PyObject *pyIsTree(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
if(t->balanced) Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
static PyObject *pyHasOverlaps(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
int rv;
uint32_t minDistance = (uint32_t) -1;
unsigned long long ominDistance;
PyObject *otuple = NULL, *oval = NULL;
rv = hasOverlaps(t, &minDistance);
ominDistance = minDistance; // ominDistance should have at least as much space as minDistance
otuple = PyTuple_New(2);
if(!otuple) {
PyErr_SetString(PyExc_RuntimeError, "Could not allocate space for a tuple!\n");
return NULL;
}
oval = PyLong_FromUnsignedLongLong(ominDistance);
if(!oval) {
PyErr_SetString(PyExc_RuntimeError, "Could not allocate space for a single integer!\n");
return NULL;
}
if(rv) {
Py_INCREF(Py_True);
PyTuple_SET_ITEM(otuple, 0, Py_True);
} else {
Py_INCREF(Py_False);
PyTuple_SET_ITEM(otuple, 0, Py_False);
}
PyTuple_SetItem(otuple, 1, oval);
return otuple;
}
static PyObject *pyFindOverlaps(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
char *chrom = NULL, *name = NULL, *transcript_id = NULL, strandChar;
int32_t i;
uint32_t start, end;
int strand = 3, strandType = 0, matchType = 0;
unsigned long lstrand, lstart, lend, lmatchType, lstrandType, llabelIdx;
overlapSet *os = NULL;
PyObject *olist = NULL, *otuple = NULL, *includeStrand = Py_False, *oscore = NULL;
if(!(PyArg_ParseTuple(args, "skkkkksO", &chrom, &lstart, &lend, &lstrand, &lmatchType, &lstrandType, &transcript_id, &includeStrand))) {
PyErr_SetString(PyExc_RuntimeError, "pyFindOverlaps received an invalid or missing argument!");
return NULL;
}
//I'm assuming that this is never called outside of the module
strandType = (int) lstrandType;
strand = (int) lstrand;
matchType = (int) matchType;
start = (uint32_t) lstart;
end = (uint32_t) lend;
os = findOverlaps(NULL, t, chrom, start, end, strand, matchType, strandType, 0, NULL);
// Did we receive an error?
if(!os) {
PyErr_SetString(PyExc_RuntimeError, "findOverlaps returned NULL!");
return NULL;
}
// Convert the overlapSet to a list of tuples
olist = PyList_New(os->l);
if(!olist) goto error;
for(i=0; i<os->l; i++) {
// Make the tuple
if(includeStrand == Py_True) {
otuple = PyTuple_New(6);
} else {
otuple = PyTuple_New(5);
}
if(!otuple) goto error;
lstart = (unsigned long) os->overlaps[i]->start;
lend = (unsigned long) os->overlaps[i]->end;
name = getAttribute(t, os->overlaps[i], transcript_id);
llabelIdx = (unsigned long) os->overlaps[i]->labelIdx;
strandChar = '.';
if(os->overlaps[i]->strand == 0) {
strandChar = '+';
} else if(os->overlaps[i]->strand == 1) {
strandChar = '-';
}
if (os->overlaps[i]->score == DBL_MAX) {
oscore = Py_BuildValue("s", ".");
} else {
oscore = Py_BuildValue("d", os->overlaps[i]->score);
}
if(!oscore) goto error;
if(includeStrand == Py_True) {
otuple = Py_BuildValue("(kkskcO)", lstart, lend, name, llabelIdx, strandChar, oscore);
} else {
otuple = Py_BuildValue("(kkskO)", lstart, lend, name, llabelIdx, oscore);
}
if(!otuple) goto error;
// Add the tuple
if(PyList_SetItem(olist, i, otuple)) goto error;
otuple = NULL;
}
os_destroy(os);
return olist;
error:
if(otuple) Py_DECREF(otuple);
if(olist) Py_DECREF(olist);
PyErr_SetString(PyExc_RuntimeError, "findOverlaps received an error!");
return NULL;
}
static PyObject *pyFindOverlappingFeatures(pyGTFtree_t *self, PyObject *args) {
GTFtree *t = self->t;
char *chrom = NULL;
int32_t i;
uint32_t start, end;
int strand = 3, strandType = 0, matchType = 0;
unsigned long lstrand, lstart, lend, lmatchType, lstrandType;
overlapSet *os = NULL;
PyObject *olist = NULL, *ostring = NULL;
if(!(PyArg_ParseTuple(args, "skkkkk", &chrom, &lstart, &lend, &lstrand, &lmatchType, &lstrandType))) {
PyErr_SetString(PyExc_RuntimeError, "pyFindOverlaps received an invalid or missing argument!");
return NULL;
}
//I'm assuming that this is never called outside of the module
strandType = (int) lstrandType;
strand = (int) lstrand;
matchType = (int) matchType;
start = (uint32_t) lstart;
end = (uint32_t) lend;
os = findOverlaps(NULL, t, chrom, start, end, strand, matchType, strandType, 0, NULL);
// Did we receive an error?
if(!os) {
PyErr_SetString(PyExc_RuntimeError, "findOverlaps returned NULL!");
return NULL;
}
if(!os->l) {
os_destroy(os);
Py_INCREF(Py_None);
return Py_None;
}
// Convert the overlapSet to a list of tuples
olist = PyList_New(os->l);
if(!olist) goto error;
for(i=0; i<os->l; i++) {
//Make the python string
ostring = PyString_FromString(val2strHT(t->htFeatures, os->overlaps[i]->feature));
if(!ostring) goto error;
// Add the item
if(PyList_SetItem(olist, i, ostring)) goto error;
ostring = NULL;
}
os_destroy(os);
return olist;
error:
if(ostring) Py_DECREF(ostring);
if(olist) Py_DECREF(olist);
PyErr_SetString(PyExc_RuntimeError, "findOverlappingFeatures received an error!");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_tree(void) {
PyObject *res;
errno = 0;
if(PyType_Ready(&pyGTFtree) < 0) return NULL;
res = PyModule_Create(&treemodule);
if(!res) return NULL;
Py_INCREF(&pyGTFtree);
PyModule_AddObject(res, "pyGTFtree", (PyObject *) &pyGTFtree);
return res;
}
#else
//Python2 initialization
PyMODINIT_FUNC inittree(void) {
errno = 0; //Sometimes libpython2.7.so is missing some links...
if(PyType_Ready(&pyGTFtree) < 0) return;
Py_InitModule3("tree", treeMethods, "A module for handling GTF files for deepTools");
}
#endif
|