12 #include "SingleRead.h"
13 #include "SingleReadQ.h"
14 #include "PairedEndRead.h"
15 #include "PairedEndReadQ.h"
16 #include "ReadIndex.h"
19 template<class ReadType>
22 ReadReader() { s = 0; indices = NULL; arr = NULL; hasPolyA = false; seedLen = -1; }
23 ReadReader(int s, char readFs[][STRLEN], bool hasPolyA = false, int seedLen = -1);
26 void setIndices(ReadIndex** indices) {
27 this->indices = indices;
30 bool locate(READ_INT_TYPE); // You should guarantee that indices exist and rid is valid, otherwise return false; If it fails, you should reset it manually!
33 bool next(ReadType& read, int flags = 7) {
34 bool success = read.read(s, (std::istream**)arr, flags);
35 if (success && seedLen > 0) { read.calc_lq(hasPolyA, seedLen); }
40 int s; // number of files
43 std::streampos *locations;
49 template<class ReadType>
50 ReadReader<ReadType>::ReadReader(int s, char readFs[][STRLEN], bool hasPolyA, int seedLen) {
53 arr = new std::ifstream*[s];
54 locations = new std::streampos[s];
56 for (int i = 0; i < s; i++) {
57 arr[i] = new std::ifstream(readFs[i]);
58 if (!arr[i]->is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", readFs[i]); exit(-1); }
59 locations[i] = arr[i]->tellg();
61 this->hasPolyA = hasPolyA;
62 this->seedLen = seedLen;
65 template<class ReadType>
66 ReadReader<ReadType>::~ReadReader() {
69 for (int i = 0; i < s; i++) {
75 if (locations != NULL) {
80 template<class ReadType>
81 bool ReadReader<ReadType>::locate(READ_INT_TYPE rid) {
82 READ_INT_TYPE crid = -1;
85 if (indices == NULL) return false;
87 //We should make sure that crid returned by each indices is the same
88 for (int i = 0; i < s; i++) {
89 READ_INT_TYPE val = indices[i]->locate(rid, *arr[i]);
90 if (i == 0) { crid = val; } else { assert(crid == val); }
93 while (crid < rid && read.read(s, (std::istream**)arr, 0)) ++crid;
95 if (crid < rid) return false;
97 std::streampos tmp[s];
98 for (int i = 0; i < s; i++) { tmp[i] = arr[i]->tellg(); }
100 if (!read.read(s, (std::istream**)arr, 0)) return false;
102 for (int i = 0; i < s; i++) {
103 locations[i] = tmp[i];
104 arr[i]->seekg(locations[i]);
110 template<class ReadType>
111 void ReadReader<ReadType>::reset() {
112 for (int i = 0; i < s; i++) {
113 arr[i]->seekg(locations[i]);
117 #endif /* READREADER_H_ */