This commit is contained in:
2007-06-13 09:56:09 +00:00
parent 697e16ae98
commit c264556629

View File

@ -15,6 +15,15 @@ void freememory(char **tab, int32_t num){
} }
} }
/**
* Check if a pattern match a string using mamberall libapat function
* @param line array containing sequence information
* @param pattern array containing patterns to test on the sequence
* @param numpattern number of pattern in pattern array
* @param error_max error rate allowed by the user
*
* @return int 1 if a pattern match, else 0
**/
int ispatternmatching(char **line, char **pattern, int32_t numpattern, int32_t error_max){ int ispatternmatching(char **line, char **pattern, int32_t numpattern, int32_t error_max){
int i; int i;
SeqPtr apatseq = NULL; SeqPtr apatseq = NULL;
@ -38,6 +47,11 @@ int ispatternmatching(char **line, char **pattern, int32_t numpattern, int32_t e
return 0; return 0;
} }
/**
*returns the result on standard output
* @param line array containing sequence information
* @param i length of line
**/
void printline(char **line, int32_t i){ void printline(char **line, int32_t i){
int32_t k=0; int32_t k=0;
for (k=0; k < i; k++) for (k=0; k < i; k++)
@ -99,36 +113,31 @@ static void ExitUsage(stat)
int main(int argc, char **argv){ int main(int argc, char **argv){
int32_t carg = 0; int32_t carg = 0;
int32_t r = 0; // number of restricted taxid int32_t r = 0; // number of restricted taxid
int32_t g = 0; // number of ignored taxid int32_t g = 0; // number of ignored taxid
int32_t v = 0; int32_t v = 0; // stores if -v mode is active
int32_t p = 0; // number of pattern int32_t p = 0; // number of pattern
int32_t i = 0; int32_t i = 0;
int32_t errflag = 0; int32_t errflag = 0;
int32_t error_max = 0; int32_t error_max = 0; // stores the error rate allowed by the user
int32_t matchingresult = 0; int32_t matchingresult = 0; // stores number of matching result
ecotaxonomy_t *taxonomy; ecotaxonomy_t *taxonomy; // stores the taxonomy
char *database = NULL; // stores the database path (for taxonomy) char *database = NULL; // stores the database path (for taxonomy)
int32_t *restricted_taxid = NULL; // stores the restricted taxid int32_t *restricted_taxid = NULL; // stores the restricted taxid
int32_t *ignored_taxid = NULL; // stores the ignored taxid int32_t *ignored_taxid = NULL; // stores the ignored taxid
char **pattern = NULL; // stores the regex pattern char **pattern = NULL; // stores the regex pattern
char *line[19] = {0}; // stores the line char *line[19] = {0}; // stores the line
FILE *file = NULL;
char *stream = ECOMALLOC(sizeof(char *)*10000,"error stream buffer allocation");
int current_taxid; int current_taxid;
char *buffer;
struct stat buf; FILE *file = NULL; // stores the data stream, stdin by default
char *stream = ECOMALLOC(sizeof(char *)*10000,"error stream buffer allocation");
char *buffer;
// check stdin /**
if (fstat(fileno(stdin),&buf) == 0) * Parse commande line options
if ( (buf.st_dev) == 0) **/
file = stdin;
while ((carg = getopt(argc, argv, "f:d:i:r:e:vh")) != -1) { while ((carg = getopt(argc, argv, "f:d:i:r:e:vh")) != -1) {
switch (carg) { switch (carg) {
@ -178,7 +187,7 @@ int main(int argc, char **argv){
} }
/** /**
* Get the leftover command line arguments back * Get the left-over command line arguments back
* and check the pattern is not more than 32 character long * and check the pattern is not more than 32 character long
*/ */
pattern = ECOMALLOC(sizeof *pattern * (argc - optind), "Error in pattern allocation"); pattern = ECOMALLOC(sizeof *pattern * (argc - optind), "Error in pattern allocation");
@ -188,15 +197,26 @@ int main(int argc, char **argv){
else else
{ {
printf("# Sorry, ecogrep doesn't handle pattern longer than 32 characters.\ printf("# Sorry, ecogrep doesn't handle pattern longer than 32 characters.\
\n# Please correct this one : %s\n",argv[optind]); \n# Please check it out : %s\n",argv[optind]);
exit(0); exit(0);
} }
} }
/**
* check standard input if no file name given in -f option
**/
if (file == NULL)
{
if (isatty(fileno(stdin)))
errflag++;
else
file = stdin;
}
/** /**
* try to get the database name from environment variable * try to get the database name from environment variable
* if no database name specified in the -d option * if no database name specified in the -d option
*/ **/
if (database == NULL) if (database == NULL)
{ {
database = getenv("ECOPCRDB"); database = getenv("ECOPCRDB");
@ -205,12 +225,10 @@ int main(int argc, char **argv){
} }
/** /**
* check everything required is provided * check at leat one processing is asked
* either patterns or taxid filters
*/ */
if ( !file || if ( !p && restricted_taxid == NULL && ignored_taxid == NULL )
!database ||
(!p && restricted_taxid == NULL && ignored_taxid == NULL)
)
errflag++; errflag++;
if (errflag) if (errflag)
@ -224,7 +242,7 @@ int main(int argc, char **argv){
/** /**
* Parse the stream * Parse the stream
*/ */
while( fgets(stream, 10000, file) ){ while( fgets(stream, 10000, file) != NULL ){
if (stream[0]!= '#') if (stream[0]!= '#')
{ {
@ -232,6 +250,7 @@ int main(int argc, char **argv){
buffer != NULL; buffer != NULL;
i++, buffer = strtok(NULL,"|")) i++, buffer = strtok(NULL,"|"))
{ {
printf("<EFBFBD><EFBFBD> %s\n",buffer);
line[i] = strdup(buffer); line[i] = strdup(buffer);
} }
@ -254,7 +273,7 @@ int main(int argc, char **argv){
continue; continue;
} }
else // v mode, invert ignore and restrict taxid else // v mode, invert ignore and restrict options
{ {
if ( (r > 0) && (eco_is_taxid_included( taxonomy, if ( (r > 0) && (eco_is_taxid_included( taxonomy,
restricted_taxid, restricted_taxid,
@ -280,12 +299,15 @@ int main(int argc, char **argv){
printf("# %d matching result\n",matchingresult); printf("# %d matching result\n",matchingresult);
/**
* clean, close and free before leaving
**/
if ( file != stdin ) if ( file != stdin )
fclose(file); fclose(file);
freememory(line,i); freememory(line,i);
freememory(pattern,p); freememory(pattern,p);
ECOFREE(pattern,"Error in free pattern"); ECOFREE(pattern,"Error in free pattern");
ECOFREE(stream,"Error in free stream"); ECOFREE(stream,"Error in free stream");
return 0; return 0;
} }