forked from liulab-dfci/TRUST4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KmerCount.hpp
259 lines (229 loc) · 4.78 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
// The call handles kmer count
#ifndef _LSONG_KMERCOUNT_HEADER
#define _LSONG_KMERCOUNT_HEADER
#include <map>
#include <algorithm>
#include "KmerCode.hpp"
#define KCOUNT_HASH_MAX 1000003
class KmerCount
{
private:
std::map<uint64_t, int> *count ;
int kmerLength ;
KmerCode kmerCode ;
int maxReadLen ;
int *c ;
int GetHash( uint64_t k )
{
return k % KCOUNT_HASH_MAX ;
}
public:
KmerCount( int k ): kmerCode( k )
{
kmerLength = k ;
maxReadLen = -1 ;
c = NULL ;
count = new std::map<uint64_t, int>[KCOUNT_HASH_MAX] ;
}
~KmerCount()
{
if ( c != NULL )
delete[] c ;
if ( count != NULL )
delete[] count ;
}
int AddCount( char *read )
{
int i ;
int len = strlen( read ) ;
if ( len < kmerLength )
return 0 ;
kmerCode.Restart() ;
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() ;
++count[ GetHash(kcode) ][ kcode ] ;
/*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 ;
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( char *file )
{
int i, j ;
FILE *fp = fopen( file, "r" ) ;
char *buffer = new char[kmerLength + 1] ;
for ( i = 0 ; i < KCOUNT_HASH_MAX ; ++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] = nucToNum[ ( it->first >> ( 2 * j ) ) & 3 ] ;
}
buffer[j] = '\0' ;
printf( ">%d\n%s\n", it->second, buffer ) ;
}
}
delete[] buffer ;
}
void SetBuffer( int sz )
{
maxReadLen = sz ;
if ( c == NULL )
c = new int[ sz ] ;
}
int GetCount( char *kmer )
{
int i ;
kmerCode.Restart() ;
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 ;
}
int GetCountStatsAndTrim( char *read, char *qual, int &minCount, int &medianCount, double &avgCount )
{
int i, k ;
int sum ;
//minCount = medianCount = avgCount = 1 ;
//return 0 ;
if ( maxReadLen == -1 )
return 0 ;
if ( c == NULL )
c = new int[ maxReadLen ] ;
int len = strlen( read ) ;
if ( len < kmerLength )
{
minCount = medianCount = avgCount = -1 ;
return 0 ;
}
kmerCode.Restart() ;
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 ;
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 Release()
{
if ( c != NULL )
delete[] c ;
if ( count != NULL )
delete[] count ;
c = NULL ;
count = NULL ;
}
} ;
#endif