13 #include "SingleRead.h"
14 #include "SingleReadQ.h"
15 #include "PairedEndRead.h"
16 #include "PairedEndReadQ.h"
17 #include "ReadIndex.h"
20 template<class ReadType>
23 ReadReader() { s = 0; indices = NULL; arr = NULL; hasPolyA = false; seedLen = -1; }
24 ReadReader(int s, char readFs[][STRLEN], bool hasPolyA = false, int seedLen = -1);
27 void setIndices(ReadIndex** indices) {
28 this->indices = indices;
31 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!
34 bool next(ReadType& read, int flags = 7) {
35 bool success = read.read(s, (std::istream**)arr, flags);
36 if (success && seedLen > 0) { read.calc_lq(hasPolyA, seedLen); }
41 int s; // number of files
44 std::streampos *locations;
50 template<class ReadType>
51 ReadReader<ReadType>::ReadReader(int s, char readFs[][STRLEN], bool hasPolyA, int seedLen) {
54 arr = new std::ifstream*[s];
55 locations = new std::streampos[s];
57 for (int i = 0; i < s; i++) {
58 arr[i] = new std::ifstream(readFs[i]);
59 if (!arr[i]->is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", readFs[i]); exit(-1); }
60 locations[i] = arr[i]->tellg();
62 this->hasPolyA = hasPolyA;
63 this->seedLen = seedLen;
66 template<class ReadType>
67 ReadReader<ReadType>::~ReadReader() {
70 for (int i = 0; i < s; i++) {
76 if (locations != NULL) {
81 template<class ReadType>
82 bool ReadReader<ReadType>::locate(READ_INT_TYPE rid) {
83 READ_INT_TYPE crid = -1;
86 if (indices == NULL) return false;
88 //We should make sure that crid returned by each indices is the same
89 for (int i = 0; i < s; i++) {
90 READ_INT_TYPE val = indices[i]->locate(rid, *arr[i]);
91 if (i == 0) { crid = val; } else { assert(crid == val); }
94 while (crid < rid && read.read(s, (std::istream**)arr, 0)) ++crid;
96 if (crid < rid) return false;
98 std::vector<std::streampos> tmp(s);
99 for (int i = 0; i < s; i++) { tmp[i] = arr[i]->tellg(); }
101 if (!read.read(s, (std::istream**)arr, 0)) return false;
103 for (int i = 0; i < s; i++) {
104 locations[i] = tmp[i];
105 arr[i]->seekg(locations[i]);
111 template<class ReadType>
112 void ReadReader<ReadType>::reset() {
113 for (int i = 0; i < s; i++) {
114 arr[i]->seekg(locations[i]);
118 #endif /* READREADER_H_ */