bpp-seq3  3.0.0
AlphabetTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Text/TextTools.h>
6 
7 #include "AlphabetTools.h"
8 
9 using namespace bpp;
10 
11 // From the STL:
12 #include <ctype.h>
13 #include <iostream>
14 #include <memory>
15 
16 using namespace std;
17 
18 /**********************************************************************************************/
19 shared_ptr<const DNA> AlphabetTools::DNA_ALPHABET = make_shared<const DNA>();
20 shared_ptr<const RNA> AlphabetTools::RNA_ALPHABET = make_shared<const RNA>();
21 shared_ptr<const CodonAlphabet> AlphabetTools::DNA_CODON_ALPHABET = make_shared<const CodonAlphabet>(dynamic_pointer_cast<const NucleicAlphabet>(DNA_ALPHABET));
22 shared_ptr<const CodonAlphabet> AlphabetTools::RNA_CODON_ALPHABET = make_shared<const CodonAlphabet>(dynamic_pointer_cast<const NucleicAlphabet>(RNA_ALPHABET));
23 shared_ptr<const ProteicAlphabet> AlphabetTools::PROTEIN_ALPHABET = make_shared<const ProteicAlphabet>();
24 shared_ptr<const DefaultAlphabet> AlphabetTools::DEFAULT_ALPHABET = make_shared<const DefaultAlphabet>();
25 
26 /**********************************************************************************************/
27 
28 int AlphabetTools::getType(char state)
29 {
30  if (state == '-')
31  return -1;
32  state = static_cast<char>(toupper(static_cast<int>(state))); // toupper works on int
33  bool d = DNA_ALPHABET->isCharInAlphabet(TextTools::toString(state));
34  bool r = RNA_ALPHABET->isCharInAlphabet(TextTools::toString(state));
35  bool p = PROTEIN_ALPHABET->isCharInAlphabet(TextTools::toString(state));
36 
37  if (!d && !r && !p)
38  return 0; // Unknown character
39  else if (d && !r && !p)
40  return 1; // DNA specific
41  else if (!d && r && !p)
42  return 2; // RNA specific
43  else if (!d && !r && p)
44  return 3; // Protein specific
45  else if (d && r && !p)
46  return 4; // Nucleotide specific
47  else if (d && !r && p)
48  return 5; // DNA or Protein specific
49  else if (!d && r && p)
50  return 6; // RNA or Protein specific
51  else
52  return 7; // Non-specific character
53 }
54 
55 /**********************************************************************************************/
56 
58 {
59  if (alphabet.getNumberOfChars() == 0)
60  return true; // Will this really happen?
61  size_t size = alphabet.intToChar(0).size();
62 
63  for (size_t i = 1; i < alphabet.getNumberOfTypes(); ++i)
64  {
65  if (alphabet.intToChar(alphabet.getStateAt(i).getNum()).size() != size)
66  return false;
67  }
68  return true;
69 }
70 
71 /**********************************************************************************************/
72 
74 {
75  return checkAlphabetCodingSize(*alphabet);
76 }
77 
78 /**********************************************************************************************/
79 
80 unsigned int AlphabetTools::getAlphabetCodingSize(const Alphabet& alphabet)
81 {
82  if (!checkAlphabetCodingSize(alphabet))
83  throw AlphabetException("Bad alphabet in function Alphabet::getAlphabetCodingSize().", &alphabet);
84  return static_cast<unsigned int>(alphabet.intToChar(0).size());
85 }
86 
87 /**********************************************************************************************/
88 
89 unsigned int AlphabetTools::getAlphabetCodingSize(const Alphabet* alphabet)
90 {
91  return getAlphabetCodingSize(*alphabet);
92 }
The alphabet exception base class.
int getNum() const
Get the state's number.
Definition: AlphabetState.h:47
static int getType(char state)
Character identification method for sequence's alphabet identification.
static std::shared_ptr< const ProteicAlphabet > PROTEIN_ALPHABET
Definition: AlphabetTools.h:38
static std::shared_ptr< const DNA > DNA_ALPHABET
Definition: AlphabetTools.h:34
static std::shared_ptr< const CodonAlphabet > RNA_CODON_ALPHABET
Definition: AlphabetTools.h:37
static std::shared_ptr< const DefaultAlphabet > DEFAULT_ALPHABET
Definition: AlphabetTools.h:39
static std::shared_ptr< const CodonAlphabet > DNA_CODON_ALPHABET
Definition: AlphabetTools.h:36
static unsigned int getAlphabetCodingSize(const Alphabet &alphabet)
In case that all states in the given alphabet have a string description of same length,...
static bool checkAlphabetCodingSize(const Alphabet &alphabet)
This checks that all characters in the alphabet are coded by a string of same length.
static std::shared_ptr< const RNA > RNA_ALPHABET
Definition: AlphabetTools.h:35
The Alphabet interface.
Definition: Alphabet.h:99
virtual unsigned int getNumberOfChars() const =0
Get the number of supported characters in this alphabet, including generic characters (e....
virtual std::string intToChar(int state) const =0
Give the string description of a state given its int description.
virtual unsigned int getNumberOfTypes() const =0
Get the number of distinct states in alphabet (e.g. return 15 for DNA alphabet). This is the number o...
virtual const AlphabetState & getStateAt(size_t stateIndex) const =0
Get a state given its index.
std::string toString(T t)
This alphabet is used to deal NumericAlphabet.