]> git.donarmstrong.com Git - rsem.git/blob - ReadReader.h
RSEM Source Codes
[rsem.git] / ReadReader.h
1 #ifndef READREADER_H_
2 #define READREADER_H_
3
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 #include<iostream>
8 #include<cassert>
9 #include<fstream>
10
11 #include "utils.h"
12 #include "SingleRead.h"
13 #include "SingleReadQ.h"
14 #include "PairedEndRead.h"
15 #include "PairedEndReadQ.h"
16 #include "ReadIndex.h"
17
18
19 template<class ReadType>
20 class ReadReader {
21 public:
22         ReadReader() { s = 0; indices = NULL; arr = NULL; }
23         ReadReader(int s, char readFs[][STRLEN]);
24         ~ReadReader();
25
26         void setIndices(ReadIndex** indices) {
27                 this->indices = indices;
28         }
29
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!
31         void reset();
32
33         bool next(ReadType& read, int flags = 7) {
34                 return read.read(s, (std::istream**)arr, flags);
35         }
36
37 private:
38         int s; // number of files
39         ReadIndex **indices;
40         std::ifstream** arr;
41         std::streampos *locations;
42 };
43
44 template<class ReadType>
45 ReadReader<ReadType>::ReadReader(int s, char readFs[][STRLEN]) {
46         assert(s > 0);
47         this->s = s;
48         arr = new std::ifstream*[s];
49         locations = new std::streampos[s];
50         indices = NULL;
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();
55         }
56 }
57
58 template<class ReadType>
59 ReadReader<ReadType>::~ReadReader() {
60         indices = NULL;
61         if (arr != NULL) {
62                 for (int i = 0; i < s; i++) {
63                         arr[i]->close();
64                         delete arr[i];
65                 }
66                 delete[] arr;
67         }
68         if (locations != NULL) {
69                 delete[] locations;
70         }
71 }
72
73 template<class ReadType>
74 bool ReadReader<ReadType>::locate(long rid) {
75         long crid = -1;
76         ReadType read;
77
78         if (indices == NULL) return false;
79
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); }
84         }
85         assert(crid <= rid);
86         while (crid < rid && read.read(s, (std::istream**)arr, 0)) ++crid;
87
88         if (crid < rid) return false;
89
90         std::streampos tmp[s];
91         for (int i = 0; i < s; i++) { tmp[i] = arr[i]->tellg(); }
92
93         if (!read.read(s, (std::istream**)arr, 0)) return false;
94
95         for (int i = 0; i < s; i++) {
96                 locations[i] = tmp[i];
97                 arr[i]->seekg(locations[i]);
98         }
99
100         return true;
101 }
102
103 template<class ReadType>
104 void ReadReader<ReadType>::reset() {
105         for (int i = 0; i < s; i++) {
106                 arr[i]->seekg(locations[i]);
107         }
108 }
109
110 #endif /* READREADER_H_ */