From d8696f0566128255db9467ac8ffa64d78a3d8565 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Wed, 20 Jun 2007 07:37:27 +0000 Subject: [PATCH] git-svn-id: https://www.grenoble.prabi.fr/svn/LECASofts/ecoPCR/branches/refactoring@69 60f365c0-8329-0410-b2a4-ec073aeeaa1d --- src/ecofind.c | 191 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 161 insertions(+), 30 deletions(-) diff --git a/src/ecofind.c b/src/ecofind.c index 0ccaf6e..edadf78 100644 --- a/src/ecofind.c +++ b/src/ecofind.c @@ -7,20 +7,87 @@ #define VERSION "0.1" /** - * display the result in sdtout + * display the result **/ -static void printresult(ecotx_t *taxon,ecotaxonomy_t *taxonomy) +static void printresult(ecotx_t *taxon,econame_t* name,ecotaxonomy_t *taxonomy) { char* rankname; + char* classname; + char* matchedname=taxon->name; + + classname="scientific name"; + if (name) + { + classname=name->classname; + matchedname=name->name; + } rankname= taxonomy->ranks->label[taxon->rank]; - printf("%10d \t| %15s \t|\t %s\n", + printf("%10d \t| %15s \t|\t %-50s \t|\t %15s \t|\t %s\n", taxon->taxid, rankname, + matchedname, + classname, taxon->name); } +/** + * display header before printing any result + **/ +static void printheader(void) +{ + printf("# %12s \t| %15s \t|\t %-50s \t|\t %-15s \t|\t %s\n#\n", + "taxonomy id", + "taxonomy rank", + "name", + "class name", + "scientific name"); +} + +/** + * display son's list for given taxon + **/ + +static void get_son(ecotaxonomy_t *taxonomy, ecotx_t *taxon, int32_t *count) +{ + int32_t i; + ecotx_t *current_taxon; + + for ( i = 0, current_taxon = taxonomy->taxons->taxon; + i < taxonomy->taxons->count; + i++, current_taxon++) + { + if (taxon->taxid == current_taxon->parent->taxid) + { + (*count)++; + printresult(current_taxon, NULL, taxonomy); + get_son(taxonomy,current_taxon,count); + } + } +} + + + +/** + * display list of rank filter option (-l option) + **/ +static void listfilteroptions(ecorankidx_t *ranks) +{ + int32_t i; + + printf("#\n"); + + for ( i=0; + i < ranks->count; + i++) + { + printf("# %s \n",ranks->label[i]); + } + + printf("#\n"); +} + /* ---------------------------------------- */ /* get back on given taxid taxonomic parent */ @@ -33,16 +100,16 @@ void gettaxidparents(int32_t taxid, ecotaxonomy_t *taxonomy) next_parent = eco_findtaxonbytaxid(taxonomy, taxid); - printf("# taxonomy id \t|\t taxonomy rank \t|\t scientific name\n#\n"); + printheader(); while ( strcmp(next_parent->name, "root") ) { - printresult(next_parent, taxonomy); + printresult(next_parent, NULL,taxonomy); next_parent = next_parent->parent; c++; } - printf("# %d parents found\n#\n",c); + printf("# %d parent(s) found\n#\n",c); } @@ -54,7 +121,7 @@ void gettaxidparents(int32_t taxid, ecotaxonomy_t *taxonomy) static void ExitUsage(stat) int stat; { - PP "usage: ecofind [-d database] [-h] [-t taxid] ... \n"); + PP "usage: ecofind [-d database] [-h] [-l] [-r taxonomic rank] [-p taxid] [-s taxid] ... \n"); PP "type \"ecofind -h\" for help\n"); if (stat) exit(stat); @@ -84,8 +151,12 @@ static void PrintHelp() PP " program located in the tools directory.\n"); PP " Write the database radical without any extension.\n\n"); PP "-h : [H]elp - print help\n\n"); - PP "-t : [T]axonomic id. Specifiying this option will display"); - PP " all parents parent information for the given taxid."); + PP "-l : [L]ist all taxonomic rank available for -r option\n\n"); + PP "-p : [P]arent's taxonomic id. Specifiying this option will display "); + PP " all parent's information for the given taxid.\n\n"); + PP "-r : [R]estrict to given taxonomic rank\n\n"); + PP "-s : [S]on's taxonomic id. Specifiying this option will display "); + PP " all son's information for the given taxid.\n\n"); PP " name pattern bearing regular expressions\n\n"); PP "------------------------------------------\n"); PP " https://www.grenoble.prabi.fr/trac/ecoPCR/wiki\n"); @@ -105,20 +176,49 @@ int main(int argc, char **argv) int32_t k,j = 0; int32_t errflag = 0; int32_t tax_count = 0; + int32_t alternative = 0; char *prefix = NULL; - ecotx_t *taxon = NULL; ecotaxonomy_t *taxonomy; + econame_t *name; + int32_t name_count; int re_error; int re_match; regex_t re_preg; - int32_t taxid = 0; + int32_t uptree = 0; + int32_t subtree = 0; + char *rankname = NULL; + int32_t rankfilter = 1; + int32_t list = 0; + + ecotx_t *subtree_parent; + int32_t count_son = 0; - while ((carg = getopt(argc, argv, "hd:t:")) != -1) { + + while ((carg = getopt(argc, argv, "had:p:s:r:l")) != -1) { switch (carg) { + case 's': /* path to the database */ + sscanf(optarg,"%d",&subtree); + break; + + case 'r': /* rank filter */ + rankname = ECOMALLOC(strlen(optarg),"allocation rankname"); + strcpy(rankname,optarg); + rankfilter = 0; + break; + case 'd': /* path to the database */ - prefix = optarg; + prefix = ECOMALLOC(strlen(optarg),"allocation prefix"); + strcpy(prefix,optarg); + break; + + case 'l': /* list rank filter options */ + list = 1; + break; + + case 'a': /* allow alternative names */ + alternative = 1; break; case 'h': /* display help */ @@ -126,8 +226,8 @@ int main(int argc, char **argv) exit(0); break; - case 't': /* taxid */ - sscanf(optarg,"%d",&taxid); + case 'p': /* taxid */ + sscanf(optarg,"%d",&uptree); break; case '?': /* bad option */ @@ -145,7 +245,7 @@ int main(int argc, char **argv) errflag++; } - if (errflag && !taxid) + if (errflag && !uptree && !rankname && !subtree) ExitUsage(errflag); /** @@ -153,20 +253,45 @@ int main(int argc, char **argv) **/ printf("# \n# opening %s database\n",prefix); - taxonomy = read_taxonomy(prefix); + taxonomy = read_taxonomy(prefix,1); tax_count = taxonomy->taxons->count; + name_count = taxonomy->names->count; - printf("# %d taxons\n", tax_count); + + /* ---------------------------------------- */ + /* list -r option possibility */ + /* ---------------------------------------- */ + if (list) + { + listfilteroptions(taxonomy->ranks); + return 0; + } + /* ---------------------------------------- */ /* display taxid parent if -t option */ /* specified in command line */ /* ---------------------------------------- */ - if (taxid) + if (uptree) { - gettaxidparents(taxid,taxonomy); + gettaxidparents(uptree,taxonomy); return 0; } + + /* ---------------------------------------- */ + /* display taxid sons if -s option */ + /* specified in command line */ + /* ---------------------------------------- */ + if (subtree) + { + printheader(); + subtree_parent = eco_findtaxonbytaxid(taxonomy,subtree); + get_son(taxonomy, subtree_parent,&count_son); + printf("# %d son(s) found\n#\n",count_son); + return 0; + } + + printf("# %d taxons\n", tax_count); /** * parse taxonomy @@ -184,20 +309,26 @@ int main(int argc, char **argv) nummatch=0; - printf("# taxonomy id \t|\t taxonomy rank \t|\t scientific name\n#\n"); + printheader(); - for (j=0,taxon=taxonomy->taxons->taxon; - j < tax_count; - taxon++,j++) - { - re_match = regexec (&re_preg, taxon->name, 0, NULL, 0); - if (re_match == 0) + for (j=0,name=taxonomy->names->names; + j < name_count; + name++,j++) + { + + if(rankname) + rankfilter = !(strcmp(rankname,taxonomy->ranks->label[name->taxon->rank])); + + re_match = regexec (&re_preg, name->name, 0, NULL, 0); + + if (!re_match && (alternative || name->is_scientificname) && rankfilter) { - printresult(taxon,taxonomy); + printresult(name->taxon,name,taxonomy); nummatch++; } - } - + + } + printf("# %d records found \n",nummatch); regfree(&re_preg); }