likelihood_vector *copy_likelihood_vectors (tree *tr, nodeptr p) { assert(tr->useRecom == FALSE); likelihood_vector *v = (likelihood_vector *) malloc(sizeof(likelihood_vector)); v->node_number = p->number; v->num_partitions = tr->NumberOfModels; v->partition_sizes = (size_t *)malloc(tr->NumberOfModels * sizeof(size_t)); v->lh_values = (double **)malloc(tr->NumberOfModels * sizeof(double *)); /* Compute LH vector sizes for each partition */ size_t rateHet, states, width, vector_size; rateHet = discreteRateCategories(tr->rateHetModel); int model; for(model = 0; model < tr->NumberOfModels; model++) { width = (size_t)tr->partitionData[model].width; states = (size_t)tr->partitionData[model].states; vector_size = virtual_width( width ) * rateHet * states * sizeof(double); v->lh_values[model] = (double *)malloc(sizeof(double) * vector_size); assert (v->lh_values[model] != NULL); v->partition_sizes[model] = vector_size; double *lh_vector_src = tr->partitionData[model].xVector[p->number - tr->mxtips - 1]; assert (lh_vector_src != NULL); vector_size = v->partition_sizes[model]; memcpy(v->lh_values[model], lh_vector_src, vector_size); } return v; }
static double evaluateGAMMA_FLEX(int *wptr, double *x1_start, double *x2_start, double *tipVector, unsigned char *tipX1, const int n, double *diagptable, const int states) { double sum = 0.0, term, *x1, *x2; int i, j, k; /* span is the offset within the likelihood array at an inner node that gets us from the values of site i to the values of site i + 1 */ const int span = states * 4; int vn = virtual_width(n); printf( "n: %d %d\n", n, vn ); if( tipX1 == 0 ) { reorder_back( x1_start, vn, span ); } reorder_back( x2_start, vn, span ); /* we distingusih between two cases here: one node of the two nodes defining the branch at which we put the virtual root is a tip. Both nodes can not be tips because we do not allow for two-taxon trees ;-) Nota that, if a node is a tip, this will always be tipX1. This is done for code simplicity and the flipping of the nodes is done before when we compute the traversal descriptor. */ /* the left node is a tip */ if(tipX1) { /* loop over the sites of this partition */ for (i = 0; i < n; i++) { /* access pre-computed tip vector values via a lookup table */ x1 = &(tipVector[states * tipX1[i]]); /* access the other(inner) node at the other end of the branch */ x2 = &(x2_start[span * i]); /* loop over GAMMA rate categories, hard-coded as 4 in RAxML */ for(j = 0, term = 0.0; j < 4; j++) /* loop over states and multiply them with the P matrix */ for(k = 0; k < states; k++) term += x1[k] * x2[j * states + k] * diagptable[j * states + k]; /* take the log of the likelihood and multiply the per-gamma rate likelihood by 1/4. Under the GAMMA model the 4 discrete GAMMA rates all have the same probability of 0.25 */ term = LOG(0.25 * FABS(term)); sum += wptr[i] * term; } } else { for (i = 0; i < n; i++) { /* same as before, only that now we access two inner likelihood vectors x1 and x2 */ x1 = &(x1_start[span * i]); x2 = &(x2_start[span * i]); for(j = 0, term = 0.0; j < 4; j++) for(k = 0; k < states; k++) term += x1[j * states + k] * x2[j * states + k] * diagptable[j * states + k]; term = LOG(0.25 * FABS(term)); sum += wptr[i] * term; } } if( tipX1 == 0 ) { reorder( x1_start, vn, span ); } reorder( x2_start, vn, span ); return sum; }