-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearchsix.cpp
300 lines (261 loc) · 6.01 KB
/
searchsix.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
#include "myutils.h"
#include "searchsix.h"
#include "seqdb.h"
#include "objmgr.h"
#include "alpha.h"
FILE *SearchHashix::m_fTab;
#include "myutils.h"
#include "searchsix.h"
#include "seqdb.h"
#include "objmgr.h"
#include "alpha.h"
void SearchHashix::AppendGSP(SHGSP *GSP)
{
m_GSPs.push_back(GSP);
}
SHGSP *SearchHashix::Extend(uint32 SeedPosQ, uint32 SeedPosT)
{
const byte *Q = m_Query->m_Seq;
const byte *T = m_SI->m_Seq;
const unsigned QL = m_Query->m_L;
const unsigned TL = m_SI->m_L;
const unsigned w = m_WordLength;
uint IdCount = 0;
uint BestIdCount = 0;
float Score = 0.0f;
float BestScore = 0.0f;
// Extend right
uint QPos = SeedPosQ;
uint TPos = SeedPosT;
uint EndPosQ = QPos;
for (;;)
{
byte t = T[TPos++];
if (t == 0)
break;
if (QPos >= QL)
break;
byte q = Q[QPos];
if (q == t)
{
++IdCount;
Score += 1.0f;
if (Score > BestScore)
{
BestIdCount = IdCount;
BestScore = Score;
EndPosQ = QPos;
}
}
else
{
Score += MISMATCH_SCORE;
float Drop = (BestScore - Score);
if (Drop > XDROP)
break;
}
++QPos;
}
// Extend left
QPos = SeedPosQ;
TPos = SeedPosT;
uint StartPosQ = SeedPosQ;
for (;;)
{
// Not needed coz nuls
//if (TPos == 0)
// break;
byte t = T[--TPos];
if (t == 0)
break;
if (QPos == 0)
break;
byte q = Q[QPos];
if (q == t)
{
++IdCount;
Score += 1.0f;
if (Score > BestScore)
{
BestIdCount = IdCount;
BestScore = Score;
StartPosQ = QPos;
}
}
else
{
Score += MISMATCH_SCORE;
float Drop = (BestScore - Score);
if (Drop > XDROP)
break;
}
--QPos;
}
if (BestScore < MIN_GSP_SCORE)
return 0;
assert(EndPosQ > StartPosQ);
assert(StartPosQ <= SeedPosQ);
uint GSPLength = EndPosQ - StartPosQ + 1;
if (GSPLength < MIN_GSP_LENGTH)
return 0;
SHGSP *GSP = new SHGSP;
uint StartPosT = SeedPosT - (SeedPosQ - StartPosQ);
uint EndPosT = StartPosT + GSPLength - 1;
asserta(EndPosT > StartPosT);
GSP->StartQ = StartPosQ;
GSP->EndQ = EndPosQ;
GSP->LoT = StartPosT;
GSP->HiT = EndPosT;
GSP->IdCount = BestIdCount;
GSP->Score = BestScore;
return GSP;
}
void SearchHashix::ClearGSPs()
{
for (vector<SHGSP *>::const_iterator p = m_GSPs.begin();
p != m_GSPs.end(); ++p)
delete *p;
m_GSPs.clear();
}
void SearchHashix::LogGSPs() const
{
Log("\n");
Log("%u GPS\n", SIZE(m_GSPs));
uint64 TotalBases = 0;
double TotalBasesPctId = 0;
for (vector<SHGSP *>::const_iterator p = m_GSPs.begin();
p != m_GSPs.end(); ++p)
{
const SHGSP &GSP = **p;
uint Len = GSP.GetLength();
double PctId = GSP.GetPctId();
TotalBases += Len;
TotalBasesPctId += Len*PctId;
bool Plus = (GSP.HiT < m_SI->m_L/2);
Log("%10u", GSP.GetLoQ());
Log(" %10u", GSP.GetHiQ());
Log(" %10u", GSP.LoT);
Log(" %c", pom(Plus));
Log(" %10u", GSP.HiT);
Log(" %8u", Len);
Log(" %6.1f", PctId);
Log("\n");
}
Log("\n");
Log("ANI %.1f\n", TotalBasesPctId/TotalBases);
Log("AFQ %.2f\n", double(TotalBases)/m_Query->m_L);
}
void SearchHashix::GetANI_AFQ(double &ANI, double &AFQ) const
{
uint64 TotalBases = 0;
double TotalBasesPctId = 0;
for (vector<SHGSP *>::const_iterator p = m_GSPs.begin();
p != m_GSPs.end(); ++p)
{
const SHGSP &GSP = **p;
uint Len = GSP.GetLength();
double PctId = GSP.GetPctId();
TotalBases += Len;
TotalBasesPctId += Len*PctId;
}
if (TotalBases == 0)
{
ANI = 0;
AFQ = 0;
}
else
{
ANI = TotalBasesPctId/TotalBases;
AFQ = double(TotalBases)/m_Query->m_L;
}
}
void SearchHashix::Search(SeqInfo *Query)
{
ClearGSPs();
m_Query = Query;
const byte *QSeq = Query->m_Seq;
const uint QL = Query->m_L;
SyncmerType ST = m_SI->m_ST;
uint k = m_SI->m_k;
uint t = m_SI->m_t;
uint w = m_SI->m_w;
m_QSI.Clear();
m_QSI.Create(ST, k, t, w, QSeq, QL);
const uint K = m_QSI.GetKmerCount();
for (uint QPos = 0; QPos < K; ++QPos)
{
if (m_QSI.IsSyncmer(QPos))
{
uint64 Kmer = m_QSI.m_Kmers[QPos];
uint64 Hash = m_QSI.KmerToHash(Kmer);
uint64 Slot = Hash%m_SI->m_SlotCount;
uint32 TPos = m_SI->GetPos(Slot);
if (TPos != UINT32_MAX)
{
SHGSP *GSP = Extend(QPos, TPos);
if (GSP != 0)
{
AppendGSP(GSP);
QPos += GSP->GetLength();
goto Next;
}
}
}
Next:;
}
}
void cmd_searchsix()
{
const string &QueryFileName = opt(searchsix);
const string &DBFileName = opt(db);
const string &STSStr = opt(sts);
vector<string> STNames;
Split(STSStr, STNames, '+');
asserta(optset_k);
asserta(optset_t || optset_w);
uint k = opt(k);
uint t = (optset_t ? opt(t) : UINT_MAX);
uint w = (optset_w ? opt(w) : UINT_MAX);
vector<SyncmerType> STs;
for (uint i = 0; i < SIZE(STNames); ++i)
{
SyncmerType ST = StrToST(STNames[i]);
STs.push_back(ST);
}
SeqDB DB;
DB.FromFasta(DBFileName);
SeqDB QDB;
QDB.FromFasta(QueryFileName);
ProgressLog("@ANI");
for (uint i = 0; i < SIZE(STNames); ++i)
{
SyncmerType ST = STs[i];
SyncmerIndex SI;
SI.FromSeqDB(ST, DB, k, t, w, 0.3);
SearchHashix SH;
SH.SetHI(&SI);
SeqInfo *Query = ObjMgr::GetSeqInfo();
const unsigned QuerySeqCount = QDB.GetSeqCount();
uint64 TotalBases= 0;
double TotalANIBases = 0.0;
double TotalAFQBases = 0.0;
for (unsigned QuerySeqIndex = 0; QuerySeqIndex < QuerySeqCount; ++QuerySeqIndex)
{
QDB.GetSI(QuerySeqIndex, *Query);
SH.Search(Query);
double ANI, AFQ;
SH.GetANI_AFQ(ANI, AFQ);
uint QL = Query->m_L;
TotalBases += QL;
TotalANIBases += ANI*QL;
TotalAFQBases += AFQ*QL;
}
const char *Name = STNames[i].c_str();
double ANI = TotalANIBases/TotalBases;
double AFQ = TotalAFQBases/TotalBases;
double IndexSizeKilo = SI.GetIndexSize()/1000.0;
ProgressLog(" %s.ANI=%.1f %s.AFQ=%.4f %s.Ix=%.1f",
Name, ANI, Name, AFQ, Name, IndexSizeKilo);
}
ProgressLog("\n");
}