forked from kausmees/prophaser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHaplotypePhaser.h
222 lines (164 loc) · 4.55 KB
/
HaplotypePhaser.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
#ifndef __HAPLOPHASER_H__
#define __HAPLOPHASER_H__
#include "Pedigree.h"
#include "VcfUtils.h"
#include <vector>
#include <chrono>
#include <algorithm>
#include "Pedigree.h"
#include "VcfUtils.h"
#include <vector>
#include <chrono>
#include <algorithm>
#include <Eigen/Dense>
#include <math.h>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::MatrixXi;
using Eigen::RowMajor;
using Eigen::Dynamic;
using namespace Eigen;
typedef Eigen::Matrix<int, Dynamic,Dynamic, RowMajor> rowmajdyn;
/**
* Represents the reference haplotypes at a loci as an ordered pair.
*
*/
struct ChromosomePair {
int first;
int second;
ChromosomePair(int a, int b) {
first = a;
second = b;
}
/**
* What of the three transition cases it is to move from this state to the given other state.
*
* cases:
*
* 0: both chromosomes switch reference
* 1: one switches
* 2: no switch
*/
int TransitionCase(ChromosomePair other) {
int num = 0;
if (first == other.first) {
num += 1;
}
if (second == other.second) {
num += 1;
}
return num;
}
};
template<class T>
class StepMemoizer
{
vector<double*> mainTable;
vector<double*> auxTable;
T* parent;
int num_markers;
bool dirup;
int step;
int auxAnchor = -1;
int totalAnchor;
using gent = void (T::*)(int, const double*, double*);
gent generator;
public:
StepMemoizer() = default;
// TODO: Copy assignment is dangerous.
StepMemoizer(T* parent, int num_markers, int num_states, bool dirup, int step, gent generator) : parent(parent), num_markers(num_markers), dirup(dirup), step(step), generator(generator) {
auxTable.resize(step - 1);
for (double*& t : auxTable) {
t = new double[num_states];
}
int numElems = (num_markers - 1) / step + 1;
mainTable.resize(numElems);
for (double*& t : mainTable) {
t = new double[num_states];
}
totalAnchor = dirup ? 0 : num_markers - 1;
}
double* operator [](int i) {
const int dir = dirup ? 1 : -1;
const int i2 = totalAnchor + i * dir;
const int anchor = i2 / step;
const int auxIndex = (i2 % step) - 1;
if (auxIndex == -1) {
return mainTable[anchor];
}
if (anchor != auxAnchor) {
double* prev = mainTable[anchor];
for (int j = 0; j < auxTable.size(); j++) {
int jorig = totalAnchor + (anchor * step + j + 1) * dir;
if (jorig < 0 || jorig >= num_markers) break;
(parent->*generator)(jorig, prev, auxTable[j]);
prev = auxTable[j];
}
auxAnchor = anchor;
}
return auxTable[auxIndex];
}
void fillAllButFirst() {
int dir = dirup ? 1 : -1;
for (int j = 1; j < mainTable.size(); j++) {
int jorig = totalAnchor + (j * step) * dir;
// Will trigger auxTable filling as needewd
(parent->*generator)(jorig, (*this)[jorig - dir], mainTable[j]);
}
}
~StepMemoizer() {
for (double* t : mainTable) {
delete[] t;
}
for (double* t : auxTable) {
delete[] t;
}
}
};
/**
Main haplotype phasing and imputation functionality.
*/
class HaplotypePhaser {
public:
// char ** haplotypes;
// char ** genotypes;
int prob_precision = 10;
int curr_hap;
MatrixXc haplotypes;
vector <double> sample_gls;
Pedigree ped;
float error;
float Ne;
double pop_const;
std::vector<double> distances;
vector<ChromosomePair> states;
// Number of reference haplotypes that will be considered when handling one sample
// The num_haps first haplotypes in the matrix haplotypes will be used.
int num_haps;
~HaplotypePhaser();
void LoadReferenceData(const String &ref_file, String &map_file);
void LoadSampleData(const String &sample_file, int sample_index);
//private:
int num_states;
int num_markers;
int num_ref_inds;
int num_inds;
void CalcSingleScaledForward(int marker, const double* prev, double* now);
void CalcSingleScaledBackward(int marker, const double* prev, double* now);
StepMemoizer<HaplotypePhaser> s_forward;
StepMemoizer<HaplotypePhaser> s_backward;
double * normalizersf = 0;
double * normalizersb = 0;
void AllocateMemory();
void CalcEmissionProbs(int marker, double * probs);
void InitPriorScaledForward();
void InitPriorScaledBackward();
void CalcScaledForward();
void CalcScaledBackward();
void GetMLHaplotypes(int * ml_states);
vector<vector<double>> GetPosteriorStats(const char * filename, bool print);
vector<vector<double>> ReadPosteriorStats(const char * filename);
void PrintGenotypesToVCF(vector<vector<int>> & ml_genotypes, const char * out_file, const char * sample_file, const char * vcf_template);
void PrintHaplotypesToVCF(vector<vector<int>> & ml_genotypes, const char * out_file, const char * sample_file, const char * vcf_template);
};
#endif