-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cpp
183 lines (136 loc) · 5.29 KB
/
build.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
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <tuple>
#include <cstring>
#include <divsufsort.h>
#include <cereal/archives/binary.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/string.hpp>
#include <string>
#include <chrono>
//#include <filesystem>
#include <sys/stat.h>
using namespace std;
using namespace std::chrono;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/************************************************DEFINED FUNCTIONS***************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
tuple<string,int> read_sequence(string&);
tuple<vector<string>,vector<uint64_t>,vector<uint64_t>> pref_func(string&, int*, int&);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/**********************************************MAIN FUNCTION*********************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]){
//Default Parameters
string reference = "./ecoli49M.fa";
string output = "./out_49M_15";
int k = 15;
//overwrite default by user input
for(size_t arg_ind = 1; arg_ind < argc; arg_ind++){
if(argv[arg_ind] == "--reference"){
reference = argv[arg_ind + 1];
}else if(argv[arg_ind] == "--output"){
output = argv[arg_ind + 1];
}else if(argv[arg_ind] == "--preftab"){
k = atoi(argv[arg_ind + 1]);
}
}
string sequence;
int n;
tie(sequence,n) = read_sequence(reference);
int *SA = (int *)malloc(n * sizeof(int));
auto start = high_resolution_clock::now();
divsufsort((const unsigned char *)sequence.c_str(), SA, n);
//k-mer prefix table
vector<string> pref_string;
vector<uint64_t> pref_first, pref_last;
if( k != 0){
tie(pref_string, pref_first, pref_last) = pref_func(sequence, SA, k);
}
auto stop = high_resolution_clock::now();
duration<double> elapsedTime = (stop - start);
//writing the output to a binary file
vector<int> SA_vec;
for(size_t j=0; j<n; j++){
SA_vec.push_back(SA[j]);
}
free(SA);
ofstream os(output, std::ios::binary);
cereal::BinaryOutputArchive archive( os );
archive(sequence, SA_vec, pref_string, pref_first, pref_last);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************READING THE REFERENCE GENOME***************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//reading data function
tuple<string,int> read_sequence(string& seq_address){
string line, id, sequence , S_rand = "ACGT";
//reading the reference genome file
ifstream infile(seq_address);
while (getline(infile,line))
{
if (line.empty())
{
continue;
}
if (line[0] == '>')
{
id = line.substr(1);
}
else
{
sequence += line;
}
}
sequence = sequence + "$";
//checking foe lower case and convert them to upper case and laso check for n/N in the reference
for (size_t i = 0; i < sequence.size(); i++)
{
if (sequence[i] == 'a' || sequence[i] == 'c' || sequence[i] == 'g' || sequence[i] == 't')
{
sequence[i] = toupper(sequence[i]);
}
else if (sequence[i] == 'n' || sequence[i] == 'N')
{
srand(time(0));
sequence[i] = S_rand[rand()%4];
}
}
return make_tuple(sequence,sequence.size());
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************CONSTRUCTING PREFIX TABLE FUNTION**********************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
tuple<vector<string>,vector<uint64_t>,vector<uint64_t>> pref_func(string& sequence, int *SA, int& k){
//vector<vector<uin64_t>> pre_table;
//vector<uin64_t> vec;
vector<string> Pref_string;
vector<uint64_t> Pref_First;
vector<uint64_t> Pref_Last;
string K_MER="", K_MER_previous="";
int counter=0;
for(size_t i = 1; i < sequence.size(); i++){
//sequence.size() - SA[i] <k ? continue:K_MER = sequence.substr(SA[i],k);
if((sequence.size() - SA[i] - 1) <k){
continue;
}else{
K_MER_previous = K_MER;
K_MER = sequence.substr(SA[i],k);
}
if(K_MER == K_MER_previous){
counter++;
Pref_Last.pop_back();
Pref_Last.push_back(Pref_First.back() + counter -1);
}
if(K_MER != K_MER_previous){
counter = 1;
Pref_string.push_back(K_MER);
Pref_First.push_back(i);
Pref_Last.push_back(i + counter -1);
}
}
return make_tuple(Pref_string, Pref_First, Pref_Last);
}