forked from deltadev/bbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromosome_tree.cc
86 lines (63 loc) · 2.31 KB
/
chromosome_tree.cc
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "chromosome_tree.h"
#include <ostream>
#include <iomanip>
#include <vector>
//iterator for id_hash, used in print()
typedef std::map<std::string, bp_tree::leaf_node>::iterator id_hashIterator;
void chromosome_tree::print(std::ostream& os){
for(id_hashIterator iterator = id_hash.begin(); iterator != id_hash.end(); ++iterator)
{
const std::string& s = iterator->first;
os << "(" << s << ", " << chrom_id(s) << ", " << chrom_size(s) << ")\n";
}
}
uint32_t chromosome_tree::chrom_id(std::string chrom_name) {
id_hashIterator it = id_hash.find(chrom_name);
if(it == id_hash.end())
throw std::runtime_error("chrom_id: chrom-name not found in index");
uint32_t output = it->second.chrom_id;
return it->second.chrom_id;
}
uint32_t chromosome_tree::chrom_size(std::string chrom_name) {
id_hashIterator it = id_hash.find(chrom_name);
if (it == id_hash.end())
throw std::runtime_error("chrom_size: chrom-name not found in index");
return it->second.chrom_size;
}
void chromosome_tree::make_in_memory_hash(std::istream& is) {
header.unpack(is);
bp_tree::header_node n_hdr;
n_hdr.unpack(is);
recursive_fill_map(n_hdr, is);
}
void chromosome_tree::recursive_fill_map(bp_tree::header_node n_h, std::istream& is) {
if (n_h.is_leaf == false) {
// Drops down a level for each node header pointed to by this
// internal_node. Probably never happens since the number of chromosomes is small.
//
for (int i = 0; i < n_h.count; ++i) {
bp_tree::internal_node in_node;
in_node.unpack(is, header.key_size);
// This is the next internal_node position, out sibling, so save pos for next
// time through the loop.
//
std::streamsize curr_off = is.tellg();
// Now go down a level.
//
is.seekg(in_node.child_offset);
bp_tree::header_node n_h_n;
n_h_n.unpack(is); //n_h_n was not getting initialized.
recursive_fill_map(n_h_n, is);
// We're done below so seek back to our sibling's position.
is.seekg(curr_off);
}
} else {
// We're on the leaf level so we can just fill up the hash map.
//
for (int i = 0; i < n_h.count; ++i) {
bp_tree::leaf_node ln;
ln.unpack(is, header.key_size);
id_hash[ln.key] = ln;
}
}
}