forked from deweylab/RSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QualDist.h
151 lines (116 loc) · 3.22 KB
/
QualDist.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
#ifndef QUALDIST_H_
#define QUALDIST_H_
#include<cstdio>
#include<cstring>
#include<cassert>
#include<string>
#include "simul.h"
//from 33 to 126 to encode 0 to 93
class QualDist {
public:
QualDist() {
memset(p_init, 0, sizeof(p_init));
memset(p_tran, 0, sizeof(p_tran));
}
QualDist& operator=(const QualDist&);
void update(const std::string&);
void finish();
double getProb(const std::string&);
void read(FILE*);
void write(FILE*);
void startSimulation();
std::string simulate(simul*, int);
void finishSimulation();
private:
static const int SIZE = 100;
double p_init[SIZE];
double p_tran[SIZE][SIZE]; //p_tran[a][b] = p(b|a)
int c2q(char c) { assert(c >= 33 && c <= 126); return c - 33; }
double *qc_init, (*qc_trans)[SIZE];
char q2c(int qval) { return (char)(qval + 33); }
};
QualDist& QualDist::operator=(const QualDist& rv) {
if (this == &rv) return *this;
memcpy(p_init, rv.p_init, sizeof(rv.p_init));
memcpy(p_tran, rv.p_tran, sizeof(rv.p_tran));
return *this;
}
void QualDist::update(const std::string& qual) {
int len = qual.size();
assert(len > 0);
++p_init[c2q(qual[0])];
for (int i = 1; i < len; i++) {
++p_tran[c2q(qual[i - 1])][c2q(qual[i])];
}
}
void QualDist::finish() {
double sum;
sum = 0.0;
for (int i = 0; i < SIZE; i++) sum += p_init[i];
for (int i = 0; i < SIZE; i++) p_init[i] /= sum;
for (int i = 0; i < SIZE; i++) {
sum = 0.0;
for (int j = 0; j < SIZE; j++) sum += p_tran[i][j];
if (sum <= 0.0) continue;
//if (isZero(sum)) continue;
for (int j = 0; j < SIZE; j++) p_tran[i][j] /= sum;
}
}
double QualDist::getProb(const std::string& qual) {
int len = qual.size();
double prob = 1.0;
assert(len > 0);
prob *= p_init[c2q(qual[0])];
for (int i = 1; i < len; i++) {
prob *= p_tran[c2q(qual[i - 1])][c2q(qual[i])];
}
return prob;
}
void QualDist::read(FILE *fi) {
int tmp_size;
assert(fscanf(fi, "%d", &tmp_size) == 1);
assert(tmp_size == SIZE);
for (int i = 0; i < SIZE; i++) { assert(fscanf(fi, "%lf", &p_init[i]) == 1); }
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) { assert(fscanf(fi, "%lf", &p_tran[i][j]) == 1); }
}
}
void QualDist::write(FILE *fo) {
fprintf(fo, "%d\n", SIZE);
for (int i = 0; i < SIZE - 1; i++) { fprintf(fo, "%.10g ", p_init[i]); }
fprintf(fo, "%.10g\n", p_init[SIZE - 1]);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE -1 ; j++) fprintf(fo, "%.10g ", p_tran[i][j]);
fprintf(fo, "%.10g\n", p_tran[i][SIZE - 1]);
}
}
void QualDist::startSimulation() {
qc_init = new double[SIZE];
qc_trans = new double[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
qc_init[i] = p_init[i];
if (i > 0) qc_init[i] += qc_init[i - 1];
}
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++) {
qc_trans[i][j] = p_tran[i][j];
if (j > 0) qc_trans[i][j] += qc_trans[i][j - 1];
}
}
std::string QualDist::simulate(simul* sampler, int len) {
int qval, old_qval;
std::string qual = "";
qval = sampler->sample(qc_init, SIZE);
qual.push_back(q2c(qval));
for (int i = 1; i < len; i++) {
old_qval = qval;
qval = sampler->sample(qc_trans[old_qval], SIZE);
qual.push_back(q2c(qval));
}
return qual;
}
void QualDist::finishSimulation() {
delete[] qc_init;
delete[] qc_trans;
}
#endif /* QUALDIST_H_ */