-
Notifications
You must be signed in to change notification settings - Fork 0
/
sa.h
125 lines (111 loc) · 2.68 KB
/
sa.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
#include<cassert>
#include<cmath>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<cstdarg>
#include<algorithm>
using namespace std;
#include "defs.h"
struct sah__compltNum2NumFunctor {
bool operator()(int x, int y) const {
if (x==y) return false;
for (int i = 0; i < genSize - max(x,y) + 1; i++) {
if (genome[x+i] != genome[y+i]) return genome[x+i] < genome[y+i];
}
return x > y; //no, this is not a mistake
}
string genome;
int genSize;
};
struct sah__compltNum2StrFunctor {
bool operator()(int x, string y) const {
return genome.substr(x, y.length()) < y;
}
string genome;
int genSize;
};
class suffixArray {
public:
int genSize;
vector<int> sa;
suffixArray(string g) {
genSize = g.size();
genome = g + g;
}
void build() {
cerr << "Building suffix array...\n";
for (int i = 0; i < genSize; i++) {
sa.push_back(i);
}
sah__compltNum2NumFunctor complt;
complt.genome = genome;
complt.genSize = genSize;
sort(sa.begin(), sa.end(), complt);
}
/*void build(int kmersize, char skip) {
cerr << "Building suffix array...\n";
for (int i = 0; i < genSize; i++) {
if (genome.find_first_of('$', i, kmersize) != string::npos) continue;
sa.push_back(i);
}
sah__compltNum2NumFunctor complt;
complt.genome = genome;
complt.genSize = genSize;
sort(sa.begin(), sa.end(), complt);
}
*/
void save(string filename, int k = 0) {
ofstream out;
open_file(out, filename);
if (k==0) {
for (int i = 0; i < sa.size(); i++) out << sa[i] << endl;
} else {
for (int i = 0; i < sa.size(); i++) out << sa[i] << "\t" << genome.substr(sa[i],k) << endl;
}
out.close();
}
void load(string filename) {
sa.clear();
ifstream in;
open_file(in, filename);
string sbuf;
while (getline(in, sbuf)) {
istringstream line(sbuf);
int val;
line >> val;
sa.push_back(val);
}
in.close();
}
int find(string kmer) {
sah__compltNum2StrFunctor complt;
complt.genome = genome;
complt.genSize = genSize;
vector<int>::iterator it = lower_bound(sa.begin(), sa.end(), kmer, complt);
if (it == sa.end() || (genome.substr(*it, kmer.length()) != kmer) ) {
return -1;
}
return *it;
}
void find(string kmer, vector<int> &hits) {
hits.clear();
sah__compltNum2StrFunctor complt;
complt.genome = genome;
complt.genSize = genSize;
vector<int>::iterator it = lower_bound(sa.begin(), sa.end(), kmer, complt);
while (it != sa.end() || (genome.substr(*it, kmer.length()) == kmer) ) {
hits.push_back(*it);
it++;
}
return;
}
private:
string genome;
};