bpp-seq  2.1.0
 All Classes Namespaces Files Functions Variables Friends Pages
AbstractAlphabet.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractAlphabet.cpp
3 // Authors: Guillaume Deuchst
4 // Julien Dutheil
5 // Sylvain Gaillard
6 // Created on: Tue Jul 22 2003
7 //
8 
9 /*
10 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12 This software is a computer program whose purpose is to provide classes
13 for sequences analysis.
14 
15 This software is governed by the CeCILL license under French law and
16 abiding by the rules of distribution of free software. You can use,
17 modify and/ or redistribute the software under the terms of the CeCILL
18 license as circulated by CEA, CNRS and INRIA at the following URL
19 "http://www.cecill.info".
20 
21 As a counterpart to the access to the source code and rights to copy,
22 modify and redistribute granted by the license, users are provided only
23 with a limited warranty and the software's author, the holder of the
24 economic rights, and the successive licensors have only limited
25 liability.
26 
27 In this respect, the user's attention is drawn to the risks associated
28 with loading, using, modifying and/or developing or reproducing the
29 software by the user in light of its specific status of free software,
30 that may mean that it is complicated to manipulate, and that also
31 therefore means that it is reserved for developers and experienced
32 professionals having in-depth computer knowledge. Users are therefore
33 encouraged to load and test the software's suitability as regards their
34 requirements in conditions enabling the security of their systems and/or
35 data to be ensured and, more generally, to use and operate it in the
36 same conditions as regards security.
37 
38 The fact that you are presently reading this means that you have had
39 knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 #include "AbstractAlphabet.h"
43 #include <Bpp/Text/TextTools.h>
44 #include <Bpp/Utils/MapTools.h>
45 
46 using namespace bpp;
47 
48 // From the STL:
49 #include <ctype.h>
50 #include <map>
51 
52 using namespace std;
53 
54 /******************************************************************************/
55 
56 void AbstractAlphabet::updateMaps_(size_t pos, const AlphabetState& st) {
57  if (letters_.find(st.getLetter()) == letters_.end())
58  letters_[st.getLetter()] = pos;
59  if (nums_.find(st.getNum()) == nums_.end())
60  nums_[st.getNum()] = pos;
61 }
62 
63 /******************************************************************************/
64 
66  // Add the state to the vector
67  alphabet_.push_back(st.clone());
68  // Update the maps
69  updateMaps_(alphabet_.size(), st);
70 }
71 
72 /******************************************************************************/
73 
74 void AbstractAlphabet::setState(size_t pos, const AlphabetState& st)
76  if (pos > alphabet_.size())
77  throw IndexOutOfBoundsException("AbstractAlphabet::setState: incorect position", pos, 0, alphabet_.size());
78  // Delete the state if not empty
79  if (alphabet_[pos] != 0)
80  delete alphabet_[pos];
81  // Put the state in the vector
82  alphabet_[pos] = st.clone();
83  // Update the maps
84  updateMaps_(pos, st);
85  }
86 
87 /******************************************************************************/
88 
89 const AlphabetState& AbstractAlphabet::getState(const std::string& letter) const throw (BadCharException) {
90  map<string, size_t>::const_iterator it = letters_.find(letter);
91  if (it == letters_.end())
92  throw BadCharException(letter, "AbstractAlphabet::getState(string): Specified base unknown", this);
93  return * (alphabet_[it->second]);
94 }
95 
96 /******************************************************************************/
97 
99  map<int, size_t>::const_iterator it = nums_.find(num);
100  if (it == nums_.end())
101  throw BadIntException(num, "AbstractAlphabet::getState(int): Specified base unknown", this);
102  return * (alphabet_[it->second]);
103 }
104 
105 /******************************************************************************/
106 
108  if (pos > alphabet_.size())
109  throw IndexOutOfBoundsException("AbstractAlphabet::getStateAt: incorect position", pos, 0, alphabet_.size());
110  return * (alphabet_[pos]);
111 }
112 
113 /******************************************************************************/
114 
116  if (pos > alphabet_.size())
117  throw IndexOutOfBoundsException("AbstractAlphabet::getStateAt: incorect position", pos, 0, alphabet_.size());
118  return * (alphabet_[pos]);
119 }
120 
121 /******************************************************************************/
122 
123 std::string AbstractAlphabet::getName(const std::string& state) const throw (BadCharException)
124 {
125  return (getState(state)).getName();
126 }
127 
128 /******************************************************************************/
129 
130 std::string AbstractAlphabet::getName(int state) const throw (BadIntException)
131 {
132  return (getState(state)).getName();
133 }
134 
135 /******************************************************************************/
136 
137 int AbstractAlphabet::charToInt(const std::string& state) const throw (BadCharException)
138 {
139  return getState(state).getNum();
140 }
141 
142 /******************************************************************************/
143 
144 std::string AbstractAlphabet::intToChar(int state) const throw (BadIntException)
145 {
146  return (getState(state)).getLetter();
147 }
148 
149 /******************************************************************************/
150 
152 {
153  map<int, size_t>::const_iterator it = nums_.find(state);
154  if (it != nums_.end())
155  return true;
156  return false;
157 }
158 
159 /******************************************************************************/
160 
161 bool AbstractAlphabet::isCharInAlphabet(const std::string& state) const
162 {
163  map<string, size_t>::const_iterator it = letters_.find(state);
164  if (it != letters_.end())
165  return true;
166  return false;
167 }
168 
169 /******************************************************************************/
170 
171 std::vector<int> AbstractAlphabet::getAlias(int state) const throw (BadIntException)
172 {
173  if (!isIntInAlphabet(state)) throw BadIntException(state, "AbstractAlphabet::getAlias(int): Specified base unknown.");
174  vector<int> v(1);
175  v[0] = state;
176  return v;
177 }
178 
179 /******************************************************************************/
180 
181 std::vector<std::string> AbstractAlphabet::getAlias(const std::string& state) const throw (BadCharException)
182 {
183  if (!isCharInAlphabet(state)) throw BadCharException(state, "AbstractAlphabet::getAlias(char): Specified base unknown.");
184  vector<string> v(1);
185  v[0] = state;
186  return v;
187 }
188 
189 /******************************************************************************/
190 
191 int AbstractAlphabet::getGeneric(const std::vector<int>& states) const throw (BadIntException) {
192  map<int, int> m;
193  for (unsigned int i = 0 ; i < states.size() ; ++i) {
194  vector<int> tmp_s = this->getAlias(states[i]); // get the states for generic characters
195  for (unsigned int j = 0 ; j < tmp_s.size() ; ++j) {
196  m[tmp_s[j]] ++; // add each state to the list
197  }
198  }
199  vector<int> ve = MapTools::getKeys(m);
200 
201  string key;
202  for (unsigned int i = 0 ; i < ve.size() ; ++i) {
203  if (!isIntInAlphabet(ve[i])) throw BadIntException(ve[i], "AbstractAlphabet::getGeneric(const vector<int>): Specified base unknown.");
204  key += "_" + TextTools::toString(ve[i]);
205  }
206  int v;
207  if (ve.size() == 1) {
208  v = ve[0];
209  } else {
210  v = this->getUnknownCharacterCode();
211  }
212  return v;
213 }
214 
215 /******************************************************************************/
216 
217 std::string AbstractAlphabet::getGeneric(const std::vector<std::string>& states) const throw (AlphabetException) {
218  map <string, int> m;
219  for (unsigned int i = 0 ; i < states.size() ; ++i) {
220  vector<string> tmp_s = this->getAlias(states[i]); // get the states for generic characters
221  for (unsigned int j = 0 ; j < tmp_s.size() ; ++j) {
222  m[tmp_s[j]] ++; // add each state to the list
223  }
224  }
225  vector<string> ve = MapTools::getKeys(m);
226 
227  string key;
228  for (unsigned int i = 0 ; i < ve.size() ; ++i) {
229  if (!isCharInAlphabet(ve[i])) throw BadCharException(ve[i], "AbstractAlphabet::getAlias(const vector<string>): Specified base unknown.");
230  key += TextTools::toString(ve[i]);
231  }
232  string v;
233  if (ve.size() == 1) {
234  v = ve[0];
235  } else {
236  throw CharStateNotSupportedException("AbstractAlphabet::getAlias(const vector<string>): No generic char state.");
237  }
238  return v;
239 }
240 
241 /******************************************************************************/
242 
243 const std::vector<int>& AbstractAlphabet::getSupportedInts() const
244 {
245  if(intList_.size() == 0)
246  {
247  intList_.resize(alphabet_.size());
248  charList_.resize(alphabet_.size());
249  for(unsigned int i = 0; i < alphabet_.size(); i++)
250  {
251  intList_[i] = alphabet_[i]->getNum();
252  charList_[i] = alphabet_[i]->getLetter();
253  }
254  }
255  return intList_;
256 }
257 
258 /******************************************************************************/
259 
260 const std::vector<std::string>& AbstractAlphabet::getSupportedChars() const
261 {
262  if(charList_.size() == 0)
263  {
264  intList_.resize(alphabet_.size());
265  charList_.resize(alphabet_.size());
266  for(unsigned int i = 0; i < alphabet_.size(); i++)
267  {
268  intList_[i] = alphabet_[i]->getNum();
269  charList_[i] = alphabet_[i]->getLetter();
270  }
271  }
272  return charList_;
273 }
274 
275 /******************************************************************************/
276