-
Notifications
You must be signed in to change notification settings - Fork 118
/
NoiseProfile.h
159 lines (126 loc) · 3.09 KB
/
NoiseProfile.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
#ifndef NOISEPROFILE_H_
#define NOISEPROFILE_H_
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<cassert>
#include "utils.h"
#include "RefSeq.h"
#include "simul.h"
class NoiseProfile {
public:
NoiseProfile() {
logp = 0.0;
memset(c, 0, sizeof(c));
memset(p, 0, sizeof(p));
}
NoiseProfile& operator=(const NoiseProfile&);
void init();
void updateC(const std::string&);
void update(const std::string&, double frac);
void finish();
void calcInitParams();
double getProb(const std::string&);
double getLogP() { return logp; }
void collect(const NoiseProfile&);
void read(FILE*);
void write(FILE*);
void startSimulation();
std::string simulate(simul*, int);
void finishSimulation();
private:
static const int NCODES = 5;
double logp;
double c[NCODES]; // counts in N0;
double p[NCODES];
double *pc; // for simulation
};
NoiseProfile& NoiseProfile::operator=(const NoiseProfile& rv) {
if (this == &rv) return *this;
logp = rv.logp;
memcpy(c, rv.c, sizeof(rv.c));
memcpy(p, rv.p, sizeof(rv.p));
return *this;
}
void NoiseProfile::init() {
memset(p, 0, sizeof(p));
}
void NoiseProfile::updateC(const std::string& readseq) {
int len = readseq.size();
for (int i = 0; i < len; i++) {
++c[get_base_id(readseq[i])];
}
}
void NoiseProfile::update(const std::string& readseq, double frac) {
int len = readseq.size();
for (int i = 0; i < len; i++) {
p[get_base_id(readseq[i])] += frac;
}
}
void NoiseProfile::finish() {
double sum;
logp = 0.0;
sum = 0.0;
for (int i = 0; i < NCODES; i++) sum += (p[i] + c[i]);
if (sum <= EPSILON) return;
for (int i = 0; i < NCODES; i++) {
p[i] = (p[i] + c[i]) / sum;
if (c[i] > 0.0) { logp += c[i] * log(p[i]); }
}
}
void NoiseProfile::calcInitParams() {
double sum;
logp = 0.0;
sum = 0.0;
for (int i = 0; i < NCODES; i++) sum += (1.0 + c[i]);
for (int i = 0; i < NCODES; i++) {
p[i] = (1.0 + c[i]) / sum;
if (c[i] > 0.0) { logp += c[i] * log(p[i]); }
}
}
double NoiseProfile::getProb(const std::string& readseq) {
double prob = 1.0;
int len = readseq.size();
for (int i = 0; i < len; i++) {
prob *= p[get_base_id(readseq[i])];
}
return prob;
}
void NoiseProfile::collect(const NoiseProfile& o) {
for (int i = 0; i < NCODES; i++)
p[i] += o.p[i];
}
void NoiseProfile::read(FILE *fi) {
int tmp_ncodes;
memset(c, 0, sizeof(c));
assert(fscanf(fi, "%d", &tmp_ncodes) == 1);
assert(tmp_ncodes == NCODES);
for (int i = 0; i < NCODES; i++)
assert(fscanf(fi, "%lf", &p[i]) == 1);
}
void NoiseProfile::write(FILE *fo) {
fprintf(fo, "%d\n", NCODES);
for (int i = 0; i < NCODES - 1; i++) {
fprintf(fo, "%.10g ", p[i]);
}
fprintf(fo, "%.10g\n", p[NCODES - 1]);
}
void NoiseProfile::startSimulation() {
pc = new double[NCODES];
for (int i = 0; i < NCODES; i++) {
pc[i] = p[i];
if (i > 0) pc[i] += pc[i - 1];
}
}
std::string NoiseProfile::simulate(simul* sampler, int len) {
std::string readseq = "";
for (int i = 0; i < len; i++) {
readseq.push_back(getCharacter(sampler->sample(pc, NCODES)));
}
return readseq;
}
void NoiseProfile::finishSimulation() {
delete[] pc;
}
#endif /* NOISEPROFILE_H_ */