| 1 | #include "FASTX_Parser.hpp" |
| 2 | |
| 3 | FastqFile::FastqFile() : kseq(NULL), file_no(0) { fnit = fnames.end(); } |
| 4 | |
| 5 | FastqFile::FastqFile(const vector<string> files) : kseq(NULL), fnames(files), file_no(0) { |
| 6 | |
| 7 | fnit = fnames.begin(); |
| 8 | fp = gzopen(fnit->c_str(), "r"); |
| 9 | kseq = kseq_init(fp); |
| 10 | |
| 11 | //std::ios::sync_with_stdio(false); |
| 12 | } |
| 13 | |
| 14 | FastqFile::FastqFile(FastqFile&& o) : fp(o.fp), kseq(o.kseq), fnames(o.fnames), file_no(o.file_no) { |
| 15 | |
| 16 | fnit = fnames.begin(); |
| 17 | |
| 18 | while (*fnit != *(o.fnit)) ++fnit; |
| 19 | |
| 20 | o.kseq = NULL; |
| 21 | } |
| 22 | |
| 23 | FastqFile& FastqFile::operator=(FastqFile&& o){ |
| 24 | |
| 25 | if (this != &o) { |
| 26 | |
| 27 | close(); |
| 28 | |
| 29 | fp = o.fp; |
| 30 | kseq = o.kseq; |
| 31 | fnames = o.fnames; |
| 32 | file_no = o.file_no; |
| 33 | |
| 34 | fnit = fnames.begin(); |
| 35 | |
| 36 | while (*fnit != *(o.fnit)) ++fnit; |
| 37 | |
| 38 | o.kseq = NULL; |
| 39 | } |
| 40 | |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | FastqFile::~FastqFile() { |
| 45 | |
| 46 | close(); |
| 47 | } |
| 48 | |
| 49 | void FastqFile::reopen() { |
| 50 | |
| 51 | close(); |
| 52 | |
| 53 | fnit = fnames.begin(); |
| 54 | fp = gzopen(fnit->c_str(), "r"); |
| 55 | kseq = kseq_init(fp); |
| 56 | } |
| 57 | |
| 58 | // returns >=0 (length of seq), -1 end of last file, -2 truncated quality string |
| 59 | int FastqFile::read_next(char* read, size_t* read_len, string &seq, size_t* seq_len, unsigned int* file_id, char* qual) { |
| 60 | |
| 61 | int r; |
| 62 | |
| 63 | if ((r = kseq_read(kseq)) >= 0) { |
| 64 | |
| 65 | memcpy(read, kseq->name.s, kseq->name.l + 1); // 0-terminated string |
| 66 | *read_len = kseq->name.l; |
| 67 | seq.assign(kseq->seq.s); |
| 68 | *seq_len = kseq->seq.l; |
| 69 | |
| 70 | if (qual != NULL) memcpy(qual, kseq->qual.s, kseq->qual.l + 1); // 0-terminated string |
| 71 | if (file_id != NULL) *file_id = file_no / 2; |
| 72 | } |
| 73 | else if (r == -1) { |
| 74 | |
| 75 | open_next(); |
| 76 | |
| 77 | if (fnit != fnames.end()) return read_next(read, read_len, seq, seq_len, file_id, qual); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | return r; |
| 82 | } |
| 83 | |
| 84 | // returns >=0 (length of seq), -1 end of last file, -2 truncated quality string |
| 85 | int FastqFile::read_next(string &seq, size_t& id) { |
| 86 | |
| 87 | const int r = kseq_read(kseq); |
| 88 | |
| 89 | if (r >= 0) seq.assign(kseq->seq.s); |
| 90 | else if (r == -1) { |
| 91 | |
| 92 | open_next(); |
| 93 | |
| 94 | if (fnit != fnames.end()){ |
| 95 | |
| 96 | id = file_no; |
| 97 | |
| 98 | return read_next(seq, id); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return r; |
| 103 | } |
| 104 | |
| 105 | // next_file_opened == true indicates the next file to read in the list has been opened: |
| 106 | // first read entry of the file has not been read yet, method returns 0. |
| 107 | // Else, method returns >= 0 (length of seq), -1 end of last file, -2 truncated quality string. |
| 108 | int FastqFile::read_next(string &seq, size_t& id, bool& next_file_opened) { |
| 109 | |
| 110 | const int r = kseq_read(kseq); |
| 111 | |
| 112 | next_file_opened = false; |
| 113 | |
| 114 | if (r >= 0) seq.assign(kseq->seq.s); |
| 115 | else if (r == -1) { |
| 116 | |
| 117 | open_next(); |
| 118 | |
| 119 | if (fnit != fnames.end()){ |
| 120 | |
| 121 | id = file_no; |
| 122 | next_file_opened = true; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return r; |
| 129 | } |
| 130 | |
| 131 | int FastqFile::read_next(stringstream& ss, size_t& id, bool& next_file_opened) { |
| 132 | |
| 133 | const int r = kseq_read(kseq); |
| 134 | |
| 135 | next_file_opened = false; |
| 136 | |
| 137 | if (r >= 0) ss << kseq->seq.s; |
| 138 | else if (r == -1) { |
| 139 | |
| 140 | open_next(); |
| 141 | |
| 142 | if (fnit != fnames.end()){ |
| 143 | |
| 144 | id = file_no; |
| 145 | next_file_opened = true; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return r; |
| 152 | } |
| 153 | |
| 154 | int FastqFile::read_next() { |
| 155 | |
| 156 | const int r = kseq_read(kseq); |
| 157 | |
| 158 | if (r == -1) { |
| 159 | |
| 160 | open_next(); |
| 161 | |
| 162 | if (fnit != fnames.end()) return read_next(); |
| 163 | } |
| 164 | |
| 165 | return r; |
| 166 | } |
| 167 | |
| 168 | vector<string>::const_iterator FastqFile::open_next() { |
| 169 | |
| 170 | if (fnit != fnames.end()) { |
| 171 | // close current file |
| 172 | kseq_destroy(kseq); |
| 173 | gzclose(fp); |
| 174 | |
| 175 | kseq = NULL; |
| 176 | |
| 177 | // get next file |
| 178 | ++fnit; |
| 179 | ++file_no; |
| 180 | |
| 181 | if (fnit != fnames.end()) { |
| 182 | |
| 183 | fp = gzopen(fnit->c_str(), "r"); |
| 184 | kseq = kseq_init(fp); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return fnit; |
| 189 | } |
| 190 | |
| 191 | void FastqFile::close() { |
| 192 | |
| 193 | if (kseq != NULL) { |
| 194 | |
| 195 | kseq_destroy(kseq); |
| 196 | gzclose(fp); |
| 197 | |
| 198 | fnit = fnames.end(); |
| 199 | kseq = NULL; |
| 200 | } |
| 201 | } |