-
Notifications
You must be signed in to change notification settings - Fork 3
/
modelfactory.h
223 lines (187 loc) · 7.47 KB
/
modelfactory.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
/***************************************************************************
* Copyright (C) 2009 by BUI Quang Minh *
* minh.bui@univie.ac.at *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MODELFACTORY_H
#define MODELFACTORY_H
#include "tools.h"
#include "modelsubst.h"
#include "rateheterogeneity.h"
/**
Store the transition matrix corresponding to evolutionary time so that one must not compute again.
For efficiency purpose esp. for protein (20x20) or codon (61x61).
The values of the map contain 3 matricies consecutively: transition matrix, 1st, and 2nd derivative
@author BUI Quang Minh <minh.bui@univie.ac.at>
*/
class ModelFactory : public unordered_map<int, double*>, public Optimization
{
public:
/**
constructor
create substitution model with possible rate heterogeneity. Create proper class objects
for two variables: model and site_rate. It takes the following field of params into account:
model_name, num_rate_cats, freq_type, store_trans_matrix
@param params program parameters
@param tree associated phylogenetic tree
*/
ModelFactory(Params ¶ms, PhyloTree *tree);
/**
blank constructor
*/
ModelFactory();
/**
* create a substitution model
* @param model_str model nme
* @param freq_type state frequency type
* @param freq_params frequency parameters
* @param tree associated phylo tree
* @param count_rates TRUE to assign rates counted from alignment, FALSE to not initialize rates
* @return substitution model created
*/
ModelSubst *createModel(string model_str, StateFreqType freq_type, string freq_params,
PhyloTree *tree, bool count_rates = true);
/**
* read site specific state frequency vectors from a file to create corresponding model (Ingo's idea)
* @param aln input alignment
* @param site_freq_file file name
* @param site_model (OUT) site to model ID map
* @param freq_vec (OUT) vector of frequency vectors
*/
void readSiteFreq(Alignment *aln, char* site_freq_file, IntVector &site_model, vector<double*> &freq_vec);
/**
get the name of the model
*/
//string getModelName();
void writeInfo(ostream &out);
/**
Start to store transition matrix for efficiency
*/
void startStoringTransMatrix();
/**
Stop storing transition matrix, e.g., when optimizing model parameters
*/
void stopStoringTransMatrix();
/**
Wrapper for computing the transition probability matrix from the model. It use ModelFactory
that stores matrix computed before for effiency purpose.
@param time time between two events
@param trans_matrix (OUT) the transition matrix between all pairs of states.
Assume trans_matrix has size of num_states * num_states.
*/
void computeTransMatrix(double time, double *trans_matrix);
/**
* wrapper for computing transition matrix times state frequency vector
* @param time time between two events
* @param state_freq state frequency vector
* @param trans_matrix (OUT) the transition matrix between all pairs of states.
* Assume trans_matrix has size of num_states * num_states.
*/
void computeTransMatrixFreq(double time, double *state_freq, double *trans_matrix);
/**
Wrapper for computing the transition probability between two states.
@param time time between two events
@param state1 first state
@param state2 second state
*/
double computeTrans(double time, int state1, int state2);
/**
Wrapper for computing the transition probability between two states
@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);
/**
Wrapper for computing the transition probability matrix and the derivative 1 and 2 from the model.
It use ModelFactory that stores matrix computed before for effiency purpose.
@param time time between two events
@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.
*/
void computeTransDerv(double time, double *trans_matrix,
double *trans_derv1, double *trans_derv2);
void computeTransDervFreq(double time, double rate_val, double *state_freq, double *trans_matrix,
double *trans_derv1, double *trans_derv2);
/**
destructor
*/
virtual ~ModelFactory();
/**
* @return #parameters of the model + # branches
*/
virtual int getNParameters();
/**
optimize model parameters and tree branch lengths
@param fixed_len TRUE to fix branch lengths, default is false
@return the best likelihood
*/
virtual double optimizeParameters(bool fixed_len = false, bool write_info = true, double epsilon = 0.001);
/**
pointer to the model, will not be deleted when deleting ModelFactory object
*/
ModelSubst *model;
/**
pointer to the site-rate heterogeneity, will not be deleted when deleting ModelFactory object
*/
RateHeterogeneity *site_rate;
/**
TRUE to store transition matrix into this hash table for computation efficiency
*/
bool store_trans_matrix;
/**
TRUE for storing process
*/
bool is_storing;
/**
* optimize model and site_rate parameters
*/
double optimizeParametersOnly(double epsilon);
/************* FOLLOWING FUNCTIONS SERVE FOR JOINT OPTIMIZATION OF MODEL AND RATE PARAMETERS *******/
/**
* TRUE to optimize all parameters simultaneously, default: FALSE
*/
bool joint_optimize;
/**
return the number of dimensions
*/
virtual int getNDim();
/**
the target function which needs to be optimized
@param x the input vector x
@return the function value at x
*/
virtual double targetFunk(double x[]);
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
*/
virtual void getVariables(double *variables);
};
#endif