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 60 61 62 63 64
|
/*
* refmap.cpp
*
* Created on: Aug 3, 2009
* Author: Ben Langmead
*/
#include <stdexcept>
#include "refmap.h"
#include "assert_helpers.h"
using namespace std;
/**
* Given a refid,offset pair in the index space, transform it into the
* reference coordinate space according to the reference mappings
* provided by the user.
*/
void ReferenceMap::map(UPair& h) const {
if(h.first >= map_.size()) {
cerr << "Could not find a reference-map entry for reference "
<< h.first << " in map file \"" << fname_ << "\""
<< endl;
throw 1;
}
h.second += map_[h.first].second;
h.first = map_[h.first].first;
}
/**
* Parse a reference-map file.
*/
void ReferenceMap::parse() {
ifstream in(fname_);
if(!in.good() || !in.is_open()) {
cerr << "Could not open reference map file " << fname_ << endl;
throw 1;
}
int c;
while((c = in.peek()) != EOF) {
if(c == '>') {
// This appears to be a name line
in.get(); // chop off the initial '>'
TIndexOffU off;
in >> off;
in.get(); // chop off tab
char buf[1024];
in.getline(buf, 1023);
if(parseNames_) {
if(names_.size() <= off) names_.resize(off+1);
names_[off] = string(buf);
}
continue;
}
TIndexOffU id, off;
in >> id >> off;
map_.resize(map_.size()+1);
map_.back().first = id;
map_.back().second = off;
while(isspace(in.peek())) in.get();
}
assert_eq(EOF, c);
in.close();
}
|