Commit patch to not break on spaces.
[bowtie.git] / ebwt.cpp
1 /*
2  * ebwt.cpp
3  *
4  *  Created on: Sep 23, 2009
5  *      Author: Ben Langmead
6  */
7
8 #include <string>
9 #include <stdexcept>
10 #include <iostream>
11 #include <fstream>
12 #include <stdlib.h>
13
14 using namespace std;
15
16 /**
17  * Try to find the Bowtie index specified by the user.  First try the
18  * exact path given by the user.  Then try the user-provided string
19  * appended onto the path of the "indexes" subdirectory below this
20  * executable, then try the provided string appended onto
21  * "$BOWTIE_INDEXES/".
22  */
23 string adjustEbwtBase(const string& cmdline,
24                                           const string& ebwtFileBase,
25                                           bool verbose = false)
26 {
27         string str = ebwtFileBase;
28         ifstream in;
29         if(verbose) cout << "Trying " << str << endl;
30         in.open((str + ".1.ebwt").c_str(), ios_base::in | ios::binary);
31         if(!in.is_open()) {
32                 if(verbose) cout << "  didn't work" << endl;
33                 in.close();
34                 str = cmdline;
35                 size_t st = str.find_last_of("/\\");
36                 if(st != string::npos) {
37                         str.erase(st);
38                         str += "/indexes/";
39                 } else {
40                         str = "indexes/";
41                 }
42                 str += ebwtFileBase;
43                 if(verbose) cout << "Trying " << str << endl;
44                 in.open((str + ".1.ebwt").c_str(), ios_base::in | ios::binary);
45                 if(!in.is_open()) {
46                         if(verbose) cout << "  didn't work" << endl;
47                         in.close();
48                         if(getenv("BOWTIE_INDEXES") != NULL) {
49                                 str = string(getenv("BOWTIE_INDEXES")) + "/" + ebwtFileBase;
50                                 if(verbose) cout << "Trying " << str << endl;
51                                 in.open((str + ".1.ebwt").c_str(), ios_base::in | ios::binary);
52                                 if(!in.is_open()) {
53                                         if(verbose) cout << "  didn't work" << endl;
54                                         in.close();
55                                 } else {
56                                         if(verbose) cout << "  worked" << endl;
57                                 }
58                         }
59                 }
60         }
61         if(!in.is_open()) {
62                 cerr << "Could not locate a Bowtie index corresponding to basename \"" << ebwtFileBase << "\"" << endl;
63                 throw 1;
64         }
65         return str;
66 }