-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_bwt.cpp
103 lines (86 loc) · 2.27 KB
/
read_bwt.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
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "rlcsa.h"
using namespace CSA;
/*
This program writes run-length encoded PLCP of the collection into a file.
*/
usint
countRuns(usint prev, uchar* buffer, usint length)
{
usint runs = 0;
for(usint i = 0; i < length; i )
{
if(buffer[i] != prev)
{
prev = buffer[i];
if(buffer[i] != 0) { runs ; }
}
}
return runs;
}
int
main(int argc, char** argv)
{
std::cout << "RLCSA to BWT converter" << std::endl;
if(argc < 2)
{
std::cout << "Usage: read_bwt base_name [buffer_size]" << std::endl;
return 1;
}
std::string base_name = argv[1];
std::string bwt_name = base_name ".bwt";
std::cout << "BWT: " << bwt_name << std::endl;
std::ofstream bwt_file(bwt_name.c_str(), std::ios_base::binary);
if(!bwt_file)
{
std::cerr << "Error creating BWT file!" << std::endl;
return 2;
}
std::cout << std::endl;
RLCSA rlcsa(base_name);
clock_t start = clock();
usint buffer_size = 0;
if(argc > 2) { buffer_size = atoi(argv[2]); }
usint n = rlcsa.getSize() rlcsa.getNumberOfSequences();
usint runs = 0, prev = CHARS;
if(buffer_size > 0)
{
for(usint i = 0; i < n; i = buffer_size)
{
pair_type range(i, std::min(i buffer_size - 1, n - 1));
uchar* bwt = rlcsa.readBWT(range);
if(bwt != 0)
{
runs = countRuns(prev, bwt, length(range));
prev = bwt[length(range) - 1];
bwt_file.write((char*)bwt, length(range));
delete[] bwt;
}
}
}
else
{
uchar* bwt = rlcsa.readBWT();
if(bwt != 0)
{
runs = countRuns(prev, bwt, n);
bwt_file.write((char*)bwt, n);
delete[] bwt;
}
}
clock_t stop = clock();
double time = ((stop - start) / (double)CLOCKS_PER_SEC);
double megabytes = n / (double)MEGABYTE;
std::cout << megabytes << " megabytes in " << time << " seconds (" << (megabytes / time) << " MB/s)" << std::endl;
std::cout << std::endl;
// Testing direct reporting of the number of runs.
// This is as expensive as reading the BWT.
std::cout << "Number of runs: " << runs << std::endl;
runs = rlcsa.countRuns();
std::cout << "Number of runs (direct count): " << runs << std::endl;
std::cout << std::endl;
bwt_file.close();
return 0;
}