-
Notifications
You must be signed in to change notification settings - Fork 0
/
dna_sequence.cc
115 lines (94 loc) · 2.64 KB
/
dna_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
/*
*
*/
#include <iostream>
#include "dna_sequence.h"
using namespace std;
//-----------------------------------------------------------------------------
Nucleotide CharToNucleotide(const char c) {
const char upperc = toupper(c);
switch (upperc) {
case 'A':
return Nucleotide::kA;
case 'C':
return Nucleotide::kC;
case 'G':
return Nucleotide::kG;
case 'T':
return Nucleotide::kT;
default:
cerr << "Invalid nucleotide char " << c << endl;
assert(false);
break;
}
__builtin_unreachable();
}
//-----------------------------------------------------------------------------
char NucleotideToChar(Nucleotide n) {
switch (n) {
case Nucleotide::kA:
return 'A';
case Nucleotide::kC:
return 'C';
case Nucleotide::kG:
return 'G';
case Nucleotide::kT:
return 'T';
default:
cerr << "Invalid nucleotide " << static_cast<int>(n) << endl;
assert(false);
break;
}
__builtin_unreachable();
}
//-----------------------------------------------------------------------------
DNASequence::DNASequence() {
}
//-----------------------------------------------------------------------------
DNASequence::DNASequence(const string& sequence, const string& identifier) :
sequence_(sequence),
identifier_(identifier) {
}
//-----------------------------------------------------------------------------
string DNASequence::MakeValidDNASequence(const string& line) {
string sequence = line;
for (int ii = 0; ii < sequence.size(); ++ii) {
if (!IsValidNucleotide(sequence[ii])) {
// Hack just make all invalid chars a T.
sequence[ii] = 'T';
}
}
return sequence;
}
//-----------------------------------------------------------------------------
bool DNASequence::IsValidDNASequence(const string& sequence) {
for (int ii = 0; ii < sequence.size(); ++ii) {
if (!IsValidNucleotide(sequence[ii])) {
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
bool DNASequence::IsValidNucleotide(const char c) {
const char upper_c = toupper(c);
if (upper_c != 'A' &&
upper_c != 'C' &&
upper_c != 'G' &&
upper_c != 'T') {
return false;
}
return true;
}
//-----------------------------------------------------------------------------
void DNASequence::PrintWithColumns() const {
cout << identifier_ << endl;
for (int ii = 0; ii < sequence_.size(); ++ii) {
if (ii % 60 == 0 && ii != 0) {
cout << endl;
}
cout << sequence_[ii];
}
cout << endl;
}
//-----------------------------------------------------------------------------