-
Notifications
You must be signed in to change notification settings - Fork 0
/
ab.cpp
400 lines (358 loc) · 9.74 KB
/
ab.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
#include<iostream>
#include<string.h>
#include<vector>
#include<set>
#include<deque>
#include<set>
#include<fstream>
#include<cstdlib>
#include"union.h"
#define _DEBUG_
using namespace std ;
char* allReadConcatenated ;
int k;
int L ;
long GetNumberOfLine(ifstream& file)
{
string line ;
long counter = 0;
while(getline(file,line))
{
counter++;
}
file.seekg(0, ios::beg);
return counter ;
}
long fixindex (const long & i) {
return (i / (2*L)) * L + (i % (2*L));
}
void ConcatenateAllReads(char * array, ifstream& file)
{
int bufsize = 200000000;
char* buff = new char[bufsize];
long counter = 0;
file.seekg(0, ios::beg);
while(!file.eof())
{
cerr<<"begin read"<<endl;
file.read(buff,bufsize);
int numberofCharRead = file.gcount();
cerr<<"end read"<<endl;
for(int i = 0 ; i < numberofCharRead ; i++)
{
if(buff[i] == 'A' || buff[i] == 'T' || buff[i] == 'G' || buff[i] == 'C' )
{
array[counter] = buff[i];
counter++;
}
}
cerr<<"end filter "<<endl;
}
return;
}
struct position_id{
long position;
long nodeID ;
};
typedef struct position_id position_id;
struct compareFunctor
{
//This will find k-dmer sorted by the first part
bool operator()(const position_id first , const position_id second) const
{
if( strncmp(&allReadConcatenated[fixindex(first.position)], &allReadConcatenated[fixindex(second.position)], k) < 0)
return true;
return false;
}
};
void FillInKmers(deque<position_id> &info, const char* fileName, int k)
{
ifstream file ;
file.open(fileName);
if(!file.is_open())
{
cerr<<"Error when openning file "<< fileName<<endl;
exit(1);
}
long x;
while (file >> x) {
struct position_id kdmer = {x,-1};
info.push_back(kdmer);
}
/*
file.seekg(0, ios::beg);
bool first = true;
while(!file.fail())
{
long a=-1;
long pos = file.tellg();
if(first)
{
file.seekg(distance -1 + pos);
first = false ;
}else
file.seekg(distance+ pos);
file >> a;
if(a == -1)
break;
struct position_id kdmer = {a,-1};
info.push_back(kdmer);
}
*/
file.close();
}
void Partition(deque< position_id> &info )
{
deque<position_id>::iterator info_iter = info.begin();
long startBlockIndex = 0 ;
long endBlockIndex =0;
long vertexID = 0 ;
cout<<"size "<<info.size()<<endl;
while(info_iter != info.end())
{
char* firstBlock = &allReadConcatenated[fixindex(info_iter->position)];
char* current = &allReadConcatenated[fixindex(info_iter->position)];
while(strncmp(firstBlock, current, k) == 0)
{
info_iter++;
endBlockIndex++;
if(info_iter == info.end())
break;
current = &allReadConcatenated[fixindex(info_iter->position)];
}
for(long i = startBlockIndex ; i < endBlockIndex ; i++) {
info[i].nodeID = vertexID ;
}
vertexID++;
startBlockIndex = endBlockIndex ;
}
}
void WriteInfo(deque<position_id> & info,const char* fileName)
{
ofstream file ;
file.open(fileName);
if(!file.is_open())
{
cerr<<"Error when opening the file "<<fileName << endl;
exit(1);
}
for(deque<position_id>::iterator it = info.begin() ; it!= info.end() ; ++it)
{
file << it->position << " " << it->nodeID <<endl;
}
file.close();
}
struct compareFunctorByNodeID
{
bool operator()(const position_id first , const position_id second) const
{
if(first.nodeID < second.nodeID)
return true;
return false ;
}
};
deque<position_id>::iterator my_lower_bound(deque<position_id> &info, position_id &kmer)
{
deque<position_id>::iterator info_iter = lower_bound(info.begin(), info.end(), kmer, compareFunctor());
return info_iter ;
}
long FindNextNode(set<long> &positionSet, deque<position_id> & info )
{
set<long> nextNodes ;
for(set<long>::iterator it = positionSet.begin() ; it!= positionSet.end(); ++it)
{
//check the position to see whether it's the last kd mers of the ldmers
long currentPosition = *it ;
if( ((currentPosition % L) - (L- k)) == 0 )
continue;
position_id kmer = {currentPosition + 1 , -1};
deque<position_id>::iterator info_iter = my_lower_bound(info, kmer );
if(info_iter == info.end() )
{
cerr<<"Some errors in binary search \n"<<endl;
assert(0);
}
nextNodes.insert(info_iter->nodeID);
if(nextNodes.size() > 1)
return -1;
}
if(nextNodes.size() == 0)
return -1;
return *(nextNodes.begin());
}
void GetSimpleGraphStructure(vector<long> &nextNodes, deque<position_id> &info)
{
vector<long> sources ;
vector<long> previousNodes(nextNodes.size(), -1);
vector<long> terminateNodes ;
for(deque<position_id>::iterator it = info.begin() ; it != info.end(); it++)
{
set<long> positionSet;
long currentNodeID = it->nodeID;
while(it!= info.end() && it->nodeID == currentNodeID)
{
positionSet.insert(it->position);
it++;
}
it--;
long nextNode = FindNextNode(positionSet, info);
nextNodes[currentNodeID] = nextNode;
}
return ;
}
set<long> GetSourcesNodes(vector<long> & nextNodes)
{
set<long> sourceNodes ;
set<long> nodesOutGoing ;
set<long> nodesInGoing;
long counter = 0;
for(vector<long>::iterator it = nextNodes.begin() ; it!= nextNodes.end() ; it++)
{
if((*it) != -1){
nodesOutGoing.insert(counter);
nodesInGoing.insert(*it);
}
counter++;
}
for(set<long>::iterator it = nodesOutGoing.begin() ; it!= nodesOutGoing.end(); it++)
{
if(nodesInGoing.find(*it) == nodesInGoing.end())
sourceNodes.insert(*it);
}
return sourceNodes ;
}
vector<long> GetNonLoop(vector<long> &path)
{
set<long> nodeSet;
vector<long> nonloopPath;
for(long i = 0 ; i < path.size() ; i ++)
{
if(nodeSet.find(path[i]) != nodeSet.end())
{
return nonloopPath;
}
nodeSet.insert(path[i]);
nonloopPath.push_back(path[i]);
}
return nonloopPath;
}
/*
simple and naive algorithm. TODO put back chasing */
vector<long> FindPath(vector<long> &nextNodes, long source)
{
vector<long> path ;
long currentNode = source;
while(nextNodes[currentNode] != -1 && path.size() <= nextNodes.size())
{
path.push_back(currentNode);
currentNode = nextNodes[currentNode];
}
path.push_back(currentNode);
if(path.size() > nextNodes.size())
{
return GetNonLoop(path);
}
return path;
}
void PrintVector(vector<long> &v, ostream &stream)
{
for(long i = 0 ; i < v.size() ; i++)
stream<<v[i]<<" ";
stream<<endl;
}
void PrintSet (set<long> &s)
{
for(set<long>::iterator it = s.begin() ; it!= s.end() ;it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void FindAndPrintContig(vector<long> &path,ostream &stream, deque<position_id> &info )
{
//find the first path:
if(path.size() == 0)
return ;
position_id kmer = {-1, path[0]};
deque<position_id>::iterator it = lower_bound(info.begin(), info.end(),kmer, compareFunctorByNodeID());
if(it == info.end())
{
cerr<<"Some error in binary search for vertexID \n";
}
stream.write(&allReadConcatenated[fixindex(it->position)] ,k);
for(long i = 1 ; i < path.size() ; i++)
{
kmer.nodeID = path[i];
it = lower_bound(info.begin(), info.end(), kmer, compareFunctorByNodeID());
stream.put( allReadConcatenated[fixindex((it->position) + k-1)]);
}
stream << endl;
}
int main(int argc, char* argv[])
{
if(argc !=4)
{
cerr<<"ab base L k"<<endl;
exit(1);
}
string base = argv[1];
string inputReadsFileName(base);
string inputKmersFileName(base);
inputReadsFileName += ".readl";
inputKmersFileName += ".readks";
L = atoi(argv[2]) ;
k = atoi(argv[3]);
string infoFileName(base);
infoFileName+= ".info";
ifstream inputReadFile ;
inputReadFile.open(inputReadsFileName.c_str());
if(!inputReadFile.is_open())
{
cerr<<"Cannot open file "<< inputReadsFileName << endl;
exit(1);
}
cout << "Counting number of lines...\n";
long numberOfLines = GetNumberOfLine(inputReadFile);
allReadConcatenated = new char[numberOfLines*2*L];
inputReadFile.close();
ifstream newInputReadFile ;
newInputReadFile.open(inputReadsFileName.c_str());
cout << "Reading read file...\n";
ConcatenateAllReads(allReadConcatenated, newInputReadFile);
newInputReadFile.close();
//read the sorted KmersFileName
deque<position_id> info;
cout<<"Reading kmer file...\n";
FillInKmers(info, inputKmersFileName.c_str(), k);
cout<<"Info size "<<info.size()<<endl;
Partition(info);
cout<<"End partioning, start sort \n";
sort(info.begin(), info.end(), compareFunctorByNodeID());
cout<<"End sort start write\n";
WriteInfo( info, infoFileName.c_str());
long maxVertexID = info[info.size()-1].nodeID;
vector<long> nextNodes(maxVertexID +1, -1) ;
cout<<"End write start get graph str\n";
GetSimpleGraphStructure(nextNodes, info);
set<long> sourceNodes = GetSourcesNodes(nextNodes);
#ifdef _DEBUG_
//PrintVector(nextNodes,cout);
//PrintSet(sourceNodes);
#endif
ofstream outFile, detailOutFile, stringOutFile ;
outFile.open((base + ".contigs").c_str());
detailOutFile.open((base + ".contigsdetail").c_str());
stringOutFile.open((base + ".contigsString").c_str());
outFile<< "L="<< L << "\tk=" << k << endl;
for(set<long>::iterator it = sourceNodes.begin(); it!= sourceNodes.end(); it++)
{
vector<long> path = FindPath(nextNodes, *it);
PrintVector(path, detailOutFile);
FindAndPrintContig(path, stringOutFile, info);
outFile << (path.size() + k)<<endl;
}
outFile.close();
detailOutFile.close();
stringOutFile.close();
return 0;
}