From 60ab503a144d86b07c2ef1e18f4ca2621c1ed657 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 10 Oct 2016 17:01:17 +0200 Subject: [PATCH] Added properties in the OBI_Taxonomy class --- python/obitools3/obidms/_obitaxo.pxd | 21 ++------ python/obitools3/obidms/_obitaxo.pyx | 80 ++++++++++++++++++---------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/python/obitools3/obidms/_obitaxo.pxd b/python/obitools3/obidms/_obitaxo.pxd index 1d42862..7f7ac52 100644 --- a/python/obitools3/obidms/_obitaxo.pxd +++ b/python/obitools3/obidms/_obitaxo.pxd @@ -2,30 +2,15 @@ from .capi.obitaxonomy cimport ecotx_t, OBIDMS_taxonomy_p -from libc.stdint cimport int32_t - cdef class OBI_Taxonomy : - cdef str name - cdef OBIDMS_taxonomy_p pointer + cdef str _name + cdef OBIDMS_taxonomy_p _pointer cpdef close(self) cdef class OBI_Taxon : - cdef ecotx_t* pointer - cdef int32_t taxid - cdef int32_t rank - cdef int32_t farest - cdef ecotx_t* parent - cdef str name - - cpdef int32_t taxid(self) - - cpdef int32_t rank(self) - - cpdef int32_t farest(self) - - cpdef OBI_Taxon parent(self) + cdef ecotx_t* _pointer diff --git a/python/obitools3/obidms/_obitaxo.pyx b/python/obitools3/obidms/_obitaxo.pyx index feb4a02..2a074d0 100644 --- a/python/obitools3/obidms/_obitaxo.pyx +++ b/python/obitools3/obidms/_obitaxo.pyx @@ -9,57 +9,79 @@ from .capi.obitaxonomy cimport obi_read_taxonomy, \ from ._obidms cimport OBIDMS from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer +from logging import raiseExceptions cdef class OBI_Taxonomy : + # TODO function to import taxonomy? + def __init__(self, OBIDMS dms, str name) : - self.name = name - self.pointer = obi_read_taxonomy(dms._pointer, str2bytes(name), True) # TODO discuss - + self._name = name + self._pointer = obi_read_taxonomy(dms._pointer, str2bytes(name), True) # TODO discuss + # TODO if not found in DMS, try to import? + def __getitem__(self, object ref): cdef ecotx_t* taxon_p - cdef object taxon_capsule + cdef object taxon_capsule if type(ref) == int : - taxon_p = obi_taxo_get_taxon_with_taxid(self.pointer, ref) + taxon_p = obi_taxo_get_taxon_with_taxid(self._pointer, ref) taxon_capsule = PyCapsule_New(taxon_p, NULL, NULL) return OBI_Taxon(taxon_capsule) + else : + raise Exception("Not implemented") + - cpdef close(self) : - if (obi_close_taxonomy(self.pointer) < 0) : + if (obi_close_taxonomy(self._pointer) < 0) : raise Exception("Error closing the taxonomy") -cdef class OBI_Taxon : # dict subclass? +cdef class OBI_Taxon : # TODO dict subclass? def __init__(self, object taxon_capsule) : - - cdef ecotx_t* taxon - - taxon = PyCapsule_GetPointer(taxon_capsule, NULL) - self.pointer = taxon - self.taxid = taxon.taxid - self.rank = taxon.rank - self.farest = taxon.farest - self.parent = taxon.parent - self.name = bytes2str(taxon.name) - - cpdef int32_t taxid(self): - return self.taxid + self._pointer = PyCapsule_GetPointer(taxon_capsule, NULL) + if self._pointer == NULL : + raise Exception("Error reading the taxonomy") + + # name property getter + @property + def name(self): + return bytes2str(self._pointer.name) - cpdef int32_t rank(self): - return self.rank - - cpdef int32_t farest(self): - return self.farest - - cpdef OBI_Taxon parent(self): + # taxid property getter + @property + def taxid(self): + return self._pointer.taxid + + # rank property getter + @property + def rank(self): + return self._pointer.rank + + # farest property getter + @property + def farest(self): + return self._pointer.farest + + # parent property getter + @property + def parent(self): cdef object parent_capsule - parent_capsule = PyCapsule_New(self.parent, NULL, NULL) + parent_capsule = PyCapsule_New(self._pointer.parent, NULL, NULL) return OBI_Taxon(parent_capsule) + + def __repr__(self): + d = {} + d['taxid'] = self.taxid + d['name'] = self.name + d['parent'] = self.parent.taxid + d['farest'] = self.farest + return str(d) + +