Esempio n. 1
0
/******************************************************************************
* This file is Copyright 1992 by Philip G. Richards.  All Rights Reserved.
* See the file README that came with this distribution for permissions on
* code usage, copying, and distribution.  It comes with absolutely no warranty.
******************************************************************************/

#include "client.h"
#include "main.h"
#include "parse.h"
#include <ctype.h>

extern char **environ;

#define ISMAGIC 0x80
#define ISPROT  0x40

void
#ifndef ANSI_PROTOTYPES
freemyargs(argc, argv)
    int argc;
    char *argv[];
#else /* ANSI_PROTOTYPES */
freemyargs(int argc, char **argv)
#endif /* ANSI_PROTOTYPES */
{
    int i;

    if (argv == (char**)0)
	return;

#ifdef MALLOCDEBUG
  {
    int oldlvl = malloc_debug(0);
#endif

    for (i = 0; i < argc && argv[i]; i++)
	(void)free(argv[i]);

#ifdef MALLOCDEBUG
    (void)malloc_debug(oldlvl);
    if (!malloc_verify())
    {
        ffprintf(STDERR, "??freemyargs() has screwed up malloc()\n");
	abort();
    }
  }
#endif
}
Esempio n. 2
0
                		/* the rest of the pattern segments */
#endif /* ANSI_PROTOTYPES */
{
    char *x;			/* scratch */
    VOIDDIR *dirp;		/* for directory reading */
    VOIDDIRENT *dp;		/* directory entry */
    struct stat st;		/* to determine files type */

#ifdef GLOBDEBUG
    ffprintf(STDDBG,"matchdir: path = '%s', pat = '%s'\n", main_path, *gpat );
#endif
    if ((dirp = OPENDIR(main_path)) == NULL)
	return;

    *path_end = '/';

    while ((dp = READDIR(dirp)) != NULL)
    {
	char *dirname;
	x = dirname = GETNAME(dp);	/* was dp->d_name */
	if (*x == '.' && (*++x == '\0' || (*x == '.' && *++x == '\0')))
	    continue;
	if (*dirname == '.' && **gpat != '.')
	    continue;

	(void)strcpy(path_end + 1, dirname);

	if (glob_match(*gpat, dirname))
	{   /* this is a match */
	    if ( *(gpat+1) == 0 )
	    {	/* and it is the last */
		add_name();	/* so eat it */
		continue;
	    }
	    if (GLOBSTAT(main_path, &st) == 0 /* else not the last */
		&& (st.st_mode & S_IFMT) == S_IFDIR)
		do_glob(path_end + strlen(dirname) + 1, gpat + 1);
	} 
    }

    (void)CLOSEDIR(dirp);

    *path_end = '\0';
}

static void
#ifndef ANSI_PROTOTYPES
add_name()
#else /* ANSI_PROTOTYPES */
add_name(void)
#endif /* ANSI_PROTOTYPES */
{
    char *np;
#ifdef GLOBDEBUG
    ffprintf(STDDBG,"Globbed: %s\n", main_path+offset);
#endif

    if (--left <= 0)
    {
	if (namelist == 0)
	    namelist = (char**)malloc(GLOBCHUNK * sizeof(char*));
	else
	    namelist = (char**)realloc(namelist,
			       (nnames+GLOBCHUNK)*sizeof(char*));

	if (namelist == NULL)
	    return;
	
	left = GLOBCHUNK;
    }

    if ((np = strdup(main_path + offset)) == 0)
	return;

    namelist[nnames++] = np;
}
Esempio n. 3
0
/* ---INFOBEGIN--- *  DO NOT DELETE THIS COMMENT BLOCK!!!
COMMAND port none "set the connection port to the remote system"
 *  ---INFOEND---  */

#include "client.h"
#include "util.h"

int
#ifndef ANSI_PROTOTYPES
main(argc, argv, envp)
    int argc;
    char *argv[];
    char *envp[];
#else /* ANSI_PROTOTYPES */
main(int argc, char **argv, char **envp)
#endif /* ANSI_PROTOTYPES */
{
    char *newport;

    if (argc < 2)
    {
	if (STDPROMPT)
	{
	    ffprintf(STDPROMPT,"(port) ");
	    newport = readword();
	}
	else
	    newport = 0;
    }
    else
	newport = argv[1];

    if (newport == 0)
	return 1;

    if (env_port)
        (void)free(env_port);
    env_port = strdup(newport);

    disconnect();

    return 0;
}
Esempio n. 4
0
/*****************************************************************************
* wrappers for the FSP remote functions
*****************************************************************************/
static char *
#ifndef ANSI_PROTOTYPES
my_getname(dp)
    VOIDDIRENT *dp;
#else /* ANSI_PROTOTYPES */
my_getname(VOIDDIRENT *dp)
#endif /* ANSI_PROTOTYPES */
{
    return ((struct rdirent *)dp)->rd_name;
}

static VOIDDIR *
#ifndef ANSI_PROTOTYPES
my_opendir(dirname)
    char *dirname;
#else /* ANSI_PROTOTYPES */
my_opendir(char *dirname)
#endif /* ANSI_PROTOTYPES */
{
    return (VOIDDIR*)util_opendir(dirname);
}

static void
#ifndef ANSI_PROTOTYPES
my_closedir(dirp)
    VOIDDIR *dirp;
#else /* ANSI_PROTOTYPES */
my_closedir(VOIDDIR *dirp)
#endif /* ANSI_PROTOTYPES */
{
    (void)util_closedir((RDIR*)dirp);
}

static VOIDDIRENT *
#ifndef ANSI_PROTOTYPES
my_readdir(dirp)
    VOIDDIR *dirp;
#else /* ANSI_PROTOTYPES */
my_readdir(VOIDDIR *dirp)
#endif /* ANSI_PROTOTYPES */
{
    return (VOIDDIRENT*)util_readdir((RDIR*)dirp);
}

void
#ifndef ANSI_PROTOTYPES
remote_glob_routines()
#else /* ANSI_PROTOTYPES */
remote_glob_routines(void)
#endif /* ANSI_PROTOTYPES */
{
    set_glob_routines(my_opendir,my_closedir,my_readdir,my_getname,util_stat);
}
#endif

#ifdef TEST

int
#ifndef ANSI_PROTOTYPES
main(argc, argv)
    int argc;
    char *argv[];
#else /* ANSI_PROTOTYPES */
main(int argc, char *argv[])
#endif /* ANSI_PROTOTYPES */
{
    char **names;

    globerr = 0;
    home = getenv("HOME");

    names = glob(argv[1]);

    if (names == 0) {
	ffprintf(STDERR, "glob error: %s\n", globerr);
	return 1;
    }

    while (*names)
	ffprintf(STDOUT,"%s\n", *names++);

    return 0;
}
/* ---INFOBEGIN--- *  DO NOT DELETE THIS COMMENT BLOCK!!!
COMMAND source local "source the contents of a file"
 *  ---INFOEND---  */

#include "client.h"
#include "main.h"

static int
#ifndef ANSI_PROTOTYPES
do_source(name)
    char *name;
#else /* ANSI_PROTOTYPES */
do_source(char *name)
#endif /* ANSI_PROTOTYPES */
{
    int retval;
    iobuffers storedfs;
    FILE *myin, *myout;
    char *myargv[3];

    if ((myin = fopen(name, "r")) == 0)
    {
	ffprintf(STDERR, "source: can't open `%s'\n", name);
	return -1;
    }

    if ((myout = fopen("/dev/null", "w")) == 0)
    {
	(void)fclose(myin);
	ffprintf(STDERR, "source: can't open `/dev/null'!!\n");
	return -1;
    }

    (void)fflush(STDOUT);
    (void)fflush(STDERR);

    storedfs = global_iobuffers;
    STDIN  = myin;
    STDOUT = STDINFO = STDWARN = STDPROMPT = myout;

    myargv[0] = "source";
    myargv[1] = name;
    myargv[2] = 0;

    retval = execute_stdin(2, myargv);

    global_iobuffers = storedfs;

    (void)fclose(myout);
    (void)fclose(myin);

    return -retval;
}

int
#ifndef ANSI_PROTOTYPES
main(argc, argv, envp)
    int argc;
    char *argv[];
    char *envp[];
#else /* ANSI_PROTOTYPES */
main(int argc, char **argv, char **envp)
#endif /* ANSI_PROTOTYPES */
{
    if (argc < 2)
    {
	ffprintf(STDERR, "source: need filename\n");
	return 1;
    }

    return util_process_arglist(argv + 1, do_source);
}
/* ---INFOBEGIN--- *  DO NOT DELETE THIS COMMENT BLOCK!!!
COMMAND source local "source the contents of a file"
 *  ---INFOEND---  */

#include "client.h"
#include "main.h"

static int
#ifndef ANSI_PROTOTYPES
do_source(name)
    char *name;
#else /* ANSI_PROTOTYPES */
do_source(char *name)
#endif /* ANSI_PROTOTYPES */
{
    int retval;
    iobuffers storedfs;
    FILE *myin, *myout;
    char *myargv[3];

    if ((myin = fopen(name, "r")) == 0)
    {
	ffprintf(STDERR, "source: can't open `%s'\n", name);
	return -1;
    }

    if ((myout = fopen("/dev/null", "w")) == 0)
    {
	(void)fclose(myin);
	ffprintf(STDERR, "source: can't open `/dev/null'!!\n");
	return -1;
    }

    (void)fflush(STDOUT);
    (void)fflush(STDERR);

    storedfs = global_iobuffers;
    STDIN  = myin;
    STDOUT = STDINFO = STDWARN = STDPROMPT = myout;

    myargv[0] = "source";
    myargv[1] = name;
    myargv[2] = 0;

    retval = execute_stdin(2, myargv);

    global_iobuffers = storedfs;

    (void)fclose(myout);
    (void)fclose(myin);

    return -retval;
}
/* ---INFOBEGIN--- *  DO NOT DELETE THIS COMMENT BLOCK!!!
COMMAND rmdir remote "remove directories from the remote system"
 *  ---INFOEND---  */

