-
Notifications
You must be signed in to change notification settings - Fork 4
/
Train.cpp
executable file
·375 lines (336 loc) · 14.1 KB
/
Train.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
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
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <set>
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <iomanip>
#include <string>
#include <string.h>
#include "Train.h"
#include "Defaults.h"
int first_step = 1;
VF initDistrib(NumMatrixTypes);
VF gapOpen(2 * NumInsertStates);
VF gapExtend(2 * NumInsertStates);
VVF emitPairs(256, VF(256, 1e-10));
VF emitSingle(256, 1e-5);
string alphabet = alphabetDefault;
string parametersOutputFilename = "tmp.train";
Train::Train(int argc, char* argv[])
{
//parse program parameters
SafeVector<string> sequenceNames = ParseParams(argc, argv);
//initialize arguments for partition function
ReadParameters();
// build new model for aligning
ProbabilisticModel model (initDistrib, gapOpen, gapExtend, emitPairs, emitSingle);
// prepare to average parameters
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] = 0;
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] = 0;
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] = 0;
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] = 0;
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] = 0;
// align each file individually
for (int i = 0; i < (int) sequenceNames.size(); i++)
{
VF thisInitDistrib (NumMatrixTypes);
VF thisGapOpen (2*NumInsertStates);
VF thisGapExtend (2*NumInsertStates);
VVF thisEmitPairs (256, VF (256, 1e-10));
VF thisEmitSingle (256, 1e-5);
MultiSequence *sequences = new MultiSequence();
assert(sequences);
sequences->LoadMFA (sequenceNames[i], true);
// align sequences
DoAlignTrain (sequences, model, thisInitDistrib, thisGapOpen, thisGapExtend, thisEmitPairs, thisEmitSingle, first_step);
// add in contribution of the derived parameters
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] += thisInitDistrib[i];
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] += thisGapOpen[i];
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] += thisGapExtend[i];
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] += thisEmitPairs[i][j];
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] += thisEmitSingle[i];
delete sequences;
}
// compute new parameters and print them out
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] /= (int) sequenceNames.size();
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] /= (int) sequenceNames.size();
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] /= (int) sequenceNames.size();
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] /= (int) sequenceNames.size();
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] /= sequenceNames.size();
PrintParameters (initDistrib, gapOpen, gapExtend, emitPairs, emitSingle,
parametersOutputFilename.c_str());
}
Train::~Train()
{
}
/////////////////////////////////////////////////////////////////
// PrintParameters()
/////////////////////////////////////////////////////////////////
void Train::PrintParameters (const VF &initDistrib, const VF &gapOpen,
const VF &gapExtend, const VVF &emitPairs, const VF &emitSingle, const char *filename)
{
// if a file name is specified
if (filename)
{
// attempt to open the file for writing
FILE *file = fopen (filename, "w");
if (!file)
{
cerr << "ERROR: Unable to write parameter file: " << filename << endl;
exit (1);
}
// if successful, then write the parameters to the file
for (int i = 0; i < NumMatrixTypes; i++) fprintf (file, "%.10f ", initDistrib[i]);
fprintf (file, "\n");
for (int i = 0; i < 2*NumInsertStates; i++) fprintf (file, "%.10f ", gapOpen[i]);
fprintf (file, "\n");
for (int i = 0; i < 2*NumInsertStates; i++) fprintf (file, "%.10f ", gapExtend[i]);
fprintf (file, "\n");
fprintf (file, "%s\n", alphabet.c_str());
for (int i = 0; i < (int) alphabet.size(); i++)
{
for (int j = 0; j <= i; j++)
fprintf (file, "%.10f ", emitPairs[(unsigned char) alphabet[i]][(unsigned char) alphabet[j]]);
fprintf (file, "\n");
}
for (int i = 0; i < (int) alphabet.size(); i++)
fprintf (file, "%.10f ", emitSingle[(unsigned char) alphabet[i]]);
fprintf (file, "\n");
fclose (file);
}
}
/////////////////////////////////////////////////////////////////
// doAlign()
//
// First computes all pairwise posterior probability matrices.
// Then, computes new parameters if training, or a final
// alignment, otherwise.
/////////////////////////////////////////////////////////////////
void Train::DoAlignTrain(MultiSequence *sequences,
const ProbabilisticModel &model, VF &initDistrib, VF &gapOpen,
VF &gapExtend, VVF &emitPairs, VF &emitSingle, int first_step)
{
assert(sequences);
//get the number of sequences
const int numSeqs = sequences->GetNumSequences();
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] = 0;
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] = 0;
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] = 0;
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] = 0;
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] = 0;
for (int a = 0; a < numSeqs - 1; a++)
{
for (int b = a + 1; b < numSeqs; b++)
{
Sequence *seq1 = sequences->GetSequence(a);
Sequence *seq2 = sequences->GetSequence(b);
//posterior probability matrix
VF* forward ;
VF* backward;
//Double Affine pair-HMM
if(first_step == 1)
{
// compute forward and backward probabilities
forward = model.ComputeForwardMatrix(seq1, seq2);
assert(forward);
backward = model.ComputeBackwardMatrix(seq1, seq2);
assert(backward);
}
else
{
//local pair-HMM
// compute forward and backward probabilities
forward = model.ComputeForwardMatrix(seq1, seq2,false);
assert(forward);
backward = model.ComputeBackwardMatrix(seq1, seq2,false);
assert(backward);
}
// compute new parameters
VF thisInitDistrib (NumMatrixTypes);
VF thisGapOpen (2*NumInsertStates);
VF thisGapExtend (2*NumInsertStates);
VVF thisEmitPairs (256, VF (256, 1e-10));
VF thisEmitSingle (256, 1e-5);
model.ComputeNewParameters (seq1, seq2, *forward, *backward, thisInitDistrib, thisGapOpen, thisGapExtend, thisEmitPairs, thisEmitSingle, true);
// add in contribution of the derived parameters
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] += thisInitDistrib[i];
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] += thisGapOpen[i];
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] += thisGapExtend[i];
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] += thisEmitPairs[i][j];
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] += thisEmitSingle[i];
delete forward;
delete backward;
}
}
// compute new parameters
for (int i = 0; i < (int) initDistrib.size(); i++) initDistrib[i] /= numSeqs * (numSeqs - 1) / 2;
for (int i = 0; i < (int) gapOpen.size(); i++) gapOpen[i] /= numSeqs * (numSeqs - 1) / 2;
for (int i = 0; i < (int) gapExtend.size(); i++) gapExtend[i] /= numSeqs * (numSeqs - 1) / 2;
for (int i = 0; i < (int) emitPairs.size(); i++)
for (int j = 0; j < (int) emitPairs[i].size(); j++) emitPairs[i][j] /= numSeqs * (numSeqs - 1) / 2;
for (int i = 0; i < (int) emitSingle.size(); i++) emitSingle[i] /= numSeqs * (numSeqs - 1) / 2;
}
/////////////////////////////////////////////////////////////////
// ReadParameters()
//
// Read initial distribution, transition, and emission
// parameters from a file.
/////////////////////////////////////////////////////////////////
void Train::ReadParameters()
{
emitPairs = VVF(256, VF(256, 1e-10));
emitSingle = VF(256, 1e-5);
if (NumInsertStates == 1)
{
for (int i = 0; i < NumMatrixTypes; i++)
initDistrib[i] = initDistrib1Default[i];
for (int i = 0; i < 2 * NumInsertStates; i++)
gapOpen[i] = gapOpen1Default[i];
for (int i = 0; i < 2 * NumInsertStates; i++)
gapExtend[i] = gapExtend1Default[i];
}
else if (NumInsertStates == 2)
{
for (int i = 0; i < NumMatrixTypes; i++)
initDistrib[i] = initDistrib2Default[i];
for (int i = 0; i < 2 * NumInsertStates; i++)
gapOpen[i] = gapOpen2Default[i];
for (int i = 0; i < 2 * NumInsertStates; i++)
gapExtend[i] = gapExtend2Default[i];
}
else
{
cerr
<< "ERROR: No default initial distribution/parameter settings exist"
<< endl << " for " << NumInsertStates
<< " pairs of insert states. Use --paramfile." << endl;
exit(1);
}
alphabet = alphabetDefault;
for (int i = 0; i < (int) alphabet.length(); i++)
{
emitSingle[(unsigned char) tolower(alphabet[i])] =
emitSingleDefault[i];
emitSingle[(unsigned char) toupper(alphabet[i])] =
emitSingleDefault[i];
for (int j = 0; j <= i; j++)
{
emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) tolower(
alphabet[j])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) tolower(alphabet[i])][(unsigned char) toupper(
alphabet[j])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) tolower(
alphabet[j])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) toupper(alphabet[i])][(unsigned char) toupper(
alphabet[j])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) tolower(
alphabet[i])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) tolower(alphabet[j])][(unsigned char) toupper(
alphabet[i])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) tolower(
alphabet[i])] = emitPairsDefault[i][j];
emitPairs[(unsigned char) toupper(alphabet[j])][(unsigned char) toupper(
alphabet[i])] = emitPairsDefault[i][j];
}
}
}
/////////////////////////////////////////////////////////////////
// GetInteger()
//
// Attempts to parse an integer from the character string given.
// Returns true only if no parsing error occurs.
/////////////////////////////////////////////////////////////////
bool GetInteger(char *data, int *val)
{
char *endPtr;
long int retVal;
assert(val);
errno = 0;
retVal = strtol(data, &endPtr, 0);
if (retVal == 0 && (errno != 0 || data == endPtr))
return false;
if (errno != 0 && (retVal == LONG_MAX || retVal == LONG_MIN))
return false;
if (retVal < (long) INT_MIN || retVal > (long) INT_MAX)
return false;
*val = (int) retVal;
return true;
}
/////////////////////////////////////////////////////////////////
// ParseParams()
//
// Parse all command-line options.
/////////////////////////////////////////////////////////////////
SafeVector<string> Train::ParseParams(int argc, char **argv)
{
if (argc < 2)
{
exit(1);
}
SafeVector<string> sequenceNames;
int tempInt;
for (int i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (!strcmp(argv[i], "-p")
|| !strcmp(argv[i], "-program"))
{
if (!GetInteger(argv[++i], &tempInt) && i < argc - 1)
{
first_step = 0;
}
else
{
if (tempInt >= 2 || tempInt < 0)
{
first_step = 0;
}
else
{
if(tempInt == 0)
{
first_step = 0;
}
else if(tempInt == 1)
{
first_step = 1;
}
}
}
}
else if (!strcmp(argv[i], "-f")
|| !strcmp(argv[i], "-file"))
{
parametersOutputFilename = string(argv[++i]);
}
// bad arguments
else
{
cerr << "ERROR: Unrecognized option: " << argv[i] << endl;
exit(1);
}
}
else
{
sequenceNames.push_back(string(argv[i]));
}
}
return sequenceNames;
}
int main(int argc, char* argv[])
{
Train train(argc, argv);
return 0;
}