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_ */
Main namespace of vdj_pipe library.
Definition: sequence_file.hpp:14
Definition: exception.hpp:23
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