-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadFastQ.cxx
384 lines (321 loc) · 10.6 KB
/
ReadFastQ.cxx
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
/*=========================================================================
Authors: Kishore Mosaliganti
at Megason Lab, Systems biology, Harvard Medical school, 2009
Copyright (c) 2009, President and Fellows of Harvard College.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
Neither the name of the President and Fellows of Harvard College
nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "KMerSource.h"
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>
#include <map>
#include <ctime>
// Multipass algorithm that exhaustively searches the k-mer space
// using available computer memory with optimal time.
// Each k-mer encountered is broken down into two prefixes and one suffix.
// The first prefix is explored in a single pass through the data.
// The second prefix is an index into std::map array.
// The suffix is binned in the corresponding map.
// Upon finishing each pass, the top candidates are collected
// Convert a value in base-4 to an index
std::string GetString( size_t val, unsigned int kMerSize )
{
std::string s;
s.resize( kMerSize );
size_t rem;
for( unsigned int i = 0; i < kMerSize; i++ )
{
rem = val%4;
switch( rem )
{
case 0 :
s.insert( kMerSize-1-i, 1, 'A' );
break;
case 1 :
s.insert( kMerSize-1-i, 1, 'C' );
break;
case 2 :
s.insert( kMerSize-1-i, 1, 'G' );
break;
case 3 :
s.insert( kMerSize-1-i, 1, 'T' );
break;
}
val = val/4;
}
return s;
}
// Convert a string to an value in base-4
void GetIndex( std::string& kmer, unsigned int& kMerSize, size_t& s )
{
s = 0;
bool newChar = false;
unsigned int val;
for( unsigned int i = 0; i < kMerSize; i++ )
{
switch( kmer[i] )
{
case 'A' :
val = 0;
break;
case 'C' :
val = 1;
break;
case 'G' :
val = 2;
break;
case 'T' :
val = 3;
break;
default :
val = 0;
newChar = true;
}
size_t power(1);
for( unsigned int j = 0; j < kMerSize-1-i; j++ )
{
power *= 4;
}
s += power * val;
}
}
// Function to flip std::pair
template<typename A, typename B>
std::pair<B,A> flip_pair(const std::pair<A,B> &p)
{
return std::pair<B,A>(p.second, p.first);
}
// Function to flip a std::map to a std::multimap
template<typename A, typename B>
std::multimap< B, A, std::greater<B> > flip_map(const std::map<A,B> &src)
{
std::multimap<B,A, std::greater<B> > dst;
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
flip_pair<A,B>);
return dst;
}
// Function to return file size
std::ifstream::pos_type filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
int main ( int argc, char* argv[] )
{
if ( argc < 4 )
{
std::cout << "Usage: " << std::endl;
std::cout << argv[0] << " iFilename iKMerSize iTopCount <PassLength> <KMerPrefixLength>" << std::endl;
std::cout << std::endl << "Last two parameters are optional" << std::endl;
return EXIT_FAILURE;
}
// Begin clock
clock_t begin = clock();
typedef std::map<std::string, size_t> KMerMapType;
typedef KMerMapType::iterator KMerMapIteratorType;
typedef std::multimap<size_t, std::string, std::greater< size_t > > AccumulatorMapType;
typedef AccumulatorMapType::iterator AccumulatorMapIteratorType;
// User-input parameter for k-mer length
unsigned int iKMerSize = atoi( argv[2] );
// User-input parameter for number of k-mer candidates
unsigned int iTopCount = atoi( argv[3] );
if ( iKMerSize == 0 )
{
std::cout << "K-Mer size needs to be larger than 0" << std::endl;
}
if ( iTopCount == 0 )
{
std::cout << "iTopCount size needs to be larger than 0" << std::endl;
}
// Prefix length to be explored in each pass of the data
// Determine this from computer memory available and file size
// Pass is 0 for small file sizes and large memories
// 2 is suitable for file sizes of 15 GB and computer memory of 10 GB
unsigned int PassLength = 2;
unsigned int kMerSizePrefix = 0.5*(iKMerSize - PassLength);
float FileSizeInMB = filesize( argv[1] )/1000000;
std::cout << "File size in MB: " << FileSizeInMB << std::endl;
if ( iKMerSize <= 5 )
{
PassLength = 0;
kMerSizePrefix = 0;
}
else if ( ( iKMerSize > 5 ) && ( iKMerSize <= 16 ) )
{
PassLength = 0;
kMerSizePrefix = iKMerSize-1;
}
else
{
kMerSizePrefix = 14;
if ( FileSizeInMB <= 5000 )
{
PassLength = 0;
}
else if ( ( FileSizeInMB > 5000 ) && ( FileSizeInMB <= 10000 ) )
{
PassLength = 1;
}
}
if ( argc > 4 )
{
PassLength = atoi( argv[4] );
}
if ( argc > 5 )
{
kMerSizePrefix = atoi( argv[5] );
}
unsigned int kMerSizeSuffix = iKMerSize - PassLength - kMerSizePrefix;
std::cout << "K-mer size: " << iKMerSize << std::endl;
std::cout << "Pass length: " << PassLength << std::endl;
std::cout << "Prefix length: " << kMerSizePrefix << std::endl;
std::cout << "Suffix length: " << kMerSizeSuffix << std::endl << std::endl;
size_t counterSize(1);
for( unsigned int j = 0; j < kMerSizePrefix; j++ )
{
counterSize *= 4;
}
size_t index, p;
std::string line, kMer, prefix, suffix, passprefix;
unsigned int len;
AccumulatorMapType KMerCounterAccumulator;
std::fstream inFile( argv[1], std::ios::in );
if( !inFile )
{
std::cout << "File not opened..." << std::endl;
return EXIT_FAILURE;
}
unsigned int numOfPasses(1);
for( unsigned int j = 0; j < PassLength; j++ )
{
numOfPasses *= 4;
}
for( unsigned int pass = 0; pass < numOfPasses; pass++ )
{
std::cout << "Pass ID: " << pass << " of " << numOfPasses-1 << std::endl;
// Initialize an array of k-mers
KMerMapType *KMerCounter;
KMerCounter = new KMerMapType[ counterSize ];
size_t readCounter(0);
while( !inFile.eof() )
{
if ( readCounter%100000 == 0 )
{
std::cout << "Finished reading " << readCounter/100000 << "K lines" << std::endl;
}
// Read in first two lines
std::getline(inFile, line);
// Read second line and determine its length
std::getline(inFile, line);
len = std::strlen( line.c_str() );
// length of the sequence needs to be larger or equal to the kmer size
if ( len >= iKMerSize )
{
// Extract k-mers and insert into the appropriate std::map
for( unsigned int i = 0; i < len-iKMerSize+1; i++ )
{
kMer = line.substr( i, iKMerSize );
if ( kMer.find('N') == std::string::npos )
{
passprefix = kMer.substr( 0, PassLength );
GetIndex( passprefix, PassLength, p );
if ( p == pass )
{
prefix = kMer.substr( PassLength, kMerSizePrefix);
GetIndex( prefix, kMerSizePrefix, index );
suffix = kMer.substr( PassLength+kMerSizePrefix, kMerSizeSuffix);
KMerCounter[index][suffix]++;
}
}
}
}
std::getline(inFile, line);
if( !inFile.eof() )
{
std::getline(inFile, line);
}
readCounter++;
}
// Flip KMerCounters into a multimap
// Merge with KMerCounterAccumuator
for( unsigned int i = 0; i < counterSize; i++ )
{
AccumulatorMapType fKMerCounter = flip_map<std::string, size_t>( KMerCounter[i] );
KMerCounter[i].clear();
// Retain iTopCount elements;
AccumulatorMapIteratorType it = fKMerCounter.begin();
unsigned countLimit = iTopCount;
if ( countLimit > fKMerCounter.size() )
{
countLimit = fKMerCounter.size();
}
for( unsigned int count = 0; count < countLimit; count++ )
{
++it;
}
fKMerCounter.erase( it, fKMerCounter.end() );
// Add to KMerCounterAccumulator and delete kMerCounter
for ( it = fKMerCounter.begin(); it != fKMerCounter.end(); ++it)
{
kMer = GetString( pass, PassLength ) +
GetString( i, kMerSizePrefix ) +
(*it).second;
KMerCounterAccumulator.insert( std::make_pair( (*it).first, kMer ) );
}
fKMerCounter.clear();
}
delete[] KMerCounter;
// Accumulate only the iTopCount values
unsigned int countLimit = iTopCount;
if ( countLimit > KMerCounterAccumulator.size() )
{
countLimit = KMerCounterAccumulator.size();
}
AccumulatorMapIteratorType it = KMerCounterAccumulator.begin();
for( unsigned int count = 0; count < countLimit; count++ )
{
++it;
}
KMerCounterAccumulator.erase( it, KMerCounterAccumulator.end() );
// Reset file stream to beginning
inFile.clear();
inFile.seekg(0, std::ios::beg);
}
inFile.close();
// Write out the top 25 k-mers
std::cout << "Print accumulator size " << std::endl;
AccumulatorMapIteratorType it = KMerCounterAccumulator.begin();
while ( it != KMerCounterAccumulator.end() )
{
std::cout << (*it).first << ' ' << (*it).second << std::endl;
++it;
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
std::cout << "K-mer extraction took " << elapsed_secs << " seconds" << std::endl;
return EXIT_SUCCESS;
}