bpp-seq3  3.0.0
AlphabetTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_ALPHABET_ALPHABETTOOLS_H
6 #define BPP_SEQ_ALPHABET_ALPHABETTOOLS_H
7 
9 #include <typeinfo>
10 
11 #include "BinaryAlphabet.h"
12 #include "IntegerAlphabet.h"
13 #include "CodonAlphabet.h"
14 #include "DNA.h"
15 #include "DefaultAlphabet.h"
16 #include "ProteicAlphabet.h"
17 #include "RNA.h"
18 #include "RNY.h"
19 #include "AllelicAlphabet.h"
20 #include "WordAlphabet.h"
21 
22 // From the STL :
23 #include <string>
24 #include <vector>
25 
26 namespace bpp
27 {
32 {
33 public:
34  static std::shared_ptr<const DNA> DNA_ALPHABET;
35  static std::shared_ptr<const RNA> RNA_ALPHABET;
36  static std::shared_ptr<const CodonAlphabet> DNA_CODON_ALPHABET;
37  static std::shared_ptr<const CodonAlphabet> RNA_CODON_ALPHABET;
38  static std::shared_ptr<const ProteicAlphabet> PROTEIN_ALPHABET;
39  static std::shared_ptr<const DefaultAlphabet> DEFAULT_ALPHABET;
40 
41 public:
43  virtual ~AlphabetTools() {}
44 
45 public:
63  static int getType(char state);
64 
73  static bool checkAlphabetCodingSize(const Alphabet& alphabet);
74 
84  static bool checkAlphabetCodingSize(const Alphabet* alphabet);
85 
93  static unsigned int getAlphabetCodingSize(const Alphabet& alphabet);
94 
105  static unsigned int getAlphabetCodingSize(const Alphabet* alphabet);
106 
111  static bool isNucleicAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<NucleicAlphabet>(alphabet); }
112 
117  static bool isDNAAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<DNA>(alphabet); }
118 
123  static bool isRNAAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<RNA>(alphabet); }
124 
129  static bool isProteicAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<ProteicAlphabet>(alphabet); }
130 
135  static bool isCodonAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<CodonAlphabet>(alphabet); }
136 
141  static bool isWordAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<CoreWordAlphabet>(alphabet); }
142 
147  static bool isRNYAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<RNY>(alphabet); }
148 
153  static bool isBinaryAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<BinaryAlphabet>(alphabet); }
154 
159  static bool isIntegerAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<IntegerAlphabet>(alphabet); }
160 
165  static bool isDefaultAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<DefaultAlphabet>(alphabet); }
166 
171  static bool isAllelicAlphabet(const Alphabet& alphabet) { return alphabetInheritsFrom<AllelicAlphabet>(alphabet); }
172 
191  static bool match(std::shared_ptr<const Alphabet> alphabet, int i, int j)
192  {
193  std::vector<int> a = alphabet->getAlias(i);
194  std::vector<int> b = alphabet->getAlias(j);
195  std::vector<int> u = VectorTools::vectorIntersection(a, b);
196  return u.size() > 0;
197  }
198 
199 private:
200  template<class Y>
201  static bool alphabetInheritsFrom(const Alphabet& alphabet)
202  {
203  try
204  {
205  std::ignore = dynamic_cast<const Y&>(alphabet);
206  return true;
207  }
208  catch (std::exception& e)
209  {
210  return false;
211  }
212  }
213 };
214 } // end of namespace bpp.
215 #endif // BPP_SEQ_ALPHABET_ALPHABETTOOLS_H
Utilitary functions dealing with alphabets.
Definition: AlphabetTools.h:32
static bool match(std::shared_ptr< const Alphabet > alphabet, int i, int j)
Tell if two characters match according to a given alphabet.
virtual ~AlphabetTools()
Definition: AlphabetTools.h:43
static int getType(char state)
Character identification method for sequence's alphabet identification.
static bool isBinaryAlphabet(const Alphabet &alphabet)
static std::shared_ptr< const ProteicAlphabet > PROTEIN_ALPHABET
Definition: AlphabetTools.h:38
static bool isRNYAlphabet(const Alphabet &alphabet)
static bool isProteicAlphabet(const Alphabet &alphabet)
static bool isNucleicAlphabet(const Alphabet &alphabet)
static bool isDefaultAlphabet(const Alphabet &alphabet)
static bool isRNAAlphabet(const Alphabet &alphabet)
static bool isCodonAlphabet(const Alphabet &alphabet)
static std::shared_ptr< const DNA > DNA_ALPHABET
Definition: AlphabetTools.h:34
static bool isIntegerAlphabet(const Alphabet &alphabet)
static bool isWordAlphabet(const Alphabet &alphabet)
static std::shared_ptr< const CodonAlphabet > RNA_CODON_ALPHABET
Definition: AlphabetTools.h:37
static bool alphabetInheritsFrom(const Alphabet &alphabet)
static std::shared_ptr< const DefaultAlphabet > DEFAULT_ALPHABET
Definition: AlphabetTools.h:39
static bool isDNAAlphabet(const Alphabet &alphabet)
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
static bool isAllelicAlphabet(const Alphabet &alphabet)
The Alphabet interface.
Definition: Alphabet.h:99
static std::vector< T > vectorIntersection(const std::vector< T > &vec1, const std::vector< T > &vec2)
This alphabet is used to deal NumericAlphabet.