File: Profile.h

package info (click to toggle)
rsem 1.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 37,664 kB
  • sloc: cpp: 19,230; perl: 1,326; python: 1,245; ansic: 547; makefile: 186; sh: 154
file content (220 lines) | stat: -rw-r--r-- 5,331 bytes parent folder | download | duplicates (5)
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
#ifndef PROFILE_H_
#define PROFILE_H_

#include<cstdio>
#include<cstring>
#include<cassert>

#include "utils.h"
#include "RefSeq.h"
#include "simul.h"


class Profile {
public:
	Profile(int = 1000);
	~Profile() {
		delete[] p;
	}

	Profile& operator=(const Profile&);

	void init();
	void update(const std::string&, const RefSeq&, int, int, double);
	void finish();

	double getProb(const std::string&, const RefSeq&, int, int);

	void collect(const Profile&);

	void read(FILE*);
	void write(FILE*);

	void startSimulation();
	std::string simulate(simul*, int, int, int, const RefSeq&);
	void finishSimulation();

private:
	static const int NCODES = 5;

	int proLen; // profile length
	int size; // # of items in p;
	double (*p)[NCODES][NCODES]; //profile matrices

	double (*pc)[NCODES][NCODES]; // for simulation
};

Profile::Profile(int maxL) {
	proLen = maxL;
	size = proLen * NCODES * NCODES;
	p = new double[proLen][NCODES][NCODES];
	memset(p, 0, sizeof(double) * size);

	//set initial parameters
	int N = NCODES - 1;
	double probN = 1e-5, portionC = 0.99; //portionC, among ACGT, the portion of probability mass the correct base takes
	double probC, probO;

	for (int i = 0; i < proLen; i++) {
		for (int j = 0; j < NCODES - 1; j++) {
			p[i][j][N] = probN;
			probC = portionC * (1.0 - probN);
			probO = (1.0 - portionC) / (NCODES - 2) * (1.0 - probN);

			for (int k = 0; k < NCODES - 1; k++) {
				p[i][j][k] = (j == k ? probC : probO);
			}
		}
		p[i][N][N] = probN;
		for (int k = 0; k < NCODES - 1; k++)
				p[i][N][k] = (1.0 - probN) / (NCODES - 1);
	}
}

Profile& Profile::operator=(const Profile& rv) {
	if (this == &rv) return *this;
	if (proLen != rv.proLen) {
		delete[] p;
		proLen = rv.proLen;
		size = rv.size;
		p = new double[rv.proLen][NCODES][NCODES];
	}
	memcpy(p, rv.p, sizeof(double) * rv.size);

	return *this;
}

void Profile::init() {
	memset(p, 0, sizeof(double) * size);
}

void Profile::update(const std::string& readseq, const RefSeq& refseq, int pos, int dir, double frac) {
	int len = readseq.size();
	for (int i = 0; i < len; i++) {
		p[i][refseq.get_id(i + pos, dir)][get_base_id(readseq[i])] += frac;
	}
}

void Profile::finish() {
	double sum;

	for (int i = 0; i < proLen; 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 Profile::getProb(const std::string& readseq, const RefSeq& refseq, int pos, int dir) {
	double prob = 1.0;
	int len = readseq.size();

	for (int i = 0; i < len; i++) {
		prob *= p[i][refseq.get_id(i + pos, dir)][get_base_id(readseq[i])];

	}

	return prob;
}

void Profile::collect(const Profile& o) {
	for (int i = 0; i < proLen; 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 Profile::read(FILE *fi) {
	int tmp_prolen, tmp_ncodes;
	assert(fscanf(fi, "%d %d", &tmp_prolen, &tmp_ncodes) == 2);
	assert(tmp_ncodes == NCODES);
	if (tmp_prolen != proLen) {
		delete[] p;
		proLen = tmp_prolen;
		size = proLen * NCODES * NCODES;
		p = new double[proLen][NCODES][NCODES];
		memset(p, 0, sizeof(double) * size);
	}

	for (int i = 0; i < proLen; 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 Profile::write(FILE* fo) {
	fprintf(fo, "%d %d\n", proLen, NCODES);
	for (int i = 0; i < proLen; 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 < proLen - 1) { fprintf(fo, "\n"); }
	}
}

void Profile::startSimulation() {
	pc = new double[proLen][NCODES][NCODES];
	for (int i = 0; i < proLen; 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 Profile::simulate(simul* sampler, int len, int pos, int dir, const RefSeq& refseq) {
	std::string readseq = "";

	for (int i = 0; i < len; i++) {
		readseq.push_back(getCharacter(sampler->sample(pc[i][refseq.get_id(i + pos, dir)], NCODES)));
	}
	return readseq;
}

void Profile::finishSimulation() {
	delete[] pc;
}

#endif /* PROFILE_H_ */