56 template<
class StringType>
58 StringType
const& label,
59 StringType
const& sites,
60 StringType
const& quality,
66 assert( sites.size() == quality.size() );
69 auto write_wrapped_ = [&]( StringType
const& str ){
70 if( line_length > 0 ) {
71 for(
size_t i = 0; i < str.size(); i += line_length ) {
75 auto const sub = str.substr( i, line_length );
76 os.write( sub.data(), sub.size() );
81 os.write( str.data(), str.size() );
89 os.write( label.data(), label.size() );
93 write_wrapped_( sites );
99 os.write( label.data(), label.size() );
103 os.write(
"+\n", 2 );
107 write_wrapped_( quality );
116 std::shared_ptr<utils::BaseOutputTarget> target
119 std::string quality_string;
131 quality_string = make_filled_quality_string_( sequence.
sites().size() );
135 throw std::runtime_error(
136 "Invalid Sequence with phred scores of different length than the sequence has sites."
141 write_sequence_( sequence.
label(), sequence.
sites(), quality_string, target->ostream() );
146 std::string
const& quality_string,
147 std::shared_ptr<utils::BaseOutputTarget> target
152 throw std::runtime_error(
153 "Cannot write Fastq sequence with provided quality string "
154 "if the sequence contains phred scores already."
159 if( quality_string.size() == sequence.
sites().size() ) {
160 write_sequence_( sequence.
label(), sequence.
sites(), quality_string, target->ostream() );
161 }
else if( quality_string.size() == 0 ) {
162 auto const filled_string = make_filled_quality_string_( sequence.
sites().size() );
163 write_sequence_( sequence.
label(), sequence.
sites(), filled_string, target->ostream() );
165 throw std::runtime_error(
166 "Invalid given quality string of different length than the sequence has sites."
173 std::shared_ptr<utils::BaseOutputTarget> target
175 for(
Sequence const& sequence : sequence_set ) {
176 write( sequence, target );
180 #if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
183 std::string_view
const& label,
184 std::string_view
const& sites,
185 std::string_view
const& quality,
186 std::shared_ptr<utils::BaseOutputTarget> target
190 std::string quality_buffer;
191 std::string_view quality_view = quality;
192 if( quality.empty() ) {
193 quality_buffer = make_filled_quality_string_( sites.size() );
194 quality_view = std::string_view( quality_buffer );
195 }
else if( quality.size() != sites.size() ) {
196 throw std::runtime_error(
197 "Invalid Sequence with quality string of different length than the sequence has sites."
202 fastq_writer_write_sequence_helper_<std::string_view>(
203 label, sites, quality_view, line_length_, repeat_label_, target->ostream()
213 std::string FastqWriter::make_filled_quality_string_(
218 if( fill_missing_quality_ == 255 ) {
219 throw std::runtime_error(
220 "Sequence without phred scores found. "
221 "Use FastqWriter::fill_missing_quality() to use dummy score values instead."
228 auto quality_string = std::string(
length, dummy_chr );
229 assert( quality_string.size() ==
length );
230 return quality_string;
233 void FastqWriter::write_sequence_(
234 std::string
const& label,
235 std::string
const& sites,
236 std::string
const& quality,
239 fastq_writer_write_sequence_helper_<std::string>(
240 label, sites, quality, line_length_, repeat_label_, os