-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequence.h
216 lines (183 loc) · 4.3 KB
/
sequence.h
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
//(c) 2016 by Authors
//This file is a part of ABruijn program.
//Released under the BSD license (see LICENSE file)
#pragma once
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdexcept>
//Immutable dna sequence class
class DnaSequence
{
public:
typedef size_t NuclType;
private:
static const int NUCL_BITS = 2;
static const int NUCL_IN_CHUNK = sizeof(NuclType) * 8 / NUCL_BITS;
struct SharedBuffer
{
SharedBuffer(): useCount(0), length(0) {}
size_t useCount;
size_t length;
std::vector<size_t> chunks;
};
public:
DnaSequence():
_complement(false)
{
_data = new SharedBuffer;
++_data->useCount;
}
~DnaSequence()
{
if (_data != nullptr)
{
//std::cout << "Destructor!\n";
--_data->useCount;
if (_data->useCount == 0)
{
//std::cout << "Deleting\n";
delete _data;
}
}
}
explicit DnaSequence(const std::string& string):
_complement(false)
{
_data = new SharedBuffer;
++_data->useCount;
if (string.empty()) return;
_data->length = string.length();
_data->chunks.assign((_data->length - 1) / NUCL_IN_CHUNK + 1, 0);
for (size_t i = 0; i < string.length(); ++i)
{
size_t chunkId = i / NUCL_IN_CHUNK;
_data->chunks[chunkId] |= dnaToId(string[i]) << (i % NUCL_IN_CHUNK) * 2;
}
}
DnaSequence(const DnaSequence& other):
_data(other._data),
_complement(other._complement)
{
++_data->useCount;
}
DnaSequence(DnaSequence&& other):
_data(other._data),
_complement(other._complement)
{
other._data = nullptr;
}
DnaSequence& operator=(const DnaSequence& other)
{
--_data->useCount;
if (_data->useCount == 0) delete _data;
_complement = other._complement;
_data = other._data;
++_data->useCount;
return *this;
}
DnaSequence& operator=(DnaSequence&& other)
{
--_data->useCount;
if (_data->useCount == 0) delete _data;
_data = other._data;
_complement = other._complement;
other._data = nullptr;
return *this;
}
size_t length() const {return _data->length;}
char at(size_t index) const
{
if (_complement)
{
index = _data->length - index - 1;
}
size_t id = (_data->chunks[index / NUCL_IN_CHUNK] >>
(index % NUCL_IN_CHUNK) * 2 ) & 3;
return idToDna(!_complement ? id : ~id & 3);
}
NuclType atRaw(size_t index) const
{
if (_complement)
{
index = _data->length - index - 1;
}
size_t id = (_data->chunks[index / NUCL_IN_CHUNK] >>
(index % NUCL_IN_CHUNK) * 2 ) & 3;
return !_complement ? id : ~id & 3;
}
//TODO: use the same shared buffer
DnaSequence complement() const
{
DnaSequence complSequence(*this);
complSequence._complement = true;
return complSequence;
}
DnaSequence substr(size_t start, size_t length) const;
std::string str() const;
static size_t dnaToId(char c)
{
return _dnaTable[(size_t)c];
}
static char idToDna(size_t id)
{
static char table[] = {'A', 'C', 'G', 'T'};
return table[id];
}
private:
static std::vector<size_t> _dnaTable;
struct TableFiller
{
TableFiller()
{
static bool tableFilled = false;
if (!tableFilled)
{
tableFilled = true;
_dnaTable.assign(256, -1); //256 chars
_dnaTable[(size_t)'A'] = 0;
_dnaTable[(size_t)'a'] = 0;
_dnaTable[(size_t)'C'] = 1;
_dnaTable[(size_t)'c'] = 1;
_dnaTable[(size_t)'G'] = 2;
_dnaTable[(size_t)'g'] = 2;
_dnaTable[(size_t)'T'] = 3;
_dnaTable[(size_t)'t'] = 3;
}
}
};
static TableFiller _filler;
SharedBuffer* _data;
bool _complement;
};
inline std::string DnaSequence::str() const
{
std::string result;
result.reserve(this->length());
for (size_t i = 0; i < this->length(); ++i)
{
result.push_back(this->at(i));
}
return result;
}
inline DnaSequence DnaSequence::substr(size_t start, size_t length) const
{
if (length == 0) throw std::runtime_error("Zero length subtring");
if (start >= _data->length) throw std::runtime_error("Incorrect substring start");
if (start + length > _data->length)
{
length = _data->length - start;
}
DnaSequence newSequence;
newSequence._data->length = length;
newSequence._data->chunks.assign((length - 1) / NUCL_IN_CHUNK + 1, 0);
for (size_t i = 0; i < length; ++i)
{
size_t nucId = this->atRaw(start + i);
size_t newChunkId = i / NUCL_IN_CHUNK;
newSequence._data->chunks[newChunkId] |= nucId << (i % NUCL_IN_CHUNK) * 2;
}
return newSequence;
}