bpp-seq-omics  2.4.1
Fastq.cpp
Go to the documentation of this file.
1 //
2 // File: Fastq.cpp
3 // Author: Sylvain Gaillard
4 // Created: 22/11/2011 09:56:29
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 22, 2011)
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 "Fastq.h"
42 
43 #include <typeinfo>
44 
45 using namespace bpp;
46 
47 bool Fastq::nextSequence(std::istream& input, Sequence& seq) const {
48  if (input && ! input.eof()) {
49  //SequenceWithQuality& sq;
50  std::string buffer;
51  while (TextTools::isEmpty(buffer) && !input.eof()) {
52  getline(input, buffer);
53  }
54  if (input.eof()) { // We hit the end of the file
55  return false;
56  }
57  // first line: seq name
58  if (buffer[0] == '@') {
59  seq.setName(std::string(buffer.begin() + 1, buffer.end()));
60  }
61  // second line: seq
62  getline(input, buffer);
63  seq.setContent(buffer);
64  // third line: seq name (again)
65  getline(input, buffer);
66  if (repeatName()) {
67  std::string secName = std::string(buffer.begin() + 1, buffer.end());
68  if (secName != seq.getName()) {
69  throw Exception("Names are not equivalent for sequence(@ line) and quality (+ line)");
70  }
71  }
72  // fourth line: quality
73  getline(input, buffer);
74  try {
75  SequenceWithQuality& sq = dynamic_cast<SequenceWithQuality&>(seq);
76  for (size_t i = 0 ; i < buffer.size() ; i++) {
77  sq.setQuality(i, static_cast<int>(buffer[i]));
78  }
79  } catch (...) {
80  }
81  return true;
82  }
83  return false;
84 }
85 
86 /******************************************************************************/
87 
88 void Fastq::writeSequence(std::ostream& output, const Sequence& seq) const
89 {
90  std::string qual(seq.size(), static_cast<char>(33));
91  try {
92  const SequenceWithQuality& sq = dynamic_cast<const SequenceWithQuality&>(seq);
93  for (size_t i = 0 ; i < sq.size() ; i++) {
94  char q = static_cast<char>(sq.getQuality(i));
95  if (q < 33 || q > 126) {
96  throw BadIntegerException("Quality must lie between 33 and 126", q);
97  }
98  qual[i] = q;
99  }
100  } catch (const std::bad_cast& e) {
101  throw Exception("seq must be a SequenceWithQuality object");
102  }
103  output << "@" << seq.getName() << std::endl;
104  output << seq.toString() << std::endl;
105  output << "+";
106  if (repeatName()) {
107  output << seq.getName();
108  }
109  output << std::endl;
110  output << qual << std::endl;
111 }
virtual std::string toString() const=0
virtual size_t size() const=0
void writeSequence(std::ostream &output, const Sequence &seq) const
Definition: Fastq.cpp:88
bool nextSequence(std::istream &input, Sequence &seq) const
Definition: Fastq.cpp:47
bool repeatName() const
Definition: Fastq.h:87
void setQuality(size_t pos, int quality)
int getQuality(size_t pos) const
virtual void setContent(const std::string &sequence)=0
virtual void setName(const std::string &name)=0
virtual const std::string & getName() const=0
bool isEmpty(const std::string &s)