-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelsubst.h
474 lines (386 loc) · 13.5 KB
/
modelsubst.h
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// C++ Interface: substmodel
//
// Description:
//
//
// Author: BUI Quang Minh, Steffen Klaere, Arndt von Haeseler <minh.bui@univie.ac.at>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef SUBSTMODEL_H
#define SUBSTMODEL_H
#include <string>
#include "utils/tools.h"
#include "utils/optimization.h"
#include "utils/checkpoint.h"
#include "phylo-yaml/statespace.h"
using namespace std;
const char OPEN_BRACKET = '{';
const char CLOSE_BRACKET = '}';
class PhyloTree;
/**
Substitution model abstract class
@author BUI Quang Minh, Steffen Klaere, Arndt von Haeseler <minh.bui@univie.ac.at>
*/
class ModelSubst: public Optimization, public CheckpointFactory
{
friend class ModelFactory;
friend class PartitionModel;
friend class IQTreeMix;
public:
/**
constructor
@param nstates number of states, e.g. 4 for DNA, 20 for proteins.
*/
ModelSubst(int nstates);
/**
@return the number of dimensions
*/
virtual int getNDim() { return 0; }
/**
@return the number of dimensions corresponding to state frequencies
*/
virtual int getNDimFreq() { return 0; }
/**
* @return model name
*/
virtual string getName() { return name; }
/**
* @return model name with parameters in form of e.g. GTR{a,b,c,d,e,f}
*/
virtual string getNameParams(bool show_fixed_params = false) { return name; }
/**
@return TRUE if model is time-reversible, FALSE otherwise
*/
virtual bool isReversible() { return true; };
/** return true if using reversible likelihood kernel, false for using non-reversible kernel */
bool useRevKernel() {
return isReversible() && !Params::getInstance().kernel_nonrev;
};
/**
fix parameters of the model
@param fix true to fix, false to not fix
@return the current state of fixing parameters
*/
virtual bool fixParameters(bool fix) {
bool current = fixed_parameters;
fixed_parameters = fix;
return current;
}
/**
* @return TRUE if this is a site-specific model, FALSE otherwise
*/
virtual bool isSiteSpecificModel() { return false; }
/**
* @return TRUE if this is a mixture model, FALSE otherwise
*/
virtual bool isMixture() { return false; }
/**
* @return TRUE if this is a liemarkov model, FALSE otherwise
*/
virtual bool isLieMarkov() { return false; }
/**
* @return TRUE if this is a mixture model and all model components share the same rate matrix, FALSE otherwise
*/
virtual bool isMixtureSameQ() { return false; }
/**
* @return TRUE if this is a DNA error model, FALSE otherwise
*/
virtual bool containDNAerror() { return false; }
/**
* get the dna error probability, by default error probability = 0
*/
virtual double getDNAErrProb(int mixture_index = 0) { return 0; }
/**
* Confer to modelpomo.h.
*
* @return TRUE if PoMo is being used, FALSE otherise.
*/
virtual bool isPolymorphismAware() { return false; }
/**
* @return the number of mixture model components
*/
virtual int getNMixtures() { return 1; }
/**
* @param cat mixture class
* @return weight of a mixture model component
*/
virtual double getMixtureWeight(int cat) { return 1.0; }
/**
* @param cat mixture class
* @return weight of a mixture model component
*/
virtual void setMixtureWeight(int cat, double weight) {}
/**
* @param cat mixture class
* @return weight of a mixture model component
*/
virtual void setFixMixtureWeight(bool fix_prop) {}
/**
* @param cat mixture class ID
* @return corresponding mixture model component
*/
virtual ModelSubst* getMixtureClass(int cat) { return NULL; }
/**
* @param cat mixture class ID
* @param m mixture model class to set
*/
virtual void setMixtureClass(int cat, ModelSubst* m) { }
/**
@return the number of rate entries, equal to the number of elements
in the upper-diagonal of the rate matrix (since model is reversible)
*/
virtual int getNumRateEntries() { return num_states*(num_states-1)/2; }
/**
set num_params variable
*/
virtual void setNParams(int num_params) {}
/**
get num_params variable
*/
virtual int getNParams() {
return 0;
}
/**
* get the size of transition matrix, default is num_states*num_states.
* can be changed for e.g. site-specific model
*/
virtual int getTransMatrixSize() { return num_states * num_states; }
/**
compute the transition probability matrix. One should override this function when defining new model.
The default is the Juke-Cantor model, valid for all kind of data (DNA, AA, Codon, etc)
@param time time between two events
@param mixture (optional) class for mixture model
@param selected_row (optional) only compute the entries of one selected row. By default, compute all rows
@param trans_matrix (OUT) the transition matrix between all pairs of states.
Assume trans_matrix has size of num_states * num_states.
*/
virtual void computeTransMatrix(double time, double *trans_matrix, int mixture = 0, int selected_row = -1);
/**
compute the transition probability between two states.
One should override this function when defining new model.
The default is the Juke-Cantor model, valid for all kind of data (DNA, AA, Codon, etc)
@param time time between two events
@param state1 first state
@param state2 second state
*/
virtual double computeTrans(double time, int state1, int state2);
/**
compute the transition probability between two states at a specific model ID, useful for partition model
One should override this function when defining new model.
The default is the Juke-Cantor model, valid for all kind of data (DNA, AA, Codon, etc)
@param time time between two events
@param model_id model ID
@param state1 first state
@param state2 second state
*/
virtual double computeTrans(double time, int model_id, int state1, int state2);
/**
compute the transition probability and its 1st and 2nd derivatives between two states.
One should override this function when defining new model.
The default is the Juke-Cantor model, valid for all kind of data (DNA, AA, Codon, etc)
@param time time between two events
@param state1 first state
@param state2 second state
@param derv1 (OUT) 1st derivative
@param derv2 (OUT) 2nd derivative
*/
virtual double computeTrans(double time, int state1, int state2, double &derv1, double &derv2);
/**
compute the transition probability and its 1st and 2nd derivatives between two states at a specific model ID
One should override this function when defining new model.
The default is the Juke-Cantor model, valid for all kind of data (DNA, AA, Codon, etc)
@param time time between two events
@param model_id model ID
@param state1 first state
@param state2 second state
@param derv1 (OUT) 1st derivative
@param derv2 (OUT) 2nd derivative
*/
virtual double computeTrans(double time, int model_id, int state1, int state2, double &derv1, double &derv2);
/**
* @return pattern ID to model ID map, useful for e.g., partition model
* @param ptn pattern ID of the alignment
*/
virtual int getPtnModelID(int ptn) { return 0; }
/**
* Get the rate parameters like a,b,c,d,e,f for DNA model!!!
Get the above-diagonal entries of the rate matrix, assuming that the last element is 1.
ONE SHOULD OVERRIDE THIS FUNCTION WHEN DEFINING NEW MODEL!!!
The default is equal rate of 1 (JC Model), valid for all kind of data.
@param rate_mat (OUT) upper-triangle rate matrix. Assume rate_mat has size of num_states*(num_states-1)/2
*/
virtual void getRateMatrix(double *rate_mat);
/**
Get the rate matrix Q. One should override this function when defining new model.
The default is equal rate of 1 (JC Model), valid for all kind of data.
@param rate_mat (OUT) upper-triagle rate matrix. Assume rate_mat has size of num_states*(num_states-1)/2
*/
virtual void getQMatrix(double *q_mat, int mixture = 0);
/**
compute the state frequency vector. One should override this function when defining new model.
The default is equal state sequency, valid for all kind of data.
@param mixture (optional) class for mixture model
@param[out] state_freq state frequency vector. Assume state_freq has size of num_states
*/
virtual void getStateFrequency(double *state_freq, int mixture = 0);
/**
set the state frequency vector.
@param state_freq state frequency vector. Assume state_freq has size of num_states
*/
virtual void setStateFrequency(double *state_freq);
/**
get frequency type
@return frequency type
*/
virtual StateFreqType getFreqType() { return FREQ_EQUAL; }
/**
set the associated tree
@param tree the associated tree
*/
virtual void setTree(PhyloTree *tree) {}
/** for reversible models, multiply likelihood with inverse eigenvectors for fast pruning algorithm
@param[in/out] state_lk state likelihood multiplied with inverse eigenvectors
*/
void multiplyWithInvEigenvector(double *state_lk);
/** compute the tip likelihood vector of a state for Felsenstein's pruning algorithm
@param state character state
@param[out] state_lk state likehood vector of size num_states
*/
virtual void computeTipLikelihood(PML::StateType state, double *state_lk);
/**
allocate memory for a transition matrix. One should override this function when defining new model
such as Gamma model. The default is to allocate a double vector of size num_states * num_states. This
is equivalent to the memory needed by a square matrix.
@return the pointer to the newly allocated transition matrix
*/
virtual double *newTransMatrix();
/**
compute the transition probability matrix.and the derivative 1 and 2
@param time time between two events
@param mixture (optional) class for mixture model
@param trans_matrix (OUT) the transition matrix between all pairs of states.
Assume trans_matrix has size of num_states * num_states.
@param trans_derv1 (OUT) the 1st derivative matrix between all pairs of states.
@param trans_derv2 (OUT) the 2nd derivative matrix between all pairs of states.
*/
virtual void computeTransDerv(double time, double *trans_matrix,
double *trans_derv1, double *trans_derv2, int mixture = 0);
/**
decompose the rate matrix into eigenvalues and eigenvectors
*/
virtual void decomposeRateMatrix() {}
/**
set number of optimization steps
@param opt_steps number of optimization steps
*/
virtual void setOptimizeSteps(int optimize_steps) { }
/**
optimize model parameters. One should override this function when defining new model.
The default does nothing since it is a Juke-Cantor type model, hence no parameters involved.
@param epsilon accuracy of the parameters during optimization
@return the best likelihood
*/
virtual double optimizeParameters(double gradient_epsilon) { return 0.0; }
/**
* @return TRUE if parameters are at the boundary that may cause numerical unstability
*/
virtual bool isUnstableParameters() { return false; }
/**
write information
@param out output stream
*/
virtual void writeInfo(ostream &out) {}
/**
report model
@param out output stream
*/
virtual void report(ostream &out) {}
virtual double *getEigenvalues() const {
return NULL;
}
virtual double *getEigenvectors() const {
return NULL;
}
virtual double *getInverseEigenvectors() const {
return nullptr;
}
virtual double *getInverseEigenvectorsTransposed() const {
return nullptr;
}
/**
* compute the memory size for the model, can be large for site-specific models
* @return memory size required in bytes
*/
virtual uint64_t getMemoryRequired() {
return num_states*sizeof(double);
}
/** @return true if model is a mixture model and it's fused with site_rate */
virtual bool isFused(){
return false;
};
/**
* get the underlying mutation model, used with PoMo model
*/
virtual ModelSubst *getMutationModel() { return this; }
/*****************************************************
Checkpointing facility
*****************************************************/
/**
start structure for checkpointing
*/
virtual void startCheckpoint();
/**
save object into the checkpoint
*/
virtual void saveCheckpoint();
/**
restore object from the checkpoint
*/
virtual void restoreCheckpoint();
/**
number of states
*/
int num_states;
/**
name of the model
*/
string name;
/**
full name of the model
*/
string full_name;
/** true to fix parameters, otherwise false */
bool fixed_parameters;
/**
state frequencies
*/
double *state_freq;
/**
state frequency type
*/
StateFreqType freq_type;
/** state set for each sequence in the alignment */
//vector<vector<int> > seq_states;
/**
destructor
*/
virtual ~ModelSubst();
protected:
/**
this function is served for the multi-dimension optimization. It should pack the model parameters
into a vector that is index from 1 (NOTE: not from 0)
@param variables (OUT) vector of variables, indexed from 1
*/
virtual void setVariables(double *variables) {}
/**
this function is served for the multi-dimension optimization. It should assign the model parameters
from a vector of variables that is index from 1 (NOTE: not from 0)
@param variables vector of variables, indexed from 1
@return TRUE if parameters are changed, FALSE otherwise (2015-10-20)
*/
virtual bool getVariables(double *variables) { return false; }
};
#endif