-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphabet.C
111 lines (81 loc) · 2.03 KB
/
Alphabet.C
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
/***********************************************************************
Alphabet.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
using namespace std;
#include "Alphabet.H"
#include <iostream>
#include "String.H"
#include <string.h>
BOOM::Alphabet::Alphabet(const char *initializer) : numElements(0)
{
for(int i=0 ; i<256 ; ++i)
charToInt[i]=intToChar[i]=INVALID_SYMBOL;
if(initializer)
{
int n=strlen(initializer);
for(int i=0 ; i<n ; ++i) add(initializer[i]);
}
}
int BOOM::Alphabet::getNumElements() const
{
return numElements;
}
int BOOM::Alphabet::add(char c)
{
charToInt[static_cast<int>(c)]=static_cast<char>(numElements);
intToChar[numElements]=c;
return numElements++;
}
ostream &operator<<(ostream &os,BOOM::Alphabet &alphabet)
{
alphabet.printOn(os);
return os;
}
bool BOOM::Alphabet::load(istream &is)
{
char buffer[260];
is.getline(buffer,260);
if(is.good())
{
char *p=static_cast<char*>(buffer);
while(*p) add(*p++);
return true;
}
return false;
}
bool BOOM::Alphabet::save(ostream &os)
{
for(int i=0 ; i<numElements ; ++i) os << intToChar[i];
os << endl;
return os.good() ? true : false;
}
Symbol BOOM::Alphabet::complement(Symbol s) const
{
return lookup(complement(lookup(s)));
}
char BOOM::Alphabet::complement(char c) const
{
switch(c)
{
case 'A': return 'T';
case 'G': return 'C';
case 'C': return 'G';
case 'T': return 'A';
case 'N': return 'N';
}
throw
BOOM::String("BOOM::Alphabet::complement: can't complement base '")+
c+"', ASCII "+int(c);
}
void BOOM::Alphabet::printOn(ostream &os)
{
for(int i=0 ; i<numElements ; ++i) os << intToChar[i] << ',';
}
bool BOOM::Alphabet::isDefined(char c) const
{
return lookup(c)!=INVALID_SYMBOL;
}