Ejemplo n.º 1
0
/* BEGIN DEFUNCT */
obreak()
{
	struct a {
		char	*nsiz;
	};
	register size_t n, d, ds;

	/*
	 * set n to new data size
	 */
	n = btoc(((struct a *)u.u_ap)->nsiz) - ctos(u.u_tsize) * stoc(1);
	if (n < 0)
		n = 0;
	/*
	 * since we can't pass a -ve argument for the difference to chksize,
	 * if d is negative, make ds equal to the final value and clear d.
	 * keep the real difference in n for later use in expand.
	 */
	ds = u.u_dsize;
	if ((n = d = clrnd(n - u.u_dsize)) < 0) {
		ds += d;
		d = 0;
	}
	if (ctob(ds + d) > u.u_rlimit[RLIMIT_DATA].rlim_cur) {
		u.u_error = ENOMEM;
		return;
	}
	if (chksize((u_int)u.u_tsize, (u_int)ds, (u_int)d, (u_int)u.u_ssize))
		return;
	if (swpexpand(ds + d, u.u_ssize, &u.u_dmap, &u.u_smap) == 0)
		return;
	expand((int)n, 0);
}
Ejemplo n.º 2
0
Archivo: rsgi.c Proyecto: sprax/coldz
void *readSGI(IMAGE *image, unsigned order, unsigned type)
{
int	xsize = image->xsize;
int	ysize = image->ysize;
int	zsize = image->zsize;
size_t	count = xsize*ysize*zsize;
int	y,z;

  switch (type) {
    case MM_USH: {
      ush *mem;
      mallocAss(mem,ush,count);
      for (z=0; z < zsize; z++) {
        for (y=0; y < ysize; y++) { 
          getrow(image,rowbuf,y,z);
          memcpy((void*)(mem+y*xsize),(void*)rowbuf,sizeof(short)*xsize);
        }
      }
      return mem;
    }
    case MM_CHR:
    case MM_UNC: {
      unc *mem;
      mallocAss(mem,unc,count);
      for (z=0; z < zsize; z++) {
        for (y=0; y < ysize; y++) { 
          getrow(image,rowbuf,y,z);
          stoc(rowbuf,charbuf,xsize);
          memcpy((void*)(mem+y*xsize),(void*)charbuf,sizeof(unc)*xsize);
        }
      }
      return mem;
    }
    default: die("readSGI: NOIMP for type %d",type); return NULL;
  }
}
Ejemplo n.º 3
0
int run(vector<string> com, int inorout, int num, int max, int **pifd, int fileInOut, int filein, int fileout)
{
    if(DEBUG){

        cout << "com size: " <<  com.size() << endl;
        cout << starOfCom(com) << endl;
    }


    if(com[0] == "ls" || com[0] == "grep")
    {
        com.push_back("--color=auto");
    }


    int status = 0;

    int star = starOfCom(com);

    //char * array[] = {stoc(com[0]), NULL};



    int issub = fork();
    if(issub == 0){

        //signal(SIGUSR1, sigusr1_handler);
        /*
        if(iB == 1)
        {
            close(1);
            close(0);
            setpgrp();
        }
        */
        setpgrp();

        if(inorout == PIPE_IN)
        {
            for(int i = 0; i < max; i++)
            {
                if(DEBUG)
                    cout << com[0] << " in!!! max: " << max << " i : " << i << endl;

                if(i != num)
                {
                    if(DEBUG)
                        cout << ">" << com[0] << " close " << i << endl;
                    close(pifd[i][0]);
                    close(pifd[i][1]);
                }
            }
            close(pifd[num][0]);
            dup2(pifd[num][1], 1);
            close(pifd[num][1]);
        }
        else if(inorout == PIPE_IAO)
        {
            for(int i = 0; i < max; i++)
            {
                if(DEBUG)
                    cout << com[0] << " in!!! max: " << max << " i : " << i << endl;
                if(i != num && i != num+1)
                {
                    if(DEBUG)
                        cout << com[0] << " close " << i << endl;
                    close(pifd[i][0]);
                    close(pifd[i][1]);
                }
            }
            close(pifd[num][1]);
            dup2(pifd[num][0], 0);
            close(pifd[num][0]);

            close(pifd[num+1][0]);
            dup2(pifd[num+1][1], 1);
            close(pifd[num+1][1]);
        }
        else if(inorout == PIPE_OUT)
        {
            for(int i = 0; i < max; i++)
            {
                if(DEBUG)
                    cout << com[0] << " in!!! max: " << max << " i : " << i << endl;
                if(i != num)
                {
                    if(DEBUG)
                        cout << ">" << com[0] << " close " << i << endl;
                    close(pifd[i][0]);
                    close(pifd[i][1]);
                }
            }
            close(pifd[num][1]);
            dup2(pifd[num][0], 0);
            close(pifd[num][0]);
        }


        if(fileInOut == F_IN)
            dup2(filein, 0);
        else if(fileInOut == F_OUT || fileInOut == F_APP)
            dup2(fileout, 1);
        else if(fileInOut == F_IN_AND_OUT || fileInOut == F_IN_AND_APP)
        {
            dup2(filein, 0);
            dup2(fileout, 1);
        }



        if(star == 0)
        {
            char ** arg = (char **)malloc((com.size()+ 1) * sizeof(char *));
            for(int i = 0; i < com.size(); i++){
                arg[i] = stoc(com[i]);
            }
            arg[com.size()] = NULL;

            execvp(com[0].c_str(), arg);

            cout << com[0] << ": command not found" << endl;
            for(int i = 0; i <= com.size(); i++)
            {
                free(arg[i]);
            }
            free(arg);
            exit(-1);
        }
        else {
            glob_t globbuf;

            globbuf.gl_offs = com.size() - star;

            int comnum = 0;
            bool isNumOne = 1;
            for(int i = 0; i < com.size(); i++){
                if(com[i].find('*') != string::npos){
                    if(isNumOne){
                        glob(com[i].c_str(), GLOB_DOOFFS, NULL, &globbuf);
                        isNumOne = 0;
                    }
                    else
                        glob(com[i].c_str(), GLOB_DOOFFS | GLOB_APPEND,
                                NULL, &globbuf);
                }
            }

            for(int i = 0; i < com.size(); i++){
                if(com[i].find('*') == string::npos){
                    globbuf.gl_pathv[comnum] = stoc(com[i]);
                    comnum ++;
                }
            }

            execvp(com[0].c_str(), &globbuf.gl_pathv[0]);
            cout << com[0] << ": command not found" << endl;
            for(int i = 0; i < globbuf.gl_offs; i++)
            {
                free(globbuf.gl_pathv[i]);
            }
            exit(-1);

        }
    }
    else {
        if(inorout == PIPE_NONE && iB != 1)
        {
            needSig = 1;
            nowcpid = issub;

            waitpid(issub, &status, WUNTRACED);
            if(WIFSTOPPED(status))
            {
                if(DEBUG)
                    cout << "stop!" << endl;

                JOB j;
                j.pid = issub;
                j.stat = 1;
                j.com = jobname;
                vecJ.push_back(j);

            }

        }

        if(iB == 1)
        {
            JOB j;
            j.pid = issub;
            j.stat = 2;
            j.com = jobname;
            vecJ.push_back(j);
        }
    }
    if(DEBUG)
        cout << "end " << com[0] << endl;

    return status;
}
Ejemplo n.º 4
0
int runpipe(vector<vector<string> > com)
{
    bool hasInFile = false;
    string inFile = "";
    bool hasOutFile = false;
    bool hasAppFile = false;
    string outFile = "";
    iB = 0;

    jobname = "";
    for(int i = 0; i < com.size(); i++)
    {
        for(int j = 0; j < com[i].size(); j++)
        {
            jobname += com[i][j];
            jobname += " ";
        }
    }

    if(DEBUG)
        cout << jobname << endl;

    for (vector<vector<string> >::iterator it = com.begin(); it != com.end();)
    {
        if ((*it)[0] == "|"){
            (*it).erase((*it).begin());
            it++;
        }
        else if((*it)[0] == "<")
        {
            hasInFile = true;
            inFile = (*it).back();
            it = com.erase(it);
        }
        else if((*it)[0] == ">")
        {
            hasOutFile = true;
            hasAppFile = false;
            outFile = (*it).back();
            it = com.erase(it);
        }
        else if((*it)[0] == ">>")
        {
            hasOutFile = false;
            hasAppFile = true;
            outFile = (*it).back();
            it = com.erase(it);
        }
        else if((*it)[0] == "&")
        {
            iB = 1;
            it = com.erase(it);
        }
        else {
            it++;
        }
    }

    int ifd, ofd;

    if(hasInFile == true)
    {
        if(DEBUG)
            cout << "In: " << inFile << endl;
        ifd = open(stoc(inFile), O_RDONLY);
    }
    if(hasAppFile == true)
    {
        if(DEBUG)
            cout << "App: " << outFile << endl;
        ofd = open(stoc(outFile), O_APPEND|O_CREAT|O_WRONLY, 0666);
    }
    if(hasOutFile == true)
    {
        if(DEBUG)
            cout << "Out: " << outFile << endl;
        remove(stoc(outFile));
        ofd = open(stoc(outFile), O_RDWR|O_CREAT, 0666);
    }


    if(DEBUG)
        cout << "com size: " << com.size() << endl;
    int stat;
    if (com.size() == 1)
    {

        if(hasInFile && !hasOutFile && !hasAppFile)
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_IN, ifd, 0);
        else if(!hasInFile && hasOutFile && !hasAppFile)
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_OUT, 0, ofd);
        else if(hasInFile && hasOutFile && !hasAppFile)
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_IN_AND_OUT, ifd, ofd);
        else if(!hasInFile && !hasOutFile && hasAppFile)
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_APP, 0, ofd);
        else if(hasInFile && !hasOutFile && hasAppFile)
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_IN_AND_APP, ifd, ofd);
        else
            stat = run(com[0], PIPE_NONE, -1, -1, NULL, F_NONE, 0, 0);

    }
    else if (com.size() >= 2)
    {
        int ** pifd = (int**)malloc((com.size()-1) * sizeof(int*));

        for(int i = 0; i < com.size() - 1; i++)
        {
            pifd[i] = (int*)malloc(2 * sizeof(int));

            if(pipe(pifd[i]) < 0)
            {
                cout << i << " pipe error" << endl;
                return -1;
            }
        }
        if(DEBUG)
            cout << "com.size() : " <<com.size() << endl;

        if(hasInFile)
            run(com[0], PIPE_IN, 0, com.size() - 1, pifd, F_IN, ifd, 0);
        else
            run(com[0], PIPE_IN, 0, com.size() - 1, pifd, F_NONE, 0, 0);


        for(int i = 1; i < com.size() - 1; i++)
        {
            run(com[i], PIPE_IAO, i-1, com.size() - 1, pifd, F_NONE, 0, 0);
        }

        if(hasAppFile)
            run(com[com.size()-1], PIPE_OUT, com.size()- 2, com.size() - 1, pifd, F_APP, 0, ofd);
        else if(hasOutFile)
            run(com[com.size()-1], PIPE_OUT, com.size()- 2, com.size() - 1, pifd, F_OUT, 0, ofd);
        else
            run(com[com.size()-1], PIPE_OUT, com.size()- 2, com.size() - 1, pifd, F_NONE, 0, 0);


        for(int i = 0; i < com.size() - 1; i++)
        {
            close(pifd[i][0]);
            close(pifd[i][1]);
        }

        do
        {
            if(DEBUG)
                cout << "wait" << endl;
        }
        while(wait(&stat) != -1);

        if(hasInFile)
            close(ifd);
        if(hasOutFile || hasAppFile)
            close(ofd);

        for(int i = 0; i < com.size() - 1; i++)
            free(pifd[i]);

        free(pifd);

    }


    return stat;

}
Ejemplo n.º 5
0
//MString unitostr(const short *s, const int sz);
MStringS::operator MString(){ return stoc((unsigned short*)data, sz); }
Ejemplo n.º 6
0
double maxlike(int nbseq, char** seq, char** seqname, char* c_tree, options opt, char* ctree1, char* ctree2){

  int i, j, k, l, ii, nbseq2, lgseq, lgsite, *drapeau, *drapeau2, cont=0, tree_is_rooted, nbparam, nbvrai, nbcompute, bl;
  char **orderedseq, racine, *site;
  double *lgbi, *lgbp, *weight, lkh, maxlkh=-1.e100, prov, rem_titv, rem_root, *freq;
  static int nbtree=0;
  FILE* outfile;

/* printmemory("deb maxlike"); */

  gamma_nbcl=opt->init->GAMMA_NCL;

  /*  READ TREE, GET NB OF TAXA */

  s_tree=(tree*)check_alloc(1, sizeof(tree));
  lgseq=(int)strlen(seq[0]);
  s_tree->alivebr=(int*)check_alloc(2*nbseq, sizeof(int));
  s_tree->node=ctos(c_tree, &nbseq2, &tree_is_rooted);
  if(strchr(c_tree, ':')) bl=1; else bl=0;


  if (nbseq2>nbseq) {
    printf("More taxa in tree file than in sequence file\n");
    exit(EXIT_FAILURE);
  }


	/* ALLOCATE AND INIT S_TREE */

  s_tree->nbseq=nbseq=nbseq2;
  s_tree->seq=seq;
  s_tree->names=seqname;

  s_tree->listgc=(double*)check_alloc(2*nbseq, sizeof(double));
  s_tree->listbr=(branche*)check_alloc(2*nbseq, sizeof(branche));
  s_tree->lgbr_init=(double*)check_alloc(2*nbseq-3, sizeof(double));
  for(i=0;i<2*nbseq-2;i++){
    s_tree->listbr[i]=(struct branche*)check_alloc(1, sizeof(struct branche));
  }

  makelistbr_unrooted(NULL, s_tree->node[0], s_tree->listbr, s_tree->alivebr, 2*nbseq-3);
  for(i=0;i<2*nbseq-3;i++) s_tree->lgbr_init[i]=s_tree->listbr[i]->length;
  for(ii=0;ii<2*nbseq-3;ii++) s_tree->alivebr[ii]=1;
  s_tree->nbalivebr=0;
  for(i=0;i<2*nbseq-3;i++) s_tree->nbalivebr+=s_tree->alivebr[i];


  set_var_param(s_tree, opt->compute);

  nbparam=s_tree->nb_var_param;

  s_tree->classlike_s=(double**)check_alloc(gamma_nbcl, sizeof(double));
  for(i=0;i<gamma_nbcl;i++)
    s_tree->classlike_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->like_s=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->likeasrv_s=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->dlike_s=(double**)check_alloc(nbparam, sizeof(double*));
  s_tree->dlikeasrv_s=(double**)check_alloc(nbparam, sizeof(double*));
  for(i=0;i<nbparam;i++){
    s_tree->dlike_s[i]=(double*)check_alloc(lgseq, sizeof(double));
    s_tree->dlikeasrv_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  }
  s_tree->d2like_s=(double**)check_alloc(nbparam, sizeof(double*));
  s_tree->d2likeasrv_s=(double**)check_alloc(nbparam, sizeof(double*));
  for(i=0;i<nbparam;i++){
    s_tree->d2like_s[i]=(double*)check_alloc(lgseq, sizeof(double));
    s_tree->d2likeasrv_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  }

  s_tree->classdlike_s=(double***)check_alloc(gamma_nbcl, sizeof(double**));
  s_tree->classd2like_s=(double***)check_alloc(gamma_nbcl, sizeof(double**));
  for(i=0;i<gamma_nbcl;i++){ 
    s_tree->classdlike_s[i]=(double**)check_alloc(nbparam, sizeof(double*));
    s_tree->classd2like_s[i]=(double**)check_alloc(nbparam, sizeof(double*));
    for(j=0;j<nbparam;j++){
      s_tree->classdlike_s[i][j]=(double*)check_alloc(lgseq, sizeof(double));
      s_tree->classd2like_s[i][j]=(double*)check_alloc(lgseq, sizeof(double));
    }
  }
  
  s_tree->deriv=(double*)check_alloc(nbparam, sizeof(double));
  s_tree->deriv2=(double*)check_alloc(nbparam, sizeof(double));

/* printmemory("fin tree alloc"); */

  for(i=0;i<2*nbseq-2;i++)
    alloc_node(s_tree->node[i], lgseq, nbparam, nbseq, (i<nbseq), 0, gamma_nbcl);
  alloc_node(root, lgseq, nbparam, nbseq, 0, 0, gamma_nbcl);

/* printmemory("fin node alloc"); */



	/* INIT LIKELIHOOD AT TIPS */

  for(i=0;i<nbseq;i++){
    for(j=0;j<4;j++){ /* 4 patterns for tip nodes */
      for(k=0;k<4;k++){ /* 4 possible states */
        for(l=0;l<gamma_nbcl;l++){
	  if(j==k) s_tree->node[i]->x[l][j][k]=1.;
	  else s_tree->node[i]->x[l][j][k]=0.;
        }
      }
    }
  }

       /* CHECK SEQUENCE FILE */

  for(i=nbseq2;i<2*nbseq2-2;i++) sprintf(s_tree->node[i]->nom, "int%d", i-nbseq2+1);

  drapeau=(int*)check_alloc(nbseq, sizeof(int));
  drapeau2=(int*)check_alloc(nbseq, sizeof(double));
  for(i=0;i<nbseq2;i++) drapeau2[i]=0;
  orderedseq=check_alloc(MAXNSP, sizeof(char*));
  

  for(i=0;i<nbseq2;i++){
    for(j=0;j<nbseq;j++){
      if(!drapeau[j] && strncmp(s_tree->node[i]->nom, seqname[j], MAXLNAME)==0){
        drapeau[j]=drapeau2[i]=1;
	orderedseq[i]=seq[j];
        break;
      }
    }
    if(drapeau2[i]==0){
      printf("Taxon %s not found in sequence file.\n", s_tree->node[i]->nom);
      exit(EXIT_FAILURE);
    }
  }
  s_tree->nbseq=nbseq=nbseq2;


	/* SET "PATTERNS" (relationships between sites) */

  s_tree->sitetopatt=check_alloc(lgseq, sizeof(int));
  weight=(double*)check_alloc(lgseq, sizeof(double));
  for(i=0;i<lgseq;i++) weight[i]=0.;
  site=(char*)check_alloc(nbseq, sizeof(char)); 
  lgsite=0;
  for(i=0;i<lgseq;i++){
    cont=0;
    for(j=0;j<nbseq;j++) site[j]=orderedseq[j][i];
    for(k=0;k<lgsite;k++){
      if(samesite(site, s_tree, k)){
        weight[k]++;
	cont=1;
        break;
      }
    }
    if(cont){
      s_tree->sitetopatt[i]=k;
      continue; 
    }
    for(j=0;j<nbseq;j++)
      s_tree->node[j]->seq[lgsite]=site[j];
    weight[lgsite]=1.;
    s_tree->sitetopatt[i]=lgsite;
    lgsite++;
  }

  free(drapeau); free(drapeau2); free(site);

  s_tree->lgseq=lgsite;
  s_tree->weight=weight;
  s_tree->weightsum=lgseq;

  /* printf("%d species, %d sites, %d patterns\n", nbseq, lgseq, lgsite);*/


	/* AMMEND INPUT */
       	
		/* ts/tv */

  rem_titv=opt->init->INIT_TITV;
  if(opt->init->INIT_TITV<=0.){
    freq=(double*)check_alloc(16, sizeof(double));
    opt->init->INIT_TITV=0.;
    nbvrai=nbseq*(nbseq-1)/2;
    for(i=0;i<nbseq;i++){
      for(j=i+1;j<nbseq;j++){
	prov=alsurbet_kim(seq[i], seq[j], freq);
  	if(prov<-0.5) nbvrai--; else opt->init->INIT_TITV+=prov;
      }
    }
    free(freq);
    opt->init->INIT_TITV/=nbvrai;
  }

		/* root */

  rem_root=opt->init->INIT_ROOT;
  if(opt->init->INIT_ROOT<=0.){
    opt->init->INIT_ROOT=root->l1/(root->l1+root->l2);
    if(!bl) opt->init->INIT_ROOT=0.5;
  }

		/* branch lengths */

  s_tree->dist=(double**)check_alloc(nbseq, sizeof(double*));
  for(i=0;i<nbseq;i++)
    s_tree->dist[i]=(double*)check_alloc(nbseq, sizeof(double));

  if(strcmp(opt->init->INIT_LENGTH, "REDO")==0){
    for(i=0;i<nbseq;i++){
      for(j=i;j<nbseq;j++){
        if(i==j) s_tree->dist[i][j]=0.;
        else s_tree->dist[i][j]=s_tree->dist[j][i]=gg95(seq[i], seq[j], opt->init->INIT_TITV);
      }
    }
  }

	/* ROOT TREE AND ORGANIZE */

  organize_tree(NULL, root);
  organize_listbr(s_tree);
  setpattern(s_tree);
  for(ii=0;ii<2*nbseq-3;ii++)
    setunderlyingbr(root, s_tree->listbr[ii], ii);
  setunderlyinggc(root, s_tree->nbseq);
  set_numerobr(root, s_tree->listbr, 2*nbseq-3);

  for(i=0;i<nbseq;i++){
    s_tree->node[i]->alive1=0;
    s_tree->node[i]->alive2=0;
    s_tree->node[i]->alive3=1;
  }
  for(i=nbseq;i<2*nbseq-2;i++){
    s_tree->node[i]->alive1=1;
    s_tree->node[i]->alive2=1;
    s_tree->node[i]->alive3=1;
  }
  root->alive1=root->alive2=1; root->alive3=-1;
  for(ii=0;ii<2*nbseq-3;ii++) s_tree->alivebr[ii]=1;


	/* CALL COMPUTE */

  if(opt->init->NBRANDOM>0)
    nbcompute=opt->init->NBRANDOM;
  else 
    nbcompute=1;


  for(l=0;l<nbcompute;l++){
    lkh=compute(s_tree, opt->init, opt->compute, opt->converge, opt->print);
    if(lkh>maxlkh) maxlkh=lkh;
    if(!opt->print->PRINT1 && !opt->print->PRINT2) printf("%f\n", lkh);
  }


  if(ctree1)
    stoc(s_tree->node, nbseq, 1, ctree1, 0);

  if(ctree2)
    stoc(s_tree->node, nbseq, 1, ctree2, 1);

  if(opt->print->EVAL_OUT){
    outfile=fopen("detailed_out", "a");
    nbtree++;
    fprintf(outfile, "%d\t%s\t%f\t%f\t%f\t%f\t%f\t%f\n", nbtree, ctree1, maxlkh, s_tree->titv, s_tree->GCanc, s_tree->gamma_shp, s_tree->covar, s_tree->pi);
    fclose(outfile);
  }


  opt->init->INIT_TITV=rem_titv;
  opt->init->INIT_ROOT=rem_root;


  /* FREE */

  free_tree(s_tree, gamma_nbcl);
  free(s_tree);
  free_node(root, lgseq, nbparam, 0, gamma_nbcl);

  free(orderedseq);

  return maxlkh;

}
Ejemplo n.º 7
0
double maxlike(int nbseq, char** seq, char** seqname, int** ttree, double* lgbi, double* lgbp, int nbbi, int l1, char** list1, int l2, char** list2, options opt, char* ctree, char* ctree1, char* ctree2){

  int i, j, k, l, ii, lgseq, lgsite, *drapeau, *drapeau2, cont=0, nbparam, nbvrai;
  char **orderedseq, racine, *site;
  double *weight, lkh, maxlkh=-1.e100, prov, **lgbi_pt, *freq;

  gamma_nbcl=opt->init->GAMMA_NCL;

  /*  INITIALIZATION */

  s_tree=(tree*)check_alloc(1, sizeof(tree));
  lgseq=(int)strlen(seq[0]);
  

  s_tree->alivebr=(int*)check_alloc(2*nbseq, sizeof(int));
  if(lgbi) 
    lgbi_pt=(double**)check_alloc(nbseq-2, sizeof(double*));
  else
    lgbi_pt=NULL;
  weight=(double*)check_alloc(lgseq, sizeof(double));
/*
debug_printree(ttree, nbseq, nbbi);
*/

  s_tree->node=ttos_unres_rooted(ttree, nbseq, nbbi, seqname, lgbi, lgbp, 0.5, l1, list1, l2, list2, lgbi_pt);

  s_tree->nbseq=nbseq;

  s_tree->seq=seq;
  s_tree->names=seqname;



  s_tree->listgc=(double*)check_alloc(2*nbseq, sizeof(double));
  s_tree->listbr=(branche*)check_alloc(2*nbseq, sizeof(branche));
  s_tree->lgbr_init=(double*)check_alloc(2*nbseq-3, sizeof(double));
  for(i=0;i<2*nbseq-2;i++){
    s_tree->listbr[i]=(struct branche*)check_alloc(1, sizeof(struct branche));
  }


  makelistbr_unrooted(NULL, s_tree->node[0], s_tree->listbr, s_tree->alivebr, 2*nbseq-3);

  for(i=0;i<2*nbseq-3;i++) s_tree->lgbr_init[i]=s_tree->listbr[i]->length;
  s_tree->nbalivebr=0;
  for(i=0;i<2*nbseq-3;i++) s_tree->nbalivebr+=s_tree->alivebr[i];


  set_var_param(s_tree, opt->compute);

  nbparam=s_tree->nb_var_param;

  s_tree->classlike_s=(double**)check_alloc(gamma_nbcl, sizeof(double));
  for(i=0;i<gamma_nbcl;i++)
    s_tree->classlike_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->like_s=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->likeasrv_s=(double*)check_alloc(lgseq, sizeof(double));
  s_tree->dlike_s=(double**)check_alloc(nbparam, sizeof(double*));
  s_tree->dlikeasrv_s=(double**)check_alloc(nbparam, sizeof(double*));
  for(i=0;i<nbparam;i++){
    s_tree->dlike_s[i]=(double*)check_alloc(lgseq, sizeof(double));
    s_tree->dlikeasrv_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  }
  s_tree->d2like_s=(double**)check_alloc(nbparam, sizeof(double*));
  s_tree->d2likeasrv_s=(double**)check_alloc(nbparam, sizeof(double*));
  for(i=0;i<nbparam;i++){
    s_tree->d2like_s[i]=(double*)check_alloc(lgseq, sizeof(double));
    s_tree->d2likeasrv_s[i]=(double*)check_alloc(lgseq, sizeof(double));
  }

  s_tree->classdlike_s=(double***)check_alloc(gamma_nbcl, sizeof(double**));
  s_tree->classd2like_s=(double***)check_alloc(gamma_nbcl, sizeof(double**));
  for(i=0;i<gamma_nbcl;i++){
    s_tree->classdlike_s[i]=(double**)check_alloc(nbparam, sizeof(double*));
    s_tree->classd2like_s[i]=(double**)check_alloc(nbparam, sizeof(double*));
    for(j=0;j<nbparam;j++){
      s_tree->classdlike_s[i][j]=(double*)check_alloc(lgseq, sizeof(double));
      s_tree->classd2like_s[i][j]=(double*)check_alloc(lgseq, sizeof(double));
    }
  }

  s_tree->deriv=(double*)check_alloc(nbparam, sizeof(double));
  s_tree->deriv2=(double*)check_alloc(nbparam, sizeof(double));


  for(i=0;i<2*nbseq-2;i++)
    alloc_node(s_tree->node[i], lgseq, nbparam, nbseq, (i<nbseq), 0, gamma_nbcl);
  alloc_node(root, lgseq, nbparam, nbseq, 0, 0, gamma_nbcl);


  for(i=0;i<nbseq;i++){
    for(j=0;j<4;j++){ /* 4 patterns for tip nodes */
      for(k=0;k<4;k++){ /* 4 possible states */
        for(l=0;l<gamma_nbcl;l++){
	  if(j==k) s_tree->node[i]->x[l][j][k]=1.;
	  else s_tree->node[i]->x[l][j][k]=0.;
        }
      }
    }
  }

  for(i=nbseq;i<2*nbseq-2;i++) sprintf(s_tree->node[i]->nom, "int%d", i-nbseq+1);

  drapeau=(int*)check_alloc(nbseq, sizeof(int));
  drapeau2=(int*)check_alloc(nbseq, sizeof(double));
  orderedseq=(char**)check_alloc(nbseq, sizeof(char*));
  for(i=0;i<nbseq;i++) drapeau2[i]=0;
  

  for(i=0;i<nbseq;i++){
    for(j=0;j<nbseq;j++){
      if(!drapeau[j] && strncmp(s_tree->node[i]->nom, seqname[j], MAXLNAME)==0){
        drapeau[j]=drapeau2[i]=1;
	orderedseq[i]=seq[j];
        break;
      }
    }
    if(drapeau2[i]==0){
      printf("Taxon %s not found in sequence file.\n", s_tree->node[i]->nom);
      exit(EXIT_FAILURE);
    }
  }
  s_tree->nbseq=nbseq;

  for(i=0;i<lgseq;i++) weight[i]=0.;
  site=(char*)check_alloc(nbseq+1, sizeof(char)); 
  lgsite=0;
  for(i=0;i<lgseq;i++){
    cont=0;
    for(j=0;j<nbseq;j++) site[j]=orderedseq[j][i];
    for(k=0;k<lgsite;k++){
      if(samesite(site, s_tree, k)){
        weight[k]++;
	cont=1;
        break;
      }
    }
    if(cont) continue; 
    for(j=0;j<nbseq;j++)
      s_tree->node[j]->seq[lgsite]=site[j];
    weight[lgsite]=1.;
    lgsite++;
  }


  s_tree->lgseq=lgsite;
  s_tree->weight=weight;
  s_tree->weightsum=lgseq;


  if(opt->init->INIT_TITV<=0.){
    freq=(double*)check_alloc(16, sizeof(double));
    opt->init->INIT_TITV=0.;
    nbvrai=nbseq*(nbseq-1)/2;
    for(i=0;i<nbseq;i++){
      for(j=i+1;j<nbseq;j++){
	prov=alsurbet_kim(seq[i], seq[j], freq);
  	if(prov<-0.5) nbvrai--; else opt->init->INIT_TITV+=prov;
      }
    }
    opt->init->INIT_TITV/=nbvrai;
    free(freq);
  }

  if(opt->init->INIT_ROOT<=0.){
    if(lgbp[0]>-0.5)
      opt->init->INIT_ROOT=root->l1/(root->l1+root->l2);
    else
      opt->init->INIT_ROOT=0.5;
  }

  s_tree->dist=(double**)check_alloc(nbseq, sizeof(double*));
  for(i=0;i<nbseq;i++)
    s_tree->dist[i]=(double*)check_alloc(nbseq, sizeof(double));

  if(strcmp(opt->init->INIT_LENGTH, "REDO")==0){
    for(i=0;i<nbseq;i++){
      for(j=i;j<nbseq;j++){
        if(i==j) s_tree->dist[i][j]=0.;
        else s_tree->dist[i][j]=s_tree->dist[j][i]=gg95(seq[i], seq[j], opt->init->INIT_TITV);
      }
    }
  }


  organize_tree(NULL, root);
  organize_listbr(s_tree);
  setpattern(s_tree);
  for(ii=0;ii<2*nbseq-3;ii++)
    setunderlyingbr(root, s_tree->listbr[ii], ii);
  setunderlyinggc(root, s_tree->nbseq);
  set_numerobr(root, s_tree->listbr, 2*nbseq-3);



  lkh=compute(s_tree, opt->init, opt->compute, opt->converge, opt->print);
  if(lkh>maxlkh) maxlkh=lkh;

  if(opt->print->PRINT0)
    printf("ln(L)=%f\n", lkh);


  if(ctree)
    stoc(s_tree->node, 1, nbseq, ctree, -1);

  if(ctree1)
    stoc(s_tree->node, 1, nbseq, ctree1, 0);

  if(ctree2){
    set_node_deduced_comp(root);
    stoc(s_tree->node, 1, nbseq, ctree2, 1);
  }



  if(lgbi){
    for(i=0;i<nbseq-3;i++){
      lgbi[i]=*(lgbi_pt[i]);
    }
  }



  /* FREEING */

  free(drapeau); free(drapeau2); free(site); free(orderedseq);
  if(lgbi) free(lgbi_pt);

  free_tree(s_tree, gamma_nbcl);
  free(s_tree);
  free_node(root, lgseq, nbparam, 0, gamma_nbcl);


  return maxlkh;

}