-
Notifications
You must be signed in to change notification settings - Fork 0
/
FitchFelsenstein.C
381 lines (328 loc) · 10.4 KB
/
FitchFelsenstein.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/****************************************************************
FitchFelsenstein.C
william.majoros@duke.edu
This is open-source software, governed by the Gnu General Public License (GPL) version 3
(see www.opensource.org).
****************************************************************/
#include <iostream>
#include <math.h>
#include "BOOM/DnaAlphabet.H"
#include "BOOM/AminoAlphabet.H"
#include "BOOM/Array2D.H"
#include "BOOM/Vector.H"
#include "BOOM/SumLogProbs.H"
#include "BOOM/BitSet.H"
#include "FitchFelsenstein.H"
#include "SafeAdd.H"
using namespace std;
using namespace BOOM;
// -----------------------------------------------------------------
// This is where the actual likelihood algorithm is implemented...
// -----------------------------------------------------------------
struct FitchLikelihooder : public TreeVisitor
{
public:
Array2D<double> L; // the dynamic programming matrix
MultSeqAlignment &A;
int numNodes, numAlpha, column, order;
Alphabet α
double likelihood;
RootNode *root; // ### for debugging only
BitSet gapSymbols;
Array1D<double> V;
FitchLikelihooder(int numNodes,int numAlpha,Alphabet &alpha,
MultSeqAlignment &A,int column,RootNode *root,int order)
: A(A), numNodes(numNodes), numAlpha(numAlpha),
alpha(alpha), L(numNodes,numAlpha), column(column),
order(order), root(root), likelihood(NEGATIVE_INFINITY),
gapSymbols(A.getGapSymbols()), V(numAlpha)
{
L.setAllTo(NEGATIVE_INFINITY);
V.setAllTo(NEGATIVE_INFINITY);
}
void reinit(int col)
{
L.setAllTo(NEGATIVE_INFINITY);
column=col;
}
virtual void processNode(LeafNode &u) // DO NOT CHANGE THIS FUNCTION
{
int id=u.getID();
Symbol a=A.getIthTrack(id)[column];
Array2D<double>::RowIn2DArray<double> row=L[id];
if(gapSymbols.isMember(a))
for(Symbol i=0 ; i<numAlpha; ++i)
row[i]=0;// missing data -- same as Seipel & Haussler
else
for(Symbol i=0 ; i<numAlpha; ++i)
row[i]=(i==a ? 0 : NEGATIVE_INFINITY);
}
virtual void processNode(RootNode &u) {
// PRECONDITION: no gaps in target sequence!
int id=u.getID();
Symbol a=A.getIthTrack(id)[column];
int childID=u.getChild()->getID();
NthOrdSubstMatrix &Pt=*u.getSubstMatrix();
likelihood=processInternalChild(a,id,childID,Pt);
}
virtual void processNode(InternalNode &u) {
int id=u.getID();
NthOrdSubstMatrix &leftPt=*u.getLeftSubstMatrix();
NthOrdSubstMatrix &rightPt=*u.getRightSubstMatrix();
int left=u.getLeft()->getID(), right=u.getRight()->getID();
Array2D<double>::RowIn2DArray<double> row=L[id];
Symbol a=A.getIthTrack(id)[column];
if(gapSymbols.isMember(a))
for(a=0 ; a<numAlpha ; ++a) {
if(gapSymbols.isMember(a)) continue;
row[a]=
processInternalChild(a,id,left,leftPt)+
processInternalChild(a,id,right,rightPt);
}
else {
for(Symbol b=0 ; b<numAlpha ; ++b) row[b]=NEGATIVE_INFINITY;
double l=processInternalChild(a,id,left,leftPt);
double r=processInternalChild(a,id,right,rightPt);
row[a]=l+r;
}
}
double processInternalChild(Symbol parentSymbol,int parentID,int child,
NthOrdSubstMatrix &noPt) {
AlignmentSeq &track=A.getIthTrack(parentID);
AlignmentSeq &childTrack=A.getIthTrack(child);
int contextBegin=column-order;
if(contextBegin<0) contextBegin=0;
int contextLen=column-contextBegin;
Sequence context;
track.getSeq().getSubsequence(contextBegin,contextLen,context);//ACO
//A.getIthTrack(root->getID()).getSeq().getSubsequence(contextBegin,contextLen,context);//TRCO
int pos=MultSeqAlignment::rightmostGapPos(context,gapSymbols);
if(pos>=0) {
throw "this should not happen (FitchFelsenstein)::processInternalChild";
Sequence temp;
context.getSubsequence(pos+1,contextLen-pos-1,temp);
context=temp;
}
SubstitutionMatrix &Pt=*noPt.lookup(context,0,-1);
Array2D<double>::RowIn2DArray<double> row=L[child];
Symbol childSym=childTrack.getSeq()[column];
double ll;
if(gapSymbols.isMember(childSym)){
V.setAllTo(NEGATIVE_INFINITY);
for(Symbol a=0 ; a<numAlpha ; ++a) {
if(!gapSymbols.isMember(a)) {
V[(int)a]=safeAdd(row[a],Pt(parentSymbol,a));
}
}
ll=sumLogProbs(V);
}
else {
ll=safeAdd(row[childSym],Pt(parentSymbol,childSym));
}
return ll;
}
double getLikelihood()
{
return likelihood;
}
};
// -----------------------------------------------------------------
// constructor
// -----------------------------------------------------------------
FitchFelsenstein::FitchFelsenstein(int order,
Phylogeny &P,
NthOrdRateMatrix &Q,
MultSeqAlignment &A,
MolecularSequenceType T,
int numThreads)
: phylogeny(P),
Q(Q),
alignment(A),
seqType(T),
alphabet(T==DNA ? (Alphabet&) DnaAlphabet::global() :
(Alphabet&) AminoAlphabet::global()),
numThreads(numThreads),
threads(numThreads),
order(order)
{
numAlpha=alphabet.size();
attachPhylogenyNodes();
}
// -----------------------------------------------------------------
// attach phylogeny nodes
// -----------------------------------------------------------------
void FitchFelsenstein::attachPhylogenyNodes()
{
struct Attacher : public TreeVisitor
{
MultSeqAlignment &A;
NthOrdRateMatrix &Q;
int numNodes, largestID;
Attacher(MultSeqAlignment &A,NthOrdRateMatrix &Q)
: A(A), Q(Q), numNodes(0), largestID(0) {}
virtual void processNode(InternalNode &V)
{
V.setLeftSubstMatrix(Q.instantiate(V.getLeftDistance()));
V.setRightSubstMatrix(Q.instantiate(V.getRightDistance()));
int id=V.getID();
if(id>largestID) largestID=id;
++numNodes;
}
virtual void processNode(LeafNode &V)
{
int id=V.getID();
if(id>largestID) largestID=id;
++numNodes;
}
virtual void processNode(RootNode &V)
{
V.setSubstMatrix(Q.instantiate(V.getBranchLength()));
int id=V.getID();
if(id>largestID) largestID=id;
++numNodes;
}
};
Attacher A(alignment,Q);
phylogeny.postorderTraversal(A);
numNodes=A.numNodes;
largestID=A.largestID;
}
// -----------------------------------------------------------------
// log-likelihood for a single column
// -----------------------------------------------------------------
double FitchFelsenstein::logLikelihood(int column)
{
PhylogenyNode *root=phylogeny.getRoot();
if(root->getNodeType()!=ROOT_NODE) {
cout<<"You must re-root the phylogeny first"<<endl;
throw 0;
}
//int rootID=root->getID();
FitchLikelihooder L(largestID+1,numAlpha,alphabet,alignment,column,
root,order);
phylogeny.postorderTraversal(L);
return L.getLikelihood();
}
// -----------------------------------------------------------------
// log-likelihood summed over all columns
// -----------------------------------------------------------------
double FitchFelsenstein::logLikelihood()
{
FitchLikelihooder L(largestID+1,numAlpha,alphabet,alignment,0,
phylogeny.getRoot(),order);
double LL=0;
int numColumns=alignment.getLength();
for(int i=0 ; i<numColumns ; ++i) {
L.reinit(i);
phylogeny.postorderTraversal(L);
LL+=L.getLikelihood();
}
return LL;
}
// -----------------------------------------------------------------
// log-likelihood for a range of columns
// -----------------------------------------------------------------
double FitchFelsenstein::logLikelihood(int begin,int end)
{
FitchLikelihooder L(largestID+1,numAlpha,alphabet,alignment,begin,
phylogeny.getRoot(),order);
double LL=0;
for(int i=begin ; i<end ; ++i) {
L.reinit(i);
phylogeny.postorderTraversal(L);
LL+=L.getLikelihood();
}
return LL;
}
double FitchFelsenstein::logLikelihood3(int begin,int end)
{
return logLikelihoodInPhase(begin,end,2);
}
double FitchFelsenstein::logLikelihoodInPhase(int begin,int end,int phase)
{
FitchLikelihooder L(largestID+1,numAlpha,alphabet,alignment,begin,
phylogeny.getRoot(),order);
double LL=0;
for(int i=begin ; i<end ; ++i)
if(i%3==phase) {
L.reinit(i);
phylogeny.postorderTraversal(L);
LL+=L.getLikelihood();
}
return LL;
}
double FitchFelsenstein::logLikelihood_bestFrame(int begin,int end)
{
FitchLikelihooder L(largestID+1,numAlpha,alphabet,alignment,begin,
phylogeny.getRoot(),order);
double bestLL=NEGATIVE_INFINITY;
int bestSampleSize=0;
for(int frame=0 ; frame<3 ; ++frame) {
double frameLL=0;
int frameSampleSize=0;
for(int i=begin ; i<end ; ++i)
if(i%3==frame) {
L.reinit(i);
phylogeny.postorderTraversal(L);
frameLL+=L.getLikelihood();
++frameSampleSize;
}
if(bestSampleSize==0 ||
frameLL/frameSampleSize>bestLL/bestSampleSize) {
bestLL=frameLL;
bestSampleSize=frameSampleSize;
}
}
return bestLL;
}
// -----------------------------------------------------------------
// multi-threaded version
// -----------------------------------------------------------------
double FitchFelsenstein::logLikelihood_threaded()
{
// Spawn all the threads
int numColumns=alignment.getLength();
int colsPerThread=numColumns/numThreads;
int nextCol=0;
for(int i=0 ; i<numThreads ; ++i)
{
int begin=nextCol;
int end=begin+colsPerThread;
if(i+1==numThreads) end=numColumns;
FitchThread *thread=new FitchThread(*this,begin,end);
threads[i]=thread;
thread->start();
nextCol=end;
}
// Wait for all the threads to finish
double L=0;
for(int i=0 ; i<numThreads ; ++i)
{
FitchThread *thread=threads[i];
thread->join();
L+=thread->getLogLikelihood();
delete thread;
}
// Return the likelihood
return L;
}
/****************************************************************
FitchThread methods
****************************************************************/
FitchThread::FitchThread(FitchFelsenstein &alg,int begin,int end)
: alg(alg),
begin(begin),
end(end)
{
// ctor
}
void FitchThread::f()
{
L=0.0;
for(int i=begin ; i<end ; ++i)
L+=alg.logLikelihood(i);
}
double FitchThread::getLogLikelihood()
{
return L;
}