forked from liulab-dfci/TRUST4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KmerIndex.hpp
194 lines (170 loc) · 4.44 KB
/
KmerIndex.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
#ifndef _LSONG_KMERINDEX_HEADER
#define _LSONG_KMERINDEX_HEADER
#include <stdio.h>
#include <stdint.h>
#include <map>
#include "defs.h"
#include "KmerCode.hpp"
#include "SimpleVector.hpp"
struct _indexInfo
{
index_t idx ;
index_t offset ;
//int strand ;
} ;
#define KINDEX_HASH_MAX 1000003
class KmerIndex
{
private:
std::map< uint64_t, SimpleVector<struct _indexInfo> > *index ;
SimpleVector<struct _indexInfo> nullHit ;
int GetHash( uint64_t k )
{
return k % KINDEX_HASH_MAX ;
}
public:
KmerIndex()
{
index = new std::map< uint64_t, SimpleVector<struct _indexInfo> >[KINDEX_HASH_MAX] ;
}
~KmerIndex()
{
int sum = 0 ;
//for ( int i = 0 ; i < hashSize ; ++i )
//{
// sum += /*sizeof( hash[i] ) +*/ hash[i].Memory() ;
//}
//printf( "%s: %d %d %d\n", __func__, hashSize, sizeof( hash[0] ), hash[0].Memory() ) ;
if ( index != NULL )
delete[] index ;
}
void Clear()
{
//index.clear() ;
int i ;
for ( i = 0 ; i < KINDEX_HASH_MAX ; ++i )
std::map<uint64_t, SimpleVector<struct _indexInfo> >().swap( index[i] ) ;
}
void Insert( KmerCode &kmerCode, index_t idx, index_t offset, int strand )
{
if ( !kmerCode.IsValid() )
return ;
struct _indexInfo newEntry ;
newEntry.idx = idx ;
newEntry.offset = offset ;
//newEntry.strand = strand ;
uint64_t kcode = kmerCode.GetCode() ;
int h = GetHash( kcode ) ;
index[h][ kcode ].PushBack( newEntry ) ;
//printf( "%d\n", hash[key].Memory() ) ;
}
void Remove( KmerCode &kmerCode, index_t idx, index_t offset, int strand )
{
if ( !kmerCode.IsValid() )
return ;
SimpleVector<struct _indexInfo> &list = *Search( kmerCode ) ;
int size = list.Size() ;
int i ;
for ( i = 0 ; i < size ; ++i )
{
if ( list[i].idx == idx && list[i].offset == offset )
break ;
}
if ( i < size )
{
//printf( "remove %d %d\n", idx, offset ) ;
list.Remove( i ) ;
}
}
SimpleVector<struct _indexInfo> *Search( KmerCode &kmerCode )
{
if ( !kmerCode.IsValid() )
return &nullHit ;
uint64_t kcode = kmerCode.GetCode() ;
int h = GetHash( kcode ) ;
std::map< uint64_t, SimpleVector<struct _indexInfo> >::iterator it = index[h].find( kcode ) ;
if ( it == index[h].end() )
return &nullHit ;
else
return &(it->second) ;
}
void BuildIndexFromRead( KmerCode &kmerCode, char *s, int len, int id, int shift = 0 )
{
int i ;
int kl = kmerCode.GetKmerLength() ;
if ( len < kl )
return ;
kmerCode.Restart() ;
//KmerCode rcKmerCode( kl ) ;
KmerCode prevKmerCode( kl ) ;
for ( i = 0 ; i < kl - 1 ; ++i )
kmerCode.Append( s[i] ) ;
for ( ; i < len ; ++i )
{
kmerCode.Append( s[i] ) ;
if ( kmerCode.IsValid() && ( i == kl || !kmerCode.IsEqual( prevKmerCode ) ) )
{
Insert( kmerCode, id, i - kl + 1 + shift, 1 ) ;
//rcKmerCode.SetCode( kmerCode.GetReverseComplementCode() ) ;
//Insert( rcKmerCode, id, len - 1 - i, -1 ) ;
}
prevKmerCode = kmerCode ;
}
}
// When merging or extending sequences, there kmer position will shift and change id.
void UpdateIndexFromRead( KmerCode &kmerCode, char *s, int len, int shift, int oldId, int id )
{
int i, j ;
int kl = kmerCode.GetKmerLength() ;
if ( len < kl )
return ;
kmerCode.Restart() ;
for ( i = 0 ; i < kl - 1 ; ++i )
kmerCode.Append( s[i] ) ;
for ( ; i < len ; ++i )
{
kmerCode.Append( s[i] ) ;
if ( kmerCode.IsValid() )
{
SimpleVector<struct _indexInfo> &list = *Search( kmerCode ) ;
int size = list.Size() ;
//printf( "kcode: %lld\n", kmerCode.GetCode() ) ;
//for ( j = 0 ; j < size ; ++j )
// printf( "before %d %d\n", list[j].idx, list[j].offset ) ;
for ( j = 0 ; j < size ; ++j )
{
if ( list[j].idx == oldId && list[j].offset == i - kl + 1 )
{
//printf( "update %d->%d: %d %d\n", oldId, id, i - kl + 1, shift ) ;
list[j].idx = id ;
list[j].offset += shift ;
break ;
}
}
}
SimpleVector<struct _indexInfo> &list = *Search( kmerCode ) ;
int size = list.Size() ;
//for ( j = 0 ; j < size ; ++j )
// printf( "test %d %d\n", list[j].idx, list[j].offset ) ;
}
}
void RemoveIndexFromRead( KmerCode &kmerCode, char *s, int len, int id, int offset )
{
int i, j ;
int kl = kmerCode.GetKmerLength() ;
if ( len < kl )
return ;
kmerCode.Restart() ;
for ( i = 0 ; i < kl - 1 ; ++i )
kmerCode.Append( s[i] ) ;
for ( ; i < len ; ++i )
{
kmerCode.Append( s[i] ) ;
if ( kmerCode.IsValid() )
{
Remove( kmerCode, id, i - kl + 1 + offset, 1 ) ;
}
}
}
} ;
#endif