bpp-seq  2.2.0
PhredPoly.cpp
Go to the documentation of this file.
1 //
2 // File: PhredPoly.cpp
3 // Created by: Sylvain Gaillard
4 // Created on: Fri Oct 31 2008
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (October 31, 2008)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "PhredPoly.h"
41 #include <Bpp/Text/TextTools.h>
42 #include <Bpp/Text/StringTokenizer.h>
43 #include <Bpp/Numeric/NumTools.h>
44 
45 using namespace bpp;
46 using namespace std;
47 
48 /******************************************************************************/
49 
50 PhredPoly::PhredPoly(double ratio) : ratio_(ratio) {}
51 
52 /******************************************************************************/
53 
54 bool PhredPoly::nextSequence(istream& input, Sequence& seq) const throw (Exception) {
55  if (!input) { throw IOException ("PhredPoly::read: fail to open stream"); }
56 
57  string temp, name, sequence = ""; // Initialization
58  bool flag = false;
59 
60  // Read first line
61  if (!input.eof()) {
62  getline(input, temp, '\n'); // Copy current line in temporary string
63  StringTokenizer st(temp, " ");
64  name = st.getToken(0);
65  }
66 
67  const Alphabet* alpha = seq.getAlphabet();
68 
69  // Main loop : for all other lines
70  while (!input.eof()) {
71  getline(input, temp, '\n'); // Copy current line in temporary string
72  StringTokenizer st(temp, " ");
73  if (st.numberOfRemainingTokens() == 12) {
74  double a = TextTools::toDouble(st.getToken(3));
75  double b = TextTools::toDouble(st.getToken(7));
76  if (a < b) {
77  NumTools::swap(a, b);
78  }
79  vector<string> v;
80  v.push_back(st.getToken(0)); // Get the called base
81  if (b / a > this->ratio_) {
82  v.push_back(st.getToken(4)); // Get the uncalled base if relative picks areas are similar
83  }
84  sequence += alpha->getGeneric(v);
85  }
86  }
87  if(name == "") {
88  throw Exception("PhredPoly::read: sequence without name!");
89  } else {
90  seq.setName(name);
91  seq.setContent(sequence);
92  flag = true;
93  }
94  return flag;
95 }
96 
97 /******************************************************************************/
virtual int getGeneric(const std::vector< int > &states) const =0
Get the generic state that match a set of states.
This alphabet is used to deal NumericAlphabet.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
PhredPoly(double ratio=0.8)
Build a new PhredPoly object.
Definition: PhredPoly.cpp:50
The sequence interface.
Definition: Sequence.h:74
bool nextSequence(std::istream &input, Sequence &seq) const
Read sequence from stream.
Definition: PhredPoly.cpp:54