forked from deweylab/RSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QProfile.h
208 lines (168 loc) · 5.13 KB
/
QProfile.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
#ifndef QPROFILE_H_
#define QPROFILE_H_
#include<cstdio>
#include<cstring>
#include<cassert>
#include "utils.h"
#include "RefSeq.h"
#include "simul.h"
class QProfile {
public:
QProfile();
QProfile& operator=(const QProfile&);
void init();
void update(const std::string&, const std::string&, const RefSeq&, int, int, double);
void finish();
double getProb(const std::string&, const std::string&, const RefSeq&, int, int);
void collect(const QProfile&);
void read(FILE*);
void write(FILE*);
void startSimulation();
std::string simulate(simul*, int, int, int, const std::string&, const RefSeq&);
void finishSimulation();
private:
static const int NCODES = 5; // number of possible codes
static const int SIZE = 100;
double p[SIZE][NCODES][NCODES]; // p[q][r][c] = p(c|r,q)
//make sure that quality score in [0, 93]
int c2q(char c) { assert(c >= 33 && c <= 126); return c - 33; }
double (*pc)[NCODES][NCODES]; // for simulation
};
QProfile::QProfile() {
memset(p, 0, sizeof(p));
//make initialized parameters
//ASSUME order of A, C, G, T, N
int N = NCODES - 1;
double probN = 1e-5;
double probC, probO; // current, other
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < NCODES - 1; j++) {
p[i][j][N] = probN;
probO = exp(-i / 10.0 * log(10.0));
probC = 1.0 - probO;
probO /= (NCODES - 2);
probC *= (1.0 - probN);
probO *= (1.0 - probN);
assert(probC >= 0.0 && probO >= 0.0);
for (int k = 0; k < NCODES - 1; k++) {
if (j == k) p[i][j][k] = probC;
else p[i][j][k] = probO;
}
}
p[i][N][N] = probN;
for (int k = 0; k < NCODES - 1; k++)
p[i][N][k] = (1.0 - probN) / (NCODES - 1);
}
}
QProfile& QProfile::operator=(const QProfile& rv) {
if (this == &rv) return *this;
memcpy(p, rv.p, sizeof(rv.p));
return *this;
}
void QProfile::init() {
memset(p, 0, sizeof(p));
}
void QProfile::update(const std::string& readseq, const std::string& qual, const RefSeq& refseq, int pos, int dir, double frac) {
int len = readseq.size();
for (int i = 0; i < len; i++) {
p[c2q(qual[i])][refseq.get_id(i + pos, dir)][get_base_id(readseq[i])] += frac;
}
}
void QProfile::finish() {
double sum;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < NCODES; j++) {
sum = 0.0;
for (int k = 0; k < NCODES; k++) sum += p[i][j][k];
if (sum < EPSILON) {
for (int k = 0; k < NCODES; k++) p[i][j][k] = 0.0;
continue;
}
for (int k = 0; k < NCODES; k++) p[i][j][k] /= sum;
}
}
}
double QProfile::getProb(const std::string& readseq, const std::string& qual, const RefSeq& refseq, int pos, int dir) {
double prob = 1.0;
int len = readseq.size();
for (int i = 0; i < len; i++) {
prob *= p[c2q(qual[i])][refseq.get_id(i + pos, dir)][get_base_id(readseq[i])];
}
return prob;
}
void QProfile::collect(const QProfile& o) {
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < NCODES; j++)
for (int k = 0; k < NCODES; k++)
p[i][j][k] += o.p[i][j][k];
}
void QProfile::read(FILE *fi) {
int tmp_size, tmp_ncodes;
assert(fscanf(fi, "%d %d", &tmp_size, &tmp_ncodes) == 2);
assert(tmp_size == SIZE && tmp_ncodes == NCODES);
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < NCODES; j++)
for (int k = 0; k < NCODES; k++)
assert(fscanf(fi, "%lf", &p[i][j][k]) == 1);
}
void QProfile::write(FILE *fo) {
fprintf(fo, "%d %d\n", SIZE, NCODES);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < NCODES; j++) {
for (int k = 0; k < NCODES - 1; k++)
fprintf(fo, "%.10g ", p[i][j][k]);
fprintf(fo, "%.10g\n", p[i][j][NCODES - 1]);
}
if (i < SIZE - 1) { fprintf(fo, "\n"); }
}
}
void QProfile::startSimulation() {
pc = new double[SIZE][NCODES][NCODES];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < NCODES; j++)
for (int k = 0; k < NCODES; k++) {
pc[i][j][k] = p[i][j][k];
if (k > 0) pc[i][j][k] += pc[i][j][k - 1];
}
//avoid sampling from 0.0!!!
double cp_sum, cp_d, cp_n;
cp_sum = cp_d = cp_n = 0.0;
for (int j = 0; j < NCODES - 1; j++) {
cp_sum += pc[i][j][NCODES - 1];
cp_d += p[i][j][j];
cp_n += p[i][j][NCODES - 1];
}
if (cp_sum == 0.0) continue;
double p_d, p_o, p_n;
p_d = cp_d / cp_sum;
p_n = cp_n / cp_sum;
p_o = (1.0 - p_d - p_n) / (NCODES - 2);
for (int j = 0; j < NCODES - 1; j++) {
if (pc[i][j][NCODES - 1] > 0.0) continue;
for (int k = 0; k < NCODES; k++) {
if (k == j) pc[i][j][k] = p_d;
else if (k == NCODES - 1) pc[i][j][k] = p_n;
else pc[i][j][k] = p_o;
if (k > 0) pc[i][j][k] += pc[i][j][k - 1];
}
}
if (pc[i][NCODES - 1][NCODES - 1] == 0.0) {
p_o = (1.0 - p_n) / (NCODES - 1);
for (int k = 0; k < NCODES; k++) {
pc[i][NCODES - 1][k] = (k < NCODES - 1 ? p_o : p_n);
if (k > 0) pc[i][NCODES - 1][k] += pc[i][NCODES - 1][k - 1];
}
}
}
}
std::string QProfile::simulate(simul* sampler, int len, int pos, int dir, const std::string& qual, const RefSeq& refseq) {
std::string readseq = "";
for (int i = 0; i < len; i++) {
readseq.push_back(getCharacter(sampler->sample(pc[c2q(qual[i])][refseq.get_id(i + pos, dir)], NCODES)));
}
return readseq;
}
void QProfile::finishSimulation() {
delete[] pc;
}
#endif /* QPROFILE_H_ */