vdj_pipe
pipeline for processing DNA sequence data
sequence_transform.hpp
Go to the documentation of this file.
1 
7 #ifndef SEQUENCE_TRANSFORM_HPP_
8 #define SEQUENCE_TRANSFORM_HPP_
9 #include <string>
10 #include "boost/range.hpp"
11 #include "boost/range/as_literal.hpp"
13 #include "vdj_pipe/exception.hpp"
16 
17 namespace vdj_pipe{
18 
21 template<class Range> inline std::string complement(Range const& r) {
22  std::string rstr(boost::size(boost::as_literal(r)), 'X');
23  std::string::reverse_iterator i = rstr.rbegin();
24  typedef typename boost::range_iterator<const Range>::type iter;
25  iter j = boost::begin(boost::as_literal(r)), end = boost::end(boost::as_literal(r));
26  for(; j != end; ++i, ++j) {
27  *i = complement((char)*j);
28  }
29  return rstr;
30 }
31 
34 inline std::string transform(
35  const boost::string_ref seq,
36  sequence_interval const& si,
37  const bool reverse
38 ) {
39  if(
40  (std::size_t)si.lower() > seq.size() ||
41  (std::size_t)si.upper() > seq.size()
42  ) BOOST_THROW_EXCEPTION(
44  << base_exception::msg_t("sequence interval and length mismatch")
45  << base_exception::int1_t(std::max(si.lower(), si.upper()))
46  << base_exception::int2_t(seq.size())
47  );
48 
49  return reverse ?
50  complement(seq.substr(si.lower(), width(si))) :
51  seq.substr(si.lower(), width(si)).to_string()
52  ;
53 }
54 
55 }//namespace vdj_pipe
56 #endif /* SEQUENCE_TRANSFORM_HPP_ */
boost::error_info< struct errinfo_int1_, int > int1_t
Definition: exception.hpp:28
Main namespace of vdj_pipe library.
Definition: keywords_variable.hpp:11
boost::error_info< struct errinfo_int2_, int > int2_t
Definition: exception.hpp:29
std::string to_string(detail::Queable_ofstream_types::val_vector const v)
Definition: find_shared.cpp:174
Nucleotide complement(const Nucleotide n)
Definition: nucleotide_index.hpp:153
boost::numeric::interval< int, boost::numeric::interval_lib::policies< boost::numeric::interval_lib::rounded_math< int >, detail::Interval_checking_policy< int > > > sequence_interval
Definition: sequence_interval.hpp:40
Definition: exception.hpp:23
boost::error_info< struct errinfo_message_, std::string > msg_t
Definition: exception.hpp:24
std::string transform(const boost::string_ref seq, sequence_interval const &si, const bool reverse)
Reverse-complement (optionally) a portion of a sequence.
Definition: sequence_transform.hpp:34