-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkmer_ext_map.cpp
166 lines (142 loc) · 4.39 KB
/
kmer_ext_map.cpp
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
#include "config.h"
#include "contig.h"
#include "kmer_ext_map.h"
#include "contig_store.h"
KmerExtMap::KmerExtMap(k_t k)
:k(k), hash_map(INITIAL_CAPACITY, 0,
(hash_map_hash_func_t) kmer_hash_K,
(hash_map_eq_func_t) kmer_eq_K, kmer_size(k))
{
}
void KmerExtMap::insert(kmer_t kmer, ext_map_t ext_map)
{
hash_map.map[kmer] = ext_map;
}
void KmerExtMap::print_ufxs(FILE* outfile)
{
for (map_type_t::iterator it = hash_map.map.begin();
it != hash_map.map.end();
it++) {
kmer_t kmer = it->first;
ext_map_t ext_map = it->second;
char left_ext = 0;
char right_ext = 0;
for (uint8_t i = 0; i < BASE::NUM_BASES; i++) {
if (ext_map.left & 1 << i) {
if (left_ext)
left_ext = 'F';
else
left_ext = base2char((base) i);
}
if (ext_map.right & 1 << i) {
if (right_ext)
right_ext = 'F';
else
right_ext = base2char((base) i);
}
}
if (!left_ext)
left_ext = 'X';
if (!right_ext)
right_ext = 'X';
fprint_kmer(outfile, kmer, k);
fprintf(outfile, "\t%c%c\n", left_ext, right_ext);
kmer_a revcmp[kmer_size(k)];
revcmp_kmer(revcmp, kmer, k);
fprint_kmer(outfile, revcmp, k);
fprintf(outfile, "\t");
if (right_ext == 'F' || right_ext == 'X')
fprintf(outfile, "%c", right_ext);
else
fprintf(outfile, "%c", inv_base(right_ext));
if (left_ext == 'F' || left_ext == 'X')
fprintf(outfile, "%c", left_ext);
else
fprintf(outfile, "%c", inv_base(left_ext));
fprintf(outfile, "\n");
}
}
void KmerExtMap::load_ufxs(FILE* infile)
{
char kmer_str[k + 1];
char left_ext;
char right_ext;
kmer_a kmer[kmer_size(k)];
while (fscanf(infile, "%s %c%c", kmer_str, &left_ext, &right_ext) != EOF) {
str2kmer(kmer, kmer_str, k);
if (!is_canonical_kmer(kmer, k)) continue;
ext_map_t ext_map;
ext_map.left = valid_base(left_ext) ? 1 << (uint8_t) char2base(left_ext) : 0;
ext_map.right = valid_base(right_ext) ? 1 << (uint8_t) char2base(right_ext) : 0;
hash_map.try_insert(kmer);
hash_map.map[kmer] = ext_map;
}
}
void KmerExtMap::build_contigs(ContigStore& contig_store)
{
for (map_type_t::iterator it = hash_map.map.begin();
it != hash_map.map.end();
it++) {
kmer_t kmer = it->first;
ext_map_t ext_map = it->second;
if (!ext_map.valid() || !ext_map.is_uu()) continue;
Contig* contig = new Contig(kmer);
contig->left_ext = ext_map.left_ext();
contig->right_ext = ext_map.right_ext();
walk(contig);
contig->revcmp();
walk(contig);
contig_store.add(contig);
}
}
KmerExtMap::map_type_t::iterator KmerExtMap::lookup_kmer(kmer_t kmer, bool& used_revcmp)
{
map_type_t::iterator it;
kmer_a revcmp[kmer_size(k)];
it = hash_map.map.find(kmer);
if (it != hash_map.map.end()) {
used_revcmp = false;
return it;
}
revcmp_kmer(revcmp, kmer, k);
it = hash_map.map.find(revcmp);
if (it != hash_map.map.end()) {
used_revcmp = true;
return it;
}
return it;
}
void KmerExtMap::walk(Contig* contig)
{
kmer_a kmer[kmer_size(k)];
bool used_revcmp;
base left_ext;
base right_ext;
while (1) {
contig->get_ext_kmer(kmer);
map_type_t::iterator it = lookup_kmer(kmer, used_revcmp);
if (it == hash_map.map.end()) break;
ext_map_t& ext_map = it->second;
if (!ext_map.valid() || !ext_map.is_uu()) break;
if (!used_revcmp) {
left_ext = ext_map.left_ext();
right_ext = ext_map.right_ext();
} else {
left_ext = inv_base(ext_map.right_ext());
right_ext = inv_base(ext_map.left_ext());
}
if (!contig->check_next_left_ext(left_ext)) break;
contig->s += base2char(contig->right_ext);
ext_map.invalidate();
contig->right_ext = right_ext;
}
}
KmerExtMap::~KmerExtMap()
{
for (map_type_t::iterator it = hash_map.map.begin();
it != hash_map.map.end();
it++) {
kmer_t kmer = it->first;
free(kmer);
}
}