forked from deweylab/RSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadIndex.h
59 lines (49 loc) · 1.25 KB
/
ReadIndex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef READINDEX_H_
#define READINDEX_H_
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include "utils.h"
struct ReadIndex {
READ_INT_TYPE nReads;
int gap, nPos;
std::streampos *index;
ReadIndex () {
nReads = 0; gap = nPos = 0;
index = NULL;
}
ReadIndex(const char* readF) {
char indexF[STRLEN];
std::ifstream fin;
sprintf(indexF, "%s.ridx", readF);
fin.open(indexF, std::ios::binary);
if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", indexF); exit(-1); }
nReads = 0; gap = nPos = 0;
index = NULL;
if (fin.is_open()) {
fin.read((char*)&nReads, sizeof(nReads));
fin.read((char*)&gap, sizeof(gap));
fin.read((char*)&nPos, sizeof(nPos));
index = new std::streampos[nPos];
for (int i = 0; i < nPos; i++) {
fin.read((char*)&index[i], sizeof(std::streampos));
}
}
}
~ReadIndex() {
nReads = 0; gap = nPos = 0;
if (index != NULL) delete[] index;
}
//rid 0-based , return crid : current seeked rid
READ_INT_TYPE locate(READ_INT_TYPE rid, std::ifstream& out) {
if (index == NULL) {
out.seekg(0, std::ios::beg);
return 0;
}
assert(rid >= 0 && rid < nReads);
out.seekg(index[rid / gap]);
return (rid / gap) * gap;
}
};
#endif /* READINDEX_H_ */