C, taxonomy: new function to get the lowest common ancestor of two taxa

This commit is contained in:
Celine Mercier
2018-11-27 16:00:29 +01:00
parent f942dd856f
commit ef8dc85f3c
2 changed files with 69 additions and 0 deletions

View File

@ -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;

View File

@ -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.
*