#include "client.h"

#ifndef ANSI_PROTOTYPES
extern char **glob();
#else /* ANSI_PROTOTYPES */
extern char **glob(char *);
#endif /* ANSI_PROTOTYPES */

static int dirty;

static int
#ifndef ANSI_PROTOTYPES
do_rmdir(name)
    char *name;
#else /* ANSI_PROTOTYPES */
do_rmdir(char *name)
#endif /* ANSI_PROTOTYPES */
{
    char *op;
    UBUF *ub;
    struct stat sbuf;

    if (!validate_operation(name, LITERAL_DIR | DIR_OWNER))
	return -1;

    if (util_stat(name, &sbuf) < 0)
    {
	ffprintf(STDERR,"rmdir: cannot remove directory `%s': no such directory\n", name);
        return -1;
    }

    if (!S_ISDIR(sbuf.st_mode))
    {
	ffprintf(STDERR,"rmdir: cannot remove directory `%s': not a directory\n", name);
        return -1;
    }

    op = util_abs_path(name);
    ub = client_interact(CC_DEL_DIR, 0L, strlen(op), op + 1, 0, NULLP);
    (void)free(op);

    if (client_intr_state > 1 || !ub)
	return -1;

    if (ub->cmd == CC_ERR)
    {
	ffprintf(STDERR,"rmdir: cannot remove directory `%s'\n", name);
	return -1;
    }

    dirty = 1;

    return 0;
}
Esempio n. 8
0
		/* NOTREACHED */
	    }

	    if ( p == '\0' )
		return FAIL;

	    if ( p == '[' ) {
		char s = *string++;
		char reverse = '\0';
		char first, last;
		char gotcha = '\0';

		NEXTP;
		if ( p == '^' ) {
		    reverse = '\1';
		    NEXTP;
		}
		if ( p == ']' ) { /* special case */
		    gotcha = (s==p);
		    NEXTP;
		}

		while (  p != ']' && !gotcha ) {
		    first = p;
		    NEXTP;
		    if ( p == '-' && pattern[1] != ']' ) {
			NEXTP;
			last = p;
			NEXTP;
		    } else
			last = first;
		    if ( first > last )
			return ERROR;
		    gotcha = (first <= s && s <= last );
		}
		while ( p != ']' )
		    NEXTP;

		if ( reverse ? gotcha : !gotcha )
		    return FAIL;
	    } else if ( p != *string )
		return FAIL;
	    else
		string++;
	}
    }
    if ( seenstar )
	return SUCCES;

    if ( *string )
	return FAIL;
    return SUCCES;
}

static char *main_path;		/* ptr to scratchpad */
static int offset;		/* no of leading char in main_path to ignore */
static char **namelist;		/* name list buildup */
static int nnames;		/* no of names found */
static int left;		/* no of slots allocated but not used yet */

#define GLOBMAXSEG	50	/* max segments in pattern */
#define GLOBCHUNK	20	/* no of slots to allocate at a time */

#ifndef MAXNAMLEN
#define MAXNAMLEN 256
#endif

int
#ifndef ANSI_PROTOTYPES
glob_path( pattern, names )
    char *pattern;
    char ***names;
#else /* ANSI_PROTOTYPES */
glob_path(char *pattern, char ***names)
#endif /* ANSI_PROTOTYPES */
{
    char mpath[ MAXPATHLEN + MAXNAMLEN + 1 ];
    char *gpat[GLOBMAXSEG];
    char *pat;

    if (pattern == 0)
	return -1;

    if ((pat = strdup(pattern)) == NULL)
	return -1;

    if (split_pat(pat, gpat) < 0)
    {
	(void)free(pat);
	return -1;
    }
    
    main_path = mpath;		/* initalisation of static storage */
    namelist = 0;
    nnames = left = 0;

    if ( *gpat && **gpat == '/' )
    {
	main_path[0] = '/';
	main_path[1] = '\0';
	offset = 0;
	do_glob(main_path, gpat + 1);
    }
    else
    {
	main_path[0] = '.';
	main_path[1] = '\0';
	offset = 2;
	do_glob(main_path + 1, gpat);
    }

    (void)free(pat);

    if (namelist == 0)
	*names = (char **)malloc(sizeof(char *));
    else
	*names = (char **)realloc(namelist, (nnames+1) * sizeof(char *));

    if (*names == 0)
	return -1;
    (*names)[nnames] = 0;
    return nnames;
}

static int
#ifndef ANSI_PROTOTYPES
split_pat(pattern, table)
    char *pattern;
    char **table;
#else /* ANSI_PROTOTYPES */
split_pat(char *pattern, char **table)
#endif /* ANSI_PROTOTYPES */
{
    char *pp = pattern;
    int size = GLOBMAXSEG;

    if (*pattern == '/')
    {
	*table++ = "/";
	--size;
    }

    do
    {
	while (*pp == '/')
	    *pp++ = '\0';
	if (*pp == '\0')
	    break;
	if (--size < 0)
	    return -1;
	*table++ = pp;
	while (*pp && *pp != '/')
	    pp++;
    }
    while (*pp);

    *table = 0;
    return 0;
}


#define ISGLOB(x) ((x)=='*' || (x)=='?' || (x)=='[')

static int
#ifndef ANSI_PROTOTYPES
no_glob(pat)
    char *pat;
#else /* ANSI_PROTOTYPES */
no_glob(char *pat)
#endif /* ANSI_PROTOTYPES */
{
    while (*pat && !ISGLOB(*pat))
	pat++;

    return (*pat == '\0');
}
	
static void
#ifndef ANSI_PROTOTYPES
do_glob(path_end, gpat)
    char *path_end;		/* ptr to the end of main_path */
    char **gpat;		/* the rest of the pattern segments */
#else /* ANSI_PROTOTYPES */
do_glob(char *path_end, char **gpat)
                   		/* ptr to the end of main_path */
                		/* the rest of the pattern segments */
#endif /* ANSI_PROTOTYPES */
{
    char *saved_end = path_end;	/* saved to be resored */
    char *pat;			/* current pattern segment */
    struct stat st;		/* to check if file exists */

#ifdef GLOBDEBUG
    ffprintf(STDDBG,"do_glob: path = '%s', pat = '%s'\n", main_path, *gpat );
#endif

    for ( ; (pat = *gpat) != 0 && no_glob(pat); gpat++ )
    {
#ifdef GLOBDEBUG
	ffprintf(STDDBG,"no_glob: path = '%s', pat = '%s'\n", main_path, pat );
#endif
	*path_end = '/';
	(void)strcpy(path_end+1, pat);
	path_end += strlen(pat) + 1;

	if (GLOBSTAT(main_path, &st) != 0 )
	{
	    *saved_end = '\0';
	    return;
	}
    }
    if (pat)
	matchdir(path_end, gpat);
    else
	add_name();

    *saved_end = '\0';
    return;
}
Esempio n. 9
0
                		/* the rest of the pattern segments */
#endif /* ANSI_PROTOTYPES */
{
    char *saved_end = path_end;	/* saved to be resored */
    char *pat;			/* current pattern segment */
    struct stat st;		/* to check if file exists */

#ifdef GLOBDEBUG
    ffprintf(STDDBG,"do_glob: path = '%s', pat = '%s'\n", main_path, *gpat );
#endif

    for ( ; (pat = *gpat) != 0 && no_glob(pat); gpat++ )
    {
#ifdef GLOBDEBUG
	ffprintf(STDDBG,"no_glob: path = '%s', pat = '%s'\n", main_path, pat );
#endif
	*path_end = '/';
	(void)strcpy(path_end+1, pat);
	path_end += strlen(pat) + 1;

	if (GLOBSTAT(main_path, &st) != 0 )
	{
	    *saved_end = '\0';
	    return;
	}
    }
    if (pat)
	matchdir(path_end, gpat);
    else
	add_name();

    *saved_end = '\0';
    return;
}

static void
#ifndef ANSI_PROTOTYPES
matchdir(path_end, gpat)
    char *path_end;		/* ptr to end of main_path */
    char **gpat;		/* the rest of the pattern segments */
#else /* ANSI_PROTOTYPES */
matchdir(char *path_end, char **gpat)
                   		/* ptr to end of main_path */
                		/* the rest of the pattern segments */
