Taxonomy: adding, writing and reading preferred names, changed some

function names, and fixed a bug with taxa indices not being properly
initialized
This commit is contained in:
Celine Mercier
2017-01-16 17:28:20 +01:00
parent 0385a92e02
commit c065c1914a
5 changed files with 444 additions and 57 deletions

View File

@ -17,4 +17,5 @@ cdef class OBI_Taxonomy :
cdef class OBI_Taxon :
cdef ecotx_t* _pointer
cdef ecotx_t* _pointer
cdef OBI_Taxonomy _tax

View File

@ -7,10 +7,10 @@ from .capi.obitaxonomy cimport obi_read_taxonomy, \
obi_write_taxonomy, \
obi_close_taxonomy, \
obi_taxo_get_taxon_with_taxid, \
obi_taxonomy_add_local_taxon, \
obi_taxo_add_local_taxon, \
obi_taxo_add_preferred_name_with_taxon, \
ecotx_t
from ._obidms cimport OBIDMS
from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
@ -42,11 +42,11 @@ cdef class OBI_Taxonomy :
if taxon_p == NULL :
raise Exception("Taxon not found")
taxon_capsule = PyCapsule_New(taxon_p, NULL, NULL)
return OBI_Taxon(taxon_capsule)
return OBI_Taxon(taxon_capsule, self)
else :
raise Exception("Not implemented")
def __iter__(self):
cdef ecotx_t* taxa
@ -60,7 +60,7 @@ cdef class OBI_Taxonomy :
for t in range(self._pointer.taxa.count):
taxon_p = <ecotx_t*> (taxa+t)
taxon_capsule = PyCapsule_New(taxon_p, NULL, NULL)
yield OBI_Taxon(taxon_capsule)
yield OBI_Taxon(taxon_capsule, self)
cpdef write(self, str prefix) :
@ -70,7 +70,7 @@ cdef class OBI_Taxonomy :
cpdef int add_taxon(self, str name, str rank_name, int parent_taxid, int min_taxid=10000000) :
cdef int taxid
taxid = obi_taxonomy_add_local_taxon(self._pointer, str2bytes(name), str2bytes(rank_name), parent_taxid, min_taxid)
taxid = obi_taxo_add_local_taxon(self._pointer, str2bytes(name), str2bytes(rank_name), parent_taxid, min_taxid)
if taxid < 0 :
raise Exception("Error adding a new taxon to the taxonomy")
else :
@ -85,10 +85,11 @@ cdef class OBI_Taxonomy :
cdef class OBI_Taxon : # TODO dict subclass?
def __init__(self, object taxon_capsule) :
def __init__(self, object taxon_capsule, OBI_Taxonomy tax) :
self._pointer = <ecotx_t*> PyCapsule_GetPointer(taxon_capsule, NULL)
if self._pointer == NULL :
raise Exception("Error reading the taxonomy")
raise Exception("Error reading a taxon (NULL pointer)")
self._tax = tax
# name property getter
@property
@ -115,14 +116,25 @@ cdef class OBI_Taxon : # TODO dict subclass?
def parent(self):
cdef object parent_capsule
parent_capsule = PyCapsule_New(self._pointer.parent, NULL, NULL)
return OBI_Taxon(parent_capsule)
return OBI_Taxon(parent_capsule, self._tax)
# preferred name property getter and setter
@property
def preferred_name(self):
if self._pointer.preferred_name != NULL :
return bytes2str(self._pointer.preferred_name)
@preferred_name.setter
def preferred_name(self, str new_preferred_name) : # @DuplicatedSignature
if (obi_taxo_add_preferred_name_with_taxon(self._tax._pointer, self._pointer, str2bytes(new_preferred_name)) < 0) :
raise Exception("Error adding a new preferred name to a taxon")
def __repr__(self):
d = {}
d['taxid'] = self.taxid
d['name'] = self.name
d['parent'] = self.parent.taxid
d['farest'] = self.farest
d['taxid'] = self.taxid
d['name'] = self.name
d['preferred name'] = self.preferred_name
d['parent'] = self.parent.taxid
d['farest'] = self.farest
return str(d)

View File

@ -13,7 +13,8 @@ cdef extern from "obidms_taxonomy.h" nogil:
int32_t farest
ecotxnode* parent
char* name
char* preferred_name
ctypedef ecotxnode ecotx_t
@ -56,4 +57,9 @@ cdef extern from "obidms_taxonomy.h" nogil:
ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
int obi_taxonomy_add_local_taxon(OBIDMS_taxonomy_p tax, const char* name, const char* rank_name, int32_t parent_taxid, int32_t min_taxid)
int obi_taxo_add_local_taxon(OBIDMS_taxonomy_p tax, const char* name, const char* rank_name, int32_t parent_taxid, int32_t min_taxid)
int obi_taxo_add_preferred_name_with_taxid(OBIDMS_taxonomy_p tax, int32_t taxid, const char* preferred_name)
int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon, const char* preferred_name)