-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlm.cpp
447 lines (388 loc) · 12.2 KB
/
lm.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <ctime>
#include <unordered_map>
#include <algorithm>
#include <sys/stat.h>
#include <sys/types.h>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <chrono>
#include "lm.h"
/*
* Constuct the compacted de bruijn graph from list of distinct kmers
*/
using namespace std;
// 10 chars to store the integer hash of a minimizer
#define MINIMIZER_STR_SIZE 10
// keep largest bucket seen, disregarding small ones
unsigned long nb_elts_in_largest_bucket = 10000;
string minimizer2string(int input_int){
long long i = input_int;
string str = to_string(i);
assert(str.size() <= MINIMIZER_STR_SIZE);
if(MINIMIZER_STR_SIZE > str.size())
str.insert(0, MINIMIZER_STR_SIZE- str.size(), '0');
return str;
}
bool nextchar(char * c){
switch(*c){
case 'a':
*c='c';
return true;
case 'c':
*c='g';
return true;
case 'g':
*c='t';
return true;
case 't':
*c='a';
return false;
default :
cout<<"Problem with nextchar: "<<c;
assert(0);
return false;
}
}
// reads k-mers and Put kmers in superbuckets
// (other behavior: just count m-mers)
void sortentry(string namefile, const int k, const int m, bool create_buckets, bool m_mer_count){
int numbersuperbucket(pow(4,m));
ofstream out[1100];
ofstream checkpoint;
// InputDot ind;
if (create_buckets)
{
for(long long i(0);i<numbersuperbucket;i++)
out[i].open(".bcalmtmp/z"+to_string(i),ofstream::app);
}
// ind.init_input(namefile,k);
ifstream in(namefile);
string kmer,waste;
while (1)
{
// string kmer = ind.next_input(k);
getline(in,kmer,' ');
getline(in,waste);
if (kmer.size()<(unsigned int)k){
break;
}
transform(kmer.begin(),kmer.end(),kmer.begin(),::tolower);
if (m_mer_count){
count_m_mers(kmer, 2*m, k);
continue;
}
int middlemin, leftmostmin, rightmostmin, min;
middlemin=minimiserrc(kmer.substr(1,k-2),2*m);
leftmostmin=minimiserrc(kmer.substr(0,2*m),2*m);
rightmostmin=minimiserrc(kmer.substr(kmer.size()-2*m,2*m),2*m);
int leftmin = (leftmostmin < middlemin) ? leftmostmin : middlemin;
int rightmin = (rightmostmin < middlemin) ? rightmostmin : middlemin ;
min = (leftmin < rightmin) ? leftmin : rightmin;
uint64_t h = min/numbersuperbucket;
if (create_buckets)
out[h]<<kmer<< minimizer2string(leftmin) << minimizer2string(rightmin) <<";";
else
cout << h << ":" << kmer<< minimizer2string(leftmin) << minimizer2string(rightmin) << ";\n";
//checkpoint << kmer << minimizer2string(leftmin) << minimizer2string(rightmin) <<";\n";
}
if (create_buckets)
cout << "initial partitioning done" << endl;
}
//copy n characters from in to out
void copylm(ifstream* in,int64_t n,ofstream* out){
int buffsize(1000000),nbbuffer(n/buffsize);
string str;
for(int j(0);j<nbbuffer;j++) {
str=readn(in,buffsize);
*out<<str;
}
str=readn(in,n-nbbuffer*buffsize);
*out<<str;
}
void copylmrv(ifstream* in,int64_t n,ofstream* out){
int buffsize(1000000),nbbuffer(n/buffsize);
int64_t pos(in->tellg());
string str;
int j;
for(j=1;j<=nbbuffer;j++){
in->seekg(pos+n-j*buffsize,ios::beg);
str=readn(in,buffsize);
*out<<reversecompletment(str);
}
int64_t rest=n-nbbuffer*buffsize;
if(rest!=0){
in->seekg(pos,ios::beg);
str=readn(in,rest);
*out<<reversecompletment(str);
}
}
//Put nodes from superbuckets to buckets
void createbucket(const string superbucketname,const int m){
int superbucketnum(stoi(superbucketname));
ifstream in(".bcalmtmp/z"+superbucketname);
if(!in.is_open()){
cerr<<"Problem with Createbucket"<<endl;
return;
}
in.seekg(0, ios_base::end);
int64_t size(in.tellg()), buffsize(1000000), numberbuffer(size/buffsize),nb(pow(4,m)),suffix;
if(size==0){
remove((".bcalmtmp/z"+superbucketname).c_str());
return;
}
in.seekg(0,ios::beg);
ofstream out[5000];
for(long long i(0);i<nb;i++){
out[i].open(".bcalmtmp/"+to_string(superbucketnum*nb+i),ofstream::app);
}
int64_t lastposition(-1),position(0),point(0),mini;
string buffer;
vector<string> miniv;
in.seekg(0);
for(int j(0);j<=numberbuffer;j++){
if(j==numberbuffer){
if(size-numberbuffer*buffsize-1!=-1){
buffer=readn(&in,size-numberbuffer*buffsize-1);
buffer+=";";
}
else
buffer="";
}
else
{
buffer=readn(&in,buffsize);
point+=buffsize;
}
for (uint64_t i(0); i<buffer.size(); i++,position++)
{
if((buffer)[i]==';'){
int leftmin, rightmin;
if(i>=(uint64_t)(MINIMIZER_STR_SIZE * 2))
{
leftmin = stoi(buffer.substr(i-(MINIMIZER_STR_SIZE * 2),MINIMIZER_STR_SIZE));
rightmin = stoi(buffer.substr(i-MINIMIZER_STR_SIZE,MINIMIZER_STR_SIZE));
}
else{
in.seekg(position-(MINIMIZER_STR_SIZE * 2));
leftmin = stoi(readn(&in,MINIMIZER_STR_SIZE));
rightmin = stoi(readn(&in,MINIMIZER_STR_SIZE));
}
string first_bucket_of_superbucket(to_string((long long)(superbucketnum*nb-1)));
mini = minbutbiggerthan(leftmin, rightmin, first_bucket_of_superbucket);
suffix=mini%nb;
in.seekg(lastposition+1,ios_base::beg);
copylm(&in,position-lastposition,&out[suffix]);
lastposition=position;
}
}
in.seekg(point);
}
remove((".bcalmtmp/z"+superbucketname).c_str());
}
//count the length of each node
vector<int64_t> countbucket(const string& name){
vector<int64_t> count;
ifstream in(".bcalmtmp/"+name);
if(in){
in.seekg( 0 , ios_base::end );
int64_t size(in.tellg()),buffsize(10),numberbuffer(size/buffsize),lastposition(-1),position(0);
if(size<2)
return count;
in.seekg(0,ios::beg);
string buffer;
for(int j(0);j<numberbuffer;j++){
buffer=readn(&in,buffsize);
for (uint64_t i(0); i<buffer.size(); i++,position++)
if((buffer)[i]==';'){
count.push_back(position-lastposition-1);
lastposition=position;
}
}
buffer=readn(&in,size-numberbuffer*buffsize);
for (uint64_t i(0); i<buffer.size(); i++,position++)
if((buffer)[i]==';'){
count.push_back(position-lastposition-1);
lastposition=position;
}
}
return count;
}
//true iff node does not contain tag
bool notag(const string& node,const int64_t start,int64_t* n){
for(uint64_t i(start);i<node.size();i++){
if (node[i] >= '0' && node[i] <= '9'){
*n=i;
return false;
}
}
return true;
}
//return length of tag
int taglength(const string& node, int64_t j){
int n=1;
for(uint64_t i(j+1);i<node.size();i++)
if ((node[i] >= '0' && node[i] <= '9') || node[i]=='+' || node[i]=='-')
n++;
else
return n;
return n;
}
//Write a node remplacing tags by their sequences
void writeit(const string& outfile,const string& node, int leftmin, int rightmin, vector<pair<int64_t,int64_t>>* tagsposition,ifstream* tagfile,int64_t j,const string& fout){
ofstream out(".bcalmtmp/"+outfile,ios::app);
char rc;
if(out){
int64_t lastposition(0),tag,tagl,position,length;
pair<int64_t,int64_t> pair;
do{
out<<node.substr(lastposition,j-lastposition-1);
tagl=taglength(node,j);
rc=node[j-1];
if(rc=='+'){
tag=stoi(node.substr(j,tagl));
}
else{
tag=stoi(reversecompletment(node.substr(j,tagl)));
}
lastposition=j+tagl;
pair=(*tagsposition)[tag];
position=pair.first;
length= pair.second;
tagfile->seekg(position,ios_base::beg);
if(rc=='+')
copylm(tagfile,length,&out);
if(rc=='-')
copylmrv(tagfile,length,&out);
}
while(!notag(node,lastposition,&j));
if(outfile!=fout)
out<<node.substr(lastposition)<< minimizer2string(leftmin) << minimizer2string(rightmin) <<";";
else
out<<node.substr(lastposition) <<";"<<endl;
}
else
cerr<<"writeitbug"<<endl;
}
void put(const string& outfile,const string& node, int leftmin, int rightmin, const string& fout){
ofstream out(".bcalmtmp/"+outfile,ios::app);
if(outfile==fout)
out<<node <<";" << endl;
else
out<<node<< minimizer2string(leftmin) << minimizer2string(rightmin) <<";";
}
void putorwrite(const string& outfile, const string& node, int leftmin, int rightmin, vector<pair<int64_t,int64_t>>* tagsposition , ifstream* tagfile,const string& fout){
int64_t i;
if(notag(node,0,&i))
put(outfile,node,leftmin, rightmin, fout);
else
writeit(outfile,node, leftmin, rightmin, tagsposition,tagfile,i,fout);
}
//Decide where to put a node
void goodplace(const string& node, int leftmin, int rightmin, const string& bucketname,vector<pair<int64_t,int64_t>>* tagsposition,ifstream* tagfile,const int m,const string& nameout){
int nb(pow(4,m)),prefixnumber(stoi(bucketname)/nb+1);
long long mini(minbutbiggerthan(leftmin, rightmin, bucketname));
if(mini==-1)
putorwrite( nameout, node, leftmin, rightmin, tagsposition,tagfile,nameout);
else{
long long minipre(mini/nb);
string miniprefix('z'+to_string(minipre));
putorwrite( ((minipre >= prefixnumber) ? miniprefix: to_string(mini)), node, leftmin, rightmin, tagsposition,tagfile,nameout);
}
}
//Compact a bucket and put the nodes on the right place
void compactbucket(const int& prefix,const int& suffix,const int k,const char *nameout,const int m){
int64_t buffsize(k),postags(0),length,nb(pow(4,m));
long long tagnumber(0),numberbucket(prefix*nb+suffix);
string fullname(to_string(numberbucket)),node,tag,end;
auto count(countbucket(fullname));
if(count.size()==0) {
remove((".bcalmtmp/"+fullname).c_str());
return;
}
// keep largest bucket seen
if (count.size() > nb_elts_in_largest_bucket)
{
nb_elts_in_largest_bucket = count.size();
system(("cp .bcalmtmp/" + fullname + " largest_bucket.dot").c_str());
}
ifstream in(".bcalmtmp/"+fullname);
ofstream tagfile(".bcalmtmp/tags"),out(".bcalmtmp/"+(string)nameout,ios_base::app);
graph g(k);
vector<pair<int64_t,int64_t>> tagsposition;
// add nodes to graph
if(in && tagfile && out){
for(auto it=count.begin();it!=count.end();it++){
length=*it;
if(length-(MINIMIZER_STR_SIZE*2)<=2*buffsize){
node=readn(&in,length+1);
g.addvertex(node.substr(0,length-(MINIMIZER_STR_SIZE*2)));
g.addleftmin(stoi(node.substr(length-(MINIMIZER_STR_SIZE*2),MINIMIZER_STR_SIZE)));
g.addrightmin(stoi(node.substr(length-MINIMIZER_STR_SIZE,MINIMIZER_STR_SIZE)));
}
else{
node=readn(&in,buffsize);
tag=to_string(tagnumber);
node+="+"+tag+"+";
tagnumber++;
copylm(&in,length-(MINIMIZER_STR_SIZE*2)-2*buffsize,&tagfile);
tagsposition.push_back(make_pair(postags,length-(MINIMIZER_STR_SIZE*2)-2*buffsize));
postags+=length-(MINIMIZER_STR_SIZE*2)-2*buffsize;
end=readn(&in,buffsize);
node+=end.substr(0,buffsize);
g.addvertex(node);
g.addleftmin(stoi(readn(&in,MINIMIZER_STR_SIZE)));
g.addrightmin(stoi(readn(&in,MINIMIZER_STR_SIZE)));
readn(&in,1); // the ';'
}
}
tagfile.close();
}
remove((".bcalmtmp/"+fullname).c_str());
g.debruijn();
g.compressh(stoi(fullname));
ifstream fichiertagin(".bcalmtmp/tags");
int node_index = 0;
for(auto it(g.nodes.begin());it!=g.nodes.end();it++)
{
if(it->size()!=0)
{
int leftmin = g.leftmins[node_index];
int rightmin = g.rightmins[node_index];
goodplace(*it, leftmin, rightmin,fullname,&tagsposition,&fichiertagin,m,nameout);
}
node_index++;
}
remove(".bcalmtmp/tags");
return;
}
//Create a file with the nodes of the compacted graph
void createoutfile(const char *namein,const char *nameout,const int k,const int m){
auto start=chrono::system_clock::now();
HashMap hm(build_hash_map(2*m));
int64_t nbsuperbucket(pow(4,m)),sys(0);
mkdir(".bcalmtmp",0777);
sys+=system("rm -rf .bcalmtmp/*");
remove(nameout);
// create the hash function
init_m_mers_table(2*m);
sortentry(namein,k,m, false, true);
create_hash_function_from_m_mers(2*m);
sortentry(namein,k,m);
for(long long i(0);i<nbsuperbucket;i++){
createbucket(to_string(i),m);
for(int j(0);j<pow(4,m);j++)
compactbucket(i,j,k,nameout,m);
}
sys+=system(("mv .bcalmtmp/"+(string)nameout+" "+(string)nameout).c_str());
if(sys!=0)
cerr<<"system call failed"<<endl;
auto end=chrono::system_clock::now();
auto waitedFor=end-start;
cout<<"Last for "<<chrono::duration_cast<chrono::seconds>(waitedFor).count()<<" seconds"<<endl;
}