#endif /* ANSI_PROTOTYPES */
{
    char *x;			/* scratch */
    VOIDDIR *dirp;		/* for directory reading */
    VOIDDIRENT *dp;		/* directory entry */
    struct stat st;		/* to determine files type */

#ifdef GLOBDEBUG
    ffprintf(STDDBG,"matchdir: path = '%s', pat = '%s'\n", main_path, *gpat );
#endif
    if ((dirp = OPENDIR(main_path)) == NULL)
	return;

    *path_end = '/';

    while ((dp = READDIR(dirp)) != NULL)
    {
	char *dirname;
	x = dirname = GETNAME(dp);	/* was dp->d_name */
	if (*x == '.' && (*++x == '\0' || (*x == '.' && *++x == '\0')))
	    continue;
	if (*dirname == '.' && **gpat != '.')
	    continue;

	(void)strcpy(path_end + 1, dirname);

	if (glob_match(*gpat, dirname))
	{   /* this is a match */
	    if ( *(gpat+1) == 0 )
	    {	/* and it is the last */
		add_name();	/* so eat it */
		continue;
	    }
	    if (GLOBSTAT(main_path, &st) == 0 /* else not the last */
		&& (st.st_mode & S_IFMT) == S_IFDIR)
		do_glob(path_end + strlen(dirname) + 1, gpat + 1);
	} 
    }

    (void)CLOSEDIR(dirp);

    *path_end = '\0';
}
Esempio n. 10
0
int DaysWhereTeacherIsAbcent(int *teacherDaysData, char teacherFirstName, char teacherLastName, int numOfTeachers){
     #if defined(_WIN32)
    _mkdir("./data");
     #else 
    mkdir("./data", 0700); 
     #endif   
    int i,k=0;
    FILE *fptr;

    if ((fptr=fopen("data/TeacherIsAbcent.txt","r"))==NULL){
        
        printf("Did not find file, creating new\n");
        fptr = fopen("data/TeacherIsAbcent.txt", "w"); 
        fputs("//This entire document is dedicated to the processing of a school schedule.\n",fptr);
        fputs("//Please do not edit this file for any reasons. Unless you know what you are doing.\n\n",fptr);
        fputs("//For the teachers, there will be data that tells what classes they can participate in,\n",fptr);
        fputs("//0 = they cannot participate, 1 = they can participate \n\n",fptr);
        fputs("//The following data will represent: \n",fptr);
        fputs("//Unique ID:     ID:\n",fptr);
        fputs("//Name:          Name:\n",fptr);
        fputs("//Days:          Day:",fptr);

        fputs("--------ALL TEACHERS--------\n",fptr);
        for(i = 0;i<numOfTeachers;i++){
            fputs("--Teacher Start--\n",fptr);
            fprintf(fptr, "Teacher Nr: %i\n",i );
            ffprintf(fptr, "ID: %s %s\n",teacherDaysData.ID[i] );
            fprintf(fptr, "Name: %s %s\n",teacherFirstName,teacherLastName );
            for(; i<teacherDaysData.numOfDays[i];k++){
                fprintf(fptr, "Day: %s\n",teacherDaysData.day[k]);
            }
            fputs("--Teacher End--\n\n",fptr);
        }
    }
        
        fclose(fptr);
    return 0;
}
Esempio n. 11
0
/*
 * get option letter from argument vector
 */
int	opterr = 1,		/* if error message should be printed */
	optind = 1,		/* index into parent argv vector */
	optopt;			/* character checked for validity */
char	*optarg;		/* argument associated with option */

#define	BADCH	(int)'?'
#define	EMSG	""

int
#ifndef ANSI_PROTOTYPES
getopt(nargc, nargv, ostr)
	int nargc;
	char **nargv;
	char *ostr;
