1#ifndef BTLLIB_SEQ_READER_SAM_MODULE_HPP
2#define BTLLIB_SEQ_READER_SAM_MODULE_HPP
4#include "btllib/cstring.hpp"
5#include "btllib/process_pipeline.hpp"
6#include "btllib/seq.hpp"
7#include "btllib/status.hpp"
16class SeqReaderSamModule
20 friend class SeqReader;
28 std::unique_ptr<ProcessPipeline> samtools_process;
29 std::unique_ptr<std::thread> loader_thread;
31 static const size_t LOADER_BLOCK_SIZE = 4096;
33 static bool buffer_valid(
const char* buffer,
size_t size);
34 template<
typename ReaderType,
typename RecordType>
35 bool read_buffer(ReaderType& reader, RecordType& record);
36 template<
typename ReaderType,
typename RecordType>
37 bool read_transition(ReaderType& reader, RecordType& record);
38 template<
typename ReaderType,
typename RecordType>
39 bool read_file(ReaderType& reader, RecordType& record);
42template<
typename ReaderType,
typename RecordType>
44SeqReaderSamModule::read_buffer(ReaderType& reader, RecordType& record)
49 ProcessPipeline version_test(
"samtools --version 2>/dev/stdout | head -n2");
52 std::string version =
"\n";
54 check_error(getline(&line, &n, version_test.out) < 2,
55 "Failed to get samtools version.");
59 check_error(getline(&line, &n, version_test.out) < 2,
60 "Failed to get samtools version.");
67 std::unique_ptr<ProcessPipeline>(
new ProcessPipeline(
"samtools fastq"));
69 std::unique_ptr<std::thread>(
new std::thread([
this, &reader]() {
70 check_error(fwrite(reader.buffer.data.data() + reader.buffer.start,
72 reader.buffer.end - reader.buffer.start,
73 samtools_process->in) !=
74 reader.buffer.end - reader.buffer.start,
75 "SeqReader SAM module: fwrite failed.");
76 reader.buffer.start = reader.buffer.end;
77 if (std::ferror(reader.source) == 0 && std::feof(reader.source) == 0) {
78 const auto p = std::fgetc(reader.source);
80 std::ungetc(p, reader.source);
81 while (std::ferror(reader.source) == 0 &&
82 std::feof(reader.source) == 0) {
83 const size_t bufsize = LOADER_BLOCK_SIZE;
85 size_t bytes_read = fread(buf, 1, bufsize, reader.source);
86 check_error(fwrite(buf, 1, bytes_read, samtools_process->in) !=
88 "SeqReader SAM module: fwrite failed.");
92 samtools_process->close_in();
94 loader_thread->detach();
98template<
typename ReaderType,
typename RecordType>
100SeqReaderSamModule::read_transition(ReaderType& reader, RecordType& record)
107template<
typename ReaderType,
typename RecordType>
109SeqReaderSamModule::read_file(ReaderType& reader, RecordType& record)
111 if (!reader.file_at_end(samtools_process->out)) {
112 reader.readline_file(record.header, samtools_process->out);
113 reader.readline_file(record.seq, samtools_process->out);
114 reader.readline_file(tmp, samtools_process->out);
115 reader.readline_file(record.qual, samtools_process->out);
Definition: bloom_filter.hpp:16
void check_error(bool condition, const std::string &msg)
void log_info(const std::string &msg)