forked from xxsds/DYNAMIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_kmer_minimizers.h
279 lines (270 loc) · 8.86 KB
/
get_kmer_minimizers.h
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
////////////////////////////////////////////////////////////////////////////////
// get_kmer_minimizers.h
// get kmer minimizer header file.
//
// holding the get kmer_minimizer method which is a basic building block of the algorithm
//
////////////////////////////////////////////////////////////////////////////////
// author: Alexander Petri
#ifndef GET_KMER_MINIMIZERS_H
#define GET_KMER_MINIMIZERS_H
#include "main.h"
#include "Minimizer.h"
#include "B-tree.hh"
#include "B_tree_node.hh"
using namespace std;
using namespace md;
//using namespace dyn;
typedef typename B_tree<int,int,7,3>::key_t _key_t;
typedef typename B_tree<int,int,7,3>::shifted_key_ptr_t _shifted_key_ptr_t;
/*
* Print the current state of the forward list to the console (for debugging reasons)
*
* @param forward the forward list to be printed to the console
*/
void print_forward_list(std::forward_list<std::string>& forward){
////cout<<"\n";
for ( auto it = forward.begin(); it != forward.end(); ++it ){
////cout<<*it<<"\n";
}
////cout<<"\n";
}
/*!
* Generate the kmer minimizers of a sequence. Inspired by Kristoffer Sahlins' get_kmer_minimizer, however
* this method uses a forward list to store the kmers. Does not generate end minimizers!!!
*
* @param sequence the sequence for which minimizers are to be generated
* @param k_size the length of the window_kmers
* @param w_size the window size (length of the subsequence in which w kmers are present)
*
* @return minimizers the minimizers for the sequence stored in a vector
*
* (@param w) not a param of this function as w can be calculated by w=w_size-k_size+1
*/
std::vector<Minimizer> get_kmer_minimizers(string& sequence, int& k_size, int& w_size){
int w = w_size - k_size+1;
string max(k_size, 'Z');
string curr_min;
int min_pos=std::numeric_limits<int>::max();
int min_diff=0;
curr_min=max;
std::vector<Minimizer> minimizers;
std::forward_list<std::string> forward;
auto beginIt=forward.begin();
auto otherIt=beginIt;
// generate the first minimizer of the sequence by finding the minimum kmer out of the first w k_mers
for(int i=0;i<w;i++){
//fill the forward list with the kmers
std::string new_kmer=sequence.substr(i,k_size);
if(forward.empty()){
otherIt = forward.insert_after(forward.before_begin(), new_kmer);
}
else{
otherIt = forward.insert_after(otherIt, new_kmer);
}
}
int itnum=0;
int startpos=0;
std::string minimum=max;
//find the minimal k_mer (the minimizer)
for ( auto it = forward.begin(); it != forward.end(); ++it ){
if(*it<minimum){
minimum=*it;
startpos=itnum;
}
itnum++;
}
//discard all kmers before the minimizer
if(startpos!=0){
for( auto it2 = 0; it2 != startpos; ++it2 ){
forward.pop_front();
}
}
forward.pop_front();
//set the otherIt iterator to the last element of the forward list
for( auto it = forward.begin(); it != forward.end(); ++it ){
otherIt=it;
}
curr_min=minimum;
//generate Minimizer object and add it to the solution
Minimizer startmini=Minimizer(startpos,minimum);
minimizers.push_back(startmini);
Minimizer mini;
min_pos=startpos;
//find the rest of the Minimizers
for (int i=w;i<sequence.length()-k_size+1;i++){
string new_kmer=sequence.substr(i,k_size);
//if the new k_mer is smaller than curr_min: We have found a minimizer! Empty forward list
if(new_kmer<curr_min){
curr_min=new_kmer;
min_pos=i;
mini.updateMinimizer(min_pos,curr_min);
minimizers.push_back(mini);
forward.clear();
}
//if we have not found a new minimizer in w consecutive kmers. Find minimum of the set of kmers->new minimizer
else if(i-min_pos==w){
otherIt = forward.insert_after(otherIt, new_kmer);
int itnum=0;
int pos=0;
std::string minimum=max;
for ( auto it = forward.begin(); it != forward.end(); ++it ){
itnum++;
if(*it<minimum){
minimum=*it;
pos=itnum;
}
}
//only discard all kmers located before the minimizer from the forward list
if(pos!=0){
for( auto it2 = 1; it2 != pos; ++it2 ){
forward.pop_front();
}
forward.pop_front();
}
for( auto it = forward.begin(); it != forward.end(); ++it ){
otherIt=it;
}
curr_min=minimum;
min_pos=min_pos+pos;
mini.updateMinimizer(min_pos,curr_min);
minimizers.push_back(mini);
}
//only add the new kmer to the forward list
else{
if(forward.empty()){
otherIt = forward.insert_after(forward.before_begin(), new_kmer);
}
else{
otherIt = forward.insert_after(otherIt, new_kmer);
}
}
}
return minimizers;
}
/*!
* Generate the kmer minimizers of a sequence. Inspired by Kristoffer Sahlins' get_kmer_minimizer, however
* this method uses a forward list to store the kmers. Does not generate end minimizers!!!
*
* @param sequence the sequence for which minimizers are to be generated
* @param k_size the length of the window_kmers
* @param w_size the window size (length of the subsequence in which w kmers are present)
*
* @return minimizers the minimizers for the sequence stored in a vector
*
* (@param w) not a param of this function as w can be calculated by w=w_size-k_size+1
*/
std::vector<Minimizer> get_kmer_minimizers_algo(string& sequence, int& k_size, int& w_size,int& posshift){
int w = w_size - k_size+1;
string max(k_size, 'Z');
string curr_min;
int min_pos=std::numeric_limits<int>::max();
int min_diff=0;
curr_min=max;
std::vector<Minimizer> minimizers;
std::forward_list<std::string> forward;
auto beginIt=forward.begin();
auto otherIt=beginIt;
int realpos=0;
// generate the first minimizer of the sequence by finding the minimum kmer out of the first w k_mers
for(int i=0;i<w;i++){
//fill the forward list with the kmers
std::string new_kmer=sequence.substr(i,k_size);
if(forward.empty()){
otherIt = forward.insert_after(forward.before_begin(), new_kmer);
}
else{
otherIt = forward.insert_after(otherIt, new_kmer);
}
}
int itnum=0;
int startpos=0;
std::string minimum=max;
//find the minimal k_mer (the minimizer)
for ( auto it = forward.begin(); it != forward.end(); ++it ){
if(*it<minimum){
minimum=*it;
startpos=itnum;
}
itnum++;
}
//discard all kmers before the minimizer
if(startpos!=0){
for( auto it2 = 0; it2 != startpos; ++it2 ){
forward.pop_front();
}
}
forward.pop_front();
//set the otherIt iterator to the last element of the forward list
for( auto it = forward.begin(); it != forward.end(); ++it ){
otherIt=it;
}
curr_min=minimum;
//generate Minimizer object and add it to the solution
realpos=startpos+posshift;
//Minimizer startmini=Minimizer(realpos,minimum);
minimizers.push_back(Minimizer(realpos,minimum));
//minimizerTree->insert(realpos,minimum);
//Minimizer mini;
min_pos=startpos;
//find the rest of the Minimizers
for (int i=w;i<sequence.length()-k_size+1;i++){
string new_kmer=sequence.substr(i,k_size);
//if the new k_mer is smaller than curr_min: We have found a minimizer! Empty forward list
if(new_kmer<curr_min){
curr_min=new_kmer;
min_pos=i;
realpos=min_pos+posshift;
//mini.updateMinimizer(realpos,curr_min);
minimizers.push_back(Minimizer(realpos, curr_min));
//minimizerTree->insert(realpos,curr_min);
forward.clear();
}
//if we have not found a new minimizer in w consecutive kmers. Find minimum of the set of kmers->new minimizer
else if(i-min_pos==w){
otherIt = forward.insert_after(otherIt, new_kmer);
int itnum=0;
int pos=0;
std::string minimum=max;
for ( auto it = forward.begin(); it != forward.end(); ++it ){
itnum++;
if(*it<minimum){
minimum=*it;
pos=itnum;
}
}
//only discard all kmers located before the minimizer from the forward list
if(pos!=0){
for( auto it2 = 1; it2 != pos; ++it2 ){
forward.pop_front();
}
forward.pop_front();
}
for( auto it = forward.begin(); it != forward.end(); ++it ){
otherIt=it;
}
curr_min=minimum;
min_pos=min_pos+pos;
realpos=min_pos+posshift;
//mini.updateMinimizer(realpos,curr_min);
minimizers.push_back(Minimizer(realpos,curr_min));
//minimizerTree->insert(realpos,curr_min);
}
//only add the new kmer to the forward list
else{
if(forward.empty()){
otherIt = forward.insert_after(forward.before_begin(), new_kmer);
}
else{
otherIt = forward.insert_after(otherIt, new_kmer);
}
}
}
//cout<<"Printing getkmerminimizers\n";
for(int i=0;i<minimizers.size();i++){
minimizers[i].printMinimizer();
}
//cout<<"Printing getkmerminimizers done\n";
return minimizers;
}
#endif