/********************************************************* * * * Main Function * * ------------- * * * ********************************************************* | Takes arguments and launches the gof simulations. | *-------------------------------------------------------*/ int main(int argc, char *argv[]) // Array of char=arguments line { //--- Declarations & Call Function ---// int i=0, j=0, k=0, count=0, howmany=0, segsites=0, okim=0, oksim=0, numsim=0, totsim=0, nokl=0, nokl0=0; int **statseg=NULL, **nbvariant=NULL, oks[3], okstot[3]; FILE *pf=NULL, *fopen(const char*, const char*); // Pointer on File for outputs (pf) and IM input file double tajd(); char **list=NULL; // Haplotype list void updatemainparams(struct params*); int gensam(struct params*, char**, int**, int*); int **imatrix(int, int); //// From rand1.c //// /* Celine changed 03/18/2010 */ void seedit(char*, FILE*, struct params *);/*/////*/ char **cmatrix(int, int); //// From params.c //// void changeparams(struct params*); void changeparamslocus(struct params*, int); struct params getpars(int, char*[], int*); //--- Structure declaration---// struct params param; //--- Get arguments ---// param=getpars(argc, argv, &howmany); // Get input by user for parameters pf=stdout; // Output /* Celine changed 03/18/2010 */ if( !param.commandlineseedflag ) seedit("s", pf, ¶m);// WRITE seeds in summary output file /*/////*/ /* Uncommented for Celine's use */ /* for(i=0;i<argc;i++) // Information on simulation fprintf(pf, "%s ", argv[i]); */////\ //---------- Initialisation & Memory allocation ------------------// nbvariant=imatrix(param.cp.npop+1, maxsites); // array of nb of frequency spectrum typeseg=(int*)malloc((unsigned)(maxsites*sizeof(int))); // type of sites statseg=imatrix(param.cp.npop+3, howmany); // Records locus specific S1, S2, Ss, Sf, changeparams(¶m); // Change estimates parameters from priors updatemainparams(¶m); // Update parameters for the coalescent oksim=totsim=okstot[0]=okstot[1]=okstot[1]=okstot[2]=0; // simulation check, total #of sim, check on statistics for all simulations for(numsim=0; numsim<param.cp.nsim;numsim++) // Loop along number of simulations for this set of parameters { //--- Initialization and reset of quality checks ---// count=nokl=nokl0=okim=oks[0]=oks[1]=oks[1]=oks[2]=0; // number of loci, # loci with ok genealogies, no set sites, #statistics ok for(i=0;i<11;i++) // Sim specific Stats param.cp.sSiFst[i]=0; //--- Loop along the loci in the simulation ---// while((howmany-count++)) { if(okim==0) // Case All loci ok in the sample { for(i=0;i<11;i++) { param.cp.lSiFst[i]=0.0; if(i<9) param.lp[count-1].tpH[i]=0; } changeparamslocus(¶m, count-1); // Get locus specific parameters list=cmatrix(param.cp.nsam, maxsites+1); // Allocate list of haplotypes segsites=gensam(¶m, list, nbvariant, param.lp[count-1].S);// Generate a new gene ARG statseg[0][count-1]=segsites; // Total number of seg sites in sample for(i=1;i<3+param.cp.npop;i++) statseg[i][count-1]=0; if((segsites>0)) // Case segsite>0: get stats { /* fprintf(pf, "segsites:%d\npositions:\n",segsites); */ /* for(i=0;i<param.cp.nsam;i++) fprintf(pf, "%s\n", list[i]); */ /* fprintf(pf, "\n"); */ if(segsites<=param.cp.nsites) // Case segsite < lenght of locus { for(k=0;k<param.cp.nsam;k++) { for(i=k+1;i<param.cp.nsam;i++) { if((k<param.lp[count-1].ni[1])&&(i<param.lp[count-1].ni[1])) // pop1 { param.lp[count-1].tpH[0]++; // # chromosomes for(j=0;j<segsites;j++) { if(list[k][j]!=list[i][j]) param.lp[count-1].tpH[1]++; // # seg sites } } else if((k>=param.lp[count-1].ni[1])&&(i>=param.lp[count-1].ni[1])) // pop2 { param.lp[count-1].tpH[2]++; // # chromosomes for(j=0;j<segsites;j++) { if(list[k][j]!=list[i][j]) param.lp[count-1].tpH[3]++; // # seg sites } } else // total sample { param.lp[count-1].tpH[4]++;; // Totsal sample size for(j=0;j<segsites;j++) { if(list[k][j]!=list[i][j]) param.lp[count-1].tpH[5]++; // total S } } }// Loop on chromosome }// Loop along all sampled sequence for the locus for(i=0;i<segsites;i++) // Calulate S statistics for the locus { if(typeseg[i]<0) statseg[3][count-1]++; // shared else if(typeseg[i]<param.cp.npop+1) statseg[typeseg[i]][count-1]++; // population specific else statseg[4][count-1]++; // fixed } for(i=1;i<5;i++) //--- Record S1 S2 Ss Sf forthe locus ---// param.cp.lSiFst[i-1]+=statseg[i][count-1]; for(i=0;i<param.cp.nsam;i++) // Free memory for this locus free(list[i]); free(list); }// End case segsite<lenght of locus else // Case segsites>lenght of locus { okim=1; // Sample have a wrong locus oksim=1; // stop this simulation break; } }// End locus polymorphic else // Locus without seg sites { okim=1; // Sample have a wrong locus (S=0) oksim=2; // 0 for all stats } }// End Sample good until now if(okim==0) // All loci good until now { nokl++; // +1 good locus nokl0++; // +1 polymorphic locus for(i=0;i<7;i++) { if(i<4) param.cp.sSiFst[i]+=param.cp.lSiFst[i]; // sum of Sk param.lp[count-1].H[i]=param.lp[count-1].tpH[i]; // locus specific stats } param.cp.lSiFst[5]=param.lp[count-1].H[1]/=param.lp[count-1].H[0]; // Hw1 param.cp.lSiFst[6]=param.lp[count-1].H[3]/=param.lp[count-1].H[2]; // Hw2 param.lp[count-1].H[5]/=param.lp[count-1].H[4]; // Hb param.cp.lSiFst[4]=param.lp[count-1].H[6]=1-((param.lp[count-1].H[1]+param.lp[count-1].H[3])/2)/ param.lp[count-1].H[5];// locis specific Fst if((param.lp[count-1].S[0]>0)&&(param.lp[count-1].ni[1]>2)) // Locus popymorphic in pop1 { param.cp.lSiFst[7]=tajd(param.lp[count-1].ni[1], param.lp[count-1].S[0], param.lp[count-1].H[1]); param.cp.sSiFst[7]+=param.cp.lSiFst[7]; oks[0]++; // +1 good stat for pop1 } if((param.lp[count-1].S[1]>0)&&(param.lp[count-1].ni[2]>2)) // Locus popymorphic in pop2 { param.cp.lSiFst[8]=tajd(param.lp[count-1].ni[2], param.lp[count-1].S[1], param.lp[count-1].H[3]); param.cp.sSiFst[8]+=param.cp.lSiFst[8]; oks[1]++; // +1 good stat for pop2 } if(statseg[1][count-1]>0) // Locus popymorphic private in pop1 param.lp[count-1].H[7]=param.cp.lSiFst[9]+=(double) param.lp[count-1].S[2]/(statseg[1][count-1]*param.lp[count-1].ni[1]*2); // p(1) if(statseg[2][count-1]>0) // Locus popymorphic private in pop2 param.lp[count-1].H[7]=param.cp.lSiFst[9]+=(double) param.lp[count-1].S[3]/(statseg[2][count-1]*param.lp[count-1].ni[2]*2); // p(1) param.cp.sSiFst[9]+=param.cp.lSiFst[9]; // sum p1 if(statseg[3][count-1]>0) // Locus popymorphic private in pop2 { param.lp[count-1].H[8]=param.cp.lSiFst[10]=(double) param.lp[count-1].S[4]/(statseg[3][count-1]*(param.lp[count-1].ni[2]+param.lp[count-1].ni[1])); // p(2) param.cp.sSiFst[10]+=param.cp.lSiFst[10]; // sum p2 oks[2]++; } param.cp.sSiFst[4]+=param.lp[count-1].H[6]; // sum Fst param.cp.sSiFst[5]+=param.lp[count-1].H[1]; // sum Hw1 param.cp.sSiFst[6]+=param.lp[count-1].H[3]; // sum Hw2 } else if(oksim==2) // Case no seg site for that locus { oksim=0; // reset checks okim=0; nokl++; // 1+ locus to count in mean (all 0 values) for(i=0;i<9;i++) param.lp[count-1].H[i]=param.lp[count-1].tpH[i]; // locus specific stats } }// End loop on loci if(nokl==howmany) // All sample good { totsim++; // 1+ good simulation for(i=0;i<4;i++) param.cp.SiFst[i]+=param.cp.sSiFst[i]; // sum of S stats along simulations for(i=4;i<11;i++) { if(((i<7)||(i>=9))&&(nokl0>0)) param.cp.SiFst[i]+=(double)param.cp.sSiFst[i]/nokl0; // mean of other stats along simulations if((i>=7)&&(i<9)&&(oks[i-7]>0)) { param.cp.SiFst[i]+=(double)param.cp.sSiFst[i]/oks[i-7]; okstot[i-7]++; } } } }// End loop on simulations if(oksim==0) // All simulations worked { /* Uncommented for Celine's use */ /* for(i=0;i<11;i++) *///// for(i=0;i<9;i++) { if((i<7)||(i>=9)) fprintf(pf, "%lg\t", (double) param.cp.SiFst[i]/(totsim)); // write mean of sum of S stats, Fst and Hws over simulations if((i>=7)&&(i<9)) { if(oks[i-7]>0) fprintf(pf, "%lg\t", (double) param.cp.SiFst[i]/(okstot[i-7])); // write mean Tds if S>0 in pops else fprintf(pf, "NA\t" ); } } fprintf(pf, "\n"); } else // Case one locus with too much seg sites { for(i=0;i<9;i++) fprintf(pf, "NA\t" ); fprintf(pf, "\n"); } /* Celine changed 03/18/2010 */ seedit("end", pf, ¶m); // in randx.c, flag[0]!="s" so create/rewrite seed in seedmimar /*/////*/ fclose(pf); free(typeseg); for(i=0;i<param.cp.npop+3;i++) { if(i<param.cp.npop+1) free(nbvariant[i]); free(statseg[i]); } free(nbvariant); free(statseg); ///////// FREE PARAM /////// for(i=0;i<param.cp.npop;i++) free(param.cp.mig_mat[i]); free(param.cp.mig_mat); free(param.cp.config); /* Celine changed 11/27/2009 */ for(i=9;i>=0;i--) if(param.cp.listevent[i]!=NULL && param.cp.listevent[i]->nextde!=NULL) free(param.cp.listevent[i]->nextde); if(param.cp.listevent!=NULL) free(param.cp.listevent); /*/////*/ free(param.cp.deventlist); free(param.cp.size); free(param.cp.alphag); for(i=0;i<3;i++) free(param.cp.uniform[i]); free(param.cp.uniform); free(param.cp.oldest); free(param.cp.newest); free(param.cp.newparam); /* Celine changed 11/27/2009 */ for(i=0;i<howmany;i++) free(param.lp[i].name); /*/////*/ free(param.lp); /* Celine changed 03/18/2010 */ free( param.tableseeds); /*/////*/ exit(0); }// End main function
int main(int argc, char *argv[]) { int i, k, howmany, segsites ; char **list, **cmatrix(), **tbsparamstrs ; FILE *pf, *fopen() ; double probss, tmrca, ttot ; void seedit( const char * ) ; void getpars( int argc, char *argv[], int *howmany ) ; int gensam( char **list, double *probss, double *ptmrca, double *pttot ) ; ntbs = 0 ; /* these next few lines are for reading in parameters from a file (for each sample) */ tbsparamstrs = (char **)malloc( argc*sizeof(char *) ) ; for( i=0; i<argc; i++) printf("%s ",argv[i]); for( i =0; i<argc; i++) tbsparamstrs[i] = (char *)malloc(30*sizeof(char) ) ; for( i = 1; i<argc ; i++) if( strcmp( argv[i],"tbs") == 0 ) argv[i] = tbsparamstrs[ ntbs++] ; count=0; if( ntbs > 0 ) for( k=0; k<ntbs; k++) scanf(" %s", tbsparamstrs[k] ); getpars( argc, argv, &howmany) ; /* results are stored in global variable, pars */ if( !pars.commandlineseedflag ) seedit( "s"); pf = stdout ; if( pars.mp.segsitesin == 0 ) { list = cmatrix(pars.cp.nsam,maxsites+1); posit = (double *)malloc( (unsigned)( maxsites*sizeof( double)) ) ; } else { list = cmatrix(pars.cp.nsam, pars.mp.segsitesin+1 ) ; posit = (double *)malloc( (unsigned)( pars.mp.segsitesin*sizeof( double)) ) ; if( pars.mp.theta > 0.0 ) { segfac = 1.0 ; for( i= pars.mp.segsitesin; i > 1; i--) segfac *= i ; } } while( howmany-count++ ) { if( (ntbs > 0) && (count >1 ) ) { for( k=0; k<ntbs; k++) { if( scanf(" %s", tbsparamstrs[k]) == EOF ) { if( !pars.commandlineseedflag ) seedit( "end" ); exit(0); } } getpars( argc, argv, &howmany) ; } fprintf(pf,"\n//"); if( ntbs >0 ) { for(k=0; k< ntbs; k++) printf("\t%s", tbsparamstrs[k] ) ; } printf("\n"); segsites = gensam( list, &probss, &tmrca, &ttot ) ; if( pars.mp.timeflag ) fprintf(pf,"time:\t%lf\t%lf\n",tmrca, ttot ) ; if( (segsites > 0 ) || ( pars.mp.theta > 0.0 ) ) { if( (pars.mp.segsitesin > 0 ) && ( pars.mp.theta > 0.0 )) fprintf(pf,"prob: %g\n", probss ) ; fprintf(pf,"segsites: %d\n",segsites); if( segsites > 0 ) fprintf(pf,"positions: "); for( i=0; i<segsites; i++) fprintf(pf,"%6.*lf ", pars.output_precision,posit[i] ); fprintf(pf,"\n"); if( segsites > 0 ) for(i=0; i<pars.cp.nsam; i++) { fprintf(pf,"%s\n", list[i] ); } } } if( !pars.commandlineseedflag ) seedit( "end" ); }