vdj_pipe
pipeline for processing DNA sequence data
parser_qual.hpp
Go to the documentation of this file.
1 
7 #ifndef PARSER_QUAL_HPP_
8 #define PARSER_QUAL_HPP_
9 #include <vector>
10 #include "boost/lexical_cast.hpp"
11 #include "boost/tokenizer.hpp"
14 
15 namespace vdj_pipe{
16 
20 public:
21  typedef Qual_record record;
22  typedef record::quality quality;
23 
24  explicit Parser_qual(File_input const& fi)
26  {
27  if( fi.format() != format::Qual ) BOOST_THROW_EXCEPTION(
28  Err()
29  << Err::msg_t("wrong file format for quality score parser")
30  << Err::str1_t(sanitize(fi.path()))
31  << Err::int1_t(fi.format())
32  );
33  next_record();
34  }
35 
36  explicit Parser_qual(
37  std::istream& is,
38  const compression::Compression compr = compression::none
39  )
40  : detail::Parser_line(is, compr)
41  {
42  next_record();
43  }
44 
45  const boost::string_ref get_id() {return Parser_line::get_id('>');}
46  void next_record() {Parser_line::seek_line('>');}
47  const boost::string_ref get_defstr() {return Parser_line::get_defstr('>');}
48 
49  quality get_qual() {
50  quality q;
51  get_qual(back_inserter(q));
52  return q;
53  }
54 
55  template<class InsertIter> void get_qual(InsertIter ii) {
56  while( fis_.istream().peek() != '>' && getline(fis_.istream(), str_) ) {
57  ++line_;
58  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
59  boost::char_separator<char> sep(" \t");
60  tokenizer tok(str_, sep);
61  for(tokenizer::const_iterator ti = tok.begin(); ! ti.at_end(); ++ti) {
62  try{
63  const int i = boost::lexical_cast<int>(*ti);
64  ii = boost::numeric_cast<quality::value_type>(i);
65  } catch(std::bad_cast const&) {
66  BOOST_THROW_EXCEPTION(
67  Err()
68  << Err::msg_t("invalid quality score")
69  << Err::str1_t(sanitize(*ti, 60))
70  << Err::line_t(line_num() - 1)
71  );
72  }
73  }
74  }
75  }
76 
77  record get_record() {
78  record qr;
79  set_meta(qr, '>');
80  try{
81  get_qual(back_inserter(qr.qual_));
82  }catch(std::exception const&) {
83  BOOST_THROW_EXCEPTION(
84  Err()
85  << Err::msg_t("invalid quality score")
86  << Err::str1_t(sanitize(qr.id_, 60))
87  << Err::str2_t(sanitize(qr.comm_, 60))
88  << Err::nested_t(boost::current_exception())
89  );
90  }
91  return qr;
92  }
93 };
94 
95 }//namespace vdj_pipe
96 #endif /* PARSER_QUAL_HPP_ */
quality qual_
Definition: sequence_record.hpp:57
Definition: sequence_record.hpp:35
Compression
File compression types.
Definition: file_properties.hpp:19
Definition: parser_line.hpp:26
Main namespace of vdj_pipe library.
Definition: sequence_file.hpp:14
File target is supposed to exist at construction time.
Definition: file.hpp:93
Definition: parser_qual.hpp:19
std::string id_
Definition: sequence_record.hpp:20
Definition: sequence_record.hpp:53
Basic line-based parser; use to derive other parsers.
Definition: parser_line.hpp:23
std::string comm_
Definition: sequence_record.hpp:23