From 7d9ab96dc1e94163b59b01a3227cef695db596f3 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Mon, 4 Jun 2007 15:34:00 +0000 Subject: [PATCH] git-svn-id: https://www.grenoble.prabi.fr/svn/LECASofts/ecoPCR/branches/refactoring@27 60f365c0-8329-0410-b2a4-ec073aeeaa1d --- src/ecofind.c | 156 +++++++++++++++++++++----------------------------- 1 file changed, 65 insertions(+), 91 deletions(-) diff --git a/src/ecofind.c b/src/ecofind.c index ffc443a..bb9a598 100644 --- a/src/ecofind.c +++ b/src/ecofind.c @@ -6,34 +6,19 @@ #include #define VERSION "0.1" - -/* ----------------------------------------------- */ -/* structures */ -/* ----------------------------------------------- */ -typedef struct { - char *sci_name; // scientific name - int32_t tax_id; // tax id use ecoPCR prefilter - int32_t tax_rank; // taxonomic rank -} tax_info ; - - - -/* ----------------------------------------------- */ -/* functions */ -/* ----------------------------------------------- */ - /** * display the result in sdtout **/ -static void printresult(tax_info **taxonlist,int *j){ - int i; - printf("taxonomy rank \t|\t taxonomy id \t|\t scientific name\n\n"); - for(i=0;i<*j;i++){ - printf("%d \t|\t %d \t|\t %s\n", - taxonlist[i]->tax_id, - taxonlist[i]->tax_rank, - taxonlist[i]->sci_name); - } + +static void printresult(ecotx_t *taxon,ecotaxonomy_t *taxonomy){ + char* rankname; + + rankname= taxonomy->ranks->label[taxon->rank]; + + printf("%10d \t| %15s \t|\t %s\n", + taxon->taxid, + rankname, + taxon->name); } /** @@ -44,7 +29,7 @@ static void printresult(tax_info **taxonlist,int *j){ static void ExitUsage(stat) int stat; { - PP "usage: ecofind [-d datafile] [-t taxon] [-h]\n"); + PP "usage: ecofind [-d datafile] [-h] ... \n"); PP "type \"ecofind -h\" for help\n"); if (stat) exit(stat); @@ -74,7 +59,7 @@ static void PrintHelp() PP " program located in the tools directory.\n"); PP " Write the datafile radical without any extension.\n\n"); PP "-h : [H]elp - print help\n\n"); - PP "-t : [T]axonomic pattern bearing regular expressions\n\n"); + PP " name pattern bearing regular expressions\n\n"); PP "------------------------------------------\n"); } @@ -87,51 +72,39 @@ static void PrintHelp() int main(int argc, char **argv) { int32_t carg = 0; - int32_t numpattern = 0; /* stores number of pattern */ int32_t nummatch = 0; int32_t k,j = 0; int32_t errflag = 0; + int32_t tax_count = 0; char *prefix = NULL; - char *taxon = NULL; - char pattern[PATTERN_NUMBER][PATTERN_LENGHT]; /* stores the pattern */ - tax_info **taxonlist = ECOMALLOC(sizeof(tax_info) * RESULT_LENGTH, /* stores the result */ - "error in **taxonlist allocation"); - ecotaxonomy_t *taxonomy; /* stores all the taxonomy */ + ecotx_t *taxon = NULL; + ecotaxonomy_t *taxonomy; int re_error; int re_match; regex_t re_preg; - while ((carg = getopt(argc, argv, "hd:t:")) != -1) { - switch (carg) { - case 'd': /* path to the database */ - prefix = optarg; - break; - - case 't': /* pattern list */ - strncpy(pattern[numpattern],optarg,PATTERN_LENGHT); - numpattern++; - break; - - case 'h': /* display help */ - PrintHelp(); - exit(0); - break; - - case '?': /* bad option */ - errflag++; - } - } - - /** - * check data integrity - * and exit if needed - **/ - if (!prefix || !pattern) - errflag++; + while ((carg = getopt(argc, argv, "hd:")) != -1) { + switch (carg) { + case 'd': /* path to the database */ + prefix = optarg; + break; - if (taxonlist == NULL) - errflag++; + case 'h': /* display help */ + PrintHelp(); + exit(0); + break; + + case '?': /* bad option */ + errflag++; + } + } + + /** + * check the path to the database is given as last argument + **/ + if ((argc - optind) < 1) + errflag++; if (errflag) ExitUsage(errflag); @@ -139,44 +112,45 @@ int main(int argc, char **argv) /** * load taxonomy using libecoPCR functions **/ - printf("-- opening %s database --\n",prefix); + printf("# \n# opening %s database\n",prefix); taxonomy = read_taxonomy(prefix); + tax_count = taxonomy->taxons->count; - printf("-- %d taxons --\n", taxonomy->taxons->count); + printf("# %d taxons\n", tax_count); /** * parse taxonomy **/ - for (k=0;ktaxons->taxon[j].name; - re_error = regcomp (&re_preg, pattern[k], REG_NOSUB | REG_EXTENDED); - if (re_error == 0) - { - re_match = regexec (&re_preg, taxon, 0, NULL, 0); - regfree(&re_preg); - if (re_match == 0) - { - taxonlist[nummatch] = ECOMALLOC(sizeof(taxonlist), - "error in taxon j allocation"); - taxonlist[nummatch]->sci_name = taxon; - taxonlist[nummatch]->tax_id = taxonomy->taxons->taxon[j].taxid; - taxonlist[nummatch]->tax_rank = taxonomy->taxons->taxon[j].rank; - nummatch++; - } - } - j++; - } while(j < taxonomy->taxons->count); + printf("#\n# searching for '%s' pattern\n",argv[k]); - printf("-- %d records found --\n\n",nummatch); - - if (nummatch > 0) - printresult(taxonlist, &nummatch); + re_error = regcomp (&re_preg, argv[k], REG_NOSUB | REG_EXTENDED | REG_ICASE); + if (re_error) + { + fprintf(stderr,"# misformed pattern '%s'\n",argv[k]); + exit(1); + } + + nummatch=0; + + printf("# taxonomy id \t|\t taxonomy rank \t|\t scientific name\n#\n"); + + 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) + { + printresult(taxon,taxonomy); + nummatch++; + } + } + + printf("# %d records found \n",nummatch); + regfree(&re_preg); } return 0;