Commit b63d0fb9 by Celine Mercier

Added C functions to write .rdx, .tdx, .ndx binary taxonomy files from a

taxonomy C structure
parent 0dfd67ec
#cython: language_level=3
from .capi.obitaxonomy cimport ecotx_t, OBIDMS_taxonomy_p
from ._obidms cimport OBIDMS
cdef class OBI_Taxonomy :
cdef str _name
cdef OBIDMS_taxonomy_p _pointer
cdef OBIDMS _dms
cpdef close(self)
cpdef _write(self, str prefix)
cdef class OBI_Taxon :
......
......@@ -4,7 +4,10 @@ from obitools3.utils cimport bytes2str, str2bytes
from .capi.obitaxonomy cimport obi_read_taxonomy, \
obi_close_taxonomy, \
obi_taxo_get_taxon_with_taxid
obi_taxo_get_taxon_with_taxid, \
write_rankidx, \
write_taxonomyidx, \
write_nameidx
from ._obidms cimport OBIDMS
......@@ -18,6 +21,7 @@ cdef class OBI_Taxonomy :
def __init__(self, OBIDMS dms, str name) :
self._dms = dms
self._name = name
self._pointer = obi_read_taxonomy(dms._pointer, str2bytes(name), True) # TODO discuss
# TODO if not found in DMS, try to import?
......@@ -39,7 +43,16 @@ cdef class OBI_Taxonomy :
cpdef close(self) :
if (obi_close_taxonomy(self._pointer) < 0) :
raise Exception("Error closing the taxonomy")
cpdef _write(self, str prefix) :
if (write_rankidx(self._dms._pointer, self._pointer, str2bytes(prefix)) < 0) :
raise Exception("Error writing the taxonomy rank file")
if (write_taxonomyidx(self._dms._pointer, self._pointer, str2bytes(prefix)) < 0) :
raise Exception("Error writing the taxonomy taxa file")
if (write_nameidx(self._dms._pointer, self._pointer, str2bytes(prefix)) < 0) :
raise Exception("Error writing the taxonomy taxa file")
cdef class OBI_Taxon : # TODO dict subclass?
......@@ -82,6 +95,6 @@ cdef class OBI_Taxon : # TODO dict subclass?
d['parent'] = self.parent.taxid
d['farest'] = self.farest
return str(d)
......@@ -40,3 +40,7 @@ cdef extern from "obidms_taxonomy.h" nogil:
ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
int write_rankidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const_char_p taxonomy_name)
int write_taxonomyidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const_char_p taxonomy_name)
int write_nameidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const_char_p taxonomy_name)
......@@ -497,6 +497,33 @@ OBIDMS_p obi_open_dms(const char* dms_path)
return NULL;
}
// Open the taxonomy directory
dms->tax_directory = opendir_in_dms(dms, TAXONOMY_DIR_NAME);
if (dms->tax_directory == NULL)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError opening the taxonomy directory");
closedir(dms->indexer_directory);
closedir(dms->view_directory);
closedir(dms->directory);
free(dms);
return NULL;
}
// Store the taxonomy directory's file descriptor
dms->tax_dir_fd = dirfd(dms->tax_directory);
if (dms->tax_dir_fd < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor of the taxonomy directory");
closedir(dms->indexer_directory);
closedir(dms->tax_directory);
closedir(dms->view_directory);
closedir(dms->directory);
free(dms);
return NULL;
}
// Initialize the list of opened columns
dms->opened_columns = (Opened_columns_list_p) malloc(sizeof(Opened_columns_list_t));
(dms->opened_columns)->nb_opened_columns = 0;
......@@ -536,7 +563,7 @@ int obi_close_dms(OBIDMS_p dms)
while ((dms->opened_columns)->nb_opened_columns > 0)
obi_close_column(*((dms->opened_columns)->columns));
// Close dms, and view and indexer directories
// Close dms, and view, indexer and taxonomy directories
if (closedir(dms->indexer_directory) < 0)
{
obi_set_errno(OBI_INDEXER_ERROR);
......@@ -551,6 +578,13 @@ int obi_close_dms(OBIDMS_p dms)
free(dms);
return -1;
}
if (closedir(dms->tax_directory) < 0)
{
obi_set_errno(OBIVIEW_ERROR);
obidebug(1, "\nError closing a taxonomy directory");
free(dms);
return -1;
}
if (closedir(dms->directory) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
......
......@@ -106,6 +106,12 @@ typedef struct OBIDMS {
int view_dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the view directory.
*/
DIR* tax_directory; /**< A directory entry usable to
* refer and scan the taxonomy directory.
*/
int tax_dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the taxonomy directory.
*/
bool little_endian; /**< Endianness of the database.
*/
Opened_columns_list_p opened_columns; /**< List of opened columns.
......
/********************************************************************
* OBIDMS taxonomy headeer file *
* OBIDMS taxonomy header file *
********************************************************************/
/**
......@@ -34,6 +34,7 @@ typedef struct ecotxnode {
int32_t taxid;
int32_t rank;
int32_t farest;
int32_t idx;
struct ecotxnode* parent;
char* name;
} ecotx_t;
......@@ -54,10 +55,10 @@ typedef struct {
typedef struct {
int32_t is_scientific_name;
int32_t is_scientific_name;
int32_t name_length;
int32_t class_length;
int32_t taxid;
int32_t taxid; // taxid idx
char names[1];
} econameformat_t;
......@@ -103,3 +104,9 @@ ecotx_t* obi_taxo_get_kingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
ecotx_t* obi_taxo_get_superkingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy);
int write_rankidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);
int write_taxonomyidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);
int write_nameidx(OBIDMS_p dms, OBIDMS_taxonomy_p tax, const char* taxonomy_name);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment