From 2452df90de22ec21df516edb14ffc540781e7e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Boyer?= Date: Wed, 16 May 2012 11:43:33 +0000 Subject: [PATCH] MOD: Added a [P]ath option displaying for each taxon its full path git-svn-id: https://www.grenoble.prabi.fr/svn/LECASofts/ecoPCR/trunk@423 60f365c0-8329-0410-b2a4-ec073aeeaa1d --- src/ecofind.c | 65 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/ecofind.c b/src/ecofind.c index e78f906..469bb6a 100644 --- a/src/ecofind.c +++ b/src/ecofind.c @@ -9,7 +9,20 @@ /** * display the result **/ -static void printresult(ecotx_t *taxon,econame_t* name,ecotaxonomy_t *taxonomy) + +void displayPath(ecotx_t *taxon, ecotaxonomy_t *taxonomy){ + + if (taxon != taxon->parent){ + displayPath(taxon->parent,taxonomy); + printf(";"); + } + if (rank_index("no rank",taxonomy->ranks) != taxon->rank) + printf("%s:", taxonomy->ranks->label[taxon->rank]); + printf("%s", taxon->name); +} + + +static void printresult(ecotx_t *taxon,econame_t* name,ecotaxonomy_t *taxonomy, int32_t pathDisplay) { char* rankname; char* classname; @@ -24,32 +37,38 @@ static void printresult(ecotx_t *taxon,econame_t* name,ecotaxonomy_t *taxonomy) rankname= taxonomy->ranks->label[taxon->rank]; - printf("%10d \t| %15s \t|\t %-50s \t|\t %15s \t|\t %s\n", + printf("%10d \t| %15s \t|\t %-50s \t|\t %15s \t|\t %s", taxon->taxid, rankname, matchedname, classname, - taxon->name); + taxon->name); + if (pathDisplay) { + printf("\t|\t"); + displayPath(taxon, taxonomy); + } + printf("\n"); } /** * display header before printing any result **/ -static void printheader(void) +static void printheader(int32_t pathDisplay) { - printf("# %12s \t| %15s \t|\t %-50s \t|\t %-15s \t|\t %s\n#\n", + printf("# %12s \t| %15s \t|\t %-50s \t|\t %-15s \t|\t %s%s\n#\n", "taxonomy id", "taxonomy rank", "name", "class name", - "scientific name"); + "scientific name", + pathDisplay ? "\t|\t path":""); } /** * display son's list for given taxon **/ -static void get_son(ecotaxonomy_t *taxonomy, ecotx_t *taxon, int32_t *count, char *rankname) +static void get_son(ecotaxonomy_t *taxonomy, ecotx_t *taxon, int32_t *count, char *rankname, int32_t pathDisplay) { int32_t i; ecotx_t *current_taxon; @@ -63,10 +82,10 @@ static void get_son(ecotaxonomy_t *taxonomy, ecotx_t *taxon, int32_t *count, cha { if (rankname == NULL || !strcmp(rankname,taxonomy->ranks->label[current_taxon->rank])) { - printresult(current_taxon, NULL, taxonomy); + printresult(current_taxon, NULL, taxonomy, pathDisplay); (*count)++; } - get_son(taxonomy,current_taxon,count,rankname); + get_son(taxonomy,current_taxon,count,rankname, pathDisplay); } } } @@ -97,23 +116,23 @@ static void listfilteroptions(ecorankidx_t *ranks) /* get back on given taxid taxonomic parent */ /* and display it */ /* ---------------------------------------- */ -void gettaxidparents(int32_t taxid, ecotaxonomy_t *taxonomy, char *rankname) +void gettaxidparents(int32_t taxid, ecotaxonomy_t *taxonomy, char *rankname, int32_t pathDisplay) { ecotx_t *next_parent; int32_t c = 0; next_parent = eco_findtaxonbytaxid(taxonomy, taxid); - printheader(); + printheader(pathDisplay); - printresult(next_parent, NULL,taxonomy); + printresult(next_parent, NULL,taxonomy, pathDisplay); while ( strcmp(next_parent->name, "root") ) { next_parent = next_parent->parent; if (rankname == NULL || !strcmp(rankname,taxonomy->ranks->label[next_parent->rank])) { - printresult(next_parent, NULL,taxonomy); + printresult(next_parent, NULL,taxonomy, pathDisplay); c++; } } @@ -164,6 +183,7 @@ static void PrintHelp() PP "-p : [P]arents : specifiying this option displays all parental tree's information for the given taxid.\n\n"); PP "-r : [R]estrict to given taxonomic rank\n\n"); PP "-s : [S]ons: specifiying this option displays all subtree's information for the given taxid.\n\n"); + PP "-P : Display taxonomic [P]ath as suplementary column in output\n\n"); PP "arguments:\n"); PP " name pattern bearing regular expressions\n\n"); PP "------------------------------------------\n"); @@ -199,12 +219,13 @@ int main(int argc, char **argv) char *rankname = NULL; int32_t rankfilter = 1; int32_t list = 0; + int32_t path = 0; ecotx_t *subtree_parent; int32_t count_son = 0; - while ((carg = getopt(argc, argv, "had:p:s:r:l")) != -1) { + while ((carg = getopt(argc, argv, "had:p:s:r:lP")) != -1) { switch (carg) { case 's': /* path to the database */ sscanf(optarg,"%d",&subtree); @@ -224,6 +245,10 @@ int main(int argc, char **argv) case 'l': /* list rank filter options */ list = 1; break; + + case 'P': /* Path output option */ + path=1; + break; case 'a': /* allow alternative names */ alternative = 1; @@ -281,7 +306,7 @@ int main(int argc, char **argv) /* ---------------------------------------- */ if (uptree) { - gettaxidparents(uptree,taxonomy,rankname); + gettaxidparents(uptree,taxonomy,rankname, path); return 0; } @@ -291,10 +316,10 @@ int main(int argc, char **argv) /* ---------------------------------------- */ if (subtree) { - printheader(); + printheader(path); subtree_parent = eco_findtaxonbytaxid(taxonomy,subtree); - printresult(subtree_parent, NULL,taxonomy); - get_son(taxonomy, subtree_parent,&count_son,rankname); + printresult(subtree_parent, NULL,taxonomy, path); + get_son(taxonomy, subtree_parent,&count_son,rankname, path); printf("# %d son(s) found\n#\n",count_son); return 0; } @@ -317,7 +342,7 @@ int main(int argc, char **argv) nummatch=0; - printheader(); + printheader(path); for (j=0,name=taxonomy->names->names; j < name_count; @@ -331,7 +356,7 @@ int main(int argc, char **argv) if (!re_match && (alternative || name->is_scientificname) && rankfilter) { - printresult(name->taxon,name,taxonomy); + printresult(name->taxon,name,taxonomy, path); nummatch++; }