From fa4e4ffaff8fc17e1db7027a11093e4dc3b2fecb Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Wed, 7 Dec 2016 14:17:57 +0100 Subject: [PATCH] Changed the cython API to create new views so as to have different functions for the different cases --- python/obitools3/commands/grep.pyx | 2 +- python/obitools3/commands/test.pyx | 19 ++++---- python/obitools3/obidms/_obidms.pxd | 26 ++++++----- python/obitools3/obidms/_obidms.pyx | 68 ++++++++++++++++------------- 4 files changed, 64 insertions(+), 51 deletions(-) diff --git a/python/obitools3/commands/grep.pyx b/python/obitools3/commands/grep.pyx index 2a75e86..77dc49d 100644 --- a/python/obitools3/commands/grep.pyx +++ b/python/obitools3/commands/grep.pyx @@ -77,7 +77,7 @@ def run(config): selection.append(i) # Create output view with the line selection - oview = d.new_view(config['obi']['outputview'], line_selection=selection, comments="obi grep: "+str(config['grep']['predicates'])+"\n") + oview = d.clone_view_with_line_selection(config['obi']['outputview'], selection, comments="obi grep: "+str(config['grep']['predicates'])+"\n") #print("\n") #print(repr(oview)) diff --git a/python/obitools3/commands/test.pyx b/python/obitools3/commands/test.pyx index 3a45289..774494a 100644 --- a/python/obitools3/commands/test.pyx +++ b/python/obitools3/commands/test.pyx @@ -8,7 +8,7 @@ import string import random -VIEW_TYPES = [None, "NUC_SEQS_VIEW"] +VIEW_TYPES = ["", "NUC_SEQS_VIEW"] COL_TYPES = ["OBI_BOOL", "OBI_CHAR", "OBI_FLOAT", "OBI_INT", "OBI_SEQ", "OBI_STR"] NUC_SEQUENCE_COLUMN = "NUC_SEQ" ID_COLUMN = "ID" @@ -212,13 +212,12 @@ def fill_view(config, infos): def random_new_view(config, infos, first=False): v_to_clone = None line_selection = None - clone = False quality_col = False # TODO if not first: infos['view_names'].append(infos['view'].name) infos['view'].close() v_to_clone = infos['dms'].open_view(random.choice(infos["view_names"])) - v_type = None + v_type = "" print_test(config, "View to clone: ") print_test(config, repr(v_to_clone)) create_line_selection = random_bool(config) @@ -230,12 +229,14 @@ def random_new_view(config, infos, first=False): #print_test(config, "New line selection: "+str(line_selection)) else : v_type = random_view_type() - infos['view'] = infos['dms'].new_view(random_unique_name(infos), - view_to_clone=v_to_clone, - line_selection=line_selection, - view_type=v_type, - comments=random_str_with_max_len(config['test']['commentsmaxlen']), - quality_column=quality_col) + + if line_selection is not None : + infos['view'] = infos['dms'].clone_view_with_line_selection(random_unique_name(infos), line_selection, comments=random_str_with_max_len(config['test']['commentsmaxlen'])) + elif v_to_clone is not None : + infos['view'] = infos['dms'].clone_view(random_unique_name(infos), v_to_clone, comments=random_str_with_max_len(config['test']['commentsmaxlen'])) + else : + infos['view'] = infos['dms'].new_view(random_unique_name(infos), view_type=v_type, comments=random_str_with_max_len(config['test']['commentsmaxlen']), quality_column=quality_col) + print_test(config, repr(infos['view'])) if v_to_clone is not None : if line_selection is None: diff --git a/python/obitools3/obidms/_obidms.pxd b/python/obitools3/obidms/_obidms.pxd index 0fe036f..64574ad 100644 --- a/python/obitools3/obidms/_obidms.pxd +++ b/python/obitools3/obidms/_obidms.pxd @@ -36,14 +36,14 @@ cdef class OBIView: 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=*) + 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, @@ -63,7 +63,9 @@ cdef class OBIView: 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_NUC_SEQS(OBIView): @@ -96,7 +98,11 @@ cdef class OBIDMS: cpdef close(self) 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, object view_to_clone=*, OBIView_line_selection line_selection=*, str view_type=*, str comments=*, bint quality_column=*) + + 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_with_line_selection(self, str view_name, OBIView_line_selection line_selection, str comments=*) + cpdef dict read_view_infos(self, str view_name) # cpdef dict read_views(self) TODO diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index aaeba5d..cb9bc6a 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -272,7 +272,7 @@ 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=None): + 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 @@ -300,13 +300,13 @@ cdef class OBIView : 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=None): + 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 is None : + 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)) @@ -507,6 +507,16 @@ cdef class OBIView : # 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 + + ###################################################################################################### @@ -682,42 +692,38 @@ cdef class OBIDMS : cpdef OBI_Taxonomy open_taxonomy(self, str taxo_name) : return OBI_Taxonomy(self, taxo_name) - + cpdef OBIView open_view(self, str view_name) : - cdef object view_class - cdef dict view_infos - - view_infos = self.read_view_infos(view_name) - - if view_infos["view_type"] == bytes2str(VIEW_TYPE_NUC_SEQS) : - view_class = OBIView_NUC_SEQS - else : - view_class = OBIView - + 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, 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 - + cpdef OBIView new_view(self, str view_name, str view_type="", bint quality_column=False, str comments="") : + 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) : - view_class = OBIView_NUC_SEQS + view_class = OBIView.get_view_subclass(view_type) + return view_class(self, view_name, new=True, comments=comments, quality_column=quality_column, view_type=view_type) + + + cpdef OBIView clone_view(self, str view_name, object view_to_clone, str comments="") : + cdef object view_class + cdef str view_type + # Get right subclass depending on view type + if type(view_to_clone) == str : + view_type = self.read_view_infos(view_to_clone)["view_type"] 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 \ - ((type(view_to_clone) == str and self.read_view_infos(view_to_clone)["view_type"] == bytes2str(VIEW_TYPE_NUC_SEQS)) or \ - (type(view_to_clone) != str and view_to_clone.type == bytes2str(VIEW_TYPE_NUC_SEQS)))) or \ - (line_selection is not None and (line_selection._view.type == bytes2str(VIEW_TYPE_NUC_SEQS))) : - view_type = bytes2str(VIEW_TYPE_NUC_SEQS) - view_class = OBIView_NUC_SEQS - - return view_class(self, view_name, new=True, view_to_clone=view_to_clone, line_selection=line_selection, comments=comments, quality_column=quality_column, view_type=view_type) + view_type = view_to_clone.type + view_class = OBIView.get_view_subclass(view_type) + return view_class(self, view_name, new=True, view_to_clone=view_to_clone, comments=comments, view_type=view_type) + + + cpdef OBIView clone_view_with_line_selection(self, str view_name, OBIView_line_selection line_selection, str comments="") : + cdef object view_class + # Get right subclass depending on view type + view_class = OBIView.get_view_subclass(line_selection._view.type) + return view_class(self, view_name, new=True, view_to_clone=line_selection._view, line_selection=line_selection, comments=comments) cpdef dict read_view_infos(self, str view_name) :