-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sequence.cc
129 lines (112 loc) · 2.72 KB
/
Sequence.cc
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
#include "Sequence.h"
#include <algorithm>
char ToUpperCase(char c) {
switch (c) {
case 'a': case 'A': return 'A';
case 'c': case 'C': return 'C';
case 't': case 'T': return 'T';
case 'g': case 'G': return 'G';
default: return 0;
}
}
char Remap(char c) {
switch (c) {
case 'a': case 'A': return 'T';
case 'c': case 'C': return 'G';
case 't': case 'T': return 'A';
case 'g': case 'G': return 'C';
default: return 0;
}
}
Sequence::Sequence()
: dalignFromat(NULL) {
}
Sequence::Sequence(const Sequence& orig, bool remapReverse)
: id(orig.id), dalignFromat(NULL) {
if (remapReverse) {
for (char c : orig.data) {
data.push_back(Remap(c));
}
reverse(data.begin(), data.end());
} else {
data = orig.data;
}
}
Sequence::~Sequence() {
if (dalignFromat != NULL) {
delete dalignFromat;
}
}
Sequence::Sequence(const string& _data, string _id)
: data(_data), id(_id), dalignFromat(NULL) {
}
char* Sequence::ToDalignFromat() {
if (dalignFromat != NULL) {
return dalignFromat + 1;
}
int l = data.length();
dalignFromat = new char[l + 2];
dalignFromat[0] = 4;
for (int i = 0; i < l; i++) {
if (data[i] == 'A') dalignFromat[i + 1] = 0;
if (data[i] == 'C') dalignFromat[i + 1] = 1;
if (data[i] == 'G') dalignFromat[i + 1] = 2;
if (data[i] == 'T') dalignFromat[i + 1] = 3;
}
dalignFromat[l + 1] = 4;
return dalignFromat + 1;
}
FASTA::FASTA(const string& _filename)
: filename(_filename) {
}
FASTA& FASTA::operator>>(Sequence& seq) {
ifstream is(filename);
string data;
string id;
string line;
if (getline(is, line)) {
if (line.length()) {
id = line.substr(1, line.length() - 1);
}
while (getline(is, line)) {
for (char c : line) {
data.push_back(ToUpperCase(c));
}
}
}
seq = Sequence(data, id);
return *this;
}
FASTQ::FASTQ(const string& filename)
: is(filename.c_str()), isOk(true) {
}
FASTQ& FASTQ::operator>>(Sequence& seq) {
string data;
string id;
string line;
if (isOk) {
isOk &= (bool)getline(is, line);
if (line.length()) {
id = line.substr(1, line.length() - 1);
}
}
if (isOk) {
isOk &= (bool)getline(is, line);
for (char c : line) {
data.push_back(ToUpperCase(c));
}
}
if (isOk) {
isOk &= (bool)getline(is, line);
}
if (isOk) {
isOk &= (bool)getline(is, line);
}
if (isOk) {
seq = Sequence(data, id);
}
else {
seq = Sequence();
}
return *this;
}