Taxonomy: new functions and improvements

This commit is contained in:
Celine Mercier
2017-10-04 15:55:13 +02:00
parent 0ab081f79e
commit 535692b020
6 changed files with 329 additions and 96 deletions

View File

@@ -148,7 +148,7 @@ static char* get_taxonomy_path(OBIDMS_p dms, const char* tax_name);
* @returns The index of a rank in the ecorankidx_t structure.
* @retval -1 if the rank was not found.
*/
static int32_t rank_index(const char* label, ecorankidx_t* ranks);
static int32_t rank_label_to_index(const char* label, ecorankidx_t* ranks);
/**
@@ -543,7 +543,7 @@ static char* get_taxonomy_path(OBIDMS_p dms, const char* tax_name)
}
static int32_t rank_index(const char* label, ecorankidx_t* ranks)
static int32_t rank_label_to_index(const char* label, ecorankidx_t* ranks)
{
char **rep;
@@ -3501,6 +3501,13 @@ ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx)
ecotx_t* current_taxon;
ecotx_t* next_taxon;
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError getting the parent of a taxon at a given rank: taxon pointer is NULL");
return NULL;
}
current_taxon = taxon;
next_taxon = current_taxon->parent;
@@ -3524,6 +3531,13 @@ ecotx_t* obi_taxo_get_taxon_with_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid
ecomerged_t *indexed_taxon;
int32_t count;
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get a taxon with its taxid: taxonomy pointer is NULL");
return NULL;
}
count = (taxonomy->merged_idx)->count;
indexed_taxon = (ecomerged_t*) bsearch((const void *) ((size_t) taxid),
@@ -3543,12 +3557,19 @@ ecotx_t* obi_taxo_get_taxon_with_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid
}
bool obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid) // TODO discuss that this doesn't work with deprecated taxids
int obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid) // TODO discuss that this doesn't work with deprecated taxids
{
ecotx_t* next_parent;
next_parent = taxon->parent;
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError checking if a taxon is under another: taxon pointer is NULL");
return -1;
}
while ((other_taxid != next_parent->taxid) && (strcmp(next_parent->name, "root")))
next_parent = next_parent->parent;
@@ -3561,19 +3582,27 @@ bool obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid) // TODO
ecotx_t* obi_taxo_get_species(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
{
static OBIDMS_taxonomy_p tax = NULL;
static int32_t rankindex = -1;
static int32_t rankindex = -1;
if (taxonomy && (tax != taxonomy))
{
rankindex = rank_index("species", taxonomy->ranks);
tax = taxonomy;
}
if (!tax || (rankindex < 0))
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the species associated with a taxon: No taxonomy defined");
obidebug(1, "\nError trying to get the species associated with a taxon: taxonomy pointer is NULL");
return NULL;
}
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the species associated with a taxon: taxon pointer is NULL");
return NULL;
}
rankindex = rank_label_to_index("species", taxonomy->ranks);
if (rankindex < 0)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the species associated with a taxon: error getting rank index");
return NULL;
}
@@ -3583,19 +3612,27 @@ ecotx_t* obi_taxo_get_species(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_genus(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
{
static OBIDMS_taxonomy_p tax = NULL;
static int32_t rankindex = -1;
static int32_t rankindex = -1;
if (taxonomy && (tax != taxonomy))
{
rankindex = rank_index("genus", taxonomy->ranks);
tax = taxonomy;
}
if (!tax || (rankindex < 0))
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the genus associated with a taxon: No taxonomy defined");
obidebug(1, "\nError trying to get the genus associated with a taxon: taxonomy pointer is NULL");
return NULL;
}
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the genus associated with a taxon: taxon pointer is NULL");
return NULL;
}
rankindex = rank_label_to_index("genus", taxonomy->ranks);
if (rankindex < 0)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the genus associated with a taxon: error getting rank index");
return NULL;
}
@@ -3605,19 +3642,27 @@ ecotx_t* obi_taxo_get_genus(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_family(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
{
static OBIDMS_taxonomy_p tax = NULL;
static int32_t rankindex = -1;
static int32_t rankindex = -1;
if (taxonomy && (tax != taxonomy))
{
rankindex = rank_index("family", taxonomy->ranks);
tax = taxonomy;
}
if (!tax || (rankindex < 0))
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the family associated with a taxon: No taxonomy defined");
obidebug(1, "\nError trying to get the family associated with a taxon: taxonomy pointer is NULL");
return NULL;
}
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the family associated with a taxon: taxon pointer is NULL");
return NULL;
}
rankindex = rank_label_to_index("family", taxonomy->ranks);
if (rankindex < 0)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the family associated with a taxon: error getting rank index");
return NULL;
}
@@ -3627,19 +3672,27 @@ ecotx_t* obi_taxo_get_family(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
ecotx_t* obi_taxo_get_kingdom(ecotx_t* taxon, OBIDMS_taxonomy_p taxonomy)
{
static OBIDMS_taxonomy_p tax = NULL;
static int32_t rankindex = -1;
static int32_t rankindex = -1;
if (taxonomy && (tax != taxonomy))
{
rankindex = rank_index("kingdom", taxonomy->ranks);
tax = taxonomy;
}
if (!tax || (rankindex < 0))
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the kingdom associated with a taxon: No taxonomy defined");
obidebug(1, "\nError trying to get the kingdom associated with a taxon: taxonomy pointer is NULL");
return NULL;
}
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the kingdom associated with a taxon: taxon pointer is NULL");
return NULL;
}
rankindex = rank_label_to_index("kingdom", taxonomy->ranks);
if (rankindex < 0)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the kingdom associated with a taxon: error getting rank index");
return NULL;
}
@@ -3649,22 +3702,36 @@ 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)
{
static OBIDMS_taxonomy_p tax = NULL;
static int32_t rankindex = -1;
if (taxonomy && (tax != taxonomy))
{
rankindex = rank_index("superkingdom", taxonomy->ranks);
tax = taxonomy;
}
if (!tax || (rankindex < 0))
if (taxonomy == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the superkingdom associated with a taxon: No taxonomy defined");
obidebug(1, "\nError trying to get the superkingdom associated with a taxon: taxonomy pointer is NULL");
return NULL;
}
if (taxon == NULL)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the superkingdom associated with a taxon: taxon pointer is NULL");
return NULL;
}
rankindex = rank_label_to_index("superkingdom", taxonomy->ranks);
if (rankindex < 0)
{
obi_set_errno(OBI_TAXONOMY_ERROR);
obidebug(1, "\nError trying to get the superkingdom associated with a taxon: error getting rank index");
return NULL;
}
return obi_taxo_get_parent_at_rank(taxon, rankindex);
}
const char* obi_taxo_rank_index_to_label(int32_t rank_idx, ecorankidx_t* ranks)
{
return (ranks->label)[rank_idx];
}

View File

@@ -308,7 +308,7 @@ int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon
* @param rankidx The index of the rank wanted.
*
* @returns A pointer on the parent taxon at the wanted rank.
* @retval NULL if no parent taxon was found at the wanted rank.
* @retval NULL if no parent taxon was found at the wanted rank or if an error occurred.
*/
ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx);
@@ -320,7 +320,7 @@ ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx);
* @param taxid The taxid of the taxon.
*
* @returns A pointer on the wanted taxon.
* @retval NULL if no taxon was found with the given taxid.
* @retval NULL if no taxon was found with the given taxid or if an error occurred.
*
* @since January 2017
* @author Celine Mercier (celine.mercier@metabarcoding.org)
@@ -334,9 +334,12 @@ ecotx_t* obi_taxo_get_taxon_with_taxid(OBIDMS_taxonomy_p taxonomy, int32_t taxid
* @param taxon A pointer on the first taxon.
* @param other_taxid The taxid of the second taxon.
*
* @returns A boolean indicating whether the first taxon is under the second taxon in the taxonomy tree.
* @returns A value indicating whether the first taxon is under the second taxon in the taxonomy tree.
* @retval 0 if the first taxon is not under the second taxon in the taxonomy tree.
* @retval 1 if the first taxon is under the second taxon in the taxonomy tree.
* @retval -1 if an error occurred.
*/
bool obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid);
int obi_taxo_is_taxon_under_taxid(ecotx_t* taxon, int32_t other_taxid);
/**
@@ -398,3 +401,16 @@ 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);
/**
* @brief Function returning the label of a rank in an ecorankidx_t structure.
*
* @param rank_idx The index of the rank.
* @param ranks A pointer on an ecorankidx_t structure.
*
* @returns The label of a rank in the ecorankidx_t structure.
* @retval NULL if there is no rank at that index.
*
* @see rank_label_to_index()
*/
const char* obi_taxo_rank_index_to_label(int32_t rank_idx, ecorankidx_t* ranks);