Taxonomy: added functions to check if a taxonomy already exists in a

DMS, and added taxdump import from a compressed file
This commit is contained in:
Celine Mercier
2018-07-28 16:48:11 +02:00
parent 1de308a856
commit 7f6d1597fc
5 changed files with 120 additions and 12 deletions

View File

@ -37,7 +37,9 @@ cdef extern from "obidms_taxonomy.h" nogil:
ctypedef OBIDMS_taxonomy_t* OBIDMS_taxonomy_p
int obi_taxonomy_exists(OBIDMS_p dms, const char* taxonomy_name)
OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const_char_p taxonomy_name, bint read_alternative_names)
OBIDMS_taxonomy_p obi_read_taxdump(const_char_p taxdump)

View File

@ -2,7 +2,8 @@
from obitools3.utils cimport str2bytes, bytes2str, tobytes, tostr
from ..capi.obitaxonomy cimport obi_read_taxonomy, \
from ..capi.obitaxonomy cimport obi_taxonomy_exists, \
obi_read_taxonomy, \
obi_read_taxdump, \
obi_write_taxonomy, \
obi_close_taxonomy, \
@ -17,6 +18,8 @@ from ..capi.obitaxonomy cimport obi_read_taxonomy, \
from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
import tarfile
cdef class Taxonomy(OBIWrapper) :
# TODO function to import taxonomy?
@ -25,6 +28,16 @@ cdef class Taxonomy(OBIWrapper) :
return <OBIDMS_taxonomy_p>(self._pointer)
@staticmethod
def exists(DMS dms, object name) :
e = obi_taxonomy_exists(dms.pointer(), tobytes(name))
if e < 0:
raise RuntimeError("Error : Cannot check if taxonomy %s exists"
% tostr(name))
else:
return e
@staticmethod
def open(DMS dms, object name) :
@ -46,27 +59,45 @@ cdef class Taxonomy(OBIWrapper) :
taxo._ranks = []
for r in range((<OBIDMS_taxonomy_p>pointer).ranks.count) :
taxo._ranks.append(obi_taxo_rank_index_to_label(r, (<OBIDMS_taxonomy_p>pointer).ranks))
return taxo
@staticmethod
def open_taxdump(DMS dms, object name) :
def open_taxdump(DMS dms, object path) :
cdef void* pointer
cdef Taxonomy taxo
pointer = <void*>obi_read_taxdump(tobytes(name))
cdef bytes path_b
cdef int idx
path_b = tobytes(path)
folder_path = path_b
if path_b.endswith(b"tar.gz") or path_b.endswith(b"tar"):
idx = path_b.index(b".tar")
folder_path = path_b[:idx]
if path_b.endswith(b"tar.gz"):
tar = tarfile.open(path_b, "r:gz")
tar.extractall(path=tostr(folder_path))
tar.close()
elif path_b.endswith(b"tar"):
tar = tarfile.open(path_b, "r:")
tar.extractall(path=tostr(folder_path))
tar.close()
pointer = <void*>obi_read_taxdump(folder_path)
if pointer == NULL :
raise RuntimeError("Error : Cannot read taxonomy %s"
% tostr(name))
% tostr(folder_path))
taxo = OBIWrapper.new_wrapper(Taxonomy, pointer)
dms.register(taxo)
taxo._dms = dms
taxo._name = tobytes(name)
taxo._name = folder_path
taxo._ranks = []
for r in range((<OBIDMS_taxonomy_p>pointer).ranks.count) :