Imported Debian patch 0.12.7-3
[bowtie.git] / refmap.cpp
1 /*
2  * refmap.cpp
3  *
4  *  Created on: Aug 3, 2009
5  *      Author: Ben Langmead
6  */
7
8 #include <stdexcept>
9 #include "refmap.h"
10 #include "assert_helpers.h"
11
12 using namespace std;
13
14 /**
15  * Given a refid,offset pair in the index space, transform it into the
16  * reference coordinate space according to the reference mappings
17  * provided by the user.
18  */
19 void ReferenceMap::map(U32Pair& h) const {
20         if(h.first >= map_.size()) {
21                 cerr << "Could not find a reference-map entry for reference "
22                                   << h.first << " in map file \"" << fname_ << "\""
23                                   << endl;
24                 throw 1;
25         }
26         h.second += map_[h.first].second;
27         h.first = map_[h.first].first;
28 }
29
30 /**
31  * Parse a reference-map file.
32  */
33 void ReferenceMap::parse() {
34         ifstream in(fname_);
35         if(!in.good() || !in.is_open()) {
36                 cerr << "Could not open reference map file " << fname_ << endl;
37                 throw 1;
38         }
39         int c;
40         while((c = in.peek()) != EOF) {
41                 if(c == '>') {
42                         // This appears to be a name line
43                         in.get(); // chop off the initial '>'
44                         uint32_t off;
45                         in >> off;
46                         in.get(); // chop off tab
47                         char buf[1024];
48                         in.getline(buf, 1023);
49                         if(parseNames_) {
50                                 if(names_.size() <= off) names_.resize(off+1);
51                                 names_[off] = string(buf);
52                         }
53                         continue;
54                 }
55                 uint32_t id, off;
56                 in >> id >> off;
57                 map_.resize(map_.size()+1);
58                 map_.back().first = id;
59                 map_.back().second = off;
60                 while(isspace(in.peek())) in.get();
61         }
62         assert_eq(EOF, c);
63         in.close();
64 }