-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.cpp
More file actions
170 lines (148 loc) · 4.76 KB
/
Hash.cpp
File metadata and controls
170 lines (148 loc) · 4.76 KB
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
#include "Hash.h"
Hash::Hash (uint32_t mismatches)
{
_mismatches = mismatches;
_readLength = 0;
_numReads = 0;
}
void
Hash::store_reads (const std::string & filename)
{
std::ifstream infile;
infile.open(filename.c_str());
if (not infile.is_open())
{
std::cerr << "Error while opening file " << filename << " for reading" << std::endl;
return;
}
bool init = true;
auto line_num = 1;
while (infile.good())
{
std::string line;
getline (infile, line);
if (line.size() > 0)
{
std::stringstream ss(line);
std::string id("");
std::string s("");
char c;
uint32_t count = 0;
while (std::isalnum(ss.peek()))
{
ss.get(c);
id += c;
}
if (not std::isblank(ss.peek())) continue; // skip incorrectly formatted lines
while (std::isblank(ss.peek()))
ss.get(c);
while (std::isalpha(ss.peek()))
{
ss.get(c);
s += c;
}
if (!init and s.length() != _readLength)
{
std::cerr << "Sequences must all have the same length (line: " << line_num << ")" << std::endl;
return;
}
while (std::isblank(ss.peek()))
ss.get(c);
while (std::isdigit(ss.peek()))
{
ss.get(c);
count = count*10 + c - 48;
}
Reads r(id,s,count);
_readsMulti.push_back(r);
_readLength = r.length();
init = false;
++line_num;
}
}
_numReads += _readsMulti.size();
_numBlocks = _mismatches + 1;
_blockSize = (_readLength + _numBlocks - 1) / _numBlocks;
_numFingerprints = (1 << 2*_blockSize);
std::cout << "num sequences: " << _numReads << std::endl;
std::cout << "block size: " << _blockSize << std::endl;
std::cout << "num fingerprints: " << _numFingerprints << std::endl;
}
void
Hash::fill_hash ()
{
_HASHcounter.assign(_numFingerprints, 0);
_HASHvalues.resize(_numReads*_numBlocks+1);
unsigned long int fp;
// compute # reads with fingerprint = fp
for (auto i = 0; i < _numReads; ++i)
{
for (auto id = 0; id < _numBlocks; ++id)
{
fp = ComputeFingerprint(i, id); // compute the fingerprint for the i-th block
_HASHcounter.at(fp)++;
}
}
// compute # reads with fingerprint < fp
uint32_t t1 = _HASHcounter.at(0);
_HASHcounter.at(0) = 0;
for (size_t i = 1; i < _numFingerprints; ++i) {
int t2 = _HASHcounter.at(i);
_HASHcounter.at(i) = _HASHcounter.at(i-1) + t1;
t1 = t2;
}
// assign read pointer and update # reads with fingerprint <= fp
for (size_t i = 0; i < _numReads; i++)
{
for (size_t id = 0; id < _numBlocks; ++id)
{
fp = ComputeFingerprint(i,id);
fragment_type f;
f.read_id = i; f.block_id = id;
_HASHvalues.at(_HASHcounter.at(fp)) = f;
_HASHcounter.at(fp)++;
}
}
// re-store # of reads with fingerprint < fp (so that it corresponds to the first position in _HASHvalues)
for (uint32_t i = _numFingerprints - 1; i > 0 ; --i)
_HASHcounter.at(i) = _HASHcounter.at(i-1);
_HASHcounter[0] = 0;
}
// TODO: this can be optimized, without converting to string
uint32_t
Hash::ComputeFingerprint (uint32_t i, size_t f_id) const
{
std::string read = _readsMulti.at(i).toString();
uint32_t fingerprint = 0;
auto start = f_id*_blockSize;
auto end = ( (f_id+1)*_blockSize < read.length() ) ? (f_id+1)*_blockSize : read.length();
for (size_t j = start; j < end; ++j)
{
char c = 0;
switch(read.at(j)) {
case 'a' : case 'A' : c = 0; break;
case 'c' : case 'C' : c = 1; break;
case 'g' : case 'G' : c = 2; break;
case 't' : case 'T' : c = 3; break;
}
fingerprint = (fingerprint << 2) + c;
}
return fingerprint;
}
// TODO: this can be optimized, without converting to string
uint32_t
Hash::ComputeHammingDistance (uint32_t i, uint32_t j) const
{
if (i >= _readsMulti.size()) std::cerr << "i >= _readsMulti.size() [" << i << "," << _readsMulti.size() << "]" << std::endl;
std::string read1 = _readsMulti.at(i).toString();
std::string read2 = _readsMulti.at(j).toString();
uint32_t d = 0;
for (size_t k = 0; k < _readLength; ++k)
{
d += (read1.at(k) != read2.at(k));
}
// std::cout << "r1: " << read1 << std::endl;
// std::cout << "r2: " << read2 << std::endl;
// std::cout << "distance = " << d << std::endl << std::endl;
return d;
}