bpp-seq3  3.0.0
NucleicAcidsReplication.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 using namespace bpp;
8 
9 using namespace std;
10 
12  std::shared_ptr<const NucleicAlphabet> nuc1,
13  std::shared_ptr<const NucleicAlphabet> nuc2) :
14  nuc1_(nuc1), nuc2_(nuc2), trans_()
15 {
16  trans_[-1] = -1;
17  trans_[0] = 3;
18  trans_[1] = 2;
19  trans_[2] = 1;
20  trans_[3] = 0;
21 
22  trans_[4] = 9;
23  trans_[5] = 8;
24  trans_[6] = 6;
25  trans_[7] = 7;
26  trans_[8] = 5;
27  trans_[9] = 4;
28 
29  trans_[10] = 13;
30  trans_[11] = 12;
31  trans_[12] = 11;
32  trans_[13] = 10;
33 
34  trans_[14] = 14;
35 }
36 
38 {
39  nuc1_->intToChar(state);
40  return trans_[state];
41 }
42 
43 std::string NucleicAcidsReplication::translate(const std::string& state) const
44 {
45  int i = nuc1_->charToInt(state);
46  return nuc2_->intToChar(trans_[i]);
47 }
48 
49 unique_ptr<Sequence> NucleicAcidsReplication::translate(const SequenceInterface& sequence) const
50 {
51  if (sequence.getAlphabet()->getAlphabetType() != getSourceAlphabet()->getAlphabetType())
52  throw AlphabetMismatchException("NucleicAcidsReplication::translate", getSourceAlphabet(), getTargetAlphabet());
53  auto alphaPtr = getTargetAlphabet();
54  auto tSeq = make_unique<Sequence>(sequence.getName(), "", sequence.getComments(), alphaPtr);
55  for (size_t i = 0; i < sequence.size(); ++i)
56  {
57  tSeq->addElement(translate(sequence.getValue(i)));
58  }
59  return tSeq;
60 }
61 
62 
64 {
65  nuc2_->intToChar(state);
66  return trans_[state];
67 }
68 
69 std::string NucleicAcidsReplication::reverse(const std::string& state) const
70 {
71  int i = nuc2_->charToInt(state);
72  return nuc1_->intToChar(trans_[i]);
73 }
74 
75 unique_ptr<Sequence> NucleicAcidsReplication::reverse(const SequenceInterface& sequence) const
76 {
77  if (sequence.getAlphabet()->getAlphabetType() != getTargetAlphabet()->getAlphabetType())
78  throw AlphabetMismatchException("NucleicAcidsReplication::reverse", getSourceAlphabet(), getTargetAlphabet());
79  auto alphaPtr = getSourceAlphabet();
80  auto rSeq = make_unique<Sequence>(sequence.getName(), "", sequence.getComments(), alphaPtr);
81  for (size_t i = 0; i < sequence.size(); ++i)
82  {
83  rSeq->addElement(reverse(sequence.getValue(i)));
84  }
85  return rSeq;
86 }
Exception thrown when two alphabets do not match.
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.
std::shared_ptr< const NucleicAlphabet > nuc2_
std::shared_ptr< const Alphabet > getTargetAlphabet() const override
Get the target alphabet.
std::shared_ptr< const Alphabet > getSourceAlphabet() const override
Get the source alphabet.
int translate(int state) const override
Translate a given state coded as a int from source alphabet to target alphabet.
int reverse(int state) const override
Translate a given state coded as a int from target alphabet to source alphabet.
NucleicAcidsReplication(std::shared_ptr< const NucleicAlphabet > nuc1, std::shared_ptr< const NucleicAlphabet > nuc2)
std::shared_ptr< const NucleicAlphabet > nuc1_
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.