#else /* ANSI_PROTOTYPES */
getopt(int nargc, char **nargv, char *ostr)
#endif /* ANSI_PROTOTYPES */
{
	static char *place = EMSG;		/* option letter processing */
	char *oli;				/* option letter list index */
	char *p;

	if (!*place) {				/* update scanning pointer */
		if (optind >= nargc || *(place = nargv[optind]) != '-') {
			place = EMSG;
			return(EOF);
		}
		if (place[1] && *++place == '-') {	/* found "--" */
			++optind;
			place = EMSG;
			return(EOF);
		}
	}					/* option letter okay? */
	if ((optopt = (int)*place++) == (int)':' ||
	    !(oli = strchr(ostr, optopt))) {
		/*
		 * if the user didn't specify '-' as an option,
		 * assume it means EOF.
		 */
		if (optopt == (int)'-')
			return(EOF);
		if (!*place)
			++optind;
		if (opterr) {
			if (!(p = strrchr(*nargv, '/')))
				p = *nargv;
			else
				++p;
			ffprintf(STDERR, "%s: illegal option -- %c\n",
			    p, optopt);
		}
		return(BADCH);
	}
	if (*++oli != ':') {			/* don't need argument */
		optarg = NULL;
		if (!*place)
			++optind;
	}
	else {					/* need an argument */
		if (*place)			/* no white space */
			optarg = place;
		else if (nargc <= ++optind) {	/* no arg */
			place = EMSG;
			if (!(p = strrchr(*nargv, '/')))
				p = *nargv;
			else
				++p;
			if (opterr)
				ffprintf(STDERR,
				    "%s: option requires an argument -- %c\n",
				    p, optopt);
			return(BADCH);
		}
	 	else				/* white space */
			optarg = nargv[optind];
		place = EMSG;
		++optind;
	}
	return(optopt);				/* dump back option letter */
}
Esempio n. 12
0
SEXP GADEM_Analysis(SEXP sequence,SEXP sizeSeq, SEXP accession, SEXP Rverbose,SEXP RnumWordGroup,SEXP RnumTop3mer,SEXP RnumTop4mer,SEXP RnumTop5mer,SEXP RnumGeneration,SEXP RpopulationSize, SEXP RpValue,SEXP ReValue,SEXP RextTrim,SEXP RminSpaceWidth,SEXP RmaxSpaceWidth,SEXP RuseChIPscore,SEXP RnumEM,SEXP RfEM, SEXP RwidthWt,SEXP RfullScan, SEXP RslideWinPWM,SEXP RstopCriterion,SEXP RnumBackgSets,SEXP RweightType,SEXP RbFileName,SEXP RListPWM,SEXP RminSites,SEXP RmaskR,SEXP Rnmotifs) 
{
  char *bFileName;
  
  SEXP ResultsGadem;
  SEXP RSpwm;
  PROTECT(ResultsGadem=NEW_LIST(100));  
  
  int increment=0;
  
  double testrand;
  
  //Number of sequences
  int numSeq = INTEGER_VALUE(sizeSeq);
  // const
//  char *Fastaheader[size];
  int incr=0;
  
  int longueur=length(sequence);
  int IncrementTemp=0;
  
  // basic settings/info
  int maxSeqLen,*seqLen;       		 // sequence info	
  double aveSeqLen;                      // sequence info
  char **seq,**rseq;
  int *geneID;         			 // sequence info
  char **oseq,**orseq;                   // copy of the original sequences
  char **sseq,**rsseq;                   // simulated seqs.
  double *bfreq1, *bfreq0=NULL;                // base frequencies
  double *ChIPScore;                     // chip score
  int maskR;				 // mask simple repeats before running the algorithm
    
  // pwms
  double ***pwm;                         // initial population of PWMs from spaced dyads
  int *pwmLen;                           // initial pwm lengths
  double **opwm2;                        // EM-derived PWM
  double ***opwm;                        // observed PWMs from identified sites
  double ***epwm;                        // em-optimized PWMs
  double **logepwm;                      // log(em-optimized PWM)
  int *pwmnewLen;                        // final motif length after extending to both ends
  
  // llr score distr.
  Pgfs *llrDist;                         // llr distribution from pgf method
  int llrDim;                            // llr distribution dimension
  int **ipwm;                            // integer pwm for computing llr score distribution
  
  // EM, motif, sites
  double pvalueCutoff;                   // user input, used to determine score cutoff based on ipwm
  int *scoreCutoff;                      // pwm score cutoff for the corresponding p-value cutoff
  double logev;                          // log of E-value of a motif;
  int useChIPscore;                      // indicator for using ChIP-seq score for seq. selection for EM
  int numEM;                             // number of EM steps
  double E_valueCutoff;                  // log E-value cutoff
  //int nsitesEM;                          // number of binding sites in sequences subjected to EM
  int minsitesEM;                        // minimal number of sites in a motif in EM sequences
  int *nsites;                           // number of binding sites in full data
  int minsites;                          // minimal number of sites in a motif in full data
  Sites **site;                          // binding sites in all sequences
  int motifCn;                           // number of motifs sought and found
  int extTrim;
  int noMotifFound;                      // none of the dyads in the population resulted in a motif
  char **pwmConsensus;                   // consensus sequences of motifs
  double pwmDistCutoff;                  // test statistic for motif pwm similarity
  char *uniqMotif;                       // motifs in a population unique or not
  int numUniq;                           // number of unique motifs in a population
  int slideWinPWM;                       // sliding window for comparing pwm similarity
  int widthWt;                           // window width in which nucleotides are given large weights for PWM optimization
  int fullScan;                          // scan scan on the original sequences or masked sequences
  
  // background
  int numBackgSets;
  
  // weights
  double **posWeight;                    // spatial weights
  int weightType;                        // four weight types 0, 1, 2, 3, or 4
  
  // words for spaced dyad
  Words *word;                           // top-ranked k-mers as the words for spaced dyads
  int numTop3mer,numTop4mer,numTop5mer;  // No. of top-ranked k-mers as words for dyads
  int maxWordSize;                       // max of the above three
  int numWordGroup;                      // number of non-zero k-mer groups
  int minSpaceWidth,maxSpaceWidth;       // min and max width of spacer of the spaced dyads
  Chrs **dyad;                           // initial population of "chromosomes"
  char **sdyad;                          // char of spaced dyads
  
  // GA
  int populationSize,numGeneration;      // GA parameters
  double maxpMutationRate;
  Fitness *fitness;                      // "chromosome" fitness
  Wheel *wheel;                          // roulette-wheel selection
  
  // to speed up only select a subset of sequences for EM algorithm
  double fEM;                            // percentage of sequences used in EM algorithm
  int numSeqEM;                          // number of sequences subject to EM
  char *Iseq;                            // Indicator if a sequence is used in EM or not
  int *emSeqLen;                         // length of sequences used in EM
  double *maxpFactor;
  
  int numCycle;                          // number of GADEM cycles
  int generationNoMotif;                 // maximal number of GA generations in a GADEM cycle resulted in no motifs
  
  // mis.
  //seed_t  seed;                          // random seed
  int motifCn2,id,numCycleNoMotif,verbose,minminSites,nmotifs;
  int startPWMfound,stopCriterion;
  char *mFileName,*oFileName,*pwmFileName,*tempRbFileName;
  time_t start;
  int cn[4],bcn[4],*seqCn,*bseqCn,avebnsites,avebnsiteSeq,totalSitesInput;
  int i; 
  int ii=0;
  int jjj=0;
  
  /*************/
  FILE * output = fopen("output.txt", "w"); 
  /*************/
  
  GetRNGstate();
  

  mFileName=alloc_char(500);         mFileName[0]='\0';
  oFileName=alloc_char(500);         oFileName[0]='\0';
  pwmFileName=alloc_char(500);       pwmFileName[0]='\0';
  bFileName=alloc_char(500);         bFileName[0]='\0';
  //tempRbFileName=alloc_char(500);    tempRbFileName[0]='\0';
  seq=NULL; aveSeqLen=0; maxSeqLen=0; 
  //minsites=-1; 
  
  startPWMfound=0;    

  maxSeqLen=0;
  for(incr=1;incr<longueur;incr=incr+2)
  { 
    if (length(STRING_ELT(sequence,(incr)))>maxSeqLen) maxSeqLen=length(STRING_ELT(sequence,(incr))); 
  }
//  fprintf(output,"maxLength=%d",maxSeqLen);
//  exit(0);
  seq=alloc_char_char(numSeq,maxSeqLen+1);
  for(incr=1;incr<longueur;incr=incr+2)
  { 
    for (int j=0; j<length(STRING_ELT(sequence,(incr))); j++)
    {
      seq[IncrementTemp][j]=CHAR(STRING_ELT(sequence,(incr)))[j];
    }
    IncrementTemp++;
  }
  
  
  verbose=LOGICAL_VALUE(Rverbose);
  numWordGroup=INTEGER_VALUE(RnumWordGroup);
  minsites=INTEGER_VALUE(RminSites);
  numTop3mer=INTEGER_VALUE(RnumTop3mer);
  numTop4mer=INTEGER_VALUE(RnumTop4mer);
  numTop5mer=INTEGER_VALUE(RnumTop5mer);
  numGeneration=INTEGER_VALUE(RnumGeneration);
  populationSize=INTEGER_VALUE(RpopulationSize);
  pvalueCutoff=NUMERIC_VALUE(RpValue);
  E_valueCutoff=NUMERIC_VALUE(ReValue);
  extTrim=INTEGER_VALUE(RextTrim);
  minSpaceWidth=INTEGER_VALUE(RminSpaceWidth);
  maxSpaceWidth=INTEGER_VALUE(RmaxSpaceWidth);
  useChIPscore=NUMERIC_VALUE(RuseChIPscore);
  numEM=INTEGER_VALUE(RnumEM);
  fEM=NUMERIC_VALUE(RfEM);
  widthWt=INTEGER_VALUE(RwidthWt);
  fullScan=INTEGER_VALUE(RfullScan);
  slideWinPWM=INTEGER_VALUE(RslideWinPWM);
  numUniq=populationSize;
  stopCriterion=INTEGER_VALUE(RstopCriterion);  
  numBackgSets=INTEGER_VALUE(RnumBackgSets);
  weightType=NUMERIC_VALUE(RweightType);
  //const char *tempRbFileName[1];

 	tempRbFileName = convertRString2Char(RbFileName);	

  //tempRbFileName[0]=CHAR(STRING_ELT(RbFileName,0));
  nmotifs = INTEGER_VALUE(Rnmotifs);
  maskR = INTEGER_VALUE(RmaskR);

  

  if(numSeq>MAX_NUM_SEQ)
  {
    error("Error: maximal number of seqences reached!\nPlease reset MAX_NUM_SEQ in gadem.h and rebuild (see installation)\n");
  }
  
  strcpy(bFileName,tempRbFileName);

  ChIPScore=alloc_double(MAX_NUM_SEQ);
  seqLen=alloc_int(MAX_NUM_SEQ); 
  geneID=alloc_int(MAX_NUM_SEQ);

//  seq=sequences;
  
//  numSeq=size;
  int len; 
  
  for (i=0; i<numSeq; i++)
  {
    len=strlen(seq[i]); 
    seqLen[i]=len;
    geneID[i]=INTEGER(accession)[i];
  }

  aveSeqLen=0; 
  for (i=0; i<numSeq; i++) aveSeqLen +=seqLen[i]; aveSeqLen /=(double)numSeq;
  
  for (i=0; i<numSeq; i++) {
    if (seqLen[i]>maxSeqLen) maxSeqLen=seqLen[i]; 
  }
  
  rseq=alloc_char_char(numSeq,maxSeqLen+1);
  oseq=alloc_char_char(numSeq,maxSeqLen+1);
  orseq=alloc_char_char(numSeq,maxSeqLen+1);
  
  for (i=0; i<numSeq; i++)
  {
    if(seqLen[i]>maxSeqLen) maxSeqLen=seqLen[i]; 
  }
  
  reverse_seq(seq,rseq,numSeq,seqLen);
  
  // make a copy of the original sequences both strands
  for (i=0; i<numSeq; i++)
  {
    for (int j=0; j<seqLen[i]; j++)
    {
      oseq[i][j]=seq[i][j];
      orseq[i][j]=rseq[i][j];
    }
    oseq[i][seqLen[i]]='\0'; orseq[i][seqLen[i]]='\0'; 
  }
    
  if (strcmp(bFileName,"NULL")!= 0)
  {
    bfreq0=alloc_double(5);
    read_background(bFileName,bfreq0);
  }

  if (GET_LENGTH(RListPWM)!= 0)
  {
    startPWMfound=1; 
  }
  else { }
  
    // check for input parameters
  if(numGeneration<1)
  { 
    error("number of generaton < 1.\n");
  }
  if(populationSize<1)
  {
    error("population size < 1.\n");
  }
  if (minSpaceWidth<0)
  { 
    error("minimal number of unspecified bases in spaced dyads <0.\n"); 
  }
  if (maxSpaceWidth<0)
  { 
    error("maximal number of unspecified bases in spaced dyads <0.\n"); 
  }
  if (minSpaceWidth>maxSpaceWidth)
  {
    error("mingap setting must <= to maxgap setting.\n\n"); 
  }
  if (maxSpaceWidth+12>MAX_PWM_LENGTH)
  {
    error("maxgap setting plus word lengths exceed <MAX_PWM_LENGTH>.\n");
  }
  if (numEM<0)
  {
    error("number of EM steps is zero.\n");
  }
  if (numEM==0)
  {
    error("number of EM steps = 0, no EM optimization is carried out.\n");
  }
  
  if (fullScan!=0 && fullScan!=1)
    fullScan=0;
  
  
  maxWordSize=0;
  if (numTop3mer>maxWordSize) maxWordSize=numTop3mer;
  if (numTop4mer>maxWordSize) maxWordSize=numTop4mer;
  if (numTop5mer>maxWordSize) maxWordSize=numTop5mer;
  
    // any one, two or three: tetramer, pentamer, hexamer
  if (numTop3mer==0 && numTop4mer==0 && numTop5mer==0)
  {
    error("maxw3, maxw4, and maxw5 all zero - no words for spaced dyads.\n");
  }
  
  // if (startPWMfound && fEM!=0.5 && fEM!=1.0 & verbose)
  // {
  //   warning("fEM argument is ignored in a seeded analysis\n");
  // }
  
  if (startPWMfound)
  {
    // if(verbose)
    // {
    //   if (populationSize!=10 && populationSize!=100) warning("pop argument is ignored in a seeded analysis, -pop is set to 10.\n");
    //   if (numGeneration!=1 && numGeneration!=5)      warning("gen argument is ignored in a seeded analysis, -gen is set to 1.\n");
    // }
    fEM=1.0;
    populationSize=FIXED_POPULATION; numGeneration=1; 
  }
  
    // number of sequences for EM
  if (fEM>1.0 || fEM<=0.0)
  { 
    error("The fraction of sequences subject to EM is %3.2f.\n",fEM);
  } 
  numSeqEM=(int)(fEM*numSeq);
  


  // memory callocations
  Iseq  =alloc_char(numSeq+1); 
  opwm2 =alloc_double_double(MAX_PWM_LENGTH,4);
  ipwm  =alloc_int_int(MAX_PWM_LENGTH,4);
  logepwm=alloc_double_double(MAX_PWM_LENGTH,4);
  emSeqLen=alloc_int(numSeqEM);
  scoreCutoff=alloc_int(1000);
  // scoreCutoff=alloc_int(populationSize);
  llrDist=alloc_distr(MAX_DIMENSION);
  posWeight=alloc_double_double(numSeq,maxSeqLen);
  sseq=alloc_char_char(MAX_NUM_SEQ,maxSeqLen+1);
  rsseq=alloc_char_char(MAX_NUM_SEQ,maxSeqLen+1);
  bfreq1=base_frequency(numSeq,seq,seqLen);

  if (strcmp(bFileName,"NULL") == 0)
  {
    bfreq0=alloc_double(5);
    for (i=0; i<4; i++)
      {
	bfreq0[i]=bfreq1[i];
      }
  }
  

  // if minN not specified, set the defaults accordingly
  if (minsites==-1) 
  {
    minsites =max(2,(int)(numSeq/20)); 
  }
  minsitesEM=(int)(fEM*minsites);
  
  maxpMutationRate=MAXP_MUTATION_RATE;
  
  // determine the distribution and critical cut point
  pwmDistCutoff=vector_similarity();
  
  /*---------- select a subset of sequences for EM only --------------*/
  if (useChIPscore==1)
  {
    select_high_scoring_seq_for_EM (ChIPScore,numSeq,numSeqEM,Iseq,fEM);
  }
  else
  {
    sample_without_replacement(Iseq,numSeqEM,numSeq);
  }
  /*-------------------- end of selection --------------------------*/
  
  if (maskR==1) mask_repetitive(geneID,seq,numSeq,seqLen,mFileName);

  if (widthWt<20)
  {
    warning("The window width of sequence centered on the nucleotides having large weights in EM for PWM optimization is small\n Motif longer than %d will not be discovered\n",widthWt);
  }
  
  time(&start);
  
    // if (weightType==1 || weightType==3) 
    //ffprintf(output,fp,"window width of sequence centered on the nucleotides having large weights for PWM optimization: %d\n",widthWt);
    //ffprintf(output,fp,"pwm score p-value cutoff for declaring binding site:\t%e\n",pvalueCutoff);
  
  if(verbose)
  {
    ffprintf(output,output,"==============================================================================================\n");
    ffprintf(output,output,"input sequence file:  %s\n",mFileName);
    fprintf(output,"number of sequences and average length:\t\t\t\t%d %5.1f\n",numSeq,aveSeqLen);
    
    fprintf(output,"Use pgf method to approximate llr null distribution\n");
    fprintf(output,"parameters estimated from sequences in:  %s\n\n",mFileName);

    if (weightType!=0) 
      fprintf(output,"non-uniform weight applies to each sequence - type:\t\t%d\n",weightType);
    fprintf(output,"number of GA generations & population size:\t\t\t%d %d\n\n",numGeneration,populationSize);
    fprintf(output,"PWM score p-value cutoff for binding site declaration:\t\t%e\n",pvalueCutoff);
    fprintf(output,"ln(E-value) cutoff for motif declaration:\t\t\t%f\n\n",E_valueCutoff);
//    fprintf(output,"number (percentage) of sequences selected for EM:\t\t%d(%4.1f\%)\n",numSeqEM,100.0*(double)numSeqEM/(double)numSeq);
    fprintf(output,"number of EM steps:\t\t\t\t\t\t%d\n",numEM);
    fprintf(output,"minimal no. sites considered for a motif:\t\t\t%d\n\n",minsites);
    fprintf(output,"[a,c,g,t] frequencies in input data:\t\t\t\t%f %f %f %f\n",bfreq1[0],bfreq1[1],bfreq1[2],bfreq1[3]);
    fprintf(output,"==============================================================================================\n");
  }
  
  // if (pgf) 
  // {
  //   if (userMarkovOrder!=0 & verbose) 
  //   {
  //     warning("The user-specified background Markov order (%d) is ignored when -pgf is set to 1\n",userMarkovOrder);
  //   }
  //   if (bFileName[0]!='\0' & verbose)
  //   {
  //     warning("The user-specified background models: %s are not used when -pgf is set to 1\n",bFileName);
  //   }
  // }
  // if (startPWMfound && fEM!=1.0  & verbose)
  // {
  //   warning("fEM argument is ignored in a seeded analysis\n");
  // }
  
    // determine seq length by counting only [a,c,g,t], seqLen is used in E-value calculation
    // determine the distribution and critical cut point
  pwmDistCutoff=vector_similarity();
  
  if      (weightType==0) assign_weight_uniform(seqLen,numSeq,posWeight);
  else if (weightType==1) assign_weight_triangular(seqLen,numSeq,posWeight);
  else if (weightType==2) assign_weight_normal(seqLen,numSeq,posWeight);
  else
  {
    error("Motif prior probability type not found - please choose: 0, 1, or 2\n");
    // fprintf(output,"Consider: -posWt 1 for strong central enrichment as in ChIP-seq\n");
    // fprintf(output,"          -posWt 0 for others\n\n");
    // exit(0);
  }
  /*    if (startPWMfound) minminSites=minsites;
   else               minminSites=(int)(0.40*minsitesEM);*/
  
  motifCn=0; noMotifFound=0; numCycle=0; numCycleNoMotif=0; 
  int compt=0;
  int lengthList=GET_LENGTH(RListPWM);
 
    /****************************************/ 
    broadcastOnce(maxSeqLen, numEM, startPWMfound, minminSites, maxpFactor, numSeq, numSeqEM, Iseq, bfreq0, posWeight, weightType, pvalueCutoff, emSeqLen, populationSize);
    /****************************************/ 

  do
  {
    if(!startPWMfound)
    {
      
      if(verbose)
      {
        fprintf(output,"*** Running an unseeded analysis ***\n");
        // fprintf(output,"\n|------------------------------------------------------------------|\n");
        // fprintf(output,"|                                                                  |\n");
        // fprintf(output,"|              *** Running an unseeded analysis ***                |\n");
        // fprintf(output,"|                                                                  |\n");
        // fprintf(output,"|------------------------------------------------------------------|\n\n");
      }
      populationSize=INTEGER_VALUE(RpopulationSize);
      numGeneration=INTEGER_VALUE(RnumGeneration);
      dyad  =alloc_chrs(populationSize,4);
      wheel =alloc_wheel(populationSize);
      fitness=alloc_fitness(populationSize);
      maxpFactor=alloc_double(populationSize);
      uniqMotif=alloc_char(populationSize+1);
      opwm  =alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      epwm=alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      pwmConsensus=alloc_char_char(populationSize,MAX_PWM_LENGTH+1);
      pwm   =alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      pwmLen=alloc_int(populationSize);
      sdyad =alloc_char_char(populationSize,MAX_PWM_LENGTH+1);
      word  =alloc_word(numWordGroup,maxWordSize);
      minminSites=(int)(0.40*minsitesEM);

        // identify top-ranked k-mers (k=3,4,5) for spaced dyads
      if(verbose)
        fprintf(output,"GADEM cycle %2d: enumerate and count k-mers... ",numCycle+1);
        
      numWordGroup=word_for_dyad(word,seq,rseq,numSeq,seqLen,bfreq1,&numTop3mer,&numTop4mer,&numTop5mer);
      
      if(verbose)
        fprintf(output,"Done.\n");
      
        // generating a "population" of spaced dyads
      if(verbose)
        fprintf(output,"Initializing GA... ");

      initialisation(dyad,populationSize,numWordGroup,word,minSpaceWidth,maxSpaceWidth,maxpFactor);
      if(verbose)
        fprintf(output,"Done.\n");
      
    }
    else
    {
      if(verbose)
      {
        fprintf(output,"*** Running an seeded analysis ***\n");
        // fprintf(output,"\n|------------------------------------------------------------------|\n");
        // fprintf(output,"|                                                                  |\n");
        // fprintf(output,"|               *** Running a seeded analysis ***                  |\n");
        // fprintf(output,"|                                                                  |\n");
        // fprintf(output,"|------------------------------------------------------------------|\n\n");
      }
      populationSize=FIXED_POPULATION; 
      dyad  =alloc_chrs(populationSize,4);
      pwm=alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      pwmLen=alloc_int(populationSize);
      maxpFactor=alloc_double(populationSize);
      uniqMotif=alloc_char(populationSize+1);
      opwm  =alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      epwm=alloc_double_double_double(populationSize,MAX_PWM_LENGTH,4);
      pwmConsensus=alloc_char_char(populationSize,MAX_PWM_LENGTH+1);
      sdyad =alloc_char_char(populationSize,MAX_PWM_LENGTH+1);
      word  =alloc_word(numWordGroup,maxWordSize);
      wheel =alloc_wheel(populationSize);
      fitness=alloc_fitness(populationSize);
      minminSites=minsites;
      int lengthMatrix;
      
      lengthMatrix=GET_LENGTH(VECTOR_ELT(RListPWM,compt));
      RSpwm=allocMatrix(REALSXP,4,(lengthMatrix/4));
      RSpwm=VECTOR_ELT(RListPWM,compt);
      
      
      pwmLen[0]=read_pwm0(RSpwm,pwm[0],lengthMatrix);
      
      for(i=1; i<populationSize; i++)
      {
        for (int j=0; j<pwmLen[0]; j++)
        {
          for (int k=0; k<4; k++)
          {
            pwm[i][j][k]=pwm[0][j][k];
          }
        }
        pwmLen[i]=pwmLen[0];
      }
      for (i=0; i<populationSize; i++)
      {
        maxpFactor[i]=FIXED_MAXPF*(i+1);
        standardize_pwm(pwm[i],pwmLen[i]);
        consensus_pwm(pwm[i],pwmLen[i],pwmConsensus[i]);
        strcpy(sdyad[i],pwmConsensus[i]);
      }
    }
    generationNoMotif=0;
    
    for (jjj=0; jjj<numGeneration; jjj++)
    {
        // convert spaced dyads to letter probability matrix
      if (!startPWMfound)
      {
        dyad_to_pwm(word,populationSize,dyad,pwm,pwmLen);
      }

    /*
      DO_APPLY(populationCalculation(maxSeqLen, numEM, fitness+ii, 
                                     startPWMfound, minminSites, maxpFactor[ii], 
                                     numSeq, numSeqEM, seq, rseq, seqLen, Iseq, 
                                     bfreq0, posWeight, weightType, 
                                     pvalueCutoff, emSeqLen, 
                                     pwm[ii], pwmLen[ii], epwm[ii], opwm[ii], 
                                     pwmConsensus[ii], scoreCutoff+ii, sdyad[ii], ii),
               populationSize, ii);
    */
      
     /* Create the structure to send to all the other slaves  */ 
      
      broadcastEveryCycle(Iseq, pwm, pwmLen, pwmConsensus, scoreCutoff, sdyad, populationSize);

      populationCalculation(maxSeqLen, numEM, fitness+ii, 
                                     startPWMfound, minminSites, maxpFactor[ii], 
                                     numSeq, numSeqEM, seq, rseq, seqLen, Iseq, 
                                     bfreq0, posWeight, weightType, 
                                     pvalueCutoff, emSeqLen, 
                                     pwm[ii], pwmLen[ii], epwm[ii], opwm[ii], 
                                     pwmConsensus[ii], scoreCutoff+ii, sdyad[ii], ii);

    /* Receive the analyzed data from all the other slaves and compile them */
    //getPopCalcResults(...);


      // for (i=0; i<5; i++)
      // {
      //   fprintf(output,"fitness.value=%lf\n",fitness[i].value);
      //   fprintf(output,"fitness.index=%d\n",fitness[i].index);
      //   fprintf(output,"maxpfactor=%lf\n",maxpFactor[i]);
      //   fprintf(output,"scoreCutoff=%d\n",scoreCutoff[i]);
      //   fprintf(output,"   spacedDyad: %s\n",sdyad[i]);
      //   
      //   for (l=0; l<pwmLen[i]; l++)
      //   {
      //     for (m=0; m<4; m++) 
      //     { 
      //       fprintf(output,"opwm[%d][%d][%d]=%lf ",i,l,m,opwm[i][l][m]);
      //       fprintf(output,"epwm[%d][%d][%d]=%lf ",i,l,m,epwm[i][l][m]);
      //       fprintf(output,"pwm[%d][%d][%d]=%lf ",i,l,m,pwm[i][l][m]);
      //     }
      //     fprintf(output,"\n");
      //   }
      //   fprintf(output,"\n");
      // }
      // 
      // testrand=runif(0,1);
      // fprintf(output,"testrand1=%lf\n",testrand);
      
      if (populationSize>1)
      {
        sort_fitness(fitness,populationSize);
      }



      // for (i=0; i<5; i++)
      // {
      //   fprintf(output,"fitness.value=%lf\n",fitness[i].value);
      //   fprintf(output,"fitness.index=%d\n",fitness[i].index);
      // }
      numUniq=check_pwm_uniqueness_dist(opwm, pwmLen,
                                        populationSize, fitness,
                                        pwmDistCutoff, E_valueCutoff,
                                        uniqMotif, slideWinPWM);


      // for (i=0; i<5; i++)
      // {
      //   fprintf(output,"fitness.value=%lf\n",fitness[i].value);
      //   fprintf(output,"fitness.index=%d\n",fitness[i].index);
      //   fprintf(output,"maxpfactor=%lf\n",maxpFactor[i]);
      //   fprintf(output,"scoreCutoff=%d\n",scoreCutoff[i]);
      //   fprintf(output,"   spacedDyad: %s\n",sdyad[i]);
      //   
      //   for (l=0; l<pwmLen[i]; l++)
      //   {
      //     for (m=0; m<4; m++) 
      //     { 
      //       fprintf(output,"opwm[%d][%d][%d]=%lf",i,l,m,opwm[i][l][m]); 
      //     }
      //     fprintf(output,"\n");
      //   }
      //   fprintf(output,"\n");
      // }
      
      if(verbose)
      {
        fprintf(output,"GADEM cycle[%3d] generation[%3d] number of unique motif: %d\n",numCycle+1,jjj+1,numUniq);
        for (i=0; i<populationSize; i++)
        {
          if (uniqMotif[i]=='1')
          {
            fprintf(output,"   spacedDyad: %s ",sdyad[fitness[i].index]);
            for (int j=strlen(sdyad[fitness[i].index]); j<maxSpaceWidth+10; j++) fprintf(output," ");
            fprintf(output,"motifConsensus: %s ",pwmConsensus[fitness[i].index]);
            for (int j=strlen(sdyad[fitness[i].index]); j<maxSpaceWidth+10; j++) fprintf(output," ");
            fprintf(output," %3.2f fitness: %7.2f\n",maxpFactor[fitness[i].index],fitness[i].value);
          }
        }
        fprintf(output,"\n");
      }


      if (jjj<numGeneration-1)
      {
        // fitness based selection with replacement
        roulett_wheel_fitness(fitness,populationSize,wheel);
        // mutation and crossover operations
        if (populationSize>1)
        {
          testrand=runif(0,1);
          if (testrand>=0.5)
          {
            mutation(dyad,numWordGroup,word,minSpaceWidth,maxSpaceWidth,wheel,populationSize,fitness,uniqMotif,
                      maxpFactor,maxpMutationRate); 
          }
          else
          {
            crossover(dyad,numWordGroup,word,minSpaceWidth,maxSpaceWidth,wheel,populationSize,fitness,uniqMotif, maxpFactor,maxpMutationRate); 
          }
        }
        else
        {
          mutation(dyad,numWordGroup,word,minSpaceWidth,maxSpaceWidth,wheel,populationSize,fitness,uniqMotif, maxpFactor,maxpMutationRate);
        }
      }
    }

    if((numCycle+1)< lengthList)
    {
      compt++;
    }
    else
    {
      startPWMfound=0;
    }
    numCycle++;


    site=alloc_site_site(numUniq+1,MAX_SITES);
    nsites=alloc_int(numUniq+1);
    pwmnewLen=alloc_int(numUniq+1); // after base extension and trimming
    seqCn=alloc_int(MAX_NUM_SEQ);
    bseqCn=alloc_int(MAX_NUM_SEQ);

    // final step user-specified background model is used
    motifCn2=0; // motifCn per GADEM cycle
    for (ii=0; ii<populationSize; ii++) 
    {

      id=fitness[ii].index;
      if(uniqMotif[ii]=='0')
      {
        continue;
      }


      // approximate the exact llr distribution using Staden's method
      // if(verbose)
      // {
      //   fprintf(output,"Approximate the exact pwm llr score distribution using the pgf method.\n");
      // }
      log_ratio_to_int(epwm[id],ipwm,pwmLen[id],bfreq0);

        // compute score distribution of the (int)PWM using Staden's method
      llrDim=pwm_score_dist(ipwm,pwmLen[id],llrDist,bfreq0);

        //fprintf(output,"Avant ScoreCutoff %d \n",scoreCutoff[id]);
      scoreCutoff[id]=determine_cutoff(llrDist,llrDim,pvalueCutoff);
        //fprintf(output,"Apres ScoreCutoff %d \n",scoreCutoff[id]);
        
      if(fullScan)
      {
        nsites[motifCn2]=scan_llr_pgf(llrDist,llrDim,site[motifCn2],numSeq,oseq,orseq,seqLen,ipwm,pwmLen[id],scoreCutoff[id],bfreq0);
      }
      else
      {
        nsites[motifCn2]=scan_llr_pgf(llrDist,llrDim,site[motifCn2],numSeq,seq,rseq,seqLen,ipwm,pwmLen[id],scoreCutoff[id],bfreq0);
      }
      if (nsites[motifCn2]>=max(2,minsites))
      {
      for (int j=0; j<numSeq; j++) seqCn[j]=0;
        for (int j=0; j<nsites[motifCn2]; j++) seqCn[site[motifCn2][j].seq]++;
        
        for (int j=0; j<4; j++) cn[j]=0;
        for (int j=0; j<numSeq; j++)
        {
          if (seqCn[j]==0) cn[0]++;
          if (seqCn[j]==1) cn[1]++;
          if (seqCn[j]==2) cn[2]++;
          if (seqCn[j]>2)  cn[3]++;
        }
        totalSitesInput=nsites[motifCn2];
        if (extTrim)
        {
          if (fullScan)
          {
            extend_alignment(site[motifCn2],numSeq,oseq,orseq,seqLen,nsites[motifCn2],pwmLen[id],&(pwmnewLen[motifCn2]));
          }
          else
          {
            extend_alignment(site[motifCn2],numSeq,seq,rseq,seqLen,nsites[motifCn2],pwmLen[id],&(pwmnewLen[motifCn2]));
          }
        }
        else
        { 
          pwmnewLen[motifCn2]=pwmLen[id];
        } 

        if (fullScan)
        {
          align_sites_count(site[motifCn2],oseq,orseq,nsites[motifCn2],pwmnewLen[motifCn2],opwm2);
        }
        else
        {
          align_sites_count(site[motifCn2],seq,rseq,nsites[motifCn2],pwmnewLen[motifCn2],opwm2);
        }
        standardize_pwm(opwm2,pwmnewLen[motifCn2]);
        logev=E_value(opwm2,nsites[motifCn2],bfreq0,pwmnewLen[motifCn2],numSeq,seqLen);

        if (logev<=E_valueCutoff)
        {
          consensus_pwm(opwm2,pwmnewLen[motifCn2],pwmConsensus[id]);
          if (fullScan)
          {
            SET_VECTOR_ELT(ResultsGadem,increment,print_result_R(site[motifCn2],nsites[motifCn2],numSeq,oseq,orseq,seqLen,logev,opwm2,pwmnewLen[motifCn2],motifCn+1,sdyad[id],pwmConsensus[id],numCycle,pvalueCutoff,maxpFactor[id],geneID));
            increment++;           
            print_motif(site[motifCn2],nsites[motifCn2],oseq,orseq,seqLen,pwmnewLen[motifCn2],motifCn+1,opwm2);
          }
          else
          {
            SET_VECTOR_ELT(ResultsGadem,increment,print_result_R(site[motifCn2],nsites[motifCn2],numSeq,seq,rseq,seqLen,logev,opwm2,pwmnewLen[motifCn2],
                                                                 motifCn+1,sdyad[id],pwmConsensus[id],numCycle,pvalueCutoff,maxpFactor[id],geneID));
            increment++;
            print_motif(site[motifCn2],nsites[motifCn2],seq,rseq,seqLen,pwmnewLen[motifCn2],motifCn+1,opwm2);
          }

          mask_sites(nsites[motifCn2],seq,rseq,seqLen,site[motifCn2],pwmnewLen[motifCn2]);

          /* ----------------------compute the average number of sites in background sequences ----------------------*/
          avebnsites=0; avebnsiteSeq=0;
          for (i=0; i<numBackgSets; i++)
          {
            simulate_background_seq(bfreq0,numSeq,seqLen,sseq);
            reverse_seq(sseq,rsseq,numSeq,seqLen);

            nsites[motifCn2]=scan_llr_pgf(llrDist,llrDim,site[motifCn2],numSeq,sseq,rsseq,seqLen,ipwm,pwmLen[id],scoreCutoff[id],bfreq0);
            
            for (int j=0; j<numSeq; j++) bseqCn[j]=0;
            for (int j=0; j<nsites[motifCn2]; j++) bseqCn[site[motifCn2][j].seq]++;
            
            for (int j=0; j<4; j++) bcn[j]=0;
            for (int j=0; j<numSeq; j++)
            {
              if (bseqCn[j]==0) bcn[0]++;
              if (bseqCn[j]==1) bcn[1]++;
              if (bseqCn[j]==2) bcn[2]++;
              if (bseqCn[j]>2)  bcn[3]++;
            }
              //ffprintf(output,fq,"background set[%2d] Seqs with 0,1,2,>2 sites: %d %d %d %d\n",i+1,bcn[0],bcn[1],bcn[2],bcn[3]);
            avebnsites+=nsites[motifCn2]; avebnsiteSeq+=(numSeq-bcn[0]);
          } 
          avebnsites/=numBackgSets; avebnsiteSeq/=numBackgSets;
          /* -----------------end compute the average number of sites in background sequences ----------------------*/
          motifCn++; motifCn2++; 

			//if((numCycle+1) > lengthList & fixSeeded)
			//	{	
			//	  numCycleNoMotif=1;
			//		startPWMfound=1;
			//		} else {
					numCycleNoMotif=0;
			//	}

        }
      }
    }
    
    /* for (int i=0; i<motifCn2; i++)
    {
      mask_sites(nsites[i],seq,rseq,seqLen,site[i],pwmnewLen[i]); 
    } */
    
    if (site[0])
    { 
      free(site[0]);
      site[0]=NULL;
    }
    if (site)
    {
      free(site);
      site=NULL;
    }
    if (nsites)
    {
      free(nsites);
      nsites=NULL;
    }
    if (pwmnewLen) 
    {
      free(pwmnewLen);
      pwmnewLen=NULL;
    }
    
    if (motifCn2==0)
      numCycleNoMotif++;   
    if (motifCn==nmotifs)
      {
	fprintf(output,"Maximal number of motifs (%d) reached\n",nmotifs);
	break;
      }
    if (numCycleNoMotif==stopCriterion)
      noMotifFound=1;
  }while (!noMotifFound);
  
  
    // fclose(fp);
  /*if (!startPWMfound) {  
   if (dyad[0])      { free(dyad[0]);         dyad[0]=NULL;    }
   if (dyad)         { free(dyad);            dyad=NULL;       }
   }*/
  if (seqLen)
  { 
    free(seqLen);
    seqLen=NULL;
  }
  if (pwm[0][0])       
  {
    free(pwm[0][0]);
    pwm[0][0]=NULL; 
  }
  if (pwm[0])
  { 
    free(pwm[0]);
    pwm[0]=NULL;     
  }
  if (pwm)             
  {
    free(pwm); 
    pwm=NULL;        
  }
  if (opwm2[0])  
  { 
    free(opwm2[0]); 
    opwm2[0]=NULL;
  }
  if (opwm2)     
  {
    free(opwm2); 
    opwm2=NULL;
  }
  if (opwm[0][0])      
  { 
    free(opwm[0][0]);
    opwm[0][0]=NULL;
  }
  if (opwm[0])    
  {
    free(opwm[0]);
    opwm[0]=NULL;
  }
  if (opwm)       
  {
    free(opwm);
    opwm=NULL;
  }
  if(ipwm[0])
  { 
    free(ipwm[0]);     
    ipwm[0]=NULL;  
  }
  if (ipwm)
  {
    free(ipwm);   
    ipwm=NULL;
  }
  if (pwmLen)   
  { 
    free(pwmLen);    
    pwmLen=NULL; 
  }
  if (seq[0])          { free(seq[0]);          seq[0]=NULL;     }
  if (seq)             { free(seq);             seq=NULL;        }
    //  if (rseq[0])         { free(rseq[0]);         rseq[0]=NULL;    }
    // if (rseq)            { free(rseq);            rseq=NULL;       }
    // if (oseq[0])         { free(oseq[0]);         oseq[0]=NULL;    }
    // if (oseq)            { free(oseq);            oseq=NULL;       }
    // if (orseq[0])        { free(orseq[0]);        orseq[0]=NULL;   }
    // if (orseq)           { free(orseq);           orseq=NULL;      }
  if (bfreq1)    
  { 
    free(bfreq1);    
    bfreq1=NULL;  
  }
  if (bfreq0)
  {
    free(bfreq0);
    bfreq0=NULL;
  }

  if (wheel)    
  { 
    free(wheel);    
    wheel=NULL;    
  }
  if (fitness)    
  { 
    free(fitness); 
    fitness=NULL;
  }
  if (mFileName)  
  { 
    free(mFileName);    
    mFileName=NULL; 
  }
  if (oFileName)    
  { 
    free(oFileName);  
    oFileName=NULL;
  }
  if (pwmFileName)    
  {
    free(pwmFileName);
    pwmFileName=NULL;
  }
  if (sdyad[0]) 
  { 
    free(sdyad[0]); 
    sdyad[0]=NULL;
  }
  if (sdyad)    
  {
    free(sdyad);
    sdyad=NULL;
  }
  if (pwmConsensus[0])
  { 
    free(pwmConsensus[0]);
    pwmConsensus[0]=NULL;
  }
  if (pwmConsensus)   
  {
    free(pwmConsensus);
    pwmConsensus=NULL;
  }
  //if (!startPWMfound && word) destroy_word(word,numWordGroup);

  PutRNGstate();
  UNPROTECT(1);
  return(ResultsGadem);
}
Esempio n. 13
0
static void analyze_clusters(int nf, t_clusters *clust, real **rmsd,
			     int natom, t_atoms *atoms, rvec *xtps, 
			     real *mass, rvec **xx, real *time,
			     int ifsize, atom_id *fitidx,
			     int iosize, atom_id *outidx,
			     char *trxfn, char *sizefn, char *transfn, 
			     char *ntransfn, char *clustidfn, bool bAverage, 
			     int write_ncl, int write_nst, real rmsmin,bool bFit,
			     FILE *log,t_rgb rlo,t_rgb rhi)
{
  FILE *fp=NULL;
  char buf[STRLEN],buf1[40],buf2[40],buf3[40],*trxsfn;
  int  trxout=0,trxsout=0;
  int  i,i1,cl,nstr,*structure,first=0,midstr;
  bool *bWrite=NULL;
  real r,clrmsd,midrmsd;
  rvec *xav=NULL;
  matrix zerobox;
  
  clear_mat(zerobox);
  
  ffprintf1(stderr,log,buf,"\nFound %d clusters\n\n",clust->ncl);
  trxsfn=NULL;
  if (trxfn) {
    /* do we write all structures? */
    if (write_ncl) {
      trxsfn = parse_filename(trxfn, max(write_ncl,clust->ncl));
      snew(bWrite,nf);
    }
    ffprintf2(stderr,log,buf,"Writing %s structure for each cluster to %s\n",
	      bAverage ? "average" : "middle", trxfn);
    if (write_ncl) {
      /* find out what we want to tell the user:
	 Writing [all structures|structures with rmsd > %g] for 
	 {all|first %d} clusters {with more than %d structures} to %s     */
      if (rmsmin>0.0)
	sprintf(buf1,"structures with rmsd > %g",rmsmin);
      else
	sprintf(buf1,"all structures");
      buf2[0]=buf3[0]='\0';
      if (write_ncl>=clust->ncl) {
	if (write_nst==0)
	  sprintf(buf2,"all ");
      } else
	sprintf(buf2,"the first %d ",write_ncl);
      if (write_nst)
	sprintf(buf3," with more than %d structures",write_nst);
      sprintf(buf,"Writing %s for %sclusters%s to %s\n",buf1,buf2,buf3,trxsfn);
      ffprintf(stderr,log,buf);
    }
  
    /* Prepare a reference structure for the orientation of the clusters  */
    if (bFit)
    reset_x(ifsize,fitidx,natom,NULL,xtps,mass);
    trxout = open_trx(trxfn,"w");
    /* Calculate the average structure in each cluster,               *
     * all structures are fitted to the first struture of the cluster */
    snew(xav,natom);
  }
  
  if (transfn || ntransfn) 
    ana_trans(clust, nf, transfn, ntransfn, log,rlo,rhi);
  
  if (clustidfn) {
    fp=xvgropen(clustidfn,"Clusters",xvgr_tlabel(),"Cluster #");
    fprintf(fp,"@    s0 symbol 2\n");
    fprintf(fp,"@    s0 symbol size 0.2\n");
    fprintf(fp,"@    s0 linestyle 0\n");
    for(i=0; i<nf; i++)
      fprintf(fp,"%8g %8d\n",time[i],clust->cl[i]);
    ffclose(fp);
  }
  if (sizefn) {
    fp=xvgropen(sizefn,"Cluster Sizes","Cluster #","# Structures");
    fprintf(fp,"@g%d type %s\n",0,"bar");
  }
  snew(structure,nf);
  fprintf(log,"\n%3s | %3s %4s | %6s %4s | cluster members\n",
	  "cl.","#st","rmsd","middle","rmsd");
  for(cl=1; cl<=clust->ncl; cl++) {
    /* prepare structures (fit, middle, average) */
    if (xav)
      for(i=0; i<natom;i++)
	clear_rvec(xav[i]);
    nstr=0;
    for(i1=0; i1<nf; i1++)
      if (clust->cl[i1] == cl) {
	structure[nstr] = i1;
	nstr++;
	if (trxfn && (bAverage || write_ncl) ) {
	  if (bFit)
	  reset_x(ifsize,fitidx,natom,NULL,xx[i1],mass);
	  if (nstr == 1)
	    first = i1;
	  else if (bFit)
	    do_fit(natom,mass,xx[first],xx[i1]);
	  if (xav)
	    for(i=0; i<natom; i++)
	      rvec_inc(xav[i],xx[i1][i]);
	}
      }
    if (sizefn)
      fprintf(fp,"%8d %8d\n",cl,nstr);
    clrmsd = 0;
    midstr = 0;
    midrmsd = 10000;
    for(i1=0; i1<nstr; i1++) {
      r = 0;
      if (nstr > 1) {
	for(i=0; i<nstr; i++)
	  if (i < i1)
	    r += rmsd[structure[i]][structure[i1]];
	  else
	    r += rmsd[structure[i1]][structure[i]];
	r /= (nstr - 1);
      }
      if ( r < midrmsd ) {
	midstr = structure[i1];
	midrmsd = r;
      }
      clrmsd += r;
    }
    clrmsd /= nstr;
    
    /* dump cluster info to logfile */
    if (nstr > 1) {
      sprintf(buf1,"%5.3f",clrmsd);
      if (buf1[0] == '0')
	buf1[0] = ' ';
      sprintf(buf2,"%5.3f",midrmsd);
      if (buf2[0] == '0')
	buf2[0] = ' ';
    } else {
      sprintf(buf1,"%5s","");
      sprintf(buf2,"%5s","");
    }
    fprintf(log,"%3d | %3d%s | %6g%s |",cl,nstr,buf1,time[midstr],buf2);
    for(i=0; i<nstr; i++) {
      if ((i % 7 == 0) && i)
	sprintf(buf,"\n%3s | %3s %4s | %6s %4s |","","","","","");
      else
	buf[0] = '\0';
      i1 = structure[i];
      fprintf(log,"%s %6g",buf,time[i1]);
    }
    fprintf(log,"\n");
    
    /* write structures to trajectory file(s) */
    if (trxfn) {
      if (write_ncl)
	for(i=0; i<nstr; i++)
	  bWrite[i]=FALSE;
      if ( cl < write_ncl+1 && nstr > write_nst ) {
	/* Dump all structures for this cluster */
	/* generate numbered filename (there is a %d in trxfn!) */
	sprintf(buf,trxsfn,cl);
	trxsout = open_trx(buf,"w");
	for(i=0; i<nstr; i++) {
	  bWrite[i] = TRUE;
	  if (rmsmin>0.0)
	    for(i1=0; i1<i && bWrite[i]; i1++)
	      if (bWrite[i1])
		bWrite[i] = rmsd[structure[i1]][structure[i]] > rmsmin;
	  if (bWrite[i])
	    write_trx(trxsout,iosize,outidx,atoms,i,time[structure[i]],zerobox,
		      xx[structure[i]],NULL);
	}
	close_trx(trxsout);
      } 
      /* Dump the average structure for this cluster */
      if (bAverage) {
	for(i=0; i<natom; i++)
	  svmul(1.0/nstr,xav[i],xav[i]);
      } else {
	for(i=0; i<natom; i++)
	  copy_rvec(xx[midstr][i],xav[i]);
	if (bFit)
	reset_x(ifsize,fitidx,natom,NULL,xav,mass);
      }
      if (bFit)
	do_fit(natom,mass,xtps,xav);
      r = cl;
      write_trx(trxout,iosize,outidx,atoms,cl,time[midstr],zerobox,xav,NULL);
    }
  }
  /* clean up */
  if (trxfn) {
    close_trx(trxout);
    sfree(xav);
    if (write_ncl)
      sfree(bWrite);
  }
  sfree(structure);
  if (trxsfn)
    sfree(trxsfn);
}
Esempio n. 14
0
/**********************************************************************
* create an argument list from a command line; the original line is
* undamaged by this function, and the values returned in the pargv
* are all malloc'd and should be free'd with freemyargs()
**********************************************************************/
int
#ifndef ANSI_PROTOTYPES
parsemyargs(comm, pargv, pmaxargc, gargc, gargv)
    char *comm;
    char **pargv[];
    int *pmaxargc;
    int gargc;
    char *gargv[];
