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:
23 
24  explicit Parser_qual(File_input const& fi)
25  : detail::Parser_line(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,
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
format::Format format() const
Definition: file.hpp:75
const boost::string_ref get_id()
Definition: parser_qual.hpp:45
File_istream fis_
Definition: parser_line.hpp:103
std::string str_
Definition: parser_line.hpp:104
record get_record()
Definition: parser_qual.hpp:77
void next_record()
Definition: parser_qual.hpp:46
Compression
File compression types.
Definition: file_properties.hpp:19
boost::error_info< struct errinfo_int1_, int > int1_t
Definition: exception.hpp:28
record::quality quality
Definition: parser_qual.hpp:22
Definition: parser_line.hpp:26
Main namespace of vdj_pipe library.
Definition: keywords_variable.hpp:11
File target is supposed to exist at construction time.
Definition: file.hpp:93
Definition: file_properties.hpp:43
Parser_line(File_input const &fi)
Definition: parser_line.hpp:34
boost::errinfo_nested_exception nested_t
Definition: exception.hpp:32
std::string const & path() const
Definition: file.hpp:74
Parser_qual(std::istream &is, const compression::Compression compr=compression::none)
Definition: parser_qual.hpp:36
Definition: parser_qual.hpp:19
boost::tokenizer< boost::char_separator< char > > tokenizer
Definition: me_factory.cpp:34
unsigned char value_type
Definition: sequence_record.hpp:37
boost::error_info< struct errinfo_str2_, std::string > str2_t
Definition: exception.hpp:26
void get_qual(InsertIter ii)
Definition: parser_qual.hpp:55
boost::error_info< struct errinfo_str1_, std::string > str1_t
Definition: exception.hpp:25
std::string id_
Definition: sequence_record.hpp:20
void set_meta(Seq_meta &sm, const char tag)
Definition: parser_line.hpp:70
const boost::string_ref get_defstr()
Definition: parser_qual.hpp:47
int line_num() const
Definition: parser_line.hpp:30
Definition: sequence_record.hpp:53
boost::error_info< struct errinfo_message_, std::string > msg_t
Definition: exception.hpp:24
std::string sanitize(const char c)
Definition: sanitize_string.cpp:53
Basic line-based parser; use to derive other parsers.
Definition: parser_line.hpp:23
std::istream & istream()
Definition: file_stream.hpp:102
boost::error_info< struct errinfo_line_n_, int > line_t
Definition: parser_line.hpp:27
std::string comm_
Definition: sequence_record.hpp:23
Definition: file_properties.hpp:20
Qual_record record
Definition: parser_qual.hpp:21
int line_
Definition: parser_line.hpp:105
Parser_qual(File_input const &fi)
Definition: parser_qual.hpp:24
quality get_qual()
Definition: parser_qual.hpp:49