Added properties in the OBI_Taxonomy class
This commit is contained in:
@ -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)
|
|
||||||
|
@ -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)
|
||||||
|
if self._pointer == NULL :
|
||||||
|
raise Exception("Error reading the taxonomy")
|
||||||
|
|
||||||
cdef ecotx_t* taxon
|
# name property getter
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return bytes2str(self._pointer.name)
|
||||||
|
|
||||||
taxon = <ecotx_t*> PyCapsule_GetPointer(taxon_capsule, NULL)
|
# taxid property getter
|
||||||
self.pointer = taxon
|
@property
|
||||||
self.taxid = taxon.taxid
|
def taxid(self):
|
||||||
self.rank = taxon.rank
|
return self._pointer.taxid
|
||||||
self.farest = taxon.farest
|
|
||||||
self.parent = taxon.parent
|
|
||||||
self.name = bytes2str(taxon.name)
|
|
||||||
|
|
||||||
cpdef int32_t taxid(self):
|
# rank property getter
|
||||||
return self.taxid
|
@property
|
||||||
|
def rank(self):
|
||||||
|
return self._pointer.rank
|
||||||
|
|
||||||
cpdef int32_t rank(self):
|
# farest property getter
|
||||||
return self.rank
|
@property
|
||||||
|
def farest(self):
|
||||||
|
return self._pointer.farest
|
||||||
|
|
||||||
cpdef int32_t farest(self):
|
# parent property getter
|
||||||
return self.farest
|
@property
|
||||||
|
def parent(self):
|
||||||
cpdef OBI_Taxon 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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user