-
Notifications
You must be signed in to change notification settings - Fork 49
/
KmerCount.hpp
321 lines (288 loc) · 6.04 KB
/
KmerCount.hpp
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
// The call handles kmer count
#ifndef _LSONG_KMERCOUNT_HEADER
#define _LSONG_KMERCOUNT_HEADER
#include <map>
#include <algorithm>
#include <pthread.h>
#include "KmerCode.hpp"
class KmerCount
{
private:
std::map<uint64_t, int> *count ;
int kmerLength ;
int maxReadLen ;
int khashMax ;
pthread_mutex_t **locks ;
int *c ;
int GetHash( uint64_t k )
{
return k % khashMax ;
}
public:
KmerCount( int k, int hmax = 1000003 )
{
kmerLength = k ;
khashMax = hmax ;
maxReadLen = -1 ;
c = NULL ;
count = new std::map<uint64_t, int>[khashMax] ;
locks = NULL ;
}
KmerCount(const KmerCount &b)
{
kmerLength = b.kmerLength ;
khashMax = b.khashMax ;
maxReadLen = -1 ;
c = NULL ;
count = new std::map<uint64_t, int>[khashMax] ;
locks = NULL ;
}
~KmerCount()
{
Release() ;
}
void SetPthreadLocks()
{
locks = new pthread_mutex_t *[khashMax] ;
int i ;
for (i = 0 ; i < khashMax ; ++i)
{
locks[i] = new pthread_mutex_t ;
pthread_mutex_init(locks[i], NULL) ;
}
}
int AddCount( char *read )
{
int i ;
int len = strlen( read ) ;
if ( len < kmerLength )
return 0 ;
KmerCode kmerCode(kmerLength) ;
for ( i = 0 ; i < kmerLength - 1 ; ++i )
kmerCode.Append( read[i] ) ;
for ( ; i < len ; ++i )
{
kmerCode.Append( read[i] ) ;
if ( kmerCode.IsValid() )
{
uint64_t kcode = kmerCode.GetCanonicalKmerCode() ;
int h = GetHash(kcode) ;
if (locks)
pthread_mutex_lock(locks[h]) ;
++count[h][ kcode ] ;
if (locks)
pthread_mutex_unlock(locks[h]) ;
/*if ( count[ GetHash( kcode ) ][ kcode ] >= 500 )
{
printf( "%s\n", read + i - kmerLength + 1 ) ;
}*/
}
}
if ( len > maxReadLen )
maxReadLen = len ;
return 1 ;
}
void AddCountFromFile( char *file )
{
FILE *fp = fopen( file, "r" ) ;
char buffer[100] ;
int i ;
KmerCode kmerCode(kmerLength) ;
while ( fscanf( fp, "%s", buffer ) != EOF )
{
int c = atoi( &buffer[ 1 ] ) ;
fscanf( fp, "%s", buffer ) ;
if ( c <= 1 )
continue ;
kmerCode.Restart() ;
for ( i = 0 ; buffer[i] ; ++i )
kmerCode.Append( buffer[i] ) ;
uint64_t kcode = kmerCode.GetCode() ;
count[ GetHash( kcode ) ][ kcode ] = c ;
}
fclose( fp ) ;
}
void Output(FILE *fp)
{
int i, j ;
char *buffer = new char[kmerLength + 1] ;
for ( i = 0 ; i < khashMax ; ++i )
{
for ( std::map<uint64_t, int>::iterator it = count[i].begin() ; it != count[i].end() ; ++it )
{
if ( it->second <= 1 )
continue ;
for ( j = 0 ; j < kmerLength ; ++j )
{
buffer[j] = numToNuc[ ( it->first >> ( 2 * j ) ) & 3 ] ;
}
buffer[j] = '\0' ;
fprintf( fp, ">%d\n%s\n", it->second, buffer ) ;
}
}
delete[] buffer ;
}
void SetBuffer( int sz )
{
maxReadLen = sz ;
if ( c == NULL )
c = new int[ sz ] ;
}
void SetBuffer()
{
if (c == NULL && maxReadLen > 0)
c = new int[maxReadLen] ;
}
int GetCount( char *kmer )
{
int i ;
KmerCode kmerCode(kmerLength) ;
for ( i = 0 ; i < kmerLength ; ++i )
kmerCode.Append( kmer[i] ) ;
if ( kmerCode.IsValid() )
{
uint64_t kcode = kmerCode.GetCanonicalKmerCode() ;
int key = GetHash( kcode ) ;
if ( count[ key ].find( kcode ) == count[key].end() )
return 0 ;
else
return count[ GetHash( kcode ) ][ kcode ] ;
}
else
return 0 ;
}
// This is thread-safe when providedBuffer is given
int GetCountStatsAndTrim( char *read, char *qual, int &minCount, int &medianCount, float &avgCount,
int *providedBuffer = NULL)
{
int i, k ;
int sum ;
//minCount = medianCount = avgCount = 1 ;
//return 0 ;
if ( maxReadLen == -1 )
return 0 ;
int *c = providedBuffer ;
if ( c == NULL )
{
if (this->c == NULL)
this->c = new int[ maxReadLen ] ;
c = this->c ;
}
int len = strlen( read ) ;
if ( len < kmerLength )
{
minCount = medianCount = avgCount = -1 ;
return 0 ;
}
KmerCode kmerCode(kmerLength) ;
for ( i = 0 ; i < kmerLength - 1 ; ++i )
kmerCode.Append( read[i] ) ;
k = 0 ;
sum = 0 ;
for ( ; i < len ; ++i )
{
kmerCode.Append( read[i] ) ;
if ( kmerCode.IsValid() )
{
uint64_t kcode = kmerCode.GetCanonicalKmerCode() ;
int key = GetHash( kcode ) ;
if ( count[ key ].find( kcode ) == count[key].end() )
c[k] = 0 ;
else
c[k] = count[ GetHash( kcode ) ][ kcode ] ;
if ( c[k] <= 0 )
c[k] = 1 ;
sum += c[k] ;
++k ;
}
}
if ( k == 0 )
{
minCount = -len ;
medianCount = -len ;
avgCount = -len ;
// Don't trim the read (kind of) when quality score is not give
// this help keep the function consistent that it only trims when
// qual score is given.
if (qual != NULL)
read[0] = '\0' ;
return 0 ;
}
// Do the trimming.
if ( qual != NULL )
{
int j ;
for ( i = k - 1 ; i >= 0 ; --i )
{
if ( c[i] > 1 )
break ;
}
// Locate the low quality region.
++i ;
//for ( j = i + kmerLength - 1 ; j < len ; ++j )
int badCnt = 0 ;
int trimStart = -1 ;
for ( j = len - 1 ; j >= i + kmerLength - 1 ; --j )
{
if ( qual[j] - 32 <= 15 )
{
++badCnt ;
if ( badCnt >= 0.1 * ( len - j ) )
trimStart = j ;
}
}
if ( trimStart > 0 )
{
k = trimStart - kmerLength + 1 ;
read[ trimStart ] = '\0' ;
qual[ trimStart ] = '\0' ;
}
if ( trimStart > 0 && trimStart < kmerLength )
{
k = 0 ;
read[0] = '\0' ;
qual[0] = '\0' ;
}
}
std::sort( c, c + k ) ;
minCount = c[0] ;
medianCount = c[k / 2] ;
avgCount = sum / (double)k ;
for ( i = 0 ; i < len ; ++i )
if ( read[i] == 'N' )
{
if ( minCount >= 0 )
minCount = 0 ;
else if ( minCount <= 0 )
--minCount ;
}
return 1 ;
}
void Clear()
{
int i ;
for (i = 0 ; i < khashMax ; ++i)
count[i].clear() ;
}
void Release()
{
if ( c != NULL )
delete[] c ;
if ( count != NULL )
delete[] count ;
c = NULL ;
count = NULL ;
if (locks != NULL)
{
int i ;
for (i = 0 ; i < khashMax ; ++i)
{
pthread_mutex_destroy(locks[i]) ;
delete locks[i] ;
}
delete[] locks ;
locks = NULL ;
}
}
} ;
#endif