bpp-seq3  3.0.0
Transliterator.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 <memory>
6 
7 using namespace std;
8 
9 #include "Transliterator.h"
10 
11 using namespace bpp;
12 
13 unique_ptr<Sequence> AbstractTransliterator::translate(const SequenceInterface& sequence) const
14 {
15  if (sequence.alphabet().getAlphabetType() != sourceAlphabet().getAlphabetType())
16  throw AlphabetMismatchException("AbstractTransliterator::translate", getSourceAlphabet(), getTargetAlphabet());
17  auto alphaPtr = getTargetAlphabet();
18  auto tSeq = make_unique<Sequence>(sequence.getName(), "", sequence.getComments(), alphaPtr);
19  int gap = sequence.getAlphabet()->getGapCharacterCode();
20  for (size_t i = 0; i < sequence.size(); ++i)
21  {
22  int state = sequence.getValue(i);
23  if (state == gap)
24  tSeq->addElement(gap);
25  else
26  tSeq->addElement(translate(state));
27  }
28  return tSeq;
29 }
30 
31 unique_ptr<Sequence> AbstractReverseTransliterator::reverse(const SequenceInterface& sequence) const
32 {
33  if (sequence.alphabet().getAlphabetType() != targetAlphabet().getAlphabetType())
34  throw AlphabetMismatchException("AbstractReverseTransliterator::reverse", getSourceAlphabet(), getTargetAlphabet());
35  auto alphaPtr = getSourceAlphabet();
36  auto rSeq = make_unique<Sequence>(sequence.getName(), "", sequence.getComments(), alphaPtr);
37  for (size_t i = 0; i < sequence.size(); ++i)
38  {
39  rSeq->addElement(reverse(sequence.getValue(i)));
40  }
41  return rSeq;
42 }
Exception thrown when two alphabets do not match.
virtual std::string getAlphabetType() const =0
Identification method.
virtual const Comments & getComments() const =0
Get the comments.
virtual const std::string & getName() const =0
Get the name of this sequence.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual size_t size() const =0
Get the number of elements in the list.
virtual const Alphabet & alphabet() const =0
Get the alphabet associated to the list.
The sequence interface.
Definition: Sequence.h:34
virtual const T & getValue(size_t pos) const =0
checked access to a character in list.
This alphabet is used to deal NumericAlphabet.