| 1 | #include "SeqLib/FastqReader.h" |
| 2 | |
| 3 | #include <cctype> |
| 4 | #include <algorithm> |
| 5 | |
| 6 | namespace SeqLib { |
| 7 | |
| 8 | bool FastqReader::Open(const std::string& f) { |
| 9 | |
| 10 | m_file = f; |
| 11 | |
| 12 | // check if file exists |
| 13 | struct stat buffer; |
| 14 | if (stat(m_file.c_str(), &buffer) != 0) { |
| 15 | std::cerr << "FastqReader: Failed to read non-existant file " << m_file << std::endl; |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | fp = NULL; |
| 20 | fp = (m_file != "-") ? gzopen(m_file.c_str(), "r") : gzdopen(fileno(stdin), "r"); |
| 21 | |
| 22 | if (!fp) { |
| 23 | std::cerr << "FastqReader: Failed to read " << m_file << std::endl; |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | seq = kseq_init(fp); // set to first seq |
| 28 | |
| 29 | return true; |
| 30 | |
| 31 | } |
| 32 | |
| 33 | FastqReader::FastqReader(const std::string& file) : m_file(file) { |
| 34 | Open(m_file); |
| 35 | } |
| 36 | |
| 37 | bool FastqReader::GetNextSequence(UnalignedSequence& s) { |
| 38 | |
| 39 | // kseq_read parses fastq and fasta |
| 40 | |
| 41 | if (!fp || !seq) |
| 42 | return false; |
| 43 | |
| 44 | // no more reads |
| 45 | if (kseq_read(seq) < 0) |
| 46 | return false; |
| 47 | |
| 48 | if (seq->name.s) |
| 49 | s.Name = std::string(seq->name.s, seq->name.l); |
| 50 | if (seq->comment.s) |
| 51 | s.Com = std::string(seq->comment.s, seq->comment.l); |
| 52 | if (seq->seq.s) |
| 53 | s.Seq = std::string(seq->seq.s, seq->seq.l); |
| 54 | if (seq->qual.s) |
| 55 | s.Qual = std::string(seq->qual.s, seq->qual.l); |
| 56 | |
| 57 | return true; |
| 58 | |
| 59 | } |
| 60 | |
| 61 | } |