-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNA.cpp
294 lines (270 loc) · 6.25 KB
/
DNA.cpp
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
// DNA.cpp: implementation of the DNA class.
//
//////////////////////////////////////////////////////////////////////
#include "DNA.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DNA::DNA()
{
}
DNA::DNA(char *seq)
{
string seqstring = seq;
std::transform (seqstring.begin(), seqstring.end(), seqstring.begin(), ToLower());
Trim(seqstring);
sequence.resize(1);
sequence[0].append(seqstring);
}
DNA::~DNA()
{
}
// Read in only a specific sequence.
bool DNA::readFASTA(const char* filename, const string &header_name, bool uppercase_only, bool lowercase_only, bool KG_ref=false)
{
bool output = true;
char buffer[5120];
int bufsize = 5120;
string junk, junk2;
junk.reserve(bufsize);
junk2.reserve(bufsize);
igzstream in;
NSeqs = 0;
in.open(filename, ios::in);
if (in.good())
{
while(!in.eof())
{
in.getline(buffer, bufsize);
if (buffer[0] == '>')
{
NSeqs++;
string s = buffer;
s = s.substr(1);
if (KG_ref == true)
{
string::size_type pos=s.find(' ',0);
s = s.substr(0,pos);
}
cout << s << endl;
if (s == header_name)
break;
}
}
header.resize(1);
sequence.resize(1);
junk = buffer;
if (KG_ref == true)
{
string::size_type pos=junk.find(' ',0);
junk = junk.substr(0,pos);
}
header[0].append(junk.substr(1));
in.getline(buffer, bufsize);
junk = buffer;
remove_upper_or_lowercase(junk, uppercase_only, lowercase_only);
std::transform (junk.begin(), junk.end(), junk.begin(), ToLower());
Trim(junk);
while(!in.eof())
{
if (junk[0] == '>')
{
break;
}
else
{
junk2 = "";
while ((junk[0] != '>') && (!in.eof()))
{
junk2.append(junk);
in.getline(buffer, bufsize);
junk = buffer;
remove_upper_or_lowercase(junk, uppercase_only, lowercase_only);
std::transform (junk.begin(), junk.end(), junk.begin(), ToLower());
Trim(junk);
}
sequence[0].append(junk2);
}
}
in.close();
SeqLength.resize(1);
SeqLength[0] = (unsigned int)(sequence[0].length());
}
else
{
cout << "Unable to read file: " << filename << endl;
abort();
}
return output;
}
// Read all sequences
bool DNA::readFASTA(const char* filename, bool uppercase_only, bool lowercase_only, bool KG_ref=false)
{
bool output = true;
int count;
int i;
char buffer[5120];
int bufsize = 5120;
int nonstandard;
string junk, junk2;
junk.reserve(bufsize);
junk2.reserve(bufsize);
igzstream in, in2;
NSeqs = 0;
in2.open(filename, ios::in);
if (in2.good())
{
while(!in2.eof())
{
in2.getline(buffer, bufsize);
if (buffer[0] == '>')
{
string s = buffer;
s = s.substr(1);
if (KG_ref == true)
{
string::size_type pos=s.find(' ',0);
s = s.substr(0,pos);
}
cout << s << endl;
NSeqs++;
}
}
header.resize(NSeqs);
sequence.resize(NSeqs);
cout << "\t" << NSeqs << " sequences" << endl;
in2.close();
in.open(filename, ios::in);
in.getline(buffer, bufsize);
junk = buffer;
if (junk[0] != '>')
{
cout << "Error first line is not a headerline" << endl;
exit(0);
}
Trim(junk);
i=0;
while(!in.eof())
{
if (junk[0] == '>')
{
if (KG_ref == false)
{
int pos = junk.find(" ");
while (pos != -1)
{
junk.replace(pos,1,"_");
pos = junk.find(" ");
}
}
else
{
string::size_type pos=junk.find(' ',0);
junk = junk.substr(0,pos);
}
header[i].append(junk.substr(1));
i++;
count = 0;
in.getline(buffer, bufsize);
junk = buffer;
remove_upper_or_lowercase(junk, uppercase_only, lowercase_only);
std::transform (junk.begin(), junk.end(), junk.begin(), ToLower());
Trim(junk);
}
else
{
junk2 = "";
while ((junk[0] != '>') && (!in.eof()))
{
junk2.append(junk);
in.getline(buffer, bufsize);
junk = buffer;
Trim(junk);
if (junk[0] != '>')
{
remove_upper_or_lowercase(junk, uppercase_only, lowercase_only);
std::transform (junk.begin(), junk.end(), junk.begin(), ToLower());
nonstandard = 1;
while (nonstandard != -1)
{
nonstandard = (int)junk.find_first_not_of(" /t/nacgtn/0");
if (nonstandard != -1)
{
cout << "Non-standard character found on line " << i << endl;
junk[nonstandard] = 'n';
}
}
}
}
sequence[i-1].append(junk2);
}
}
in.close();
SeqLength.resize(NSeqs);
for (i=0; i < NSeqs; i++)
{
SeqLength[i] = (unsigned int)(sequence[i].length());
}
}
else
{
cout << "Unable to open file: " << filename << endl;
abort();
}
return output;
}
void DNA::Trim(string &str)
{
// trim leading whitespace
string::size_type notwhite = str.find_first_not_of(" \t\n\v\b\r\f\a\\\?\'\"\0");
str.erase(0,notwhite);
// trim trailing whitespace
notwhite = str.find_last_not_of(" \t\n\v\b\r\f\a\\\?\'\"\0");
str.erase(notwhite+1);
}
int DNA::CountMotifOccurances(motif &Motif)
{
int i;
int count = 0;
bool match;
for (i=NSeqs; i--;)
{
match = Motif.IsMotifInSequence(sequence[i].c_str(), Motif.sequence.c_str(), 0);
if (match)
{
// have found motif so we're done
count++;
}
else
{
// we didn't find the motif, so we need to test the complement
match = Motif.IsMotifInSequence(sequence[i].c_str(), Motif.complement.c_str() ,0 );
if (match)
{
count++;
}
}
}
return count;
}
void DNA::remove_upper_or_lowercase(string &string1, bool uppercase_only, bool lowercase_only)
{
if (uppercase_only == true)
{
size_t position = string1.find_first_of("acgt");
while ( position != string::npos )
{
string1.replace( position, 1, "N" );
position = string1.find_first_of("acgt", position + 1 );
}
}
else if (lowercase_only == true)
{
size_t position = string1.find_first_of("ACGT");
while ( position != string::npos )
{
string1.replace( position, 1, "N" );
position = string1.find_first_of("ACGT", position + 1 );
}
}
}