vdj_pipe
pipeline for processing DNA sequence data
print_sequence.hpp
Go to the documentation of this file.
1 
7 #ifndef PRINT_SEQUENCE_HPP_
8 #define PRINT_SEQUENCE_HPP_
9 #include <ostream>
10 #include <string>
11 #include <vector>
12 #include "boost/assert.hpp"
13 #include "boost/foreach.hpp"
14 #include "boost/lexical_cast.hpp"
15 #include "boost/utility/string_ref.hpp"
17 
18 namespace vdj_pipe{
19 
26 template<class Ch,class Tr> inline std::basic_ostream<Ch,Tr>&
28  std::basic_ostream<Ch,Tr>& os,
29  const boost::string_ref str,
30  const Ch delim,
31  const unsigned length
32 ) {
33  std::size_t n0 = 0;
34  for( ; str.size() > n0 + length; n0 += length) {
35  os << str.substr(n0, length) << delim;
36  }
37  os << str.substr(n0);
38  return os;
39 }
40 
47 template<class Ch,class Tr> inline std::basic_ostream<Ch,Tr>& print_fasta(
48  std::basic_ostream<Ch,Tr>& os,
49  std::string const& name,
50  Seq_record::sequence const& sequence,
51  const unsigned length = 80
52 ) {
53  os << '>' << name << '\n';
54  print_split(os, sequence, '\n', length);
55  os << '\n';
56  return os;
57 }
58 
65 template<class Ch,class Tr> inline std::basic_ostream<Ch,Tr>& print_fasta(
66  std::basic_ostream<Ch,Tr>& os,
67  const std::string& name,
68  const boost::string_ref sequence,
69  const unsigned length = 80
70 ) {
71  os << '>' << name << '\n';
72  print_split(os, sequence, '\n', length);
73  os << '\n';
74  return os;
75 }
76 
83 template<class Ch,class Tr> inline std::basic_ostream<Ch,Tr>& print_qual(
84  std::basic_ostream<Ch,Tr>& os,
85  std::string const& name,
86  Qual_record::quality const& qual,
87  const unsigned length = 80
88 ) {
89  os << '>' << name;
90  unsigned pos = length;
91  typedef Qual_record::quality::value_type qual_type;
92  BOOST_FOREACH(const qual_type q, qual) {
93  const std::string qs = boost::lexical_cast<std::string>((int)q);
94  if( (pos + qs.size() + 1) > length ) {
95  os << '\n';
96  pos = 0;
97  } else {
98  os << ' ';
99  ++pos;
100  }
101  os << qs;
102  pos += qs.size();
103  }
104  os << '\n';
105  return os;
106 }
107 
115 template<class Ch,class Tr> inline std::basic_ostream<Ch,Tr>& print_fastq(
116  std::basic_ostream<Ch,Tr>& os,
117  std::string const& name,
118  Seq_record::sequence const& sequence,
119  Qual_record::quality const& qual,
120  const int offset = 33
121 ) {
122  BOOST_ASSERT(sequence.size() == qual.size());
123  os
124  << '@' << name << '\n'
125  << sequence << '\n'
126  << "+\n";
127  BOOST_FOREACH(const int q, qual) {
128  os << (char)(q + offset);
129  }
130  os << '\n';
131  return os;
132 }
133 
134 }//namespace vdj_pipe
135 #endif /* PRINT_SEQUENCE_HPP_ */
std::basic_ostream< Ch, Tr > & print_split(std::basic_ostream< Ch, Tr > &os, const boost::string_ref str, const Ch delim, const unsigned length)
print string splitting it into parts of equal length
Definition: print_sequence.hpp:27
Main namespace of vdj_pipe library.
Definition: keywords_variable.hpp:11
const std::size_t n
Definition: vector_set_test.cpp:26
std::string sequence
Definition: sequence_record.hpp:29
std::basic_ostream< Ch, Tr > & print_fasta(std::basic_ostream< Ch, Tr > &os, std::string const &name, Seq_record::sequence const &sequence, const unsigned length=80)
print FASTA record
Definition: print_sequence.hpp:47