-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash_BWT.cpp
452 lines (386 loc) · 7.43 KB
/
Hash_BWT.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "stdafx.h"
#include "Hash_BWT.h"
//初始化
Hash_BWT::Hash_BWT()
{
string str[] = { "A", "C", "G", "T" };
for (auto s : str)
{
nt.push_back(s);
}
}
bool Hash_BWT::indic(string s)
{
for (int i = 0; i < dic.size(); i++)
{
if (s == dic[i])
{
return true;
}
}
return false;
}
//Summary: 构建k-mer索引
//Parameters:
// k: 索引序列的长度
void Hash_BWT::makeHash(int k)
{
vector<int> sep;
int n = 1;
HashIndex[IntToS(0, k)].push_back(1);
for (int i = 0; i < pow(4, k); i++)
{
string s = IntToS(i, k);
//cout << s;
dic.push_back(s);
for (; n < T.length(); n++)
{
if (Matrix[n].substr(0, k)==s)
{
continue;
}
else
{
if (Matrix[n].substr(0, k).find("$") != -1)
{
n++;
}
HashIndex[s].push_back(n);
HashIndex[IntToS(i + 1, k)].push_back(n);
break;
}
}
//sep = search(s);
//HashIndex[s].push_back(sep[0]);
//HashIndex[s].push_back(sep[1]);
//cout << sep[0] << "\t" << sep[1] << endl;
}
HashIndex[IntToS(pow(4, k)-1, k)].push_back(T.length());
//cout << HashIndex["AAA"][0] << "\t" << HashIndex["AAA"][1];
return;
}
//Summary: 用二进制数映射到序列
//Parameters:
// s: 当前数
// k: 序列长度
//Return : 对应的序列
string Hash_BWT::IntToS(int s, int k)
{
string res;
for (int i = 0; i < k; i++)
{
// 位操作:取最后两位
int x = s & 3;
res += nt[x];
s >>= 2;
}
//cout << res << endl;
string r(res.rbegin(), res.rend());
return r;
}
//Summary: 抽屉原理划分待查序列,然后验证所有位置,放回符合的位置
//Parameters:
// s: 待查训练
// k: hash索引序列长度
// e: 允许的错误率
//Return : 符合匹配的位置
void Hash_BWT::Hash_search(string sub, int k, float e)
{
int n = sub.length();
//cout << n - 1 << endl;
// 抽屉原理,将sub分成sn段
float sn = n*e;
sn = floor(sn) + 1;
int each_ln = floor(n / sn);
int yu = n - each_ln*sn;
//if (each_ln*sn != n)
//{
// sn = ceil(n / each_ln);
//}
if (sn == 1) {
each_ln--;
}
if (each_ln < k)
{
cout << "分段后长度小于k,无效,退出。";
return;
}
cout << "待查序列:" << sub << endl;
cout << "总长度:" << n << endl;
cout << "段:" << sn << endl;
cout << "每段长度:" << each_ln << endl;
int i = 0;
vector<string> sub_arr;
vector<vector<int>> sub_res;
while (i<sn)
{
//分段问题。38bp。0.3错误率。允许错误11.6bp。分成12段。每段4个。报错。
if (i*each_ln>n)
{
sn = i;
cout << "实际分段:" << sn << endl;
break;
}
string s;
if (i*each_ln + each_ln + yu == n)
{
s = sub.substr(i*each_ln);
}
else
{
s = sub.substr(i*each_ln, each_ln);
}
sub_arr.push_back(s);
vector<int> res;
int sp, ep, j;
//cout << s << s.length() << endl;
// Si最后一个片段长度不一定
if (s.length() != k){
if (s.length() < k)
{
j = s.length() - 1;
//cout << "sp" << endl;
char c = s[j];
sp = C[c] + 1;
if (c != 'T')
{
ep = C[toNext[c]] + 1;
}
else{
ep = T.length();
}
j--;
}
else{
// length > k
string lastK = s.substr(s.length() - k, k);
if (indic(lastK) == true)
{
sp = HashIndex[lastK][0];
ep = HashIndex[lastK][1];
if (sp == -1) {
i++;
//res.push_back(-1);
continue;
}
//倒数k+1个字符起继续 BWT搜索
j = s.length() - k - 1;
}
else
{
j = -1;
sp = 0;
ep = 0;
}
}
//cout << s << "\t" << s.substr(s.length() - k, k) << endl;
if (j >= 0)
{
char c = s[j];
while (sp < ep && j > -1)
{
c = s[j];
//cout << c << endl;
sp = LFC(sp, c);
ep = LFC(ep, c);
j--;
}
}
}
else{
// length == k的情况
string lastK = s.substr(s.length() - k, k);
if (indic(lastK) == true)
{
sp = HashIndex[lastK][0];
ep = HashIndex[lastK][1];
}
else
{
j = -1;sp = 0;ep = 0;
}
}
if (sp == ep)
{
cout << s << "\tNooo...\n";
res.push_back(-1);
}
else{
cout << s << "\t";
for (int i = sp; i < ep; i++)
{
cout << SA[i] << "\t";
res.push_back(SA[i]);
}
cout << endl;
}
//cout << "这一段" << sp << ep << endl;
sub_res.push_back(res);
i++;
}
//sub_res[0].push_back(55);
//去重
sub_res = Check(sub_res, each_ln);
Check2(sub_res, each_ln);
// 调试输出
cout << "去重后结果" << endl;
for (int i = 0; i < sub_res.size(); i++)
{
for (int j = 0; j < sub_res[i].size(); j++)
{
cout << sub_res[i][j] << "\t";
}
cout << endl;
}
//取相应长度字符串,计算hamming
vector<int> Final_res;
for (int i = 0; i < sub_res.size(); i++)
{
for (int j = 0; j < sub_res[i].size(); j++)
{
int or_index = sub_res[i][j] - i*each_ln;
if (or_index <0)
{
continue;
}
string or = T.substr(or_index, n);
int d2 = Hamming(or, sub);
int d = editDis(or, sub);
if (d <= n*e)
{
Final_res.push_back(or_index);
}
cout << "原始串:" << or_index <<"\t"<< T.substr(or_index, n) << endl;
cout << "edit距离:" << d << "\thamming 距离" << d2 << endl;
//cout << or_index << endl;
}
cout << endl;
}
// todo:判重?
cout << "result:" << endl;
for (int i = 0; i < Final_res.size(); i++)
{
cout << Final_res[i] << "\t";
}
cout << endl;
return;
}
//Summary: 判断数组中是否已有某个数
//Parameters:
// s: 数组
// num: 待查数
//Return : bool
bool Hash_BWT::HasPre(vector<int> s, int num)
{
for (auto i : s)
{
if (i == num)
return true;
}
return false;
}
vector<vector<int>> Hash_BWT::Check2(vector<vector<int>> &sub_res, int length)
{
for (int i = 0; i < sub_res.size()-1; i++)
{
for (int ii = 0; ii < sub_res[i].size(); ii++)
{
for (int j = i + 1; j < sub_res.size(); j++)
{
for (int k = 0; k < sub_res[j].size(); k++)
{
if (sub_res[j][k] == sub_res[i][ii] + length*(j - i))
{
sub_res[j].erase(sub_res[j].begin() + k);
}
}
}
}
}
return sub_res;
}
//Summary: 事先判断分段的待查序列对应的原始序列位置是否有重复,减少计算量。
//Parameters:
// sub_res: 各分段序列对应位置数组
// length: 分段长度
//Return : 去重后的对应位置数组
vector<vector<int>> Hash_BWT::Check(vector<vector<int>> &sub_res, int length)
{
//
if (sub_res[0][0] == -1)
{
sub_res[0].erase(sub_res[0].begin());
}
for (int i = sub_res.size() - 1; i > 0; i--)
{
string f;
for (int j = 0; j < sub_res[i].size(); j++)
{
if (sub_res[i][j] == -1)
{
f += "X";
//continue;
break;
}
if (HasPre(sub_res[i - 1], sub_res[i][j] - length))
{
f += "X";
}
else{
f += "O";
}
}
for (int j = sub_res[i].size() - 1; j >= 0; j--)
{
if (f[j] == 'O')
continue;
else
sub_res[i].erase(sub_res[i].begin() + j);
}
}
return sub_res;
}
//Summary: 计算两个序列之间的haming距离
//Parameters:
// c1:序列1
// c2:序列2
//Return : hamming距离长度
int Hash_BWT::Hamming(string c1, string c2)
{
int res = 0;
for (int i = 0; i < c1.length(); i++)
{
if (c1[i] != c2[i])
{
res++;
}
}
return res;
}
//Summary: 主函数,用于输出结果
void Hash_BWT::run()
{
clock_t start, finish;
Read_Reference("test.fa");
vector<string> res = Read_Subs("sub.fa");
preprocess();
int k = 6;
start = clock();
makeHash(k);
finish = clock();
float d2 = (double)(finish - start) / CLOCKS_PER_SEC;
printf("hash索引:%f\n", d2);
//Hash_search("AACCCCGTCTCTACTGAAAAATACAAAAAAAAATTAGC", k, 0.1);
//Hash_search("AACCCCGTCTCTACTGAAAAATACAAAAAAAAATTAGCCG", 3, 0.1);
for (auto s : res){
start = clock();
Hash_search(s, k, 0.1);
finish = clock();
d2 = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f\n", d2);
}
cout << endl;
}
Hash_BWT::~Hash_BWT()
{
}