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
|
#include <Python.h>
#include <inttypes.h>
#include "py2bit.h"
static PyObject *py2bitOpen(PyObject *self, PyObject *args, PyObject *kwds) {
char *fname = NULL;
PyObject *storeMaskedO = Py_False;
pyTwoBit_t *pytb;
int storeMasked = 0;
TwoBit *tb = NULL;
static char *kwd_list[] = {"fname", "storeMasked", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|O", kwd_list, &fname, &storeMaskedO)) goto error;
if(storeMaskedO == Py_True) storeMasked = 1;
//Open the file
tb = twobitOpen(fname, storeMasked);
if(!tb) goto error;
pytb = PyObject_New(pyTwoBit_t, &pyTwoBit);
if(!pytb) goto error;
pytb->storeMasked = storeMasked;
pytb->tb = tb;
return (PyObject*) pytb;
error:
if(tb) twobitClose(tb);
PyErr_SetString(PyExc_RuntimeError, "Received an error during file opening!");
return NULL;
}
PyObject *py2bitEnter(pyTwoBit_t *self, PyObject *args) {
TwoBit *tb = self->tb;
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
Py_INCREF(self);
return (PyObject*) self;
}
static void py2bitDealloc(pyTwoBit_t *self) {
if(self->tb) twobitClose(self->tb);
PyObject_DEL(self);
}
static PyObject *py2bitClose(pyTwoBit_t *self, PyObject *args) {
if(self->tb) twobitClose(self->tb);
self->tb = NULL;
Py_INCREF(Py_None);
return Py_None;
}
//Returns the file size, number of chromosomes/contigs, total sequence length and total masked length
static PyObject *py2bitInfo(pyTwoBit_t *self, PyObject *args) {
TwoBit *tb = self->tb;
PyObject *ret = NULL, *val = NULL;
uint32_t i, j, foo;
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
ret = PyDict_New();
//file size
val = PyLong_FromUnsignedLongLong(tb->sz);
if(!val) goto error;
if(PyDict_SetItemString(ret, "file size", val) == -1) goto error;
Py_DECREF(val);
//nContigs
val = PyLong_FromUnsignedLong(tb->hdr->nChroms);
if(!val) goto error;
if(PyDict_SetItemString(ret, "nChroms", val) == -1) goto error;
Py_DECREF(val);
//sequence length
foo = 0;
for(i=0; i<tb->hdr->nChroms; i++) foo += tb->idx->size[i];
val = PyLong_FromUnsignedLong(foo);
if(!val) goto error;
if(PyDict_SetItemString(ret, "sequence length", val) == -1) goto error;
Py_DECREF(val);
//hard-masked length
foo = 0;
for(i=0; i<tb->hdr->nChroms; i++) {
for(j=0; j<tb->idx->nBlockCount[i]; j++) {
foo += tb->idx->nBlockSizes[i][j];
}
}
val = PyLong_FromUnsignedLong(foo);
if(!val) goto error;
if(PyDict_SetItemString(ret, "hard-masked length", val) == -1) goto error;
Py_DECREF(val);
//soft-masked length
if(tb->idx->maskBlockStart) {
foo = 0;
for(i=0; i<tb->hdr->nChroms; i++) {
for(j=0; j<tb->idx->maskBlockCount[i]; j++) {
foo += tb->idx->maskBlockSizes[i][j];
}
}
val = PyLong_FromUnsignedLong(foo);
if(!val) goto error;
if(PyDict_SetItemString(ret, "soft-masked length", val) == -1) goto error;
Py_DECREF(val);
}
return ret;
error:
Py_XDECREF(val);
Py_XDECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Received an error while gathering information on the 2bit file!");
return NULL;
}
static PyObject *py2bitChroms(pyTwoBit_t *self, PyObject *args) {
PyObject *ret = NULL, *val = NULL;
TwoBit *tb = self->tb;
char *chrom = NULL;
uint32_t i;
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
if(!(PyArg_ParseTuple(args, "|s", &chrom)) || !chrom) {
ret = PyDict_New();
if(!ret) goto error;
for(i=0; i<tb->hdr->nChroms; i++) {
val = PyLong_FromUnsignedLong(tb->idx->size[i]);
if(!val) goto error;
if(PyDict_SetItemString(ret, tb->cl->chrom[i], val) == -1) goto error;
Py_DECREF(val);
}
} else {
for(i=0; i<tb->hdr->nChroms; i++) {
if(strcmp(tb->cl->chrom[i], chrom) == 0) {
ret = PyLong_FromUnsignedLong(tb->idx->size[i]);
if(!ret) goto error;
break;
}
}
}
if(!ret) {
Py_INCREF(Py_None);
ret = Py_None;
}
return ret;
error :
Py_XDECREF(val);
Py_XDECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Received an error while adding an item to the output dictionary!");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
PyObject *PyString_FromString(char *seq) {
return PyUnicode_FromString(seq);
}
#endif
static PyObject *py2bitSequence(pyTwoBit_t *self, PyObject *args, PyObject *kwds) {
PyObject *ret = NULL;
TwoBit *tb = self->tb;
char *seq, *chrom;
unsigned long startl = 0, endl = 0;
uint32_t start, end, len;
static char *kwd_list[] = {"chrom", "start", "end", NULL};
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|kk", kwd_list, &chrom, &startl, &endl)) {
PyErr_SetString(PyExc_RuntimeError, "You must supply at least a chromosome!");
return NULL;
}
len = twobitChromLen(tb, chrom);
if(len == 0) {
PyErr_SetString(PyExc_RuntimeError, "The specified chromosome doesn't exist in the 2bit file!");
return NULL;
}
if(endl > len) endl = len;
end = (uint32_t) endl;
if(startl >= endl && startl > 0) {
PyErr_SetString(PyExc_RuntimeError, "The start value must be less then the end value (and the end of the chromosome");
return NULL;
}
start = (uint32_t) startl;
seq = twobitSequence(tb, chrom, start, end);
if(!seq) {
PyErr_SetString(PyExc_RuntimeError, "There was an error while fetching the sequence!");
return NULL;
}
ret = PyString_FromString(seq);
free(seq);
if(!ret) {
PyErr_SetString(PyExc_RuntimeError, "Received an error while converting the C-level char array to a python string!");
return NULL;
}
return ret;
}
static PyObject *py2bitBases(pyTwoBit_t *self, PyObject *args, PyObject *kwds) {
PyObject *ret = NULL, *val = NULL;
PyObject *fractionO = Py_True;
TwoBit *tb = self->tb;
char *chrom;
void *o = NULL;
unsigned long startl = 0, endl = 0;
uint32_t start, end, len;
static char *kwd_list[] = {"chrom", "start", "end", "fraction", NULL};
int fraction = 1;
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|kkO", kwd_list, &chrom, &startl, &endl, &fractionO)) {
PyErr_SetString(PyExc_RuntimeError, "You must supply at least a chromosome!");
return NULL;
}
len = twobitChromLen(tb, chrom);
if(len == 0) {
PyErr_SetString(PyExc_RuntimeError, "The specified chromosome doesn't exist in the 2bit file!");
return NULL;
}
if(endl > len) endl = len;
end = (uint32_t) endl;
if(startl >= endl && startl > 0) {
PyErr_SetString(PyExc_RuntimeError, "The start value must be less then the end value (and the end of the chromosome");
return NULL;
}
start = (uint32_t) startl;
if(fractionO == Py_False) fraction = 0;
o = twobitBases(tb, chrom, start, end, fraction);
if(!o) {
PyErr_SetString(PyExc_RuntimeError, "Received an error while determining the per-base metrics.");
return NULL;
}
ret = PyDict_New();
if(!ret) goto error;
//A
if(fraction) val = PyFloat_FromDouble(((double*)o)[0]);
else val = PyLong_FromUnsignedLong(((uint32_t*)o)[0]);
if(!val) goto error;
if(PyDict_SetItemString(ret, "A", val) == -1) goto error;
Py_DECREF(val);
//C
if(fraction) val = PyFloat_FromDouble(((double*)o)[1]);
else val = PyLong_FromUnsignedLong(((uint32_t*)o)[1]);
if(!val) goto error;
if(PyDict_SetItemString(ret, "C", val) == -1) goto error;
Py_DECREF(val);
//T
if(fraction) val = PyFloat_FromDouble(((double*)o)[2]);
else val = PyLong_FromUnsignedLong(((uint32_t*)o)[2]);
if(!val) goto error;
if(PyDict_SetItemString(ret, "T", val) == -1) goto error;
Py_DECREF(val);
//G
if(fraction) val = PyFloat_FromDouble(((double*)o)[3]);
else val = PyLong_FromUnsignedLong(((uint32_t*)o)[3]);
if(!val) goto error;
if(PyDict_SetItemString(ret, "G", val) == -1) goto error;
Py_DECREF(val);
free(o);
return ret;
error:
if(o) free(o);
Py_XDECREF(ret);
Py_XDECREF(val);
PyErr_SetString(PyExc_RuntimeError, "Received an error while constructing the output dictionary!");
return NULL;
}
static PyObject *py2bitHardMaskedBlocks(pyTwoBit_t *self, PyObject *args, PyObject *kwds) {
PyObject *ret = NULL, *tup = NULL;
TwoBit *tb = self->tb;
char *chrom;
unsigned long startl = 0, endl = 0, totalBlocks = 0, tid;
uint32_t start, end, len, blockStart, blockEnd, i, j;
static char *kwd_list[] = {"chrom", "start", "end", NULL};
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|kk", kwd_list, &chrom, &startl, &endl)) {
PyErr_SetString(PyExc_RuntimeError, "You must supply at least a chromosome!");
return NULL;
}
//Get the chromosome ID
for(i=0; i<tb->hdr->nChroms; i++) {
if(strcmp(tb->cl->chrom[i], chrom) == 0) {
tid = i;
break;
}
}
len = twobitChromLen(tb, chrom);
if(len == 0) {
PyErr_SetString(PyExc_RuntimeError, "The specified chromosome doesn't exist in the 2bit file!");
return NULL;
}
if(endl == 0) endl = len;
if(endl > len) endl = len;
end = (uint32_t) endl;
if(startl >= endl && startl > 0) {
PyErr_SetString(PyExc_RuntimeError, "The start value must be less then the end value (and the end of the chromosome");
return NULL;
}
start = (uint32_t) startl;
// Count the total number of overlapping N-masked blocks
for(i=0; i<tb->idx->nBlockCount[tid]; i++) {
blockStart = tb->idx->nBlockStart[tid][i];
blockEnd = blockStart + tb->idx->nBlockSizes[tid][i];
if(blockStart < end && blockEnd > start) totalBlocks++;
}
// Form the output
ret = PyList_New(totalBlocks);
if(!ret) goto error;
if(totalBlocks == 0) return ret;
for(i=0, j=0; i<tb->idx->nBlockCount[tid]; i++) {
blockStart = tb->idx->nBlockStart[tid][i];
blockEnd = blockStart + tb->idx->nBlockSizes[tid][i];
if(blockStart < end && blockEnd > start) {
tup = Py_BuildValue("(kk)", (unsigned long) blockStart, (unsigned long) blockEnd);
if(!tup) goto error;
if(PyList_SetItem(ret, j++, tup)) goto error;
}
}
return ret;
error:
if(ret) Py_XDECREF(ret);
if(tup) Py_XDECREF(tup);
PyErr_SetString(PyExc_RuntimeError, "Received an error while constructing the output list and tuples!");
return NULL;
}
static PyObject *py2bitSoftMaskedBlocks(pyTwoBit_t *self, PyObject *args, PyObject *kwds) {
PyObject *ret = NULL, *tup = NULL;
TwoBit *tb = self->tb;
char *chrom;
unsigned long startl = 0, endl = 0, totalBlocks = 0, tid;
uint32_t start, end, len, blockStart, blockEnd, i, j;
static char *kwd_list[] = {"chrom", "start", "end", NULL};
if(!tb) {
PyErr_SetString(PyExc_RuntimeError, "The 2bit file handle is not open!");
return NULL;
}
if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|kk", kwd_list, &chrom, &startl, &endl)) {
PyErr_SetString(PyExc_RuntimeError, "You must supply at least a chromosome!");
return NULL;
}
//Get the chromosome ID
for(i=0; i<tb->hdr->nChroms; i++) {
if(strcmp(tb->cl->chrom[i], chrom) == 0) {
tid = i;
break;
}
}
len = twobitChromLen(tb, chrom);
if(len == 0) {
PyErr_SetString(PyExc_RuntimeError, "The specified chromosome doesn't exist in the 2bit file!");
return NULL;
}
if(endl == 0) endl = len;
if(endl > len) endl = len;
end = (uint32_t) endl;
if(startl >= endl && startl > 0) {
PyErr_SetString(PyExc_RuntimeError, "The start value must be less then the end value (and the end of the chromosome");
return NULL;
}
start = (uint32_t) startl;
if(!tb->idx->maskBlockStart) {
PyErr_SetString(PyExc_RuntimeError, "The file was not opened with storeMasked=True! Consequently, there are no stored soft-masked regions.");
return NULL;
}
// Count the total number of overlapping soft-masked blocks
for(i=0; i<tb->idx->maskBlockCount[tid]; i++) {
blockStart = tb->idx->maskBlockStart[tid][i];
blockEnd = blockStart + tb->idx->maskBlockSizes[tid][i];
if(blockStart < end && blockEnd > start) totalBlocks++;
}
// Form the output
ret = PyList_New(totalBlocks);
if(!ret) goto error;
if(totalBlocks == 0) return ret;
for(i=0, j=0; i<tb->idx->maskBlockCount[tid]; i++) {
blockStart = tb->idx->maskBlockStart[tid][i];
blockEnd = blockStart + tb->idx->maskBlockSizes[tid][i];
if(blockStart < end && blockEnd > start) {
tup = Py_BuildValue("(kk)", (unsigned long) blockStart, (unsigned long) blockEnd);
if(!tup) goto error;
if(PyList_SetItem(ret, j++, tup)) goto error;
}
}
return ret;
error:
if(ret) Py_XDECREF(ret);
if(tup) Py_XDECREF(tup);
PyErr_SetString(PyExc_RuntimeError, "Received an error while constructing the output list and tuples!");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_py2bit(void) {
PyObject *res;
if(PyType_Ready(&pyTwoBit) < 0) return NULL;
res = PyModule_Create(&py2bitmodule);
if(!res) return NULL;
Py_INCREF(&pyTwoBit);
PyModule_AddObject(res, "py2bit", (PyObject *) &pyTwoBit);
PyModule_AddStringConstant(res, "__version__", pyTwoBitVersion);
return res;
}
#else
//Python2 initialization
PyMODINIT_FUNC initpy2bit(void) {
PyObject *res;
if(PyType_Ready(&pyTwoBit) < 0) return;
res = Py_InitModule3("py2bit", tbMethods, "A module for handling 2bit files");
Py_INCREF(&pyTwoBit);
PyModule_AddObject(res, "py2bit", (PyObject *) &pyTwoBit);
PyModule_AddStringConstant(res, "__version__", pyTwoBitVersion);
}
#endif
|