Added properties in the OBI_Taxonomy class

This commit is contained in:
Celine Mercier
2016-10-10 17:01:17 +02:00
parent 2dcfdc59fc
commit 60ab503a14
2 changed files with 54 additions and 47 deletions

View File

@ -2,30 +2,15 @@
from .capi.obitaxonomy cimport ecotx_t, OBIDMS_taxonomy_p from .capi.obitaxonomy cimport ecotx_t, OBIDMS_taxonomy_p
from libc.stdint cimport int32_t
cdef class OBI_Taxonomy : cdef class OBI_Taxonomy :
cdef str name cdef str _name
cdef OBIDMS_taxonomy_p pointer cdef OBIDMS_taxonomy_p _pointer
cpdef close(self) cpdef close(self)
cdef class OBI_Taxon : cdef class OBI_Taxon :
cdef ecotx_t* pointer 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)

View File

@ -9,57 +9,79 @@ from .capi.obitaxonomy cimport obi_read_taxonomy, \
from ._obidms cimport OBIDMS from ._obidms cimport OBIDMS
from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
from logging import raiseExceptions
cdef class OBI_Taxonomy : cdef class OBI_Taxonomy :
# TODO function to import taxonomy?
def __init__(self, OBIDMS dms, str name) : def __init__(self, OBIDMS dms, str name) :
self.name = name self._name = name
self.pointer = obi_read_taxonomy(dms._pointer, str2bytes(name), True) # TODO discuss 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): def __getitem__(self, object ref):
cdef ecotx_t* taxon_p cdef ecotx_t* taxon_p
cdef object taxon_capsule cdef object taxon_capsule
if type(ref) == int : 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) taxon_capsule = PyCapsule_New(taxon_p, NULL, NULL)
return OBI_Taxon(taxon_capsule) return OBI_Taxon(taxon_capsule)
else :
raise Exception("Not implemented")
cpdef close(self) : cpdef close(self) :
if (obi_close_taxonomy(self.pointer) < 0) : if (obi_close_taxonomy(self._pointer) < 0) :
raise Exception("Error closing the taxonomy") 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) : def __init__(self, object taxon_capsule) :
self._pointer = <ecotx_t*> PyCapsule_GetPointer(taxon_capsule, NULL)
cdef ecotx_t* taxon if self._pointer == NULL :
raise Exception("Error reading the taxonomy")
taxon = <ecotx_t*> PyCapsule_GetPointer(taxon_capsule, NULL)
self.pointer = taxon # name property getter
self.taxid = taxon.taxid @property
self.rank = taxon.rank def name(self):
self.farest = taxon.farest return bytes2str(self._pointer.name)
self.parent = taxon.parent
self.name = bytes2str(taxon.name)
cpdef int32_t taxid(self):
return self.taxid
cpdef int32_t rank(self): # taxid property getter
return self.rank @property
def taxid(self):
cpdef int32_t farest(self): return self._pointer.taxid
return self.farest
# rank property getter
cpdef OBI_Taxon parent(self): @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 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) 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)