/**************************************************************** APES::likelihood_nonLinked() ****************************************************************/ double APES::likelihood_nonLinked(const MultSeqAlignment &A) { // First, compute the contribution of the emission terms by applying // Felsenstein's algorithm to all columns of the alignment MultSeqAlignment augmented(alphabet,gapSymbol); int numTaxa=taxa.size(), L=A.getLength(); for(int i=0 ; i<numTaxa ; ++i) { const String name=taxa[i].getName(); if(taxa[i].getNode()->getNodeType()==LEAF_NODE) { AlignmentSeq *newTrack=new AlignmentSeq(name,i,A.getGapSymbols()); newTrack->getSeq()=A.getTrackByName(name).getSeq(); augmented.addTrack(newTrack); } else augmented.findOrCreateTrack(name).extendToLength(L,gapSymbol); } double logP=0; // int L=augmented.getLength(); PhylogenyNode *root=tree->getRoot(); ProfileFelsenstein F(augmented,PureDnaAlphabet::global(),alphabetMap); //######### STATE state=1; // ### NEED TO CHANGE THIS WHEN I ADD FUNCTIONAL STATES //######### for(int i=0 ; i<L ; ++i) logP+=F.logLikelihood(i,root,state); // Next, process each branch individually to assess the transition // probabilities for the transducers int n=branches.size(); for(int i=0 ; i<n ; ++i) { BranchAttributes &attr=branchAttributes[i]; BranchHMM *hmm=attr.getHMM(); int parentID=attr.getParentID(), childID=attr.getChildID(); GapPattern &parentGaps=*taxa[parentID].getGapPattern(); GapPattern &childGaps=*taxa[childID].getGapPattern(); logP+=getTransProbs_nonLinked(*hmm,parentGaps,childGaps); } return logP; }
int main() { Log::l("main", "Begin"); //Matrix a(2, 2); //float aValues[4] = {4, 5, 2, 7}; Matrix a(4, 4); float aValues[] = {2, -4, 1, -3, 4, 16, -3, 1, 6, -2, -5, 1, -2, -8, 1, -1}; a.fill(aValues); Log::l("main", "Matrix a"); a.print(); //Matrix b(2, 1); //float bValues[] = {8, 3}; Matrix b(4, 1); float bValues[] = {6, -10, -3, 0}; b.fill(bValues); Log::l("main", "Matrix b"); b.print(); MatrixAugmented augmented(&a, &b); Log::l("main", "MatrixAugmented augmented"); augmented.print(); Matrix results = MatrixAugmented::Gauss(augmented); Log::l("main", "Matrix results"); results.print(); Matrix verify = Matrix::Multiply(a, results); Log::l("main", "Matrix verify"); verify.print(); Log::l("main", "End"); return 0; }
Matrix Matrix::invert() const { assert (mRows == mCols); // stick the identity matrix on right side Matrix augmented(mRows, 2*mRows); double temporary, r; int i, j, k, temp; int dimension = mRows; /* copy matrix over to left side of augmented matrix */ for (i = 1; i <= dimension; i++) { for (j = 1; j <= dimension; j++) { augmented(i,j) = p[i-1][j-1]; } } /* augmenting with identity matrix of similar dimensions */ for (i = 1; i <= dimension; i++) { for (j = dimension + 1; j <= 2*dimension; j++) { if (i == j - dimension) { augmented(i,j) = 1; } else { augmented(i,j) = 0; } } } /* using gauss-jordan elimination */ for(j = 1; j <= dimension; j++) { temp = j; /* finding maximum jth column element in last (dimension-j) rows */ for (i = j+1; i <= dimension; i++) { if (abs(augmented(i,j)) > abs(augmented(temp,j))) { temp = i; } } /* swapping row which has maximum jth column element */ if (temp != j) { for (k = 1; k <= 2*dimension; k++) { temporary = augmented(j,k); augmented(j,k) = augmented(temp,k); augmented(temp,k) = temporary; } } /* performing row operations to form required identity matrix out of the input matrix */ for (i = 1; i <= dimension; i++) { if (i != j) { r = augmented(i,j); for (k = 1; k <= 2*dimension; k++) { augmented(i,k) -= (augmented(j,k) / augmented(j,j)) * r; } } else { r = augmented(i,j); for(k = 1; k <= 2*dimension; k++) { augmented(i,k) /= r; } } } } /* The inverse is left on the right side of the augmented matrix. Retrieve it. */ Matrix inverse(mRows, mCols); for (i = 1; i <= dimension; i++) { for (j = 1; j <= dimension; j++) { inverse(i,j) = augmented(i,j + dimension); } } return inverse; }