-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKmerCount.hpp
219 lines (192 loc) · 4.33 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
// The call handles kmer count
#ifndef _LSONG_KMERCOUNT_HEADER
#define _LSONG_KMERCOUNT_HEADER
#include <map>
#include <algorithm>
#include <math.h>
#include "KmerCode.hpp"
#define KCOUNT_HASH_MAX 103
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(): kmerCode(31)
{
kmerLength = 31 ;
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 ;
}
void Release()
{
if ( c != NULL )
delete[] c ;
if ( count != NULL )
delete[] count ;
c = NULL ;
count = NULL ;
}
// Jaccard index
double GetCountSimilarityJaccard(const KmerCount &b)
{
int i ;
int countA = 0 ;
int countB = 0 ;
int sharedCount = 0 ;
int tmp ;
for (i = 0 ; i < KCOUNT_HASH_MAX ; ++i)
{
for (std::map<uint64_t, int>::iterator it = count[i].begin() ; it != count[i].end() ; ++it)
{
countA += it->second ;
if (b.count[i].find(it->first) != b.count[i].end())
{
tmp = b.count[i][it->first] ;
sharedCount += (it->second < tmp ? it->second : tmp) ;
}
}
for (std::map<uint64_t, int>::iterator it = b.count[i].begin() ; it != b.count[i].end() ; ++it)
{
countB += it->second ;
}
}
return (double)sharedCount / (countA + countB - sharedCount) ;
}
// How similar is a to b ;
// Note that this is not symmetric
double GetCountSimilarity(const KmerCount &b)
{
int i ;
int countA = 0 ;
int sharedCount = 0 ;
int tmp ;
for (i = 0 ; i < KCOUNT_HASH_MAX ; ++i)
{
for (std::map<uint64_t, int>::iterator it = count[i].begin() ; it != count[i].end() ; ++it)
{
countA += it->second ;
if (b.count[i].find(it->first) != b.count[i].end())
{
sharedCount += it->second ;
}
}
}
return (double)sharedCount / (double)countA ;
//return pow((double)sharedCount / (double)countA, 1.0 / (kmerLength) );
//return -log((double)sharedCount / (double)countA) / (double)kmerLength;
}
} ;
#endif