Example #1
0
void SetupAAMatrix()
{
	int i,j,k;
	double mr;
	double sum;
	double U[SQNUM_AA], V[SQNUM_AA], T1[SQNUM_AA], T2[SQNUM_AA];

	k=0;
	for (i=0; i<NUM_AA-1; i++) {
		for (j=i+1; j<NUM_AA; j++) {
			Qij[i*NUM_AA+j] = Qij[j*NUM_AA+i] = aaRelativeRate[k++];
		}
	}

	for (i=0; i<NUM_AA; i++) {
		for (j=0; j<NUM_AA; j++) {
			Qij[i*NUM_AA+j] *= aaFreq[j];
		}
	}

	mr=0;
	for (i=0; i<NUM_AA; i++) {
		sum = 0;
		Qij[i*NUM_AA+i]=0;
		for (j=0; j<NUM_AA; j++) {
			sum += Qij[i*NUM_AA+j];
		}
		Qij[i*NUM_AA+i] = -sum;
		mr += aaFreq[i] * sum;
	}

	abyx(1.0/mr, Qij, SQNUM_AA);

	if ((k=eigen(1, Qij, NUM_AA, Root, T1, U, V, T2))!=0) {
		fprintf(stderr, "\ncomplex roots in SetupAAMatrix");
		exit(EXIT_FAILURE);
	}
	xtoy (U, V, SQNUM_AA);
	matinv (V, NUM_AA, NUM_AA, T1);
	for (i=0; i<NUM_AA; i++) {
   		for (j=0; j<NUM_AA; j++) {
   			for (k=0; k<NUM_AA; k++) {
   				Cijk[i*SQNUM_AA+j*NUM_AA+k] = U[i*NUM_AA+k]*V[k*NUM_AA+j];
   			}
   		}
   	}
}
Example #2
0
double lfun (double x[], int np)
{
  GaussLegendreRule(&xI, &wI, com.npoints);
  for (Gtree=0; Gtree<com.nGtree;Gtree++){
    for(igrid=0; igrid<K*K; igrid++){
      if(Gtree==G1a||Gtree==G1b || Gtree==G1c){
	if(com.model==M2SIM3s){
	  xtoy(Q,Q1,DIM*DIM);
	  complexroots (Q1, Pt1, t[0]*t[1]);
	  if(0 && igrid==0){
	    debug_print(Pt1, DIM);
	  }
	  xtoy(Q,Q1,DIM*DIM);
	  complexroots (Q1, Pt0, t[0]-t[0]*t[1]);
	  if(0 && igrid==0){
	    debug_print(Pt0, DIM);
	  }
	  if (Gtree==G1c){
	  }
	  else if (Gtree==G1b){
	  }
	  else if (Gtree==G1a){
	  }
	}
	b[1]=t[0]*t[1];	
	b[0]=t[0]-t[0]*t[1];
      }
      else if(Gtree==G2a || Gtree==G2b || Gtree==G2c || Gtree== G3a || Gtree ==G3b || Gtree ==G3c){
	if(com.model==M2SIM3s){
	  xtoy(Q,Q1,DIM*DIM);
	  complexroots (Q1, Pt0, tau1-t[1]); /*ÔÚpt.cÀֱ½ÓÓÿÉÄÜÓÐÎÊÌâ*/
	  xtoy(Q,Q1,DIM*DIM);
	  complexroots (Q1, Pt1, t[1]);
	  //printf("\n In lfun after end2\n");
	  for(s=1;s<=8;s++){ //problems about s-1
	    if (Gtree==G2c || Gtree==G3c)
	      f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2(11,Pt0,DIM) + Pt1[s,2,DIM]*is2(20,Pt0,DIM)) * 2/theta1
		+      (Pt1[get_ij(s,7,DIM)] * is2(17,Pt0,DIM) + Pt1[s,8,DIM]*is2(14,Pt0,DIM)) * 2/theta2;
	    else if (Gtree==G2b || Gtree==G3b)
	      f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2(10,Pt0,DIM) + Pt1[s,3,DIM]*is2(19,Pt0,DIM))*2/theta1
		+(Pt1[get_ij(s,6,DIM)] * is2(16,Pt0,DIM) + Pt1[s,8,DIM]*is2(13,Pt0,DIM))*2/theta2;
	    else if (Gtree==G2a || Gtree==G3a)
	      f[s-1]=(Pt1[get_ij(s,1,DIM)] * is2( 9,Pt0,DIM) + Pt1[s,5,DIM]*is2(18,Pt0,DIM)) *2/theta1
		+(Pt1[get_ij(s,4,DIM)] * is2(15,Pt0,DIM) + Pt1[s,8,DIM]*is2(12,Pt0,DIM)) *2/theta2;
	  } 
	}
	if(Gtree==G2a || Gtree==G2b || Gtree==G2c){
	  g=exp(-t[0]);
	  for(s=1;s<=8;s++)
	    f[s-1]*=g;
	  b[1]=t[1];
	  b[0]= 2.0*t[0]/theta5+tau1-t[1];
	}
	else if(Gtree==G3a || Gtree==G3b || Gtree==G3c){
	  g= exp(-t[0])*exp(-2*(tau0-tau1)/theta5);
	  for(s=1;s<=8;s++)
	    f[s-1]*=g;
	  b[1]=t[1];
	  b[0]= 2.0*t[0]/theta4+tau0-t[1];
	}
      }

      else{
	//printf("\n In lfun after end3\n");
	if (Gtree==G4a || Gtree==G4b || Gtree==G4c){
	  g= exp(-3*t[0]*t[1]-t[0]*(1-t[1]));
	  b[1]=theta5/2*t[0]*t[1]+tau1;
	  b[0]=theta5/2*t[0]+tau1-b[1];
	}//above checked
	else if (Gtree==G5a || Gtree==G5b || Gtree==G5c){
	  g=exp(-2*t[1]-t[0])* exp(-2.0*(tau0-tau1)/theta5);
	  b[1]=theta5/2*t[1]+tau1;
	  b[0]=theta4/2*t[0]+tau0-b[1];
	}
	else if (Gtree==G6a || Gtree==G6b || Gtree==G6c){
	  g= exp(-3*t[1]-t[0])* exp(-6.0*(tau0-tau1)/theta5);
	  b[1]=theta4/2*t[1]+tau0;
	  b[0]=theta4/2*(t[0]+t[1])+tau0-b[1];
	} /*end else if*/
	if(com.model==M2SIM3s){
	  xtoy(Q,Q1,DIM*DIM);
	  complexroots (Q1, Ptau1, tau1);	
	  for(s=1;s<=8;s++)
	    f[s-1]= is3(s,Ptau1,DIM)*g; 
	}
      }
    
      for(s=0;s<8;s++)
	{
	  com.wwprior[Gtree][igrid][s] = f[s] * com.wwprior[Gtree][igrid][0];
	  //printf("wwprior[%d][%d][%d]=%f  ",Gtree, igrid,s,com.wwprior[Gtree][igrid][s]);
	}
      //printf("\n"); //hhh
      if(!com.fix_locusrate)
	p0124Fromb0b1(com.bp0124[Gtree]+igrid*5, b);
      else {
	com.bp0124[Gtree][igrid*2+0] = b[0];
	com.bp0124[Gtree][igrid*2+1] = b[1];
      }
      /* printf("%-3s %5d y%9.5f%9.5f t%9.5f%9.5f b%9.5f%9.5f\n",
	 GtreeStr[Gtree], igrid+1, y[0],y[1], t[0],t[1], b[0],b[1]); */
      //printf("In lfun after igrid loop !!!!\n"); zzz
    }
	
    for(locus=0; locus<com.ndata; locus++) {
      Li = lnpD_locus(locus,com.state[locus]);
      if(com.model==M1DiscreteBeta)
	com.pDclass[itau1*com.ndata+locus] = Li;
      else
	lnL += Li;
      n = com.Nij + locus*5;
      if(debug) printf("%d\t%d\t%d\t%d\t%d\t%.6f\n", n[0],n[1],n[2],n[3],n[4], Li);
    }
	
    return(-lnL);
  }
