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; }
23 ReadReader(int s, char readFs[][STRLEN]);
26 void setIndices(ReadIndex** indices) {
27 this->indices = indices;
30 bool locate(long); // 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 return read.read(s, (std::istream**)arr, flags);
38 int s; // number of files
41 std::streampos *locations;
44 template<class ReadType>
45 ReadReader<ReadType>::ReadReader(int s, char readFs[][STRLEN]) {
48 arr = new std::ifstream*[s];
49 locations = new std::streampos[s];
51 for (int i = 0; i < s; i++) {
52 arr[i] = new std::ifstream(readFs[i]);
53 if (!arr[i]->is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", readFs[i]); exit(-1); }
54 locations[i] = arr[i]->tellg();
58 template<class ReadType>
59 ReadReader<ReadType>::~ReadReader() {
62 for (int i = 0; i < s; i++) {
68 if (locations != NULL) {
73 template<class ReadType>
74 bool ReadReader<ReadType>::locate(long rid) {
78 if (indices == NULL) return false;
80 //We should make sure that crid returned by each indices is the same
81 for (int i = 0; i < s; i++) {
82 long val = indices[i]->locate(rid, *arr[i]);
83 if (i == 0) { crid = val; } else { assert(crid == val); }
86 while (crid < rid && read.read(s, (std::istream**)arr, 0)) ++crid;
88 if (crid < rid) return false;
90 std::streampos tmp[s];
91 for (int i = 0; i < s; i++) { tmp[i] = arr[i]->tellg(); }
93 if (!read.read(s, (std::istream**)arr, 0)) return false;
95 for (int i = 0; i < s; i++) {
96 locations[i] = tmp[i];
97 arr[i]->seekg(locations[i]);
103 template<class ReadType>
104 void ReadReader<ReadType>::reset() {
105 for (int i = 0; i < s; i++) {
106 arr[i]->seekg(locations[i]);
110 #endif /* READREADER_H_ */