bpp-seq3  3.0.0
PhredPoly.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/Numeric/NumTools.h>
7 #include <Bpp/Text/TextTools.h>
8 
9 #include "PhredPoly.h"
10 
11 using namespace bpp;
12 using namespace std;
13 
14 /******************************************************************************/
15 
16 PhredPoly::PhredPoly(double ratio) : ratio_(ratio) {}
17 
18 /******************************************************************************/
19 
20 bool PhredPoly::nextSequence(istream& input, Sequence& seq) const
21 {
22  if (!input)
23  {
24  throw IOException ("PhredPoly::read: fail to open stream");
25  }
26 
27  string temp, name, sequence = ""; // Initialization
28  bool flag = false;
29 
30  // Read first line
31  if (!input.eof())
32  {
33  getline(input, temp, '\n'); // Copy current line in temporary string
34  StringTokenizer st(temp, " ");
35  name = st.getToken(0);
36  }
37 
38  auto alphaPtr = seq.getAlphabet();
39 
40  // Main loop : for all other lines
41  while (!input.eof())
42  {
43  getline(input, temp, '\n'); // Copy current line in temporary string
44  StringTokenizer st(temp, " ");
45  if (st.numberOfRemainingTokens() == 12)
46  {
47  double a = TextTools::toDouble(st.getToken(3));
48  double b = TextTools::toDouble(st.getToken(7));
49  if (a < b)
50  {
51  NumTools::swap(a, b);
52  }
53  vector<string> v;
54  v.push_back(st.getToken(0)); // Get the called base
55  if (b / a > this->ratio_)
56  {
57  v.push_back(st.getToken(4)); // Get the uncalled base if relative picks areas are similar
58  }
59  sequence += alphaPtr->getGeneric(v);
60  }
61  }
62  if (name == "")
63  {
64  throw Exception("PhredPoly::read: sequence without name!");
65  }
66  else
67  {
68  seq.setName(name);
69  seq.setContent(sequence);
70  flag = true;
71  }
72  return flag;
73 }
74 
75 /******************************************************************************/
void setName(const std::string &name) override
Set the name of this sequence.
Definition: CoreSequence.h:172
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
Definition: SymbolList.h:120
static void swap(T &a, T &b)
bool nextSequence(std::istream &input, Sequence &seq) const
Definition: PhredPoly.cpp:20
PhredPoly(double ratio=0.8)
Build a new PhredPoly object.
Definition: PhredPoly.cpp:16
double ratio_
Definition: PhredPoly.h:24
A basic implementation of the Sequence interface.
Definition: Sequence.h:117
void setContent(const std::string &sequence) override
Set the whole content of the sequence.
Definition: Sequence.cpp:20
size_t numberOfRemainingTokens() const
const std::string & getToken(size_t pos) const
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
This alphabet is used to deal NumericAlphabet.