From ef8dc85f3c72eec8a071c06a696483515d069bb3 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Tue, 27 Nov 2018 16:00:29 +0100 Subject: [PATCH] C, taxonomy: new function to get the lowest common ancestor of two taxa --- src/obidms_taxonomy.c | 57 +++++++++++++++++++++++++++++++++++++++++++ src/obidms_taxonomy.h | 12 +++++++++ 2 files changed, 69 insertions(+) diff --git a/src/obidms_taxonomy.c b/src/obidms_taxonomy.c index 1e113b5..5822773 100755 --- a/src/obidms_taxonomy.c +++ b/src/obidms_taxonomy.c @@ -3528,6 +3528,63 @@ int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon } +ecotx_t* obi_taxo_get_lca(ecotx_t* taxon1, ecotx_t* taxon2) // TODO could be more efficient maybe +{ + ecotx_t* current_taxon; + ecotx_t* next_taxon; + ecotx_t* lca; + ecotx_t* path1[1000]; + ecotx_t* path2[1000]; + int i,j; + + if ((taxon1 == NULL) || (taxon2 == NULL)) + { + obi_set_errno(OBI_TAXONOMY_ERROR); + obidebug(1, "\nError getting the LCA of two taxons: at least one of the taxon pointers is NULL"); + return NULL; + } + + // Get path of first taxon // TODO function to get path? + current_taxon = taxon1; + next_taxon = current_taxon->parent; + path1[0] = current_taxon; + i=0; + while (current_taxon != next_taxon) // root node + { + current_taxon = next_taxon; + next_taxon = current_taxon->parent; + i++; + path1[i] = current_taxon; + } + i--; + + // Get path of second taxon // TODO function to get path? + current_taxon = taxon2; + next_taxon = current_taxon->parent; + path2[0] = current_taxon; + j=0; + while (current_taxon != next_taxon) // root node + { + current_taxon = next_taxon; + next_taxon = current_taxon->parent; + j++; + path2[j] = current_taxon; + } + j--; + + while ((i>=0) && (j>=0) && (path1[i] == path2[j])) + { + i--; + j--; + } + i++; + + lca = path1[i]; + + return lca; +} + + ecotx_t* obi_taxo_get_parent_at_rank(ecotx_t* taxon, int32_t rankidx) { ecotx_t* current_taxon; diff --git a/src/obidms_taxonomy.h b/src/obidms_taxonomy.h index 8b5c130..c5ba099 100755 --- a/src/obidms_taxonomy.h +++ b/src/obidms_taxonomy.h @@ -320,6 +320,18 @@ int obi_taxo_add_preferred_name_with_taxid(OBIDMS_taxonomy_p tax, int32_t taxid, int obi_taxo_add_preferred_name_with_taxon(OBIDMS_taxonomy_p tax, ecotx_t* taxon, const char* preferred_name); +/** + * @brief Function returning the last common ancestor of two taxa. + * + * @param taxon1 A pointer on the first taxon. + * @param taxon2 A pointer on the second taxon. + * + * @returns A pointer on the last common ancestor of the two taxa. + * @retval NULL if an error occurred. + */ +ecotx_t* obi_taxo_get_lca(ecotx_t* taxon1, ecotx_t* taxon2); + + /** * @brief Function returning the parent of a taxon at a given rank. *