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 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

View File

@ -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 = <ecotx_t*> 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 = <ecotx_t*> 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)