bpp-seq3  3.0.0
SequenceWalker.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 <iostream>
6 
7 
8 using namespace std;
9 
10 #include "SequenceWalker.h"
11 
12 #include <Bpp/Text/TextTools.h>
13 
14 using namespace bpp;
15 
16 size_t SequenceWalker::getAlignmentPosition(size_t seqPos)
17 {
18  if (seqPos == seqPos_)
19  return alnPos_;
20  if (seqPos > seqPos_)
21  {
22  // Move forward
23  while (alnPos_ < seq_->size() && seqPos_ < seqPos)
24  {
25  if (alnPos_ == seq_->size() - 1)
26  throw Exception("SequenceWalker::getAlignmentPosition(). Forward1. Position out of bound.");
27  ++alnPos_;
28 
29  if ((*seq_)[alnPos_] != gap_)
30  {
31  ++seqPos_;
32  }
33  }
34  if (seqPos_ != seqPos)
35  throw Exception("SequenceWalker::getAlignmentPosition(). Forward2. Position out of bound (" + TextTools::toString(alnPos_) + ")");
36  }
37  else
38  {
39  // Move backward
40  if (alnPos_ == 0)
41  throw Exception("SequenceWalker::getAlignmentPosition(). Backward. Position out of bound.");
42  while (alnPos_ > 0 && seqPos_ > seqPos)
43  {
44  --alnPos_;
45  if ((*seq_)[alnPos_] != gap_)
46  {
47  --seqPos_;
48  }
49  }
50  if (seqPos_ != seqPos)
51  throw Exception("SequenceWalker::getAlignmentPosition(). Position out of bound.");
52  }
53  return alnPos_;
54 }
55 
56 size_t SequenceWalker::getSequencePosition(size_t alnPos)
57 {
58  if (alnPos == alnPos_)
59  return seqPos_;
60  if (alnPos >= seq_->size())
61  throw Exception("SequenceWalker::getSequencePosition(). Position out of bound.");
62  if (alnPos > alnPos_)
63  {
64  // Move forward
65  while (alnPos_ < alnPos)
66  {
67  ++alnPos_;
68  if ((*seq_)[alnPos_] != gap_)
69  {
70  ++seqPos_;
71  }
72  }
73  }
74  else
75  {
76  // Move backward
77  while (alnPos_ > alnPos)
78  {
79  if (seqPos_ == 0)
80  return 0;
81  --alnPos_;
82  if ((*seq_)[alnPos_ + 1] != gap_)
83  {
84  --seqPos_;
85  }
86  }
87  }
88  return seqPos_;
89 }
This alphabet is used to deal NumericAlphabet.