-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_builder.cpp
165 lines (156 loc) · 4.4 KB
/
index_builder.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
/**
* @file index_builder.cpp
* @author MohammadJavad Rezaei Seraji <mjrezaei (at) ce.sharif.edu>
*
* @section LICENCE
*
*
* Copyright (C) 2017-2020
* MohammadJavad Rezaei Seraji <mjrezaei (at) ce.sharif.edu>
* Seyed Abolfazl Motahari <motahari (at) sharif.edu
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <vector>
#include <fstream>
#define puu pair< unsigned int, unsigned int >
#include "BWT.h"
static void
print_usage(const char *program)
{
fprintf(stderr, "USAGE: %s <input> <outputFolder>\n", program);
fprintf(stderr, " input : reference file to be indexed\n");
fprintf(stderr, "\n");
fprintf(stderr, "EXAMPLE: %s alice29.fa <file path>\n",program);
fprintf(stderr, "\n");
return;
}
char toLower( char ch ){
if( ch >= 'A' && ch <= 'Z' )
return 'a' + ch - 'A';
return ch;
}
int main(int argc, char** argv) {
int32_t opt, samplerate;
string inname;
uint8_t* T;
uint32_t n;
//cout << argc << endl;
/* parse command line parameter */
if (argc != 3) {
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
string pAdr = argv[2];
if( pAdr[pAdr.length() - 1] != '/' )
pAdr += '/';
opt = -1;
inname = "";
/* read filenames */
inname = argv[1];
int pt = inname.length() - 1;
while( inname[pt] != '/' )
pt--;
//inname = inname.substr( pt + 1 );
string refPrefix = pAdr + inname.substr( pt + 1 );
cout << refPrefix << endl;
string idxname;
/* load input file */
unsigned int offSet = 0;
vector< string > refNames;
vector< unsigned int > refOffset;
ifstream fin( inname );
string line, Ref;
while( fin >> line ){
if( line[0] == '>' ){
string refName = line.substr( 1 );
refNames.push_back( refName );
refOffset.push_back( offSet );
}
else{
bool valid = true;
for( unsigned int i = 0; i < line.length(); i++ ){
line[i] = toLower( line[i] );
if( line[i] != 'a' && line[i] != 'c' && line[i] != 'g' && line[i] != 't' && line[i] != 'n' ){
valid = false;
}
}
if( !valid )
continue;
Ref += line;
offSet += line.length();
}
}
{
uint8_t* T = new uint8_t[Ref.length()];
for( uint32_t i = 0; i < Ref.length(); i++ )
T[i] = Ref[i];
FILE* fout = fopen( ( refPrefix + ".rinfo" ).c_str(), "wb" );
ofstream fout2( ( refPrefix + ".cinfo" ) );
CompressedString ref;
ref.set( T, Ref.length() );
ref.zeroPos = Ref.length();
ref.save( fout );
fclose( fout );
delete[] T;
for( int i = 0; i < refNames.size(); i++ )
fout2 << refNames[i] << ' ' << refOffset[i] << endl;
}
n = Ref.length();
cerr << "ref len = " << n << endl;
T = new uint8_t[n + 100];
// buiding forward bwt
{
idxname = refPrefix + ".fm";
for( unsigned int i = 0; i < n; i++ )
T[i] = Ref[i];
T[n] = 0;
cerr << "Building Forward BWT ...\n";
BWT fm( T, n + 1, 0 );
fm.save( idxname );
//cout << "done\n";
}
cerr << "Forward bwt building is done!\n\n";
// buiding reverse bwt
{
//delete[] T;
T = new uint8_t[n + 100];
idxname = refPrefix + ".rev.fm";
unsigned int ptr = 0;
for( long long i = n - 1; i >= 0; i--, ptr++ ){
//cerr << ptr << endl;
T[ptr] = Ref[i];
}
T[n] = 0;
cerr << "Building Reverse BWT ...\n";
BWT rev( T, n + 1, 1 );
rev.save( idxname );
}
cerr << "Reverse bwt building is done!\n";
return (EXIT_SUCCESS);
}