This repository has been archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcvtree-sequential.cpp
277 lines (239 loc) · 6.41 KB
/
cvtree-sequential.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <dirent.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
int number_bacteria;
vector<string> bacteria_name;
long M, M1, M2;
short code[27] = { 0, 2, 1, 2, 3, 4, 5, 6, 7, -1, 8, 9, 10, 11, -1, 12, 13, 14, 15, 16, 1, 17, 18, 5, 19, 3};
#define encode(ch) code[ch-'A']
#define LEN 6
#define AA_NUMBER 20
#define EPSILON 1e-010
void Init() {
M2 = 1;
for (int i = 0; i < LEN-2; i++) { // M2 = AA_NUMBER ^ (LEN-2);
M2 *= AA_NUMBER;
}
M1 = M2 * AA_NUMBER; // M1 = AA_NUMBER ^ (LEN-1);
M = M1 *AA_NUMBER; // M = AA_NUMBER ^ (LEN);
}
class Bacteria {
private:
vector<long> vectorA;
vector<long> vectorB;
vector<long> one_l;
long indexs;
long total;
long total_l;
long complement;
void InitVectors() {
vectorA = vector<long>(M, 0.0);
vectorB = vector<long>(M1, 0.0);
one_l = vector<long>(AA_NUMBER, 0.0);
total = 0;
total_l = 0;
complement = 0;
}
void init_buffer(char* buffer) {
complement++;
indexs = 0;
for (int i = 0; i < LEN-1; i++) {
short enc = encode(buffer[i]);
one_l[enc]++;
total_l++;
indexs = indexs * AA_NUMBER + enc;
}
vectorB[indexs]++;
}
void cont_buffer(char ch) {
short enc = encode(ch);
one_l[enc]++;
total_l++;
long index = indexs * AA_NUMBER + enc;
vectorA[index]++;
total++;
indexs = (indexs % M2) * AA_NUMBER + enc;
vectorB[indexs]++;
}
public:
long count;
double* tv;
long *ti;
Bacteria(const char* filename) {
FILE * bacteria_file = fopen(filename,"r");
if (bacteria_file == NULL) {
throw invalid_argument("BAD DATA DIRECTORY. Please specify full relative path.");
}
InitVectors();
char ch;
while ((ch = fgetc(bacteria_file)) != EOF) {
if (ch == '>') {
while (fgetc(bacteria_file) != '\n'); // skip rest of line
char buffer[LEN-1];
fread(buffer, sizeof(char), LEN-1, bacteria_file);
init_buffer(buffer);
} else if (ch == '\r') {
// Skip carriage return.
} else if (ch != '\n') {
cont_buffer(ch);
}
}
fclose (bacteria_file);
long total_plus_complement = total + complement;
double total_div_2 = total * 0.5;
int i_mod_aa_number = 0;
int i_div_aa_number = 0;
long i_mod_M1 = 0;
long i_div_M1 = 0;
double one_l_div_total[AA_NUMBER];
for (int i = 0; i < AA_NUMBER; i++) {
one_l_div_total[i] = (double)one_l[i] / total_l;
}
double* second_div_total = new double[M1];
for (int i = 0; i < M1; i++) {
second_div_total[i] = (double)vectorB[i] / total_plus_complement;
}
count = 0;
double* t = new double[M];
for(long i = 0; i < M; i++) {
double p1 = second_div_total[i_div_aa_number];
double p2 = one_l_div_total[i_mod_aa_number];
double p3 = second_div_total[i_mod_M1];
double p4 = one_l_div_total[i_div_M1];
double stochastic = (p1 * p2 + p3 * p4) * total_div_2;
if (i_mod_aa_number == AA_NUMBER-1) {
i_mod_aa_number = 0;
i_div_aa_number++;
} else {
i_mod_aa_number++;
}
if (i_mod_M1 == M1-1) {
i_mod_M1 = 0;
i_div_M1++;
} else {
i_mod_M1++;
}
if (stochastic > EPSILON) {
t[i] = (vectorA[i] - stochastic) / stochastic;
count++;
} else {
t[i] = 0;
}
}
delete second_div_total;
vector<long>().swap(vectorA);
vector<long>().swap(vectorB);
vector<long>().swap(one_l);
tv = new double[count];
ti = new long[count];
int pos = 0;
for (long i = 0; i < M; i++) {
if (t[i] != 0) {
tv[pos] = t[i];
ti[pos] = i;
pos++;
}
}
delete t;
}
};
void ReadDataDir(char* data_dir) {
// Retrieve filenames in data directory.
// Based on https://stackoverflow.com/a/612176
DIR *dir;
struct dirent *ent;
if ((dir = opendir (data_dir)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
string filename = ent->d_name;
if (filename != "." && filename != "..") {
bacteria_name.push_back(data_dir + filename);
number_bacteria++;
}
}
closedir (dir);
sort(bacteria_name.begin(), bacteria_name.end());
} else {
perror ("");
}
}
double CompareBacteria(Bacteria* b1, Bacteria* b2) {
double correlation = 0;
double vector_len1=0;
double vector_len2=0;
long p1 = 0;
long p2 = 0;
while (p1 < b1->count && p2 < b2->count) {
long n1 = b1->ti[p1];
long n2 = b2->ti[p2];
if (n1 < n2) {
double t1 = b1->tv[p1];
vector_len1 += (t1 * t1);
p1++;
} else if (n2 < n1) {
double t2 = b2->tv[p2];
p2++;
vector_len2 += (t2 * t2);
} else {
double t1 = b1->tv[p1++];
double t2 = b2->tv[p2++];
vector_len1 += (t1 * t1);
vector_len2 += (t2 * t2);
correlation += t1 * t2;
}
}
while (p1 < b1->count) {
long n1 = b1->ti[p1];
double t1 = b1->tv[p1++];
vector_len1 += (t1 * t1);
}
while (p2 < b2->count) {
long n2 = b2->ti[p2];
double t2 = b2->tv[p2++];
vector_len2 += (t2 * t2);
}
return correlation / (sqrt(vector_len1) * sqrt(vector_len2));
}
void CompareAllBacteria() {
Bacteria** b = new Bacteria*[number_bacteria];
for(int i = 0; i < number_bacteria; i++) {
// printf("load %.2d of %.2d - %s\n", i+1, number_bacteria, bacteria_name[i].c_str());
b[i] = new Bacteria(bacteria_name[i].c_str());
}
vector<vector<double>> results(number_bacteria);
for(int i = 0; i < number_bacteria-1; i++) {
results[i] = vector<double>(number_bacteria, 0.0);
for(int j = i+1; j < number_bacteria; j++) {
double correlation = CompareBacteria(b[i], b[j]);
results[i][j] = correlation;
// printf("%.2d %.2d -> %.20lf\n", i, j, correlation);
}
}
ofstream output;
output.open("results-sequential.csv");
output << "indexA" << ',' << "bacteriaA" << ',' << "indexB" << ',' << "bacteriaB" << ',' << "correlation" << endl;
for (int i = 0; i < number_bacteria-1; i++) {
for (int j = i+1; j < number_bacteria; j++) {
output << i << ',' << bacteria_name[i] << ',' << j << ',' << bacteria_name[j] << ',' << results[i][j] << endl;
}
}
output.close();
}
int main(int argc,char * argv[]) {
if (argc < 2) {
throw invalid_argument("Please specify data directory.");
}
time_t t1 = time(NULL);
Init();
ReadDataDir(argv[1]);
CompareAllBacteria();
time_t t2 = time(NULL);
printf("time elapsed: %d seconds\n", t2 - t1);
return 0;
}