Cython: View: new method to print a view to a buffer (e.g. stdout)

This commit is contained in:
Celine Mercier
2020-07-24 16:03:23 +02:00
parent 9a9afde113
commit 46ca693ca9
4 changed files with 48 additions and 4 deletions

View File

@ -39,4 +39,6 @@ cdef class Nuc_Seq_Stored(Seq_Stored) :
cpdef set_quality_char(self, object new_qual, int offset=*) cpdef set_quality_char(self, object new_qual, int offset=*)
cpdef object build_quality_array(self, list quality) cpdef object build_quality_array(self, list quality)
cpdef bytes build_reverse_complement(self) cpdef bytes build_reverse_complement(self)
cpdef str get_str(self) cpdef str get_str(self)
cpdef repr_bytes(self)

View File

@ -431,9 +431,12 @@ cdef class Nuc_Seq_Stored(Seq_Stored) :
return len(self._view.get_column(NUC_SEQUENCE_COLUMN).get_line(self.index)) return len(self._view.get_column(NUC_SEQUENCE_COLUMN).get_line(self.index))
def __repr__(self): def __repr__(self):
return bytes2str(self.repr_bytes())
cpdef repr_bytes(self):
if self.quality is None: if self.quality is None:
formatter = FastaFormat() formatter = FastaFormat()
else: else:
formatter = FastqFormat() formatter = FastqFormat()
return bytes2str(formatter(self)) return formatter(self)

View File

@ -20,6 +20,10 @@ cdef class View(OBIWrapper):
cdef DMS _dms cdef DMS _dms
cdef inline Obiview_p pointer(self) cdef inline Obiview_p pointer(self)
cpdef print_to_output(self,
object output,
bint noprogressbar=*)
cpdef delete_column(self, cpdef delete_column(self,
object column_name, object column_name,
@ -61,6 +65,8 @@ cdef class Line :
cdef index_t _index cdef index_t _index
cdef View _view cdef View _view
cpdef repr_bytes(self)
cdef register_view_class(bytes view_type_name, cdef register_view_class(bytes view_type_name,
type view_class) type view_class)

View File

@ -6,6 +6,8 @@ cdef dict __VIEW_CLASS__= {}
from libc.stdlib cimport malloc from libc.stdlib cimport malloc
from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport
from ..capi.obiview cimport Alias_column_pair_p, \ from ..capi.obiview cimport Alias_column_pair_p, \
obi_new_view, \ obi_new_view, \
obi_open_view, \ obi_open_view, \
@ -48,10 +50,13 @@ from ..capi.obidms cimport obi_import_view
from obitools3.format.tab import TabFormat from obitools3.format.tab import TabFormat
from cpython.exc cimport PyErr_CheckSignals
import importlib import importlib
import inspect import inspect
import pkgutil import pkgutil
import json import json
import sys
cdef class View(OBIWrapper) : cdef class View(OBIWrapper) :
@ -184,7 +189,31 @@ cdef class View(OBIWrapper) :
for column_name in self.keys() : for column_name in self.keys() :
s = s + repr(self[column_name]) + '\n' s = s + repr(self[column_name]) + '\n'
return s return s
cpdef print_to_output(self, object output, bint noprogressbar=False):
cdef int i
cdef Line entry
self.checkIsActive(self)
# Initialize the progress bar
if noprogressbar == False:
pb = ProgressBar(len(self))
else:
pb = None
i=0
for entry in self:
PyErr_CheckSignals()
if pb is not None:
pb(i)
output.write(entry.repr_bytes()+b"\n")
i+=1
if pb is not None:
pb(len(self), force=True)
print("", file=sys.stderr)
def keys(self): def keys(self):
@ -757,8 +786,12 @@ cdef class Line :
def __repr__(self): def __repr__(self):
return bytes2str(self).repr_bytes()
cpdef repr_bytes(self):
formatter = TabFormat(header=False) formatter = TabFormat(header=False)
return bytes2str(formatter(self)) return formatter(self)
# View property getter # View property getter