From e323d8e702faabd658d4b0661ad1f5ca57f4ec81 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 29 Feb 2016 16:33:30 +0100 Subject: [PATCH] Cython classes for nucleotide sequences (outside or in the context of a view) --- python/obitools3/obidms/_obidms.pyx | 31 ++++++---- python/obitools3/obidms/_obiseq.cfiles | 18 ++++++ python/obitools3/obidms/_obiseq.pxd | 34 +++++++++++ python/obitools3/obidms/_obiseq.pyx | 78 ++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 python/obitools3/obidms/_obiseq.cfiles create mode 100644 python/obitools3/obidms/_obiseq.pxd create mode 100644 python/obitools3/obidms/_obiseq.pyx diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index 54c00ff..a2ec7f5 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -1,7 +1,5 @@ #cython: language_level=3 -from pathlib import Path - from obitools3.utils cimport bytes2str, str2bytes from .capi.obidms cimport obi_dms, \ @@ -23,9 +21,12 @@ from .capi.obitypes cimport const_char_p, \ name_data_type, \ only_ATGC # discuss -from ._obidms cimport OBIDMS -from ._obidms cimport OBIDMS_column -from ._obidms cimport OBIView_line +from ._obidms cimport OBIDMS, \ + OBIDMS_column, \ + OBIView, \ + OBIView_line + +from ._obiseq cimport OBI_Nuc_Seq, OBI_Nuc_Seq_Stored from ._obidmscolumn_int cimport OBIDMS_column_int, \ OBIDMS_column_multi_elts_int @@ -45,8 +46,6 @@ from ._obidmscolumn_str cimport OBIDMS_column_str, \ from ._obidmscolumn_seq cimport OBIDMS_column_seq, \ OBIDMS_column_multi_elts_seq -from ._obidms cimport OBIView, OBIView_line - from .capi.obiview cimport Obiview_p, \ Obiviews_infos_all_p, \ Obiview_infos_p, \ @@ -414,7 +413,7 @@ cdef class OBIView : # Declarations cdef index_t lines_used cdef index_t line_nb - cdef OBIView_line line + cdef OBIView_line line # TODO for NUC SEQS View # Yield each line TODO line class lines_used = (self.pointer).line_count @@ -563,6 +562,18 @@ cdef class OBIView_NUC_SEQS(OBIView): for column_n in self.columns : (self.columns[column_n]).update_pointer() + + def __getitem__(self, object item) : + if type(item) == str : + return (self.columns)[item] + elif type(item) == int : # TODO int? + return OBI_Nuc_Seq_Stored(self, item) + + + def __setitem__(self, index_t line_idx, OBI_Nuc_Seq sequence_obj) : + for key in sequence_obj : + self[line_idx][key] = sequence_obj[key] + ############################################# @@ -644,7 +655,7 @@ cdef class OBIDMS : view_infos = self.read_view_infos(view_name) - if view_infos["view_type"] == bytes2str(VIEW_TYPE_NUC_SEQS) : # TODO not gonna work + if view_infos["view_type"] == bytes2str(VIEW_TYPE_NUC_SEQS) : view_class = OBIView_NUC_SEQS else : view_class = OBIView @@ -657,7 +668,7 @@ cdef class OBIDMS : cdef object view_class if view_type is not None : - if view_type == bytes2str(VIEW_TYPE_NUC_SEQS) : # TODO not gonna work + if view_type == bytes2str(VIEW_TYPE_NUC_SEQS) : view_class = OBIView_NUC_SEQS else : view_class = OBIView diff --git a/python/obitools3/obidms/_obiseq.cfiles b/python/obitools3/obidms/_obiseq.cfiles new file mode 100644 index 0000000..c92a0c5 --- /dev/null +++ b/python/obitools3/obidms/_obiseq.cfiles @@ -0,0 +1,18 @@ +../../../src/obidms.h +../../../src/obidms.c +../../../src/obiview.h +../../../src/obiview.c +../../../src/obidmscolumn.h +../../../src/obidmscolumn.c +../../../src/obidmscolumndir.h +../../../src/obidmscolumndir.c +../../../src/obierrno.h +../../../src/obierrno.c +../../../src/obilittlebigman.h +../../../src/obilittlebigman.c +../../../src/obitypes.h +../../../src/obitypes.c +../../../src/private_at_functions.h +../../../src/private_at_functions.c +../../../src/obiavl.h +../../../src/obiavl.c diff --git a/python/obitools3/obidms/_obiseq.pxd b/python/obitools3/obidms/_obiseq.pxd new file mode 100644 index 0000000..3dbd4fe --- /dev/null +++ b/python/obitools3/obidms/_obiseq.pxd @@ -0,0 +1,34 @@ +#cython: language_level=3 + +from ._obidms cimport OBIView_line + + +cdef class OBI_Seq(dict) : + cdef str id + cdef str description + cdef str sequence + + cpdef set_id(self, str id) + cpdef get_id(self) + cpdef set_description(self, str description) + cpdef get_description(self) + cpdef get_sequence(self) + + +cdef class OBI_Nuc_Seq(OBI_Seq) : + #cpdef str reverse_complement(self) + cpdef set_sequence(self, str sequence) + + +cdef class OBI_Nuc_Seq_Stored(OBIView_line) : + cdef str id + cdef str description + cdef str sequence + + cpdef set_id(self, str id) + cpdef get_id(self) + cpdef set_description(self, str description) + cpdef get_description(self) + cpdef set_sequence(self, str sequence) + cpdef get_sequence(self) + # cpdef str reverse_complement(self) diff --git a/python/obitools3/obidms/_obiseq.pyx b/python/obitools3/obidms/_obiseq.pyx new file mode 100644 index 0000000..4e10350 --- /dev/null +++ b/python/obitools3/obidms/_obiseq.pyx @@ -0,0 +1,78 @@ +#cython: language_level=3 + +from obitools3.utils cimport bytes2str, str2bytes + +from .capi.obiview cimport NUC_SEQUENCE_COLUMN, \ + ID_COLUMN, \ + DESCRIPTION_COLUMN + +cdef class OBI_Seq(dict) : + + def __init__(self, str id, str seq, str description=None) : + self.set_id(id) + self.set_sequence(seq) + if description is not None : + self.set_description(description) + + cpdef set_id(self, str id) : + self.id = id + self[bytes2str(ID_COLUMN)] = id + + cpdef get_id(self) : + return self.id + + cpdef set_description(self, str description) : + self.description = description + self[bytes2str(DESCRIPTION_COLUMN)] = description + + cpdef get_description(self) : + return self.description + + cpdef get_sequence(self) : + return self.sequence + + def __str__(self) : + return self.sequence # or not + + +cdef class OBI_Nuc_Seq(OBI_Seq) : + + cpdef set_sequence(self, str sequence) : + self.sequence = sequence + self[bytes2str(NUC_SEQUENCE_COLUMN)] = sequence + +# cpdef str reverse_complement(self) : TODO in C ? +# pass + + +cdef class OBI_Nuc_Seq_Stored(OBIView_line) : + + cpdef set_id(self, str id) : + self.id = id + self[bytes2str(ID_COLUMN)] = id + + cpdef get_id(self) : + return self.id + + cpdef set_description(self, str description) : + self.description = description + self[bytes2str(DESCRIPTION_COLUMN)] = description + + cpdef get_description(self) : + return self.description + + cpdef set_sequence(self, str sequence) : + self.sequence = sequence + self[bytes2str(NUC_SEQUENCE_COLUMN)] = sequence + + cpdef get_sequence(self) : + return self.sequence + + def __str__(self) : + return self.sequence # or not + +# cpdef str reverse_complement(self) : TODO in C ? +# pass + + # TODO static method to import? +