]> git.donarmstrong.com Git - rsem.git/blob - ReadReader.h
Added .gitignore file back
[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 #include<vector>
11
12 #include "utils.h"
13 #include "SingleRead.h"
14 #include "SingleReadQ.h"
15 #include "PairedEndRead.h"
16 #include "PairedEndReadQ.h"
17 #include "ReadIndex.h"
18
19
20 template<class ReadType>
21 class ReadReader {
22 public:
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);
25         ~ReadReader();
26
27         void setIndices(ReadIndex** indices) {
28                 this->indices = indices;
29         }
30
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!
32         void reset();
33
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); }
37                 return success;
38         }
39
40 private:
41         int s; // number of files
42         ReadIndex **indices;
43         std::ifstream** arr;
44         std::streampos *locations;
45
46         bool hasPolyA;
47         int seedLen;
48 };
49
50 template<class ReadType>
51 ReadReader<ReadType>::ReadReader(int s, char readFs[][STRLEN], bool hasPolyA, int seedLen) {
52         assert(s > 0);
53         this->s = s;
54         arr = new std::ifstream*[s];
55         locations = new std::streampos[s];
56         indices = NULL;
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();
61         }
62         this->hasPolyA = hasPolyA;
63         this->seedLen = seedLen;
64 }
65
66 template<class ReadType>
67 ReadReader<ReadType>::~ReadReader() {
68         indices = NULL;
69         if (arr != NULL) {
70                 for (int i = 0; i < s; i++) {
71                         arr[i]->close();
72                         delete arr[i];
73                 }
74                 delete[] arr;
75         }
76         if (locations != NULL) {
77                 delete[] locations;
78         }
79 }
80
81 template<class ReadType>
82 bool ReadReader<ReadType>::locate(READ_INT_TYPE rid) {
83         READ_INT_TYPE crid = -1;
84         ReadType read;
85
86         if (indices == NULL) return false;
87
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); }
92         }
93         assert(crid <= rid);
94         while (crid < rid && read.read(s, (std::istream**)arr, 0)) ++crid;
95
96         if (crid < rid) return false;
97
98         std::vector<std::streampos> tmp(s);
99         for (int i = 0; i < s; i++) { tmp[i] = arr[i]->tellg(); }
100
101         if (!read.read(s, (std::istream**)arr, 0)) return false;
102
103         for (int i = 0; i < s; i++) {
104                 locations[i] = tmp[i];
105                 arr[i]->seekg(locations[i]);
106         }
107
108         return true;
109 }
110
111 template<class ReadType>
112 void ReadReader<ReadType>::reset() {
113         for (int i = 0; i < s; i++) {
114                 arr[i]->seekg(locations[i]);
115         }
116 }
117
118 #endif /* READREADER_H_ */