]> git.donarmstrong.com Git - rsem.git/blob - buildReadIndex.cpp
Added error detection for cases such as a read's two mates having different names...
[rsem.git] / buildReadIndex.cpp
1 #include<cstdio>
2 #include<cstring>
3 #include<cstdlib>
4 #include<string>
5 #include<fstream>
6 #include<iostream>
7
8 #include "utils.h"
9 using namespace std;
10
11 int gap;
12 bool hasQ;
13
14 void buildIndex(char* readF, int gap, bool hasQ) {
15         int nPos;
16         READ_INT_TYPE nReads;
17         bool success;
18         string line;
19         char idxF[STRLEN];
20         char buf[sizeof(nReads) + sizeof(gap) + sizeof(nPos)];
21         streampos startPos;
22
23         sprintf(idxF, "%s.ridx", readF);
24
25         ifstream fin(readF);
26         if (!fin.is_open()) { fprintf(stderr, "Cannot open %s! It may not exist.\n", readF); exit(-1); }
27         ofstream fout(idxF, ios::binary);
28
29         startPos = fout.tellp();
30         memset(buf, 0, sizeof(buf));
31         fout.write((char*)buf, sizeof(buf));
32
33         nReads = 0; nPos = 0;
34         do {
35                 streampos pos = fin.tellg();
36                 success = true;
37
38                 success = (getline(fin, line));
39                 if (!success) continue;
40                 success = (getline(fin, line));
41                 if (!success) continue;
42
43                 if (hasQ) {
44                         success = (getline(fin, line));
45                         if (!success) continue;
46                         success = (getline(fin, line));
47                         if (!success) continue;
48                 }
49
50                 if (nReads % gap == 0) {
51                         ++nPos;
52                         fout.write((char*)&pos, sizeof(pos));
53                 }
54                 ++nReads;
55
56                 if (verbose && nReads % 1000000 == 0) { cout<< "FIN "<< nReads<< endl; }
57         } while (success);
58
59         fout.seekp(startPos);
60         fout.write((char*)&nReads, sizeof(nReads));
61         fout.write((char*)&gap, sizeof(gap));
62         fout.write((char*)&nPos, sizeof(nPos));
63
64         fin.close();
65         fout.close();
66
67         if (verbose) { cout<< "Build Index "<< readF<< " is Done!"<< endl; }
68 }
69
70 int main(int argc, char* argv[]) {
71         if (argc < 5) {
72                 printf("Usage : rsem-build-read-index gap hasQ quiet readFile1, readFile2, ...\n");
73                 exit(-1);
74         }
75
76         gap = atoi(argv[1]);
77         hasQ = atoi(argv[2]);
78         verbose = !atoi(argv[3]);
79         for (int i = 4; i < argc; i++) {
80                 buildIndex(argv[i], gap, hasQ);
81         }
82
83         return 0;
84 }