-
Notifications
You must be signed in to change notification settings - Fork 6
BGZIP support #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rhpvorderman
wants to merge
8
commits into
develop
Choose a base branch
from
bgzip
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
BGZIP support #228
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
123b0bf
Start on bgzip module threaded reading
rhpvorderman b59e795
Implement bgzip skipper
rhpvorderman 8a81c58
Simplify bgzip header check
rhpvorderman cd97677
Correct implementation of algorithm
rhpvorderman ac876ab
Get reader in workable state
rhpvorderman deaa073
Massive reduction of memory usage
rhpvorderman 3a8b747
Set one thread as a default
rhpvorderman 6455ec0
Remove unused function
rhpvorderman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
import io | ||
import sys | ||
|
||
from isal.igzip_threaded import _ThreadedGzipReader, _ThreadedBGzipReader | ||
|
||
def main(): | ||
file = sys.argv[1] | ||
with io.BufferedReader( | ||
_ThreadedBGzipReader(file, threads=8) | ||
) as f: | ||
while True: | ||
block = f.read(128 * 1024) | ||
if block == b"": | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, | ||
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 | ||
# Python Software Foundation; All Rights Reserved | ||
|
||
# This file is part of python-isal which is distributed under the | ||
# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2. | ||
|
||
def find_last_bgzip_end(__data: bytes) -> int: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, | ||
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 | ||
Python Software Foundation; All Rights Reserved | ||
|
||
This file is part of python-isal which is distributed under the | ||
PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2. | ||
|
||
This file is not originally from the CPython distribution. But it does | ||
contain mostly example code from the Python docs. Also dual licensing just | ||
for this one file seemed silly. | ||
*/ | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#include <stdbool.h> | ||
|
||
#define FEXTRA 4 | ||
|
||
static inline uint16_t load_u16_le(const void *address) { | ||
#if PY_BIG_ENDIAN | ||
uint8_t *mem = address; | ||
return mem[0] | (mem[1] << 8); | ||
#else | ||
return *(uint16_t *)address; | ||
#endif | ||
} | ||
|
||
static PyObject *find_last_bgzip_end(PyObject *module, PyObject *buffer_obj) { | ||
Py_buffer buf; | ||
int ret = PyObject_GetBuffer(buffer_obj, &buf, PyBUF_SIMPLE); | ||
if (ret == -1) { | ||
return NULL; | ||
} | ||
const uint8_t *data = buf.buf; | ||
Py_ssize_t data_length = buf.len; | ||
const uint8_t *data_end = data + data_length; | ||
const uint8_t *cursor = data; | ||
|
||
while (true) { | ||
if (cursor + 18 > data_end) { | ||
break; | ||
} | ||
uint8_t magic1 = cursor[0]; | ||
uint8_t magic2 = cursor[1]; | ||
uint8_t method = cursor[2]; | ||
uint8_t flags = cursor[3]; | ||
uint16_t xlen = load_u16_le(cursor + 10); | ||
uint8_t si1 = cursor[12]; | ||
uint8_t si2 = cursor[13]; | ||
uint16_t subfield_length = load_u16_le(cursor + 14); | ||
if ( | ||
magic1 != 31 || | ||
magic2 != 139 || | ||
method != 8 || | ||
flags != FEXTRA || | ||
xlen != 6 || | ||
si1 != 66 || | ||
si2 != 67 || | ||
subfield_length != 2 | ||
) { | ||
PyErr_Format( | ||
PyExc_ValueError, | ||
"Incorrect bgzip header:\n" | ||
"magic: %x, %x\n" | ||
"method: %x\n" | ||
"flags: %x\n" | ||
"xlen: %d\n" | ||
"si1, si2: %d, %d \n" | ||
"subfield_length: %d", | ||
magic1, magic2, method, flags, xlen, si1, si2, subfield_length | ||
); | ||
return NULL; | ||
} | ||
uint16_t block_size = load_u16_le(cursor + 16); | ||
const uint8_t *new_start = cursor + block_size + 1; | ||
if (new_start > data_end) { | ||
break; | ||
} | ||
cursor = new_start; | ||
} | ||
return PyLong_FromSsize_t(cursor - data); | ||
} | ||
|
||
static PyMethodDef _bgzip_methods[] = { | ||
{"find_last_bgzip_end", find_last_bgzip_end, METH_O, NULL}, | ||
{NULL}, | ||
}; | ||
|
||
static struct PyModuleDef _bgzip_module = { | ||
PyModuleDef_HEAD_INIT, | ||
.m_name = "_bgzip", | ||
.m_doc = NULL, | ||
.m_size = -1, | ||
.m_methods = _bgzip_methods, | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__bgzip(void) | ||
{ | ||
PyObject *m = PyModule_Create(&_bgzip_module); | ||
if (m == NULL) { | ||
return NULL; | ||
} | ||
return m; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a BgzipFormatError or something in this module to raise.