Try for a new API
This commit is contained in:
@ -30,41 +30,6 @@ cdef class OBIDMS_column_line:
|
|||||||
cdef index_t _index
|
cdef index_t _index
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIView:
|
|
||||||
|
|
||||||
cdef Obiview_p _pointer
|
|
||||||
cdef dict _columns
|
|
||||||
|
|
||||||
cdef Obiview_p _open_or_create_view(self,
|
|
||||||
OBIDMS dms,
|
|
||||||
str view_name,
|
|
||||||
bint new=*,
|
|
||||||
object view_to_clone=*,
|
|
||||||
index_t* line_selection_p=*,
|
|
||||||
str comments=*,
|
|
||||||
bint quality_column=*,
|
|
||||||
str view_type=*)
|
|
||||||
|
|
||||||
cpdef delete_column(self, str column_name)
|
|
||||||
cpdef add_column(self,
|
|
||||||
str column_name,
|
|
||||||
obiversion_t version_number=*,
|
|
||||||
str alias=*,
|
|
||||||
str type=*,
|
|
||||||
index_t nb_lines=*,
|
|
||||||
index_t nb_elements_per_line=*,
|
|
||||||
list elements_names=*,
|
|
||||||
str indexer_name=*,
|
|
||||||
str associated_column_name=*,
|
|
||||||
obiversion_t associated_column_version=*,
|
|
||||||
str comments=*,
|
|
||||||
bint create=*
|
|
||||||
)
|
|
||||||
cpdef change_column_alias(self, str current_alias, str new_alias)
|
|
||||||
cpdef update_column_pointers(self)
|
|
||||||
cpdef close(self)
|
|
||||||
@staticmethod
|
|
||||||
cdef object get_view_subclass(str view_type)
|
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIView_line :
|
cdef class OBIView_line :
|
||||||
@ -85,7 +50,6 @@ cdef class OBIDMS:
|
|||||||
|
|
||||||
cpdef close(self)
|
cpdef close(self)
|
||||||
cpdef OBI_Taxonomy open_taxonomy(self, str taxo_name)
|
cpdef OBI_Taxonomy open_taxonomy(self, str taxo_name)
|
||||||
cpdef OBIView open_view(self, str view_name)
|
|
||||||
|
|
||||||
cpdef OBIView new_view(self, str view_name, str view_type=*, bint quality_column=*, str comments=*)
|
cpdef OBIView new_view(self, str view_name, str view_type=*, bint quality_column=*, str comments=*)
|
||||||
cpdef OBIView clone_view(self, str view_name, object view_to_clone, str comments=*)
|
cpdef OBIView clone_view(self, str view_name, object view_to_clone, str comments=*)
|
||||||
|
@ -76,6 +76,8 @@ from .capi.obiview cimport Obiview_p, \
|
|||||||
from libc.stdlib cimport malloc
|
from libc.stdlib cimport malloc
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIDMS_column :
|
cdef class OBIDMS_column :
|
||||||
|
|
||||||
# Note: should only be initialized through a subclass
|
# Note: should only be initialized through a subclass
|
||||||
@ -266,251 +268,6 @@ cdef class OBIDMS_column_line :
|
|||||||
######################################################################################################
|
######################################################################################################
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIView :
|
|
||||||
|
|
||||||
def __init__(self, OBIDMS dms, str view_name, bint new=False, object view_to_clone=None, OBIView_line_selection line_selection=None, str comments="", bint quality_column=False, str view_type=""):
|
|
||||||
|
|
||||||
cdef index_t* line_selection_p = NULL
|
|
||||||
cdef int i
|
|
||||||
cdef str col_alias
|
|
||||||
cdef OBIDMS_column_p column_p
|
|
||||||
cdef object subclass
|
|
||||||
|
|
||||||
if line_selection is not None :
|
|
||||||
# Get the name of the associated view to clone
|
|
||||||
view_to_clone = line_selection._view_name # TODO discuss. This makes it possible for the view to clone to be closed. If a view to clone was given it is not checked.
|
|
||||||
# Build the C array corresponding to the line selection
|
|
||||||
line_selection_p = <index_t*> malloc((len(line_selection) + 1) * sizeof(index_t)) # +1 for the -1 flagging the end of the array
|
|
||||||
for i in range(len(line_selection)) :
|
|
||||||
line_selection_p[i] = line_selection[i]
|
|
||||||
line_selection_p[len(line_selection)] = -1 # flagging the end of the array
|
|
||||||
|
|
||||||
self._pointer = self._open_or_create_view(dms, view_name, new=new, view_to_clone=view_to_clone, line_selection_p=line_selection_p, comments=comments, quality_column=quality_column, view_type=view_type)
|
|
||||||
|
|
||||||
# Go through columns to build dictionaries of corresponding python instances # TODO make function?
|
|
||||||
self._columns = {}
|
|
||||||
for i in range(self._pointer.infos.column_count) :
|
|
||||||
col_alias = bytes2str(self._pointer.infos.column_references[i].alias)
|
|
||||||
column_p = <OBIDMS_column_p> (self._pointer.columns)[i]
|
|
||||||
subclass = OBIDMS_column.get_subclass_type(column_p)
|
|
||||||
self._columns[col_alias] = subclass(self, col_alias)
|
|
||||||
|
|
||||||
|
|
||||||
cdef Obiview_p _open_or_create_view(self, OBIDMS dms, str view_name, bint new=False, object view_to_clone=None, index_t* line_selection_p=NULL, str comments="", bint quality_column=False, str view_type=""):
|
|
||||||
|
|
||||||
cdef Obiview_p view = NULL
|
|
||||||
|
|
||||||
# Create the view if needed, with the right type
|
|
||||||
if new :
|
|
||||||
if view_type == "" :
|
|
||||||
if view_to_clone is not None :
|
|
||||||
if type(view_to_clone) == str :
|
|
||||||
view = obi_new_view_cloned_from_name(dms._pointer, str2bytes(view_name), str2bytes(view_to_clone), line_selection_p, str2bytes(comments))
|
|
||||||
else :
|
|
||||||
view = obi_new_view(dms._pointer, str2bytes(view_name), (<OBIView> view_to_clone)._pointer, line_selection_p, str2bytes(comments))
|
|
||||||
else :
|
|
||||||
view = obi_new_view(dms._pointer, str2bytes(view_name), NULL, line_selection_p, str2bytes(comments))
|
|
||||||
elif view_type == bytes2str(VIEW_TYPE_NUC_SEQS) :
|
|
||||||
if view_to_clone is not None :
|
|
||||||
if type(view_to_clone) == str :
|
|
||||||
view = obi_new_view_nuc_seqs_cloned_from_name(dms._pointer, str2bytes(view_name), str2bytes(view_to_clone), line_selection_p, str2bytes(comments), quality_column)
|
|
||||||
else :
|
|
||||||
view = obi_new_view_nuc_seqs(dms._pointer, str2bytes(view_name), (<OBIView> view_to_clone)._pointer, line_selection_p, str2bytes(comments), quality_column)
|
|
||||||
else :
|
|
||||||
view = obi_new_view_nuc_seqs(dms._pointer, str2bytes(view_name), NULL, line_selection_p, str2bytes(comments), quality_column)
|
|
||||||
else :
|
|
||||||
raise Exception("View type not recognized")
|
|
||||||
|
|
||||||
# Else, open the existing view
|
|
||||||
else :
|
|
||||||
view = obi_open_view(dms._pointer, str2bytes(view_name))
|
|
||||||
|
|
||||||
if view == NULL :
|
|
||||||
raise Exception("Error creating/opening a view")
|
|
||||||
|
|
||||||
return view
|
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self) :
|
|
||||||
cdef str s
|
|
||||||
s = str(self.name) + "\n" + str(self.comments) + "\n" + str(self.line_count) + " lines\n"
|
|
||||||
for column_name in self._columns :
|
|
||||||
s = s + repr(self._columns[column_name]) + '\n'
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
cpdef delete_column(self, str column_name) :
|
|
||||||
cdef str column_n
|
|
||||||
if obi_view_delete_column(self._pointer, str2bytes(column_name)) < 0 :
|
|
||||||
raise Exception("Problem deleting a column from a view")
|
|
||||||
# Update the dictionary of column objects:
|
|
||||||
(self._columns).pop(column_name)
|
|
||||||
self.update_column_pointers()
|
|
||||||
|
|
||||||
|
|
||||||
cpdef add_column(self,
|
|
||||||
str column_name,
|
|
||||||
obiversion_t version_number=-1,
|
|
||||||
str alias='',
|
|
||||||
str type='',
|
|
||||||
index_t nb_lines=0,
|
|
||||||
index_t nb_elements_per_line=1,
|
|
||||||
list elements_names=None,
|
|
||||||
str indexer_name="",
|
|
||||||
str associated_column_name="",
|
|
||||||
obiversion_t associated_column_version=-1,
|
|
||||||
str comments="",
|
|
||||||
bint create=True
|
|
||||||
) :
|
|
||||||
|
|
||||||
cdef bytes column_name_b
|
|
||||||
cdef bytes elements_names_b
|
|
||||||
cdef object subclass
|
|
||||||
cdef OBIDMS_column_p column_p
|
|
||||||
|
|
||||||
column_name_b = str2bytes(column_name)
|
|
||||||
if alias == '' :
|
|
||||||
alias = column_name
|
|
||||||
alias_b = column_name_b
|
|
||||||
else :
|
|
||||||
alias_b = str2bytes(alias)
|
|
||||||
|
|
||||||
if elements_names is None :
|
|
||||||
elements_names_b = str2bytes("")
|
|
||||||
else :
|
|
||||||
elements_names_b = str2bytes(';'.join(elements_names))
|
|
||||||
|
|
||||||
if type : # TODO make C function that does that
|
|
||||||
if type == 'OBI_INT' :
|
|
||||||
data_type = OBI_INT
|
|
||||||
elif type == 'OBI_FLOAT' :
|
|
||||||
data_type = OBI_FLOAT
|
|
||||||
elif type == 'OBI_BOOL' :
|
|
||||||
data_type = OBI_BOOL
|
|
||||||
elif type == 'OBI_CHAR' :
|
|
||||||
data_type = OBI_CHAR
|
|
||||||
elif type == 'OBI_QUAL' :
|
|
||||||
data_type = OBI_QUAL
|
|
||||||
elif type == 'OBI_STR' :
|
|
||||||
data_type = OBI_STR
|
|
||||||
elif type == 'OBI_SEQ' :
|
|
||||||
data_type = OBI_SEQ
|
|
||||||
else :
|
|
||||||
raise Exception("Invalid provided data type")
|
|
||||||
|
|
||||||
if (obi_view_add_column(self._pointer, column_name_b, version_number, alias_b,
|
|
||||||
data_type, nb_lines, nb_elements_per_line,
|
|
||||||
elements_names_b, str2bytes(indexer_name),
|
|
||||||
str2bytes(associated_column_name), associated_column_version,
|
|
||||||
str2bytes(comments), create) < 0) :
|
|
||||||
raise Exception("Problem adding a column in a view")
|
|
||||||
|
|
||||||
# Get the column pointer
|
|
||||||
column_p = obi_view_get_column(self._pointer, alias_b)
|
|
||||||
|
|
||||||
# Open and store the subclass
|
|
||||||
subclass = OBIDMS_column.get_subclass_type(column_p)
|
|
||||||
(self._columns)[alias] = subclass(self, alias)
|
|
||||||
|
|
||||||
|
|
||||||
cpdef change_column_alias(self, str current_alias, str new_alias):
|
|
||||||
cdef OBIDMS_column column
|
|
||||||
if (obi_view_create_column_alias(self._pointer, str2bytes(current_alias), str2bytes(new_alias)) < 0) :
|
|
||||||
raise Exception("Problem changing a column alias")
|
|
||||||
# Update the dictionaries of column objects
|
|
||||||
self._columns[new_alias] = self._columns[current_alias]
|
|
||||||
column = self._columns[new_alias]
|
|
||||||
column._alias = new_alias
|
|
||||||
(self._columns).pop(current_alias)
|
|
||||||
|
|
||||||
|
|
||||||
cpdef update_column_pointers(self):
|
|
||||||
cdef str column_n
|
|
||||||
cdef OBIDMS_column column
|
|
||||||
for column_n in self._columns :
|
|
||||||
column = self._columns[column_n]
|
|
||||||
column._pointer = <OBIDMS_column_p*> obi_view_get_pointer_on_column_in_view(self._pointer, str2bytes(column_n))
|
|
||||||
|
|
||||||
|
|
||||||
cpdef close(self) :
|
|
||||||
if (obi_save_and_close_view(self._pointer) < 0) :
|
|
||||||
raise Exception("Problem closing a view")
|
|
||||||
self._pointer = NULL
|
|
||||||
self._columns = {}
|
|
||||||
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
# Iteration on each line of all columns
|
|
||||||
|
|
||||||
# Declarations
|
|
||||||
cdef index_t line_nb
|
|
||||||
cdef OBIView_line line
|
|
||||||
|
|
||||||
# Yield each line
|
|
||||||
for line_nb in range(self.line_count) :
|
|
||||||
line = self[line_nb]
|
|
||||||
yield line
|
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self, object item) :
|
|
||||||
if type(item) == str :
|
|
||||||
return (self._columns)[item]
|
|
||||||
elif type(item) == int :
|
|
||||||
return OBIView_line(self, item)
|
|
||||||
|
|
||||||
|
|
||||||
def __contains__(self, str column_name):
|
|
||||||
return (column_name in self._columns)
|
|
||||||
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return(self.line_count)
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self) :
|
|
||||||
cdef OBIView_line line
|
|
||||||
cdef str to_print
|
|
||||||
to_print = ""
|
|
||||||
for line in self :
|
|
||||||
to_print = to_print + str(line) + "\n"
|
|
||||||
return to_print
|
|
||||||
|
|
||||||
|
|
||||||
# line_count property getter
|
|
||||||
@property
|
|
||||||
def line_count(self):
|
|
||||||
return self._pointer.infos.line_count
|
|
||||||
|
|
||||||
# name property getter
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
return bytes2str(self._pointer.infos.name)
|
|
||||||
|
|
||||||
# view type property getter
|
|
||||||
@property
|
|
||||||
def type(self): # @ReservedAssignment
|
|
||||||
return bytes2str(self._pointer.infos.view_type)
|
|
||||||
|
|
||||||
# columns property getter
|
|
||||||
@property
|
|
||||||
def columns(self):
|
|
||||||
return self._columns
|
|
||||||
|
|
||||||
# comments property getter
|
|
||||||
@property
|
|
||||||
def comments(self):
|
|
||||||
return bytes2str(self._pointer.infos.comments)
|
|
||||||
# TODO setter that concatenates new comments?
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
cdef object get_view_subclass(str view_type) :
|
|
||||||
cdef object subclass
|
|
||||||
if view_type == bytes2str(VIEW_TYPE_NUC_SEQS) :
|
|
||||||
view_class = OBIView_NUC_SEQS
|
|
||||||
else :
|
|
||||||
view_class = OBIView
|
|
||||||
return view_class
|
|
||||||
|
|
||||||
|
|
||||||
######################################################################################################
|
######################################################################################################
|
||||||
@ -633,12 +390,6 @@ cdef class OBIDMS :
|
|||||||
return OBI_Taxonomy(self, taxo_name)
|
return OBI_Taxonomy(self, taxo_name)
|
||||||
|
|
||||||
|
|
||||||
cpdef OBIView open_view(self, str view_name) :
|
|
||||||
cdef object view_class
|
|
||||||
view_class = OBIView.get_view_subclass(self.read_view_infos(view_name)["view_type"])
|
|
||||||
return view_class(self, view_name)
|
|
||||||
|
|
||||||
|
|
||||||
cpdef OBIView new_view(self, str view_name, str view_type="", bint quality_column=False, str comments="") :
|
cpdef OBIView new_view(self, str view_name, str view_type="", bint quality_column=False, str comments="") :
|
||||||
cdef object view_class
|
cdef object view_class
|
||||||
# Get right subclass depending on view type
|
# Get right subclass depending on view type
|
||||||
|
@ -6,6 +6,7 @@ from .capi.obiview cimport NUC_SEQUENCE_COLUMN, \
|
|||||||
ID_COLUMN, \
|
ID_COLUMN, \
|
||||||
DEFINITION_COLUMN, \
|
DEFINITION_COLUMN, \
|
||||||
QUALITY_COLUMN
|
QUALITY_COLUMN
|
||||||
|
from docutils.nodes import definition
|
||||||
|
|
||||||
|
|
||||||
cdef str __str__ID_COLUMN__ = bytes2str(ID_COLUMN)
|
cdef str __str__ID_COLUMN__ = bytes2str(ID_COLUMN)
|
||||||
@ -19,11 +20,20 @@ cdef class OBI_Seq(dict) :
|
|||||||
def __init__(self, str id,
|
def __init__(self, str id,
|
||||||
object seq,
|
object seq,
|
||||||
str definition=None,
|
str definition=None,
|
||||||
dict attributes=None) :
|
dict tags=None) :
|
||||||
self.id = id
|
|
||||||
self.seq = seq
|
if tags is not None:
|
||||||
|
self.update(tags)
|
||||||
|
|
||||||
|
self._id = id
|
||||||
|
self._seq = seq
|
||||||
|
|
||||||
if definition is not None :
|
if definition is not None :
|
||||||
self.definition = definition
|
self._definition = definition
|
||||||
|
else:
|
||||||
|
self._definition = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self,str key):
|
def __getitem__(self,str key):
|
||||||
if key == __str__ID_COLUMN__:
|
if key == __str__ID_COLUMN__:
|
||||||
@ -33,6 +43,14 @@ cdef class OBI_Seq(dict) :
|
|||||||
else:
|
else:
|
||||||
return dict.__getitem__(self,key)
|
return dict.__getitem__(self,key)
|
||||||
|
|
||||||
|
def __setitem__(self,str key,object value):
|
||||||
|
if key == __str__ID_COLUMN__:
|
||||||
|
self._id=value
|
||||||
|
elif key == __str__DEFINITION_COLUMN__:
|
||||||
|
self._definition=value
|
||||||
|
else:
|
||||||
|
dict.__setitem__(self,key,value)
|
||||||
|
|
||||||
# sequence id property getter and setter
|
# sequence id property getter and setter
|
||||||
@property
|
@property
|
||||||
def id(self): # @ReservedAssignment
|
def id(self): # @ReservedAssignment
|
||||||
@ -61,14 +79,37 @@ cdef class OBI_Seq(dict) :
|
|||||||
|
|
||||||
cdef class OBI_Nuc_Seq(OBI_Seq) :
|
cdef class OBI_Nuc_Seq(OBI_Seq) :
|
||||||
|
|
||||||
|
def __init__(self, str id,
|
||||||
|
object seq,
|
||||||
|
str quality=None,
|
||||||
|
str definition=None,
|
||||||
|
dict tags=None) :
|
||||||
|
OBI_Seq.__init__(self,id,seq,tags)
|
||||||
|
if quality is not None:
|
||||||
|
self._quality=quality
|
||||||
|
else:
|
||||||
|
self._quality=None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self,str key):
|
def __getitem__(self,str key):
|
||||||
if key == __str__NUC_SEQUENCE_COLUMN__:
|
if key == __str__NUC_SEQUENCE_COLUMN__:
|
||||||
return self._id
|
return self._seq
|
||||||
elif key == __str__QUALITY_COLUMN__:
|
elif key == __str__QUALITY_COLUMN__:
|
||||||
return self._quality
|
return self._quality
|
||||||
else:
|
else:
|
||||||
return OBI_Seq.__getitem__(self,key)
|
return OBI_Seq.__getitem__(self,key)
|
||||||
|
|
||||||
|
def __setitem__(self,str key, object value):
|
||||||
|
if key == __str__NUC_SEQUENCE_COLUMN__:
|
||||||
|
self._seq = value
|
||||||
|
elif key == __str__QUALITY_COLUMN__:
|
||||||
|
self._quality=value
|
||||||
|
else:
|
||||||
|
OBI_Seq.__setitem__(self,key,value)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cpdef object get_str_quality(self) : # TODO not ideal. Make quality_int and quality_str properties
|
cpdef object get_str_quality(self) : # TODO not ideal. Make quality_int and quality_str properties
|
||||||
return self._quality
|
return self._quality
|
||||||
|
|
||||||
|
54
python/obitools3/obidms/_obiview.pxd
Normal file
54
python/obitools3/obidms/_obiview.pxd
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
cdef class OBIView:
|
||||||
|
|
||||||
|
cdef Obiview_p _pointer
|
||||||
|
cdef dict _columns
|
||||||
|
|
||||||
|
cdef __init_columns__(self):
|
||||||
|
|
||||||
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
OBIView_line_selection line_selection=*,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name)
|
||||||
|
|
||||||
|
cpdef delete_column(self, str column_name)
|
||||||
|
cpdef add_column(self,
|
||||||
|
str column_name,
|
||||||
|
obiversion_t version_number=*,
|
||||||
|
str alias=*,
|
||||||
|
str type=*,
|
||||||
|
index_t nb_lines=*,
|
||||||
|
index_t nb_elements_per_line=*,
|
||||||
|
list elements_names=*,
|
||||||
|
str indexer_name=*,
|
||||||
|
str associated_column_name=*,
|
||||||
|
obiversion_t associated_column_version=*,
|
||||||
|
str comments=*,
|
||||||
|
bint create=*
|
||||||
|
)
|
||||||
|
cpdef change_column_alias(self, str current_alias, str new_alias)
|
||||||
|
cpdef update_column_pointers(self)
|
||||||
|
|
||||||
|
cpdef OBIView_line_selection new_selection(self,
|
||||||
|
list lines=*)
|
||||||
|
|
||||||
|
cdef class OBIView_line_selection(list):
|
||||||
|
|
||||||
|
cdef OBIView _view
|
||||||
|
cdef str _view_name
|
||||||
|
|
||||||
|
cdef index_t* __build_binary_list__(self)
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
||||||
|
|
324
python/obitools3/obidms/_obiview.pyx
Normal file
324
python/obitools3/obidms/_obiview.pyx
Normal file
@ -0,0 +1,324 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
|
||||||
|
cdef class OBIView :
|
||||||
|
|
||||||
|
def __init__(self,int __internalCall__):
|
||||||
|
if __internalCall__!=987654:
|
||||||
|
raise RuntimeError('OBIView constructor cannot be called directly')
|
||||||
|
self._pointer = NULL
|
||||||
|
self._columns = {}
|
||||||
|
|
||||||
|
cdef __init_columns__(self):
|
||||||
|
cdef size_t i
|
||||||
|
cdef str col_alias
|
||||||
|
cdef OBIDMS_column_p column_p
|
||||||
|
cdef Obiview_p pointer = self._pointer
|
||||||
|
|
||||||
|
self._columns = {}
|
||||||
|
|
||||||
|
for i in range(pointer.infos.column_count) :
|
||||||
|
col_alias = bytes2str(pointer.infos.column_references[i].alias)
|
||||||
|
column_p = <OBIDMS_column_p> (pointer.columns)[i]
|
||||||
|
subclass = OBIDMS_column.get_subclass_type(column_p)
|
||||||
|
self._columns[col_alias] = subclass(self, col_alias)
|
||||||
|
|
||||||
|
|
||||||
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view(self._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._pointer,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_new_view(dms._pointer,
|
||||||
|
str2bytes(view_name),
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot create view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_open_view(dms._pointer,
|
||||||
|
str2bytes(view_name))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot open view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
def __dealloc__(self):
|
||||||
|
if (obi_save_and_close_view(self._pointer) < 0) :
|
||||||
|
raise Exception("Problem closing a view")
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self) :
|
||||||
|
cdef str s
|
||||||
|
s = str(self.name) + "\n" + str(self.comments) + "\n" + str(self.line_count) + " lines\n"
|
||||||
|
for column_name in self._columns :
|
||||||
|
s = s + repr(self._columns[column_name]) + '\n'
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
cpdef delete_column(self, str column_name) :
|
||||||
|
cdef str column_n
|
||||||
|
if obi_view_delete_column(self._pointer, str2bytes(column_name)) < 0 :
|
||||||
|
raise Exception("Problem deleting a column from a view")
|
||||||
|
# Update the dictionary of column objects:
|
||||||
|
(self._columns).pop(column_name)
|
||||||
|
self.update_column_pointers()
|
||||||
|
|
||||||
|
|
||||||
|
cpdef add_column(self,
|
||||||
|
str column_name,
|
||||||
|
obiversion_t version_number=-1,
|
||||||
|
str alias='',
|
||||||
|
str type='',
|
||||||
|
index_t nb_lines=0,
|
||||||
|
index_t nb_elements_per_line=1,
|
||||||
|
list elements_names=None,
|
||||||
|
str indexer_name="",
|
||||||
|
str associated_column_name="",
|
||||||
|
obiversion_t associated_column_version=-1,
|
||||||
|
str comments="",
|
||||||
|
bint create=True
|
||||||
|
) :
|
||||||
|
|
||||||
|
cdef bytes column_name_b
|
||||||
|
cdef bytes elements_names_b
|
||||||
|
cdef object subclass
|
||||||
|
cdef OBIDMS_column_p column_p
|
||||||
|
|
||||||
|
column_name_b = str2bytes(column_name)
|
||||||
|
if alias == '' :
|
||||||
|
alias = column_name
|
||||||
|
alias_b = column_name_b
|
||||||
|
else :
|
||||||
|
alias_b = str2bytes(alias)
|
||||||
|
|
||||||
|
if elements_names is None :
|
||||||
|
elements_names_b = str2bytes("")
|
||||||
|
else :
|
||||||
|
elements_names_b = str2bytes(';'.join(elements_names))
|
||||||
|
|
||||||
|
if type : # TODO make C function that does that
|
||||||
|
if type == 'OBI_INT' :
|
||||||
|
data_type = OBI_INT
|
||||||
|
elif type == 'OBI_FLOAT' :
|
||||||
|
data_type = OBI_FLOAT
|
||||||
|
elif type == 'OBI_BOOL' :
|
||||||
|
data_type = OBI_BOOL
|
||||||
|
elif type == 'OBI_CHAR' :
|
||||||
|
data_type = OBI_CHAR
|
||||||
|
elif type == 'OBI_QUAL' :
|
||||||
|
data_type = OBI_QUAL
|
||||||
|
elif type == 'OBI_STR' :
|
||||||
|
data_type = OBI_STR
|
||||||
|
elif type == 'OBI_SEQ' :
|
||||||
|
data_type = OBI_SEQ
|
||||||
|
else :
|
||||||
|
raise Exception("Invalid provided data type")
|
||||||
|
|
||||||
|
if (obi_view_add_column(self._pointer, column_name_b, version_number, alias_b,
|
||||||
|
data_type, nb_lines, nb_elements_per_line,
|
||||||
|
elements_names_b, str2bytes(indexer_name),
|
||||||
|
str2bytes(associated_column_name), associated_column_version,
|
||||||
|
str2bytes(comments), create) < 0) :
|
||||||
|
raise Exception("Problem adding a column in a view")
|
||||||
|
|
||||||
|
# Get the column pointer
|
||||||
|
column_p = obi_view_get_column(self._pointer, alias_b)
|
||||||
|
|
||||||
|
# Open and store the subclass
|
||||||
|
subclass = OBIDMS_column.get_subclass_type(column_p)
|
||||||
|
(self._columns)[alias] = subclass(self, alias)
|
||||||
|
|
||||||
|
|
||||||
|
cpdef change_column_alias(self, str current_alias, str new_alias):
|
||||||
|
cdef OBIDMS_column column
|
||||||
|
if (obi_view_create_column_alias(self._pointer, str2bytes(current_alias), str2bytes(new_alias)) < 0) :
|
||||||
|
raise Exception("Problem changing a column alias")
|
||||||
|
# Update the dictionaries of column objects
|
||||||
|
self._columns[new_alias] = self._columns[current_alias]
|
||||||
|
column = self._columns[new_alias]
|
||||||
|
column._alias = new_alias
|
||||||
|
(self._columns).pop(current_alias)
|
||||||
|
|
||||||
|
|
||||||
|
cpdef update_column_pointers(self):
|
||||||
|
cdef str column_n
|
||||||
|
cdef OBIDMS_column column
|
||||||
|
for column_n in self._columns :
|
||||||
|
column = self._columns[column_n]
|
||||||
|
column._pointer = <OBIDMS_column_p*> obi_view_get_pointer_on_column_in_view(self._pointer, str2bytes(column_n))
|
||||||
|
|
||||||
|
cpdef OBIView_line_selection new_selection(self,list lines=None):
|
||||||
|
return OBIView_line_selection(self,lines)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
# Iteration on each line of all columns
|
||||||
|
|
||||||
|
# Declarations
|
||||||
|
cdef index_t line_nb
|
||||||
|
cdef OBIView_line line
|
||||||
|
|
||||||
|
# Yield each line
|
||||||
|
for line_nb in range(self.line_count) :
|
||||||
|
line = self[line_nb]
|
||||||
|
yield line
|
||||||
|
|
||||||
|
|
||||||
|
def __getitem__(self, object item) :
|
||||||
|
if type(item) == str :
|
||||||
|
return (self._columns)[item]
|
||||||
|
elif type(item) == int :
|
||||||
|
return OBIView_line(self, item)
|
||||||
|
|
||||||
|
|
||||||
|
def __contains__(self, str column_name):
|
||||||
|
return (column_name in self._columns)
|
||||||
|
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return(self.line_count)
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self) :
|
||||||
|
cdef OBIView_line line
|
||||||
|
cdef str to_print
|
||||||
|
to_print = ""
|
||||||
|
for line in self :
|
||||||
|
to_print = to_print + str(line) + "\n"
|
||||||
|
return to_print
|
||||||
|
|
||||||
|
|
||||||
|
# line_count property getter
|
||||||
|
@property
|
||||||
|
def line_count(self):
|
||||||
|
return self._pointer.infos.line_count
|
||||||
|
|
||||||
|
# name property getter
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return bytes2str(self._pointer.infos.name)
|
||||||
|
|
||||||
|
# view type property getter
|
||||||
|
@property
|
||||||
|
def type(self): # @ReservedAssignment
|
||||||
|
return bytes2str(self._pointer.infos.view_type)
|
||||||
|
|
||||||
|
# columns property getter
|
||||||
|
@property
|
||||||
|
def columns(self):
|
||||||
|
return self._columns
|
||||||
|
|
||||||
|
# comments property getter
|
||||||
|
@property
|
||||||
|
def comments(self):
|
||||||
|
return bytes2str(self._pointer.infos.comments)
|
||||||
|
# TODO setter that concatenates new comments?
|
||||||
|
|
||||||
|
|
||||||
|
cdef class OBIView_line_selection(list):
|
||||||
|
|
||||||
|
def __init__(self, OBIView view, lines=None) :
|
||||||
|
if view._pointer == NULL:
|
||||||
|
raise Exception("Error: trying to create a line selection with an invalidated view")
|
||||||
|
self._view = view
|
||||||
|
self._view_name = view.name
|
||||||
|
|
||||||
|
if lines is not None:
|
||||||
|
self.extend(lines)
|
||||||
|
|
||||||
|
def extend(self, iterable):
|
||||||
|
cdef index_t i
|
||||||
|
cdef index_t max_i = self._view.line_count
|
||||||
|
|
||||||
|
for i in iterable:
|
||||||
|
if i > max_i:
|
||||||
|
raise RuntimeError("Error: trying to select line %d beyond the line count %d of view %s" %
|
||||||
|
(i,
|
||||||
|
max_i,
|
||||||
|
self._view_name)
|
||||||
|
)
|
||||||
|
list.append(self,i)
|
||||||
|
|
||||||
|
def append(self, index_t idx) :
|
||||||
|
if idx >= self._view.line_count :
|
||||||
|
raise RuntimeError("Error: trying to select line %d beyond the line count %d of view %s" %
|
||||||
|
(i,
|
||||||
|
max_i,
|
||||||
|
self._view_name)
|
||||||
|
)
|
||||||
|
list.append(self,idx)
|
||||||
|
|
||||||
|
cdef index_t* __build_binary_list__(self):
|
||||||
|
cdef index_t* line_selection_p = NULL
|
||||||
|
cdef int i
|
||||||
|
cdef size_t l_selection = len(self)
|
||||||
|
|
||||||
|
line_selection_p = <index_t*> malloc((l_selection + 1) * sizeof(index_t)) # +1 for the -1 flagging the end of the array
|
||||||
|
for i in range(l_selection) :
|
||||||
|
line_selection_p[i] = line_selection[i]
|
||||||
|
line_selection_p[l_selection] = -1 # flagging the end of the array
|
||||||
|
|
||||||
|
return line_selection_p
|
||||||
|
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view(self._view._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._view._pointer,
|
||||||
|
self.__build_binary_list__(),
|
||||||
|
str2bytes(comments))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
|
@ -4,5 +4,22 @@ from ._obidms cimport OBIView
|
|||||||
from ._obiseq cimport OBI_Nuc_Seq, OBI_Nuc_Seq_Stored
|
from ._obiseq cimport OBI_Nuc_Seq, OBI_Nuc_Seq_Stored
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIView_NUC_SEQS(OBIView):
|
cdef class OBIView_NUC_SEQS_QUAL(OBIView_NUC_SEQS):
|
||||||
pass
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
OBIView_line_selection line_selection=*,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name)
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_line_selection(OBIView_line_selection):
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
||||||
|
@ -5,6 +5,68 @@ from .capi.obitypes cimport index_t
|
|||||||
|
|
||||||
cdef class OBIView_NUC_SEQS(OBIView):
|
cdef class OBIView_NUC_SEQS(OBIView):
|
||||||
|
|
||||||
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view_nuc_seqs(self._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._pointer,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments),
|
||||||
|
False)
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_new_view_nuc_seqs(dms._pointer,
|
||||||
|
str2bytes(view_name),
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments),
|
||||||
|
False)
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot create view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_open_view(dms._pointer,
|
||||||
|
str2bytes(view_name))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot open view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
cpdef OBIView_line_selection new_selection(self,list lines=None):
|
||||||
|
return OBIView_NUC_SEQS_line_selection(self,lines)
|
||||||
|
|
||||||
def __getitem__(self, object item) :
|
def __getitem__(self, object item) :
|
||||||
if type(item) == str :
|
if type(item) == str :
|
||||||
@ -17,3 +79,27 @@ cdef class OBIView_NUC_SEQS(OBIView):
|
|||||||
for key in sequence_obj :
|
for key in sequence_obj :
|
||||||
self[line_idx][key] = sequence_obj[key]
|
self[line_idx][key] = sequence_obj[key]
|
||||||
|
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_line_selection(OBIView_line_selection):
|
||||||
|
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view(self._view._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._view._pointer,
|
||||||
|
self.__build_binary_list__(),
|
||||||
|
str2bytes(comments))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
25
python/obitools3/obidms/_obiview_nuc_seq_qual.pxd
Normal file
25
python/obitools3/obidms/_obiview_nuc_seq_qual.pxd
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
from ._obidms cimport OBIView
|
||||||
|
from ._obiseq cimport OBI_Nuc_Seq, OBI_Nuc_Seq_Stored
|
||||||
|
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_QUAL(OBIView_NUC_SEQS):
|
||||||
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
OBIView_line_selection line_selection=*,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name)
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_QUAL_line_selection(OBIView_NUC_SEQS_line_selection):
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=*)
|
90
python/obitools3/obidms/_obiview_nuc_seq_qual.pyx
Normal file
90
python/obitools3/obidms/_obiview_nuc_seq_qual.pyx
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
from .capi.obitypes cimport index_t
|
||||||
|
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_QUAL(OBIView_NUC_SEQS):
|
||||||
|
|
||||||
|
cpdef OBIView clone(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS_QUAL(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view_nuc_seqs(self._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._pointer,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments),
|
||||||
|
True)
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView new(OBIDMS dms,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS_QUAL(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_new_view_nuc_seqs(dms._pointer,
|
||||||
|
str2bytes(view_name),
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
str2bytes(comments),
|
||||||
|
True)
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot create view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
cpdef OBIView open(OBIDMS dms,
|
||||||
|
str view_name):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS_QUAL(987654) # @DuplicatedSignature
|
||||||
|
|
||||||
|
view._pointer = obi_open_view(dms._pointer,
|
||||||
|
str2bytes(view_name))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot open view %s" % view_name)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
|
cdef class OBIView_NUC_SEQS_QUAL_line_selection(OBIView_NUC_SEQS_line_selection):
|
||||||
|
|
||||||
|
cpdef OBIView materialize(self,
|
||||||
|
str view_name,
|
||||||
|
str comments=""):
|
||||||
|
|
||||||
|
cdef OBIView view = OBIView_NUC_SEQS_QUAL(987654)
|
||||||
|
|
||||||
|
view._pointer = obi_new_view(self._view._pointer.dms,
|
||||||
|
str2bytes(view_name),
|
||||||
|
self._view._pointer,
|
||||||
|
self.__build_binary_list__(),
|
||||||
|
str2bytes(comments))
|
||||||
|
|
||||||
|
if view._pointer == NULL :
|
||||||
|
raise RuntimeError("Error : Cannot clone view %s into view %s"
|
||||||
|
% (str(self.name),
|
||||||
|
view_name)
|
||||||
|
)
|
||||||
|
|
||||||
|
view.__init_columns__(self)
|
||||||
|
|
||||||
|
return view
|
Reference in New Issue
Block a user