-
Notifications
You must be signed in to change notification settings - Fork 1
/
em.cpp
184 lines (143 loc) · 3.63 KB
/
em.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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2007 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <vector>
#include <map>
#include <cassert>
#include "plink.h"
#include "options.h"
#include "helper.h"
#include "genogroup.h"
#include "phase.h"
#include "haplowindow.h"
extern ofstream LOG;
using namespace std;
////////////////////////////////////////////////
// Original, single window EM algorithm, without
// genoGrouping -- this is now used for the meta-EM
void HaploPhase::performEM_original()
{
vector_t uc(nh, 0); // unambigous counts
vector_t ac(nh, 0); // ambigous counts
// Count numbers of unambigous haplotypes
// as these stay constant throughout EM
for (int i=0; i<P.n; i )
{
if (P.sample[i]->founder && include[i])
{
if (!ambig[i])
{
uc[hap1[i][0]] ;
if ( ! (haploid || (X && P.sample[i]->sex)))
uc[hap2[i][0]] ;
}
}
}
//////////////////
// Begin E-M
double sampleLogLikelihood = 0;
for (int j=0; j<= par::haplo_plem_meta_iter; j )
{
///////////
// E-step
for (int i=0; i<P.n; i )
{
if (P.sample[i]->founder && include[i])
{
if (ambig[i])
{
double s=0;
// Haploid phases...
if (haploid || (X && P.sample[i]->sex))
{
for (int z=0; z<hap1[i].size(); z )
{
pp[i][z] = f[hap1[i][z]];
s = pp[i][z];
}
}
else // ... or diploid
{
for (int z=0; z<hap1[i].size(); z )
{
pp[i][z] = f[hap1[i][z]] * f[hap2[i][z]];
if (hap1[i][z] != hap2[i][z])
pp[i][z] *= 2;
s = pp[i][z];
}
}
for (int z=0; z<hap1[i].size(); z )
pp[i][z] /= s;
}
}
}
///////////
// M-step
// unambiguous counts
for (int h=0; h<nh; h )
f[h] = uc[h];
// then add the fractional ones
for (int i=0; i<P.n; i )
if (P.sample[i]->founder && include[i])
if (ambig[i])
{
if (haploid || (X && P.sample[i]->sex))
{
for (int z=0; z<hap1[i].size(); z )
f[hap1[i][z]] = pp[i][z];
}
else
for (int z=0; z<hap1[i].size(); z )
{
f[hap1[i][z]] = pp[i][z];
f[hap2[i][z]] = pp[i][z];
}
}
// validN is the total number of *chromosomes*
for (int h=0; h<nh; h )
f[h] /= (double)validN;
///////////////////////
// Update likelihood
if (j % par::haplo_plem_meta_likelihood_iter == 0)
{
double lnl = 0;
for (int i=0; i<P.n; i )
{
if (P.sample[i]->founder && include[i])
{
double lk = 0;
if (haploid || (X && P.sample[i]->sex))
{
for (int z=0; z<hap1[i].size(); z )
lk = f[hap1[i][z]];
}
else
for (int z=0; z<hap1[i].size(); z )
{
lk = f[hap1[i][z]] * f[hap2[i][z]];
if (hap1[i][z] != hap2[i][z])
lk = f[hap1[i][z]] * f[hap2[i][z]];
}
lnl -= log(lk);
}
}
if (j > 0 &&
sampleLogLikelihood - lnl < par::haplo_plem_meta_tol )
{
break;
}
sampleLogLikelihood = lnl;
}
}
}