Example #3
0
void RateMatrix::SetupMatrix(bool transition_probability_independent_sites_setup)
{
	unsigned int i,j,k;
	double mr;
	double sum;
	double U[numStates_squared+1], V[numStates_squared+1], T1[numStates_squared+1], T2[numStates_squared+1];
	double Qij2pass[numStates_squared+1];

	//////////
	/// transition_probability_independent_sites_setup:
	/// If we are calculating the transition probabilities as independent sites models, then we need
	/// to run the code in the if statements. However, if we are doing the pseudo-dependent sites
	/// calculations, we want to skip those code segments. 
	/// Segment #1: 
	///   * Sets the Qij entries to be the input substitution model. 
	///     -> For pseudo-dependent sites, we excise the Q matrix from the 4^N X 4^N sequence model.
	/// Segment #2:
	///	  * Sets Qij so that branch length corresponds to expected number of changes.
	///     -> pseudo-dependent sites branch lengths correspond not to 1 change per site, but whatever
	///        the dependent sites model constrains it to.
	//////////
	CheckFrequencies();
	k=0;
	if (transition_probability_independent_sites_setup) {
		for (i=0; i<numStates-1; i++) {
			for (j=i+1; j<numStates; j++) {
				Qij.at(i*numStates+j) = Qij.at(j*numStates+i) = Sij.at(k++);
			}
		}

		for (i=0; i<numStates; i++) {
			for (j=0; j<numStates; j++) { 
				Qij.at(i*numStates+j) *= pi.at(j);
			}
		}

		mr=0;		
		for (i=0; i<numStates; i++) {
			sum = 0;
			Qij.at(i*numStates+i)=0; 
			for (j=0; j<numStates; j++) { 
				sum += Qij.at(i*numStates+j);
			}
			Qij.at(i*numStates+i) = -sum; 
			mr += pi.at(i) * sum;
		}

		//////////
		/// Multiplies every element of Qij by 1.0/mr. This is abyx.
		//////////
		for (i=0; i<numStates_squared; Qij.at(i)*=1.0/mr,i++) ; 
	}

	// Testcode: Print Qij.
	//if (!transition_probability_independent_sites_setup) printQij();
	
	//Testcode: does sum = 1?
	sum = 0;
	double partial = 0;
	for (i = 0; i < numStates; i++) {
		partial = 0;
		for (j = 0; j < numStates; j++) {
			if (i != j)
				partial += Qij.at(i*numStates+j);
		}
		sum += pi.at(i) * partial;
	}
	//if (!transition_probability_independent_sites_setup) cerr << "Sum: " << sum << endl;

	for (i = 0; i < numStates_squared; i++) Qij2pass[i]=Qij.at(i);
	if ((k=eigen(1, Qij2pass, numStates, Root, T1, U, V, T2))!=0) {
		fprintf(stderr, "\ncomplex roots in SetupMatrix");
		exit(EXIT_FAILURE);
	}
	xtoy (U, V, numStates_squared);
	matinv (V, numStates, numStates, T1);
	vector<unsigned int> squared (numStates, 0), power_1 (numStates, 0);
	for (i=0; i<numStates; i++) { squared[i]=i*numStates_squared; power_1[i]=i*numStates; }
	for (i=0; i<numStates; i++) {
   		for (j=0; j<numStates; j++) {
   			for (k=0; k<numStates; k++) {
				Cijk.at(squared[i]+power_1[j]+k) = U[power_1[i]+k]*V[power_1[k]+j];
  			}
  		}
   	}
}
Example #4
0
File: ex06a.c Project: RJ-Jesus/AC1
int main(void) {
    print_int10(xtoy(10, 2));
    print_char('\n');
    print_int10(xtoy(-100, 4));
    return 0;
}