-
Notifications
You must be signed in to change notification settings - Fork 15
/
ri-count.cpp
174 lines (109 loc) · 3.61 KB
/
ri-count.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
// Copyright (c) 2017, Nicola Prezza. All rights reserved.
// Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
#include <iostream>
#include "internal/r_index.hpp"
#include "internal/utils.hpp"
using namespace ri;
using namespace std;
string check = string();//check occurrences on this text
bool hyb=false;
void help(){
cout << "ri-count: number of occurrences of the input patterns." << endl << endl;
cout << "Usage: ri-count <index> <patterns>" << endl;
//cout << " -h use hybrid bitvectors instead of elias-fano in both RLBWT and predecessor structures. -h is required "<<endl;
//cout << " if the index was built with -h options enabled."<<endl;
cout << " <index> index file (with extension .ri)" << endl;
cout << " <patterns> file in pizza&chili format containing the patterns." << endl;
exit(0);
}
void parse_args(char** argv, int argc, int &ptr){
assert(ptr<argc);
string s(argv[ptr]);
ptr++;
/*if(s.compare("-h")==0){
hyb=true;
}else*/{
cout << "Error: unknown option " << s << endl;
help();
}
}
template<class idx_t>
void count(std::ifstream& in, string patterns){
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
string text;
bool c = false;
if(check.compare(string()) != 0){
c = true;
ifstream ifs1(check);
stringstream ss;
ss << ifs1.rdbuf();//read the file
text = ss.str();
}
auto t1 = high_resolution_clock::now();
idx_t idx;
idx.load(in);
auto t2 = high_resolution_clock::now();
cout << "searching patterns ... " << endl;
ifstream ifs(patterns);
//read header of the pizza&chilli input file
//header example:
//# number=7 length=10 file=genome.fasta forbidden=\n\t
string header;
std::getline(ifs, header);
ulint n = get_number_of_patterns(header);
ulint m = get_patterns_length(header);
uint last_perc = 0;
ulint occ_tot=0;
//extract patterns from file and search them in the index
for(ulint i=0;i<n;++i){
uint perc = (100*i)/n;
if(perc>last_perc){
cout << perc << "% done ..." << endl;
last_perc=perc;
}
string p = string();
for(ulint j=0;j<m;++j){
char c;
ifs.get(c);
p+=c;
}
occ_tot += idx.occ(p);
}
double occ_avg = (double)occ_tot / n;
cout << endl << occ_avg << " average occurrences per pattern" << endl;
ifs.close();
auto t3 = high_resolution_clock::now();
//printRSSstat();
uint64_t load = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
cout << "Load time : " << load << " milliseconds" << endl;
uint64_t search = std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2).count();
cout << "number of patterns n = " << n << endl;
cout << "pattern length m = " << m << endl;
cout << "total number of occurrences occ_t = " << occ_tot << endl;
cout << "Total time : " << search << " milliseconds" << endl;
cout << "Search time : " << (double)search/n << " milliseconds/pattern (total: " << n << " patterns)" << endl;
cout << "Search time : " << (double)search/occ_tot << " milliseconds/occurrence (total: " << occ_tot << " occurrences)" << endl;
}
int main(int argc, char** argv){
if(argc < 3)
help();
int ptr = 1;
while(ptr<argc-2)
parse_args(argv, argc, ptr);
string idx_file(argv[ptr]);
string patt_file(argv[ptr+1]);
std::ifstream in(idx_file);
bool fast;
//fast or small index?
in.read((char*)&fast,sizeof(fast));
cout << "Loading r-index" << endl;
if(hyb){
count<r_index<sparse_hyb_vector,rle_string_hyb> >(in, patt_file);
}else{
count<r_index<> >(in, patt_file);
}
in.close();
}