-
Notifications
You must be signed in to change notification settings - Fork 120
/
alt.h
294 lines (254 loc) · 8.26 KB
/
alt.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright 2015, Daehwan Kim <[email protected]>
*
* This file is part of HISAT 2.
*
* HISAT 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HISAT 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HISAT 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALT_H_
#define ALT_H_
#include <iostream>
#include <fstream>
#include <limits>
#include "assert_helpers.h"
#include "word_io.h"
#include "mem_ids.h"
using namespace std;
enum ALT_TYPE {
ALT_NONE = 0,
ALT_SNP_SGL, // single nucleotide substitution
ALT_SNP_INS, // small insertion wrt reference genome
ALT_SNP_DEL, // small deletion wrt reference genome
ALT_SNP_ALT, // alternative sequence (to be implemented ...)
ALT_SPLICESITE,
ALT_EXON
};
template <typename index_t>
struct ALT {
ALT() {
reset();
}
void reset() {
type = ALT_NONE;
pos = len = 0;
seq = 0;
}
ALT_TYPE type;
union {
index_t pos;
index_t left;
};
union {
index_t len;
index_t right;
};
union {
uint64_t seq; // used to store 32 bp, but it can be used to store a pointer to EList<uint64_t>
struct {
union {
bool fw;
bool reversed;
};
bool excluded;
};
};
public:
// in order to support a sequence longer than 32 bp
bool snp() const { return type == ALT_SNP_SGL || type == ALT_SNP_DEL || type == ALT_SNP_INS; }
bool splicesite() const { return type == ALT_SPLICESITE; }
bool mismatch() const { return type == ALT_SNP_SGL; }
bool gap() const { return type == ALT_SNP_DEL || type == ALT_SNP_INS || type == ALT_SPLICESITE; }
bool deletion() const { return type == ALT_SNP_DEL; }
bool insertion() const { return type == ALT_SNP_INS; }
bool exon() const { return type == ALT_EXON; }
bool operator< (const ALT& o) const {
if(pos != o.pos) return pos < o.pos;
if(type != o.type) {
if(type == ALT_NONE || o.type == ALT_NONE) {
return type == ALT_NONE;
}
if(type == ALT_SNP_INS) return true;
else if(o.type == ALT_SNP_INS) return false;
return type < o.type;
}
if(len != o.len) return len < o.len;
if(seq != o.seq) return seq < o.seq;
return false;
}
bool compatibleWith(const ALT& o) const {
if(pos == o.pos) return false;
// sort the two SNPs
const ALT& a = (pos < o.pos ? *this : o);
const ALT& b = (pos < o.pos ? o : *this);
if(a.snp()) {
if(a.type == ALT_SNP_DEL || a.type == ALT_SNP_INS) {
if(b.pos <= a.pos + a.len) {
return false;
}
}
} else if(a.splicesite()) {
if(b.pos <= a.right + 2) {
return false;
}
} else {
assert(false);
}
return true;
}
bool isSame(const ALT& o) const {
if(type != o.type)
return false;
if(type == ALT_SNP_SGL) {
return pos == o.pos && seq == o.seq;
} else if(type == ALT_SNP_DEL || type == ALT_SNP_INS || type == ALT_SPLICESITE) {
if(type == ALT_SNP_INS) {
if(seq != o.seq)
return false;
}
if(reversed == o.reversed) {
return pos == o.pos && len == o.len;
} else {
if(reversed) {
return pos - len + 1 == o.pos && len == o.len;
} else {
return pos == o.pos - o.len + 1 && len == o.len;
}
}
} else {
assert(false);
}
return true;
}
#ifndef NDEBUG
bool repOk() const {
if(type == ALT_SNP_SGL) {
if(len != 1) {
assert(false);
return false;
}
if(seq > 3) {
assert(false);
return false;
}
} else if(type == ALT_SNP_DEL) {
if(len <= 0) {
assert(false);
return false;
}
if(seq != 0) {
assert(false);
return false;
}
} else if(type == ALT_SNP_INS) {
if(len <= 0) {
assert(false);
return false;
}
} else if(type == ALT_SPLICESITE) {
assert_lt(left, right);
assert_leq(fw, 1);
}else {
assert(false);
return false;
}
return true;
}
#endif
bool write(ofstream& f_out, bool bigEndian) const {
writeIndex<index_t>(f_out, pos, bigEndian);
writeU32(f_out, type, bigEndian);
writeIndex<index_t>(f_out, len, bigEndian);
writeIndex<uint64_t>(f_out, seq, bigEndian);
return true;
}
bool read(ifstream& f_in, bool bigEndian) {
pos = readIndex<index_t>(f_in, bigEndian);
type = (ALT_TYPE)readU32(f_in, bigEndian);
assert_neq(type, ALT_SNP_ALT);
len = readIndex<index_t>(f_in, bigEndian);
seq = readIndex<uint64_t>(f_in, bigEndian);
return true;
}
};
template <typename index_t>
struct Haplotype {
Haplotype() {
reset();
}
void reset() {
left = right = 0;
alts.clear();
}
index_t left;
index_t right;
EList<index_t, 1> alts;
bool operator< (const Haplotype& o) const {
if(left != o.left) return left < o.left;
if(right != o.right) return right < o.right;
return false;
}
bool write(ofstream& f_out, bool bigEndian) const {
writeIndex<index_t>(f_out, left, bigEndian);
writeIndex<index_t>(f_out, right, bigEndian);
writeIndex<index_t>(f_out, alts.size(), bigEndian);
for(index_t i = 0; i < alts.size(); i++) {
writeIndex<index_t>(f_out, alts[i], bigEndian);
}
return true;
}
bool read(ifstream& f_in, bool bigEndian) {
left = readIndex<index_t>(f_in, bigEndian);
right = readIndex<index_t>(f_in, bigEndian);
assert_leq(left, right);
index_t num_alts = readIndex<index_t>(f_in, bigEndian);
alts.resizeExact(num_alts); alts.clear();
for(index_t i = 0; i < num_alts; i++) {
alts.push_back(readIndex<index_t>(f_in, bigEndian));
}
return true;
}
};
template <typename index_t>
class ALTDB {
public:
ALTDB() :
_snp(false),
_ss(false),
_exon(false)
{}
virtual ~ALTDB() {}
bool hasSNPs() const { return _snp; }
bool hasSpliceSites() const { return _ss; }
bool hasExons() const { return _exon; }
void setSNPs(bool snp) { _snp = snp; }
void setSpliceSites(bool ss) { _ss = ss; }
void setExons(bool exon) { _exon = exon; }
EList<ALT<index_t> >& alts() { return _alts; }
EList<string>& altnames() { return _altnames; }
EList<Haplotype<index_t> >& haplotypes() { return _haplotypes; }
EList<index_t>& haplotype_maxrights() { return _haplotype_maxrights; }
const EList<ALT<index_t> >& alts() const { return _alts; }
const EList<string>& altnames() const { return _altnames; }
const EList<Haplotype<index_t> >& haplotypes() const { return _haplotypes; }
const EList<index_t>& haplotype_maxrights() const { return _haplotype_maxrights; }
private:
bool _snp;
bool _ss;
bool _exon;
EList<ALT<index_t> > _alts;
EList<string> _altnames;
EList<Haplotype<index_t> > _haplotypes;
EList<index_t> _haplotype_maxrights;
};
#endif /*ifndef ALT_H_*/