From 36ac315125d1cc81b19e417c57a66acad43f75a8 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Tue, 8 Nov 2016 11:23:54 +0100 Subject: [PATCH] Fixed bugs with python view type when creating a new view, and a bug when trying to guess the obi type of a nucleotide sequence when its type was bytes --- python/obitools3/obidms/_obidms.pyx | 31 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index db0fcf9..8832133 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -279,7 +279,7 @@ cdef class OBIView : 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. @@ -289,7 +289,7 @@ cdef class OBIView : 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=None) + 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 = {} @@ -314,7 +314,7 @@ cdef class OBIView : view = obi_new_view(dms._pointer, str2bytes(view_name), ( 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 == VIEW_TYPE_NUC_SEQS : + 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) @@ -322,7 +322,9 @@ cdef class OBIView : view = obi_new_view_nuc_seqs(dms._pointer, str2bytes(view_name), ( 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)) @@ -593,8 +595,10 @@ cdef class OBIView_line : def __setitem__(self, str column_name, object value): # TODO detect multiple elements (dict type)? put somewhere else? but more risky (in get) # TODO OBI_QUAL ? - cdef type value_type - cdef str value_obitype + cdef type value_type + cdef str value_obitype + cdef bytes value_b + if column_name not in self._view : if value == None : raise Exception("Trying to create a column from a None value (can't guess type)") @@ -605,8 +609,12 @@ cdef class OBIView_line : value_obitype = 'OBI_FLOAT' elif value_type == bool : value_obitype = 'OBI_BOOL' - elif value_type == str : - if only_ATGC(str2bytes(value)) : # TODO detect IUPAC? + elif value_type == str or value_type == bytes : + if value_type == str : + value_b = str2bytes(value) + else : + value_b = value + if only_ATGC(value_b) : # TODO detect IUPAC? value_obitype = 'OBI_SEQ' elif len(value) == 1 : value_obitype = 'OBI_CHAR' @@ -706,7 +714,7 @@ cdef class OBIDMS : cpdef OBIView new_view(self, str view_name, object view_to_clone=None, OBIView_line_selection line_selection=None, str view_type=None, str comments="", bint quality_column=False) : cdef object view_class - + # Get right subclass depending on view type if view_type is not None : if view_type == bytes2str(VIEW_TYPE_NUC_SEQS) : @@ -714,9 +722,10 @@ cdef class OBIDMS : else : view_class = OBIView # Check the type of the view to clone if there is one # TODO make generic for future other view types - if view_to_clone is not None and \ + if (view_to_clone is not None and \ ((type(view_to_clone) == str and self.read_view_infos(view_to_clone)["view_type"] == bytes2str(VIEW_TYPE_NUC_SEQS)) or \ - isinstance(view_to_clone, OBIView_NUC_SEQS)) : + isinstance(view_to_clone, OBIView_NUC_SEQS))) or \ + (line_selection is not None and isinstance(line_selection._view, OBIView_NUC_SEQS)) : view_type = bytes2str(VIEW_TYPE_NUC_SEQS) view_class = OBIView_NUC_SEQS