-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrateheterotachy.cpp
307 lines (266 loc) · 8.66 KB
/
rateheterotachy.cpp
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
//
// rateheterotachy.cpp
// iqtree
//
// Created by Minh Bui on 11/8/16.
//
//
#include "tree/phylotree.h"
#include "rateheterotachy.h"
RateHeterotachy::RateHeterotachy(int ncat, string params, PhyloTree *tree) : RateHeterogeneity() {
phylo_tree = tree;
prop = NULL;
fix_params = 0;
optimize_steps = 0;
setNCategory(ncat);
if (params.empty()) return;
DoubleVector params_vec;
try {
// detect the seperator
char separator = ',';
if (params.find('/') != std::string::npos)
separator = '/';
convert_double_vec_with_distributions(params.c_str(), params_vec, separator);
if (params_vec.size() != ncategory)
outError("Number of parameters for rate heterotachy model must equal number of categories");
int i;
double sum_prop;
for (i = 0, sum_prop = 0.0; i < ncategory; i++) {
prop[i] = params_vec[i];
sum_prop += prop[i];
}
if (fabs(sum_prop-1.0) > 1e-5)
{
outWarning("Normalizing category proportions so that sum of them not equal to 1");
normalize_frequencies(prop, ncategory, sum_prop);
}
// Minh: Please double check this one. It isn't quite so
// clear what fix_params is doing, as it seems to take values
// 0, 1 or 2. -- MDW
//BQM: that OK
if (!(tree->params->optimize_from_given_params)) {
fix_params = 1;
} // else fix_params == 0 still.
} catch (string &str) {
outError(str);
}
}
/**
destructor
*/
RateHeterotachy::~RateHeterotachy() {
delete [] prop;
prop = nullptr;
}
void RateHeterotachy::setNCategory(int ncat) {
ncategory = ncat;
if (optimize_steps == 0) {
optimize_steps = ncat * 100;
}
// initialize with gamma rates
delete [] prop;
prop = new double[ncategory];
int i;
for (i = 0; i < ncategory; i++)
prop[i] = (1.0-getPInvar())/ncategory;
name = "+H";
name += convertIntToString(ncategory);
full_name = "Rate heterotachy";
full_name += " with " + convertIntToString(ncategory) + " categories";
}
void RateHeterotachy::startCheckpoint() {
checkpoint->startStruct("RateHeterotachy" + convertIntToString(ncategory));
}
/**
save object into the checkpoint
*/
void RateHeterotachy::saveCheckpoint() {
startCheckpoint();
CKP_ARRAY_SAVE(ncategory, prop);
endCheckpoint();
RateHeterogeneity::saveCheckpoint();
}
/**
restore object from the checkpoint
*/
void RateHeterotachy::restoreCheckpoint() {
RateHeterogeneity::restoreCheckpoint();
startCheckpoint();
CKP_ARRAY_RESTORE(ncategory, prop);
endCheckpoint();
}
int RateHeterotachy::getNDim() {
if (fix_params) return 0;
return ncategory-1;
}
/**
* @return model name with parameters in form of e.g. GTR{a,b,c,d,e,f}
*/
string RateHeterotachy::getNameParams() {
stringstream str;
str << "+H" << ncategory << "{";
for (int i = 0; i < ncategory; i++) {
if (i > 0) str << ",";
str << prop[i];
}
str << "}";
return str.str();
}
void RateHeterotachy::writeInfo(ostream &out) {
if (fix_params != 2) {
out << "Heterotachy weights: ";
for (int i = 0; i < ncategory; i++)
out << " " << prop[i];
out << endl;
}
DoubleVector lenvec;
phylo_tree->treeLengths(lenvec);
out << "Heterotachy tree lengths:";
for (int j = 0; j < lenvec.size(); j++)
out << " " << lenvec[j];
out << endl;
}
void RateHeterotachy::writeParameters(ostream &out) {
for (int i = 0; i < ncategory; i++)
out << "\t" << prop[i];
}
/**
optimize parameters. Default is to optimize gamma shape
@return the best likelihood
*/
double RateHeterotachy::optimizeParameters(double gradient_epsilon) {
if (fix_params) {
return phylo_tree->computeLikelihood();
}
if (verbose_mode >= VB_MED) {
cout << "Optimizing " << name << " model parameters by EM algorithm..." << endl;
}
return optimizeWithEM();
}
double RateHeterotachy::optimizeWithEM() {
// first compute _pattern_lh_cat
phylo_tree->computePatternLhCat(WSL_RATECAT);
size_t nptn = phylo_tree->aln->getNPattern();
size_t nmix = ncategory;
double *new_prop = aligned_alloc<double>(nmix);
double *ratio_prop = aligned_alloc<double>(nmix);
// EM algorithm loop described in Wang, Li, Susko, and Roger (2008)
for (int step = 0; step < optimize_steps; step++) {
// E-step
if (step > 0) {
// convert _pattern_lh_cat taking into account new weights
for (size_t ptn = 0; ptn < nptn; ptn++) {
double *this_lk_cat = phylo_tree->_pattern_lh_cat + ptn*nmix;
for (size_t c = 0; c < nmix; c++) {
this_lk_cat[c] *= ratio_prop[c];
}
}
}
memset(new_prop, 0, nmix*sizeof(double));
for (size_t ptn = 0; ptn < nptn; ptn++) {
double *this_lk_cat = phylo_tree->_pattern_lh_cat + ptn*nmix;
double lk_ptn = phylo_tree->ptn_invar[ptn];
for (size_t c = 0; c < nmix; c++) {
lk_ptn += this_lk_cat[c];
}
ASSERT(lk_ptn != 0.0);
lk_ptn = phylo_tree->ptn_freq[ptn] / lk_ptn;
for (size_t c = 0; c < nmix; c++) {
new_prop[c] += this_lk_cat[c] * lk_ptn;
}
}
bool converged = true;
double new_pinvar = 0.0;
for (size_t c = 0; c < nmix; c++) {
new_prop[c] /= phylo_tree->getAlnNSite();
// Make sure that probabilities do not get zero
if (new_prop[c] < 1e-10) new_prop[c] = 1e-10;
// check for convergence
converged = converged && (fabs(prop[c]-new_prop[c]) < 1e-4);
ratio_prop[c] = new_prop[c] / prop[c];
if (std::isnan(ratio_prop[c])) {
cerr << "BUG: " << new_prop[c] << " " << prop[c] << " " << ratio_prop[c] << endl;
}
prop[c] = new_prop[c];
new_pinvar += prop[c];
}
new_pinvar = fabs(1.0 - new_pinvar);
if (new_pinvar > 1e-6) {
converged = converged && (fabs(getPInvar()-new_pinvar) < 1e-4);
// TODO fix p_pinvar
setPInvar(new_pinvar);
// phylo_tree->getRate()->setOptimizePInvar(false);
phylo_tree->computePtnInvar();
phylo_tree->clearAllPartialLH();
}
if (converged) break;
}
aligned_free(ratio_prop);
aligned_free(new_prop);
// aligned_free(lk_ptn);
return phylo_tree->computeLikelihood();
/*
size_t ptn, c;
size_t nptn = phylo_tree->aln->getNPattern();
size_t nmix = ncategory;
const double MIN_PROP = 1e-4;
double new_prop[nmix];
// EM algorithm loop described in Wang, Li, Susko, and Roger (2008)
// first compute _pattern_lh_cat
double score;
score = phylo_tree->computePatternLhCat(WSL_RATECAT);
memset(new_prop, 0, nmix*sizeof(double));
// E-step
// decoupled weights (prop) from _pattern_lh_cat to obtain L_ci and compute pattern likelihood L_i
for (ptn = 0; ptn < nptn; ptn++) {
double *this_lk_cat = phylo_tree->_pattern_lh_cat + ptn*nmix;
double lk_ptn = phylo_tree->ptn_invar[ptn];
for (c = 0; c < nmix; c++) {
lk_ptn += this_lk_cat[c];
}
assert(lk_ptn != 0.0);
lk_ptn = phylo_tree->ptn_freq[ptn] / lk_ptn;
// transform _pattern_lh_cat into posterior probabilities of each category
for (c = 0; c < nmix; c++) {
this_lk_cat[c] *= lk_ptn;
new_prop[c] += this_lk_cat[c];
}
}
// M-step, update weights according to (*)
int maxpropid = 0;
double new_pinvar = 0.0;
for (c = 0; c < nmix; c++) {
new_prop[c] = new_prop[c] / phylo_tree->getAlnNSite();
if (new_prop[c] > new_prop[maxpropid])
maxpropid = c;
}
// regularize prop
bool zero_prop = false;
for (c = 0; c < nmix; c++) {
if (new_prop[c] < MIN_PROP) {
new_prop[maxpropid] -= (MIN_PROP - new_prop[c]);
new_prop[c] = MIN_PROP;
zero_prop = true;
}
}
double sum_prop = 0.0;
for (c = 0; c < nmix; c++) {
// check for convergence
sum_prop += new_prop[c];
prop[c] = new_prop[c];
new_pinvar += new_prop[c];
}
new_pinvar = 1.0 - new_pinvar;
if (new_pinvar > 1e-4 && getPInvar() != 0.0) {
setPInvar(new_pinvar);
// setOptimizePInvar(false);
phylo_tree->computePtnInvar();
}
assert(fabs(sum_prop+new_pinvar-1.0) < MIN_PROP);
// sort the rates in increasing order
if (sorted_rates) {
// TODO sort tree lengths per category
}
return phylo_tree->computeLikelihood();
*/
}