-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBWT2.cpp
194 lines (163 loc) · 3.38 KB
/
BWT2.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
#include "stdafx.h"
#include "BWT2.h"
//初始化
BWT2::BWT2()
{
string str[] = { "AA", "AC", "AG", "AT", "CA", "CC", "CG", "CT", "GA", "GC", "GG", "GT", "TA", "TC", "TG", "TT", "UU" };
for (int i = 0; i < 17; i++)
{
Index2.push_back(str[i]);
}
}
//Summary: 计算给定双字符的下一个双字符
//Parameters:
// c: 当前的双字符
//Return : 下一个双字符
string BWT2::toNext2(string c)
{
int res = 0;
for (int i = 0; i < 16; i++)
{
if (Index2[i] <= c)
res++;
}
//cout << Index2[res];
return Index2[res];
}
//Summary: 构建二阶BWT索引
//Return : 无
void BWT2::makebwts2()
{
int n = T.length();
// 构造BWT2(S)
for (int i = 0; i < n; i++)
{
BWTS2.push_back(Matrix[i].substr(n - 2, 2));
// 调试输出
//cout << Matrix[i] << "\t" << SA[i] << "\t" << Matrix[i].substr(n - 2, 2) << endl;;
}
//cout << BWTS << endl;
return;
}
//Summary: 利用二阶索引计算从第1行到第r行,c双字符出现的次数
//Parameters:
// r: 截止行数
// c: 计算的双字符
//Return : 出现的次数
int BWT2::Occ2(int r, string c)
{
int res = 0;
for (int i = 0; i < r; i++)
{
if (BWTS2[i][0] == c[0])
{
if (BWTS2[i][1] == c[1])
res += 1;
}
}
return res;
}
//Summary: 利用二阶索引计算双字符c的起始行数
//Parameters:
// c: 计算的双字符
//Return : 行数
int BWT2::getC2(string c)
{
if (c == "UU")
return T.length();
int n = Matrix.size(), res = 0;
for (int i = 0; i < n; i++)
{
if (Matrix[i].substr(0, 2) < c)
{
res++;
}
}
return res;
}
//Summary: 利用二阶索引计算要跳转的行数
//Parameters:
// r: 当前行数
// c: 当前双字符
//Return : 行数
int BWT2::LFC2(int r, string c)
{
return getC2(c) + Occ2(r, c);
}
//Summary: 精切匹配条件下,利用二阶索引,在参考串中搜索待查序列所在位置
//Parameters:
// sub:待查序列
//Return : 匹配位置数组
void BWT2::search2(string sub)
{
int n = sub.length();
string c = sub.substr(n - 2, 2);
int sp, ep;
sp = getC2(c);
//cout << toNext2(c) << endl;
ep = getC2(toNext2(c));
//cout << sp << "\t" << ep << endl;
int i = n - 2;
//cout << i << endl;
clock_t start, finish;
while (sp < ep && i>1)
{
start = clock();
//c = sub[i];
c = sub.substr(i - 2, 2);
//cout << c
sp = LFC2(sp, c);
ep = LFC2(ep, c);
i -= 2;
finish = clock();
//printf("%f\n", (double)(finish - start) / CLOCKS_PER_SEC);
//cout << i << "\t" << c << "\t" << sp << "\t" << ep << endl;
}
if (i == 1)
{
// todo:
sp = LFC(sp, sub[0]);
ep = LFC(ep, sub[0]);
//cout << sp << "\t" << ep << endl;
}
if (sp == ep)
{
cout << sub << "\tNooo...\n";
}
else{
cout << sub << "\t";
for (int i = sp; i < ep; i++)
{
cout << SA[i] << "\t";
}
cout << endl;
}
}
//Summary: 主函数,用于输出结果
void BWT2::run()
{
clock_t start, mid, finish;
Read_Reference("test.fa");
vector<string> res = Read_Subs("sub.fa");
preprocess();
start = clock();
makebwts2();
finish = clock();
float d1 = (double)(finish - start) / CLOCKS_PER_SEC;
printf("2index: %f\n", d1);
for (auto s : res){
start = clock();
search(s);
mid = clock();
search2(s);
finish = clock();
float d1 = (double)(mid - start) / CLOCKS_PER_SEC;
float d2 = (double)(finish - mid) / CLOCKS_PER_SEC;
//float d2 = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f---%f\n", d1, d2);
}
return;
}
BWT2::~BWT2()
{
}