#else /* ANSI_PROTOTYPES */
parsemyargs(char *comm, char ***pargv, int *pmaxargc, int gargc, char **gargv)
#endif /* ANSI_PROTOTYPES */
{
    int argc, i, j, rdsp, inquotes;
    char *buff, *magic;

    if ((buff = strdup(comm)) == (char*)0)
    {
	ffprintf(STDERR, "??out of memory in parsemyargs()\n");
	return 0;
    }

    /* count words -- ok, I know this can be done better, but I'm lazy */
    for (i = 0, argc = 0, rdsp = 1, inquotes = 0; buff[i]; i++)
    {
	/* buff[i] != 0 => next test can only succeed if inquotes != 0 */
	if (inquotes == '\\' || buff[i] == inquotes)
	{
	    inquotes = 0;
	    rdsp = 0;
	}
	else if (inquotes)
	{
	    ;
	}
	else if (isspace(buff[i]))
	{
	    if (!rdsp)
		buff[i] = '\0';
	    while (isspace(buff[i+1]))
		i++;
	    rdsp = 1;
	}
	else
	{
	    if (rdsp)
	    {
		/* check if we are starting a comment */
		if (buff[i] == '#')
		    break;

		/***********************************************************
		* if we have a single character command, pretend we have
		* spaces after it, even if we don't (true for first word only)
		***********************************************************/
		rdsp = (argc == 0)
		       && (buff[i] == '!' || buff[i] == '@');

		argc++;

		/***********************************************************
		* if the first character in the word is an unquoted `|', then
		* consider that as the beginning of the final word (which
		* will take the rest of the line)
		***********************************************************/
		if (buff[i] == '|')
		    break;
	    }

	    if (buff[i] == '"' || buff[i] == '\'' || buff[i] == '\\')
		inquotes = buff[i];
	}
    }

    if (inquotes)
    {
	ffprintf(STDERR, "?unclosed quotes\n");
	(void)free(buff);
	return 0;
    }

    if (argc > *pmaxargc - 1)
    {
	*pmaxargc = argc + 1;

	if (*pargv == (char**)0)
	    *pargv = (char**)malloc(*pmaxargc * sizeof(char*));
	else
	    *pargv = (char**)realloc((char*)*pargv, *pmaxargc * sizeof(char*));

	if (*pargv == (char**)0)
	{
	    ffprintf(STDERR, "??out of memory in parsemyargs()\n");
	    *pmaxargc = 0;
	    return 0;
	}
    }

    if ((magic = strdup(comm)) == (char*)0)
    {
	ffprintf(STDERR, "??out of memory in parsemyargs() for magic\n");
	(void)free(buff);
	*pmaxargc = 0;
	return 0;
    }

    /* set up *pargv[] */
    for (i = 0, j = 0; i < argc; i++, j++)
    {
	int k, d;
	
	while (isspace(buff[j]))
	    j++;

	if (i == 0 && (buff[j] == '!' || buff[j] == '@'))
	    switch (buff[j])
	    {
	      case '!':
		(*pargv)[i] = "shell";
		break;
	      case '@':
		(*pargv)[i] = BUILTIN;
		break;
	      default:
		(*pargv)[i] = "error";
		break;
	    }
	else if (i == argc - 1 && buff[j] == '|')
	    (*pargv)[i] = buff + j;
	else
	{
	    (*pargv)[i] = buff + j;
	    k = j;
	    while (buff[j])
		j++;

	    /***********************************************************
	    * at this point, (*pargv)[i] to buff+j is the word, still with
	    * quote characters in it; at this point we remove the quotes
	    * we mark magic characters (non-8bit clean) with a 0x80 top
	    * bit; outside of quotes, ?*{[$\ are all magic; inside `"'s
	    * just $\ are magic; inside "'"s nothing is magic; a magic \
	    * turns off the magic ability of the next character
	    ***********************************************************/
	    for (inquotes = 0, d = k; k <= j; k++)
		if ((inquotes & 0x7f) == '\\')
		{
		    inquotes = (inquotes & ISMAGIC)? '"': 0;
		    buff[d]    = buff[k];
		    magic[d++] = ISPROT;
		}
		else if (inquotes)
		{
		    if (inquotes == '"' && buff[k] == '\\')
			inquotes |= ISMAGIC;
		    else
		    {
			if (buff[k] != inquotes)
			{
			    buff[d]    = buff[k];
			    magic[d++] = (inquotes != '\''
					  && buff[k] == '$')? ISMAGIC: 0;
			}
			else
			    inquotes = 0;
		    }
		}
		else if (buff[k] == '"' || buff[k] == '\'' || buff[k] == '\\')
		    inquotes = buff[k];
		else
		{
		    buff[d]    = buff[k];
		    magic[d++] = (inquotes != '\'' &&
				    (buff[k] == '?' || buff[k] == '*' ||
				     buff[k] == '{' || buff[k] == '[' ||
				     buff[k] == '$'))? ISMAGIC: 0;
		}
	}
    }

    for (i = 0; i < argc; i++)
	(*pargv)[i] = strdup((*pargv)[i]);

    (*pargv)[argc] = (char*)0;
    (void)free(buff);
    (void)free(magic);

    return argc;
}