/usr/include/jellyfish/whole_sequence_parser.hpp is in libjellyfish-2.0-dev 2.2.8-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #ifndef __JELLYFISH_WHOLE_SEQUENCE_PARSER_HPP__
#define __JELLYFISH_WHOLE_SEQUENCE_PARSER_HPP__
#include <string>
#include <memory>
#include <jellyfish/err.hpp>
#include <jellyfish/cooperative_pool2.hpp>
#include <jellyfish/cpp_array.hpp>
#include <jellyfish/parser_common.hpp>
namespace jellyfish {
struct header_sequence_qual {
std::string header;
std::string seq;
std::string qual;
};
struct sequence_list {
size_t nb_filled;
std::vector<header_sequence_qual> data;
};
template<typename StreamIterator>
class whole_sequence_parser : public jellyfish::cooperative_pool2<whole_sequence_parser<StreamIterator>, sequence_list> {
typedef jellyfish::cooperative_pool2<whole_sequence_parser<StreamIterator>, sequence_list> super;
struct stream_status {
file_type type;
std::string buffer;
stream_type stream;
stream_status() : type(DONE_TYPE) { }
};
cpp_array<stream_status> streams_;
StreamIterator& streams_iterator_;
size_t files_read_; // nb of files read
size_t reads_read_; // nb of reads read
public:
/// Size is the number of buffers to keep around. It should be
/// larger than the number of thread expected to read from this
/// class. nb_sequences is the number of sequences to read into a
/// buffer. 'begin' and 'end' are iterators to a range of istream.
whole_sequence_parser(uint32_t size, uint32_t nb_sequences,
uint32_t max_producers, StreamIterator& streams) :
super(max_producers, size),
streams_(max_producers),
streams_iterator_(streams),
files_read_(0),
reads_read_(0)
{
for(auto it = super::element_begin(); it != super::element_end(); ++it) {
it->nb_filled = 0;
it->data.resize(nb_sequences);
}
for(uint32_t i = 0; i < max_producers; ++i) {
streams_.init(i);
open_next_file(streams_[i]);
}
}
inline bool produce(uint32_t i, sequence_list& buff) {
stream_status& st = streams_[i];
switch(st.type) {
case FASTA_TYPE:
read_fasta(st, buff);
break;
case FASTQ_TYPE:
read_fastq(st, buff);
break;
#ifdef HAVE_HTSLIB
case SAM_TYPE:
read_sam(st, buff);
break;
#endif
case DONE_TYPE:
return true;
}
if(st.stream.good())
return false;
// Reach the end of file, close current and try to open the next one
open_next_file(st);
return false;
}
size_t nb_files() const { return files_read_; }
size_t nb_reads() const { return reads_read_; }
protected:
void open_next_file(stream_status& st) {
st.stream.reset();
st.stream = std::move(streams_iterator_.next());
if(!st.stream.good()) {
st.type = DONE_TYPE;
return;
}
++files_read_;
// Update the type of the current file and move past first header
// to beginning of sequence.
if(st.stream.standard) {
switch(st.stream.standard->peek()) {
case EOF: return open_next_file(st);
case '>':
st.type = FASTA_TYPE;
break;
case '@':
st.type = FASTQ_TYPE;
break;
default:
throw std::runtime_error("Unsupported format"); // Better error management
}
}
#ifdef HAVE_HTSLIB
else if(st.stream.sam) {
st.type = SAM_TYPE;
}
#endif
else {
st.type = DONE_TYPE;
}
}
void read_fasta(stream_status& st, sequence_list& buff) {
size_t& nb_filled = buff.nb_filled;
const size_t data_size = buff.data.size();
auto& stream = *st.stream.standard;
for(nb_filled = 0; nb_filled < data_size && stream.peek() != EOF; ++nb_filled) {
++reads_read_;
header_sequence_qual& fill_buff = buff.data[nb_filled];
stream.get(); // Skip '>'
std::getline(stream, fill_buff.header);
fill_buff.seq.clear();
for(int c = stream.peek(); c != '>' && c != EOF; c = stream.peek()) {
std::getline(stream, st.buffer); // Wish there was an easy way to combine the
fill_buff.seq.append(st.buffer); // two lines avoiding copying
}
}
}
void read_fastq(stream_status& st, sequence_list& buff) {
size_t& nb_filled = buff.nb_filled;
const size_t data_size = buff.data.size();
auto& stream = *st.stream.standard;
for(nb_filled = 0; nb_filled < data_size && stream.peek() != EOF; ++nb_filled) {
++reads_read_;
header_sequence_qual& fill_buff = buff.data[nb_filled];
stream.get(); // Skip '@'
std::getline(stream, fill_buff.header);
if(stream.peek() != '+')
std::getline(stream, fill_buff.seq);
else
fill_buff.seq.clear();
while(stream.peek() != '+' && stream.peek() != EOF) {
std::getline(stream, st.buffer); // Wish there was an easy way to combine the
fill_buff.seq.append(st.buffer); // two lines avoiding copying
}
if(!stream.good())
throw std::runtime_error("Truncated fastq file");
stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(stream.peek() != '+')
std::getline(stream, fill_buff.qual);
else
fill_buff.qual.clear();
while(fill_buff.qual.size() < fill_buff.seq.size() && stream.good()) {
std::getline(stream, st.buffer);
fill_buff.qual.append(st.buffer);
}
if(fill_buff.qual.size() != fill_buff.seq.size())
throw std::runtime_error("Invalid fastq file: wrong number of quals");
if(stream.peek() != EOF && stream.peek() != '@')
throw std::runtime_error("Invalid fastq file: header missing");
// std::cerr << nb_filled << ": " << fill_buff.seq << '\n'
// << nb_filled << ": " << fill_buff.qual << '\n';
}
}
#ifdef HAVE_HTSLIB
void read_sam(stream_status& st, sequence_list& buff) {
size_t& nb_filled = buff.nb_filled;
const size_t data_size = buff.data.size();
auto& stream = *st.stream.sam;
for(nb_filled = 0; nb_filled < data_size && stream.next() >= 0; ++nb_filled) {
++reads_read_;
header_sequence_qual& fill_buff = buff.data[nb_filled];
fill_buff.header = stream.qname();
auto& seq = fill_buff.seq;
auto& qual = fill_buff.qual;
seq.resize(stream.seq_len());
qual.resize(stream.seq_len());
for(ssize_t i = 0; i < stream.seq_len(); ++i) {
seq[i] = stream.base(i);
qual[i] = stream.qual(i) + '!';
}
}
}
#endif
};
} // namespace jellyfish
#endif /* __JELLYFISH_WHOLE_SEQUENCE_PARSER_HPP__ */
|