-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sequence.h
355 lines (309 loc) · 9.2 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/****************************************************************************
*
* LongAGE -- Ultra-long Alignment with Gap Excision
* Copyright (C) Alexej Abyzov
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Creative Commons license
* (Attribution-NonCommerical).
* See terms at http://creativecommons.org/licenses/by-nc/2.5/legalcode
*
* Author: Alexej Abyzov
*/
#ifndef __SEQUENCE_H__
#define __SEQUENCE_H__
// Nucleotide sequence
// Allowed nucleotides are A,C,T,G,U,X,N in both lower and upper cases
// Other nucleotides (R,Y,M,K,S,W,B,D,H,V) are also alowed by not scored
class Sequence
{
private:
string _seq,_name;
int _start; // 1 based coordinate
bool _reverse;
public:
inline string &name() { return _name; }
inline string &sequence() { return _seq; }
inline int start() { return _start; } // 1 based coordinate
inline int reverse() { return _reverse; }
public:
Sequence(const char *seq) : _seq(seq), _name(""),_start(1),_reverse(false),
_next(NULL),_prev(NULL)
{
string ret = "";
for (int i = 0;i < _seq.length();i++) {
char c = _seq[i];
if (c == ' ') continue; // Space
else if (isNuc(c)) ret += c; // Nucleotide
else if (!isGap(c)) { // Gap
cerr<<"Unrecognized nucleotide '"<<c<<"' in sequence."<<endl;
_seq = "";
return;
}
}
_seq = ret;
}
Sequence(string file,int start = 1,int end = -1) : _seq(""), _name(""),
_start(1),_reverse(false),
_next(NULL),_prev(NULL)
{
if (start > 0 && end > 0 && end < start) {
cerr<<"Invalid coordinates ("<<start<<", "<<end<<"). "
<<"Start is larger than end."<<endl;
cerr<<"No sequence read!"<<endl;
return;
}
if (start >= 0) _start = start;
ifstream in(file.c_str());
if (!in) {
cerr<<"Can't open input file '"<<file<<"'."<<endl;
return;
}
char l[1024];
while (!in.eof() && in.getline(l,1024))
if (in.gcount() != 0) break;
if (l[0] != '>') {
cerr<<"Can't find fasta header in the file '"<<file<<"'."<<endl;
return;
}
_name = &l[1];
string line = "";
int n_passed = 0;
while (!in.eof()) {
in>>line;
string checked = "";
for (int i = 0;i < line.length();i++) {
char c = line[i];
if (c == ' ') continue; // Space
else if (isNuc(c)) checked += c; // Nucleotide
else if (!isGap(c)) { // Gap
cerr<<"Unrecognized nucleotide '"<<c
<<"' in file '"<<file<<"'."<<endl;
_seq = "";
return;
}
}
int n_checked = checked.length();
if (n_checked == 0) break;
int new_passed = n_passed + n_checked;
if (new_passed < start) { ; // Not reached start yet
} else if (n_passed < start && new_passed >= end) { // Spans whole region
_seq += checked.substr(start - n_passed - 1,end - start + 1);
} else if (n_passed < start && new_passed >= start) { // Spans start only
_seq += checked.substr(start - n_passed - 1);
} else if (end < start) { // No end
_seq += checked;
} else if (new_passed < end) { // Not reached end yet
_seq += checked;
} else if (n_passed < end && new_passed >= end) { // Spans end only
_seq += checked.substr(0,end - n_passed);
}
n_passed = new_passed;
line = "";
}
in.close();
}
private:
Sequence(string &seq,string &name,int start,bool rev) : _seq(seq),
_name(name),
_start(start),
_reverse(rev),
_next(NULL),
_prev(NULL)
{}
public:
Sequence *clone()
{
return new Sequence(_seq,_name,_start,_reverse);
}
static char complement(char c)
{
if (c == 'a') return 't';
else if (c == 'c') return 'g';
else if (c == 't') return 'a';
else if (c == 'g') return 'c';
else if (c == 'A') return 'T';
else if (c == 'C') return 'G';
else if (c == 'T') return 'A';
else if (c == 'G') return 'C';
else if (c == 'u') return 'a';
else if (c == 'U') return 'A';
return c;
}
static bool isNuc(char c)
{
if (c == 'A' || c == 'a' ||
c == 'T' || c == 't' ||
c == 'C' || c == 'c' ||
c == 'G' || c == 'g' ||
c == 'U' || c == 'u' ||
c == 'X' || c == 'x' ||
c == 'N' || c == 'n' ||
c == 'R' || c == 'r' ||
c == 'Y' || c == 'y' ||
c == 'M' || c == 'm' ||
c == 'K' || c == 'k' ||
c == 'S' || c == 's' ||
c == 'W' || c == 'w' ||
c == 'B' || c == 'b' ||
c == 'D' || c == 'd' ||
c == 'H' || c == 'h' ||
c == 'V' || c == 'v') return true;
return false;
}
static char gap() { return '-'; }
static bool isGap(char c)
{
if (c == '-') return true;
if (c == ' ') return true;
return false;
}
static bool sameNuc(char a,char b)
{
char aa = toupper(a), bb = toupper(b);
if ((aa == 'A' || aa == 'C' || aa == 'T' || aa == 'G' || aa == 'U') &&
(bb == 'A' || bb == 'C' || bb == 'T' || bb == 'G' || bb == 'U') &&
aa == bb) return true;
return false;
}
void revcom()
{
if (_reverse) _start -= _seq.length() - 1;
else _start += _seq.length() - 1;
_reverse = !_reverse;
string ret = "";
for (int i = _seq.length() - 1;i >= 0;i--) ret += complement(_seq.at(i));
_seq = ret;
}
Sequence *substr(int start,int end) // 1 based coordinates
{
if (start > 0 && end > 0 && start > end) return NULL;
if (start <= 0) start = 1;
if (end <= 0 || end > _seq.length()) end = _seq.length();
int s = start, e = end;
if (_reverse) {
s = _seq.length() - e + 1;
e = _seq.length() - s + 1;
}
string tmp = "";
if (_seq.length() > 0) tmp = _seq.substr(s - 1,e - s + 1);
return new Sequence(tmp,_name,_start + start - 1,_reverse);
}
// Next and previous sequences
private:
Sequence *_next,*_prev;
public:
Sequence *next() { return _next; }
Sequence *prev() { return _prev; }
// Adding sequence before
bool addBefore(Sequence *newSequence)
{
// Check if input is good
if (!newSequence) return false;
// Check if same object
if (newSequence == this) return false;
// Check if the call comes from addAfter
if (newSequence->next() == this && !_prev) {
_prev = newSequence;
return true;
}
// Chack if they are already paired
if (newSequence->prev() == this && _next == newSequence) return true;
// Check if it can be added
if (_prev) return false;
if (newSequence->next()) return false;
if (newSequence->prev()) return false;
_prev = newSequence; // Set previous
if (!newSequence->addAfter(this)) { // Update next for newSequence
_prev = NULL;
return false;
}
return true;
}
// Adding atom after
bool addAfter(Sequence *newSequence)
{
// Check if not null
if (!newSequence) return false;
// Check if same object
if (newSequence == this) return true;
// Check if the call comes from addAfter
if (newSequence->prev() == this && !_next) {
_next = newSequence;
return true;
}
// Chack if they are already paired
if (newSequence->prev() == this && _next == newSequence) return true;
// Check if it can be added
if (_next) return false;
if (newSequence->next()) return false;
if (newSequence->prev()) return false;
_next = newSequence; // Set next
if (!newSequence->addBefore(this)) { // Update previous for newSequence
_next = NULL;
return false;
}
return true;
}
static Sequence* parseSequences(string file)
{
Sequence *first = NULL,*last = NULL;
ifstream in(file.c_str());
if (!in) {
cerr<<"Can't open input file '"<<file<<"'."<<endl;
return first;
}
string name = "",seq = "";
char *line = new char[65536];
while (!in.eof()) {
in.getline(line,65536);
int n = in.gcount();
if (n == 0) continue;
if (line[0] == '>') {
if (name.length() > 0) {
Sequence *s = new Sequence(seq,name,1,false);
if (first) {
last->addAfter(s);
last = s;
} else first = last = s;
}
name = &line[1];
seq = "";
continue;
}
string checked = "";
for (int i = 0;i < n - 1;i++) {
char c = line[i];
if (isNuc(c)) checked += c;
else if (isspace(c)) ;
else if (!isGap(c)) {
cerr<<"Unrecognized nucleotide '"<<c
<<"' in file '"<<file<<"'."<<endl;
seq = "";
name = "";
}
}
seq += checked;
}
in.close();
if (name.length() > 0) {
Sequence *s = new Sequence(seq,name,1,false);
if (first) {
last->addAfter(s);
last = s;
} else first = last = s;
}
delete[] line;
return first;
}
static void deleteSequences(Sequence *seqs)
{
Sequence *s = seqs;
while (s) {
Sequence *next = s->next();
delete s;
s = next;
}
}
};
#endif