bpp-seq  2.2.0
AlphabetTools.cpp
Go to the documentation of this file.
1 //
2 // File: AlphabetTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Fri Oct 10 17:27:39 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for sequences analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "AlphabetTools.h"
41 #include <Bpp/Text/TextTools.h>
42 
43 using namespace bpp;
44 
45 // From the STL:
46 #include <ctype.h>
47 #include <iostream>
48 
49 using namespace std;
50 
51 /**********************************************************************************************/
52 
57 
58 /**********************************************************************************************/
59 
60 int AlphabetTools::getType(char state)
61 {
62  if (state == '-')
63  return -1;
64  state = static_cast<char>(toupper(static_cast<int>(state))); // toupper works on int
65  bool d = DNA_ALPHABET.isCharInAlphabet(TextTools::toString(state));
66  bool r = RNA_ALPHABET.isCharInAlphabet(TextTools::toString(state));
67  bool p = PROTEIN_ALPHABET.isCharInAlphabet(TextTools::toString(state));
68 
69  if (!d && !r && !p)
70  return 0; // Unknown character
71  else if (d && !r && !p)
72  return 1; // DNA specific
73  else if (!d && r && !p)
74  return 2; // RNA specific
75  else if (!d && !r && p)
76  return 3; // Protein specific
77  else if (d && r && !p)
78  return 4; // Nucleotide specific
79  else if (d && !r && p)
80  return 5; // DNA or Protein specific
81  else if (!d && r && p)
82  return 6; // RNA or Protein specific
83  else
84  return 7; // Non-specific character
85 }
86 
87 /**********************************************************************************************/
88 
90 {
91  if (alphabet.getNumberOfChars() == 0)
92  return true; // Will this really happen?
93  size_t size = alphabet.intToChar(0).size();
94  for (int i = 1; i < static_cast<int>(alphabet.getNumberOfTypes()); ++i)
95  {
96  if (alphabet.intToChar(i).size() != size)
97  return false;
98  }
99  return true;
100 }
101 
102 /**********************************************************************************************/
103 
105 {
106  return checkAlphabetCodingSize(*alphabet);
107 }
108 
109 /**********************************************************************************************/
110 
112 {
113  if (!checkAlphabetCodingSize(alphabet))
114  throw AlphabetException("Bad alphabet in function Alphabet::getAlphabetCodingSize().");
115  return static_cast<unsigned int>(alphabet.intToChar(0).size());
116 }
117 
118 /**********************************************************************************************/
119 
121 {
122  return getAlphabetCodingSize(*alphabet);
123 }
124 
125 /**********************************************************************************************/
126 
static unsigned int getAlphabetCodingSize(const Alphabet &alphabet)
In case that all states in the given alphabet have a string description of same length, send this length; e.g. 3 for codon alphabets.
static const RNA RNA_ALPHABET
Definition: AlphabetTools.h:63
static const DNA DNA_ALPHABET
Definition: AlphabetTools.h:62
This alphabet is used to deal NumericAlphabet.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
This alphabet is used to deal with proteins.
The DefaultAlphabet class.
The alphabet exception base class.
static int getType(char state)
Character identification method for sequence&#39;s alphabet identification.
static bool checkAlphabetCodingSize(const Alphabet &alphabet)
This checks that all characters in the alphabet are coded by a string of same length.
This alphabet is used to deal with DNA sequences.
Definition: DNA.h:60
This alphabet is used to deal with RNA sequences.
Definition: RNA.h:58
static const DefaultAlphabet DEFAULT_ALPHABET
Definition: AlphabetTools.h:65
static const ProteicAlphabet PROTEIN_ALPHABET
Definition: AlphabetTools.h:64