-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnucsequences.hxx
318 lines (263 loc) · 7.98 KB
/
nucsequences.hxx
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
#ifndef NUCSEQUENCES_HXX
#define NUCSEQUENCES_HXX
#include "nucsequences.hpp"
template <bool SUBMATCHES, bool MISMATCHES, bool BWT>
void NucSequence::search(NucQuery & query, const int & mismatches, const int & submatches)
{
// Alias to the sequence we are looking for
const string & word = query.sequence();
// Size of the word
saidx_t size = word.size();
search<MISMATCHES,BWT>(query,mismatches);
if(SUBMATCHES)
{
if(size >= submatches)
{
NucQuery * last = &query;
for(int i=size-submatches; i>=0; --i)
{
last->next = new NucQuery;
NucQuery * elt = last->next;
elt->name(query.name());
elt->sequence(word.substr(i, submatches));
elt->sense(query.sense());
search<MISMATCHES,BWT>(*elt,mismatches);
last = elt;
}
last->next = 0;
// We merge adjacent submatches
NucQuery * elt = query.next;
NucQuery * nxt = elt->next;
while(nxt != 0)
{
string str1(elt->sequence(),0,elt->sequence().size()-1);
string str2(nxt->sequence(),1,nxt->sequence().size()-1);
NucQuery * lastprev = last;
if(str1 == str2)
{
vector<int> ind;
for(int i=0;i<elt->count(); ++i)
for(int j=0;j<nxt->count(); ++j)
if(elt->position(i) == (nxt->position(j)+1))
ind.push_back(i);
if(ind.size() > 0)
{
string tmp(nxt->sequence(),0,1);
tmp += elt->sequence();
last->next = new NucQuery;
last = last->next;
last->name(query.name());
last->sequence(tmp);
last->sense(query.sense());
last->next = 0;
for(int i=ind.size()-1;i>=0; --i)
{
last->addPosition(elt->position(ind[i])-1);
elt->removePosition(ind[i]);
}
}
}
if(str1 == str2 || elt->sequence().size() < nxt->sequence().size())
{
vector<int> ind2rm;
for(int i=0;i<elt->count(); ++i)
for(int j=0;j<lastprev->count(); ++j)
if(elt->position(i) == lastprev->position(j))
ind2rm.push_back(i);
for(int i=ind2rm.size()-1;i>=0; --i)
elt->removePosition(ind2rm[i]);
}
elt = nxt;
nxt = elt->next;
}
// List cleaning
elt = &query;
nxt = elt->next;
while(nxt != last)
{
if(nxt->count() == 0)
{
elt->next = nxt->next;
nxt->next = 0;
delete nxt;
}
else
elt = nxt;
nxt = elt->next;
}
if(last->sequence() == query.sequence() || last->count() == 0)
{
elt->next = 0;
delete last;
}
}
}
}
template <bool MISMATCHES, bool BWT>
void NucSequence::search(NucQuery & query, const int & mismatches)
{
// Alias to the sequence we are looking for
const string & word = query.sequence();
// Size of the word
saidx_t size = word.size();
if(BWT)
{
if(!MISMATCHES)
{
// We initialize the loop variables
// Low index
saidx_t low = 0;
// High index
saidx_t high = _seqsize+1;
// We search for character in ith position
// with consideration to the previous character treated
for(saidx_t i=size-1; i>=0 && low < high; --i)
{
// ith character in word
char c = word[i];
// Corresponding index
short ic = _nuc[c];
// Occurrences (table divided in blocks)
// Blocks indexes
saidx_t lowb = low/_blocksize;
saidx_t highb = high/_blocksize;
// Blocks values
saidx_t lowocc = _occ[ic+lowb*_nchar];
saidx_t highocc = _occ[ic+highb*_nchar];
// Remaining characters to browse in BWT
short lowmodb = low%_blocksize;
short highmodb = high%_blocksize;
// Counting remaining characters in BWT (low)
for(short a=0; a<lowmodb; ++a)
if(_sequence[a+lowb*_blocksize] == c)
++lowocc;
// Counting remaining characters in BWT (high)
for(short a=0; a<highmodb; ++a)
if(_sequence[a+highb*_blocksize] == c)
++highocc;
// New low and high indexes
low = _C[ic] + lowocc;
high = _C[ic] + highocc;
}
// We store their positions
for(saidx_t k=low; k<high; ++k)
query.addPosition(_SA[k]);
}
else
{
// We initialize the loop variables
// Character being treated
saidx_t i = size-1;
//list<Candidate> candidates;
//candidates.push_front(Candidate(0,0,_seqsize+1));
Candidates * init = 0;
Candidates * lst = new Candidates(0,(saidx_t)0,_seqsize+1,init);
// We search for character in ith position
// with consideration to the previous character treated
while(lst != 0 && i >= 0)
{
Candidates * ptr = lst;
Candidates ** prev = &lst;
while(ptr != 0)
{
// We get the candidate triplet
int count = ptr->count;
saidx_t clow = ptr->low;
saidx_t chigh = ptr->high;
// Map iteration
map<char,unsigned char>::iterator it = _nuc.begin();
++it;
while(it!=_nuc.end())
{
char c = it->first;
char ic = it->second;
// Local low & high
saidx_t high = chigh;
saidx_t low = clow;
// Occurrences (table divided in blocks)
// Blocks indexes
saidx_t lowb = low/_blocksize;
saidx_t highb = high/_blocksize;
// Blocks values
saidx_t lowocc = _occ[ic+lowb*_nchar];
saidx_t highocc = _occ[ic+highb*_nchar];
// Remaining characters to browse in BWT
short lowmodb = low%_blocksize;
short highmodb = high%_blocksize;
// Counting remaining characters in BWT (low)
for(short a=0; a<lowmodb; ++a)
if(_sequence[a+lowb*_blocksize] == c)
++lowocc;
// Counting remaining characters in BWT (high)
for(short a=0; a<highmodb; ++a)
if(_sequence[a+highb*_blocksize] == c)
++highocc;
// New low and high indexes
low = _C[ic] + lowocc;
high = _C[ic] + highocc;
if(c != word[i] && low < high && count<mismatches)
{
lst = new Candidates(count+1,low,high,lst);
if(lst->next == ptr)
prev = &(lst->next);
}
else if(c == word[i] && low < high)
{
lst = new Candidates(count,low,high,lst);
if(lst->next == ptr)
prev = &(lst->next);
}
++it;
}
*prev = ptr->next;
delete ptr;
ptr = *prev;
}
// Previous character
--i;
}
// We store their positions
Candidates * ptr = lst;
while(ptr != 0)
{
for(saidx_t k=ptr->low; k<ptr->high; ++k)
query.addPosition(_SA[k]);
Candidates * prev = ptr;
ptr = ptr->next;
delete prev;
}
}
}
else
{
if(!MISMATCHES)
{
size_t pos = string::npos;
do {
pos = _sequence.find(word,pos+1);
if(pos != string::npos)
query.addPosition((saidx_t)pos);
} while(pos != string::npos);
}
else
{
saidx_t seqsize = _sequence.size();
if(seqsize >= size)
{
saidx_t searchedseqsize = seqsize - size;
// For each position, we check the number of mismatches
for(saidx_t pos=0; pos <= searchedseqsize; ++pos)
{
int miss = 0;
for(saidx_t j=0; j<size; ++j)
if(word[j] != _sequence[j+pos])
++miss;
// If miss <= mismatch, we found one more.
if(miss <= mismatches)
query.addPosition(pos);
}
}
}
}
}
#endif // NUCSEQUENCES_HXX