Esempio n. 1
0
int main(int argc, char* argv[])
{
  int num_haplo;
  double curr_loglik, prev_loglik, delta;
  struct tree *tree1, *tree2;
  struct haplotype **results;

  /* check for too few arguments */
  if(argc != 6) {
    printf("Error: wrong number of arguments - \"BEAGLEboth <input file 1> <input file 2> <output file for> <output file rev> <output file both>\".\n");
    return 1;
  }

  tree1 = read_input(argv[1]);
  tree2 = read_input(argv[2]);

  prep_tree(tree1, tree2);
  prep_tree(tree2, tree1);

  print_tree(argv[3], tree1, 0);
  print_tree(argv[4], tree2, 1);

  prev_loglik = loglik(tree1);
  do {
    calc_tree(tree1, tree2);
    calc_tree(tree2, tree1);
    curr_loglik = loglik(tree1);
    delta = (curr_loglik-prev_loglik)/curr_loglik;
    prev_loglik = curr_loglik;
    } while(delta > TOL);

  calc_tree(tree1, tree2);
  calc_tree(tree2, tree1);

  print_tree(argv[5], tree1, 0);

  return 0;
}
Esempio n. 2
0
main(int argc, char *argv[])
{int *permC, *permV, *permV2;
  int loopz, loopa, loopc, cluster,loopvertexk,loopvertexj,j,i,k,Current,start,outloopc, newcluster;
  int V,vertexk,vertexj,cl,errors,numlinks;
  float meanfield0,meanfield1,logfn1,logfn0,loglik1,loglik0;
  float tmp;
  
  static float zma[MAXV][MAXC],zm[MAXV][MAXC], zmat[MAXV][MAXC];
  static float zmatmp[MAXV][MAXC],zmtmp[MAXV][MAXC], matmp[MAXC];
  static float ma[MAXC], atmp[MAXC], manew[MAXC], floatval[MAXV*MAXC];
  int c,cc;
  static bool A[MAXV][MAXV], AA[MAXV][MAXV]; 
  float T1, T0, logprior0, logprior1, logpost0, logpost1;
 
  char LinksInputFile[40], tmps[4];
  int C=atoi(argv[4]);
  int Zloops=atoi(argv[5]);
  int Aloops=atoi(argv[6]);
  float beta=atof(argv[7]);
  float Abeta=atof(argv[8]);
  float Bbeta=atof(argv[9]);
  //  float Threshold=atof(argv[10]);
  float Threshold=THRESHOLD;
  int Vtoupdate=atoi(argv[10]);
  bool DISP_ERRORS=atoi(argv[11]);
  int BurninSteps=atoi(argv[12]);
  float BurninBeta=atof(argv[13]);
  //  char OutputFile=argv[2];
  /* get the adjacency matrixc */
 
  FILE *fpLinkFile, *fpOutputFile, *fpInitialFile;
  int counter, vertex;
  int ii[MAXLINKS],jj[MAXLINKS];
  float fl;

  srand(time(0));

  bool dontstart=0;
  bool randominit=1;
  bool singleinit=0;
  
  fpLinkFile = fopen(argv[1], "r");
  if(fpLinkFile==NULL){
    printf("Error: can't open LinkFile %s\n",argv[1]);
    dontstart=1;}

  if (strcmp(argv[3],"random")==0)
    {singleinit=0; randominit=1;printf("\nRandom initialisation.\n");}

  if (strcmp(argv[3],"single")==0)
    {singleinit=1; randominit=1;printf("\nReplicated single cluster initialisation.\n");}
  
  if (randominit==0)
    {fpInitialFile = fopen(argv[3], "r");
      if(fpInitialFile==NULL){
	printf("Error: can't open InitialFile %s\n",argv[3]);
	dontstart=1; 
      }}
  
  if (dontstart==1){return 1;}
  
  else
    /* start the analysis : */  
    {
      counter=0;
      while(!feof(fpLinkFile)){ 
	fscanf(fpLinkFile, "%d %d", &ii[counter], &jj[counter]);
	counter++; 
      }      
      fclose(fpLinkFile);
      numlinks=counter; 
      printf("LinkFile read successfully.\n");
           
      /* find the number of vertices : */ 
      int nums[2];
      nums[0]=max_value(ii,numlinks);
      nums[1]=max_value(jj,numlinks);
      V = max_value(nums,2);
      printf("Number of vertices =%d\n",V);          
      if (Vtoupdate==0){Vtoupdate=V;}
      printf("Will update at each iteration %d randomly selected vertices\n",Vtoupdate);          

      if (randominit==0){
	cluster=0;
	while(!feof(fpInitialFile)){
	  for (vertex=0;vertex<V;vertex++){
	    fscanf(fpInitialFile, "%f ",&zm[vertex][cluster]);
	  }
	  ++cluster;
	}       
	fclose(fpInitialFile);
	printf("InitFile read successfully.\n");
	if (C==0){C=cluster;}}     
      
      /* setup mean alpha */
      for (i=0;i<C;i++){ma[i]=1.0;}
      
      /*   initialise Z   */
      for (i=0;i<C;i++)
	{ for (j=0;j<V;j++)
	    {if (randominit==1){zm[j][i]=1.0*(rand()>0.5*RAND_MAX);} /* initial to 0.05 not important */
	      zma[j][i]=ma[i]*zm[j][i]; 
	    }
	}
      /* setup adj matrix based on links */
      for (i=0;i<V;i++)
	{ for (j=0;j<i;j++)
	    {
	      A[j][i]=0; A[i][j]=0;
	    }
	  A[i][i]=1; 
	}
      for (counter=0;counter<numlinks;counter++)
	{A[ii[counter]-1][jj[counter]-1]=1;A[jj[counter]-1][ii[counter]-1]=1;}     
      
      if (singleinit==1)
	{printf("\nFinding first a single cluster based on random initialisation....\n");
	  updateZ(argv[2],zm,zma,ma,A,BurninSteps,1,V,V,BurninBeta,Threshold,DISP_ERRORS); 
	  for (i=1;i<C;i++)
	    { for (j=0;j<V;j++)
		{zm[j][i]=zm[k][0];zma[j][i]=ma[i]*zm[j][i]; 
		}
	    }
	}
      
	printf("\n\nFinding the clique matrix marginals p(z[vertex][cluster]=1|adjacency matrix):\n");

	if (BurninSteps>0){updateZ(argv[2],zm,zma,ma,A,BurninSteps,C,V,Vtoupdate,BurninBeta,Threshold,DISP_ERRORS);}
	
	for (loopa=0;loopa<=Aloops;loopa++)
	  {if (loopa==0) {updateZ(argv[2],zm,zma,ma,A,Zloops,C,V,Vtoupdate,beta,Threshold,DISP_ERRORS);}
	    else
	      {updateZ(argv[2],zm,zma,ma,A,Zloops,C,V,Vtoupdate,beta,Threshold,DISP_ERRORS);}
	    
	 if (loopa<Aloops){
	  /* update alpha */
	  printf("\n[%d]Updating number of clusters",loopa);
	  for (i=0;i<V;i++){
	    for (cluster=0;cluster<C;cluster++){zmat[i][cluster]=zma[i][cluster];}}
	  for (c=0;c<C;c++){manew[c]=0.0;}
	  
	  for (cluster=0;cluster<C;cluster++)
	    {
	      for (i=0;i<V;i++){zmat[i][cluster]=zm[i][cluster];}
	      loglik1=loglik(zmat,A,V,C,beta,Threshold);	 
	      for (i=0;i<V;i++){zmat[i][cluster]=0.0;}
	      loglik0=loglik(zmat,A,V,C,beta,Threshold);
	      for (i=0;i<V;i++){zmat[i][cluster]=zma[i][cluster];}
	      for (c=0;c<C;c++){atmp[c]=ma[c];}
	      atmp[cluster]=1.0;
	      T1=0.0;
	      for (c=0;c<C;c++){T1+=atmp[c];}
	      T0=1.0*C-T1;
	      logprior1=betalog(Abeta+T1,Bbeta+T0);
	      atmp[cluster]=0.0;
	      T1=0.0;
	      for (c=0;c<C;c++){T1+=atmp[c];}
	      T0=1.0*C-T1;
	      logprior0=betalog(Abeta+T1,Bbeta+T0);
	      logpost1=logprior1+loglik1;
	      logpost0=logprior0+loglik0;
	      ma[cluster]=1.0/(1.0+exp(logpost0-logpost1));
	    }	 	 
	  
	  cluster=0; newcluster=0; 
	  for (cluster=0;cluster<C;cluster++){
	    matmp[cluster]=ma[cluster];	    
	    for (i=0;i<V;i++){	  
	      zmatmp[i][cluster]=zma[i][cluster];
	      zmtmp[i][cluster]=zm[i][cluster];
	    }
	  }
	  for (cluster=0;cluster<C;cluster++){
	    if (ma[cluster]>SMALLVALUE){	 
	      for (i=0;i<V;i++){
		zma[i][newcluster]=zmatmp[i][cluster];
		zm[i][newcluster]=zmtmp[i][cluster];
	      }
		ma[newcluster]=matmp[cluster];
		newcluster++;		
	    }
	  }
	  C=newcluster;
	}
	}
    }
}