-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeedGraph.tpp
124 lines (104 loc) · 3.42 KB
/
SeedGraph.tpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//-*-C-*- for emacs
/***** LOCUS *****/
template<class T>
bool SeedGraph<T>::Locus::operator < (const Locus& x) const{
if(read_idx == x.read_idx) return pos < x.pos;
else return read_idx < x.read_idx;
}
template<class T>
bool SeedGraph<T>::Locus::operator == (const Locus& x) const{
return (read_idx == x.read_idx) && (pos == x.pos);
}
/***** NODE *****/
template<class T>
bool SeedGraph<T>::Node::operator < (const Node& x) const{
return key < x.key;
}
template<class T>
bool SeedGraph<T>::Node::operator == (const Node& x) const{
return key == x.key;
}
template<class T>
void SeedGraph<T>::Node::addPath(const size_t read_idx, const size_t cur_pos,
const size_t prev_pos, Node* prev){
this->paths.emplace(std::piecewise_construct,
std::forward_as_tuple(read_idx, cur_pos),
std::forward_as_tuple(prev));
if(prev != nullptr){
auto it = prev->paths.find(Locus(read_idx, prev_pos));
it->second.next = this;
}
}
template<class T>
auto SeedGraph<T>::Node::getPathExact(const size_t read_idx,
const size_t cur_pos) const{
return paths.find(Locus(read_idx, cur_pos));
}
template<class T>
auto SeedGraph<T>::Node::getPathLowerBD(const size_t read_idx,
const size_t pos_lower_bd) const{
auto it = paths.lower_bound(Locus(read_idx, pos_lower_bd));
if(it->first.read_idx == read_idx) return it;
else return paths.end();
}
template<class T> template<class... Args>
std::string SeedGraph<T>::Node::toString(bool long_fmt,
std::string (*decode)(const T&, Args...), Args... args) const{
std::string result = "n" + std::to_string(id)
+ " [label=\"" + decode(key, args...) + "\"];\n";
if(long_fmt){
for(const auto& p : paths){
result += "read " + std::to_string(p.first.read_idx)
+ ", pos " + std::to_string(p.first.pos) + "\nprev: "
+ (p.second.prev? decode(p.second.prev->key, args...) : "--")
+ "\nnext: "
+ (p.second.next? decode(p.second.next->key, args...) : "--")
+ "\n";
}
}
return result;
}
template<class T> template<class... Args>
std::string SeedGraph<T>::Node::toString2(bool long_fmt,
std::string (*decode)(const T&, Args...), Args... args) const{
std::string result = "n" + std::to_string(id2)
+ " [label=\"" + decode(key, args...) + "\"];\n";
if(long_fmt){
for(const auto& p : paths){
result += "read " + std::to_string(p.first.read_idx)
+ ", pos " + std::to_string(p.first.pos) + "\nprev: "
+ (p.second.prev? decode(p.second.prev->key, args...) : "--")
+ "\nnext: "
+ (p.second.next? decode(p.second.next->key, args...) : "--")
+ "\n";
}
}
return result;
}
/***** SEEDGRAPH *****/
template<class T>
size_t SeedGraph<T>::numNodes() const{
return nodes.size();
}
template<class T>
typename SeedGraph<T>::Node* SeedGraph<T>::getNode(T key) const{
auto it = nodes.find(key);
if(it != nodes.end()) return &(it->second);
else return nullptr;
}
template<class T>
typename SeedGraph<T>::Node* SeedGraph<T>::addNode(T key){
auto it = nodes.lower_bound(key);
if(it == nodes.end() || it->first != key){//key does not exist in map
size_t id = nodes.size();
it = nodes.emplace_hint(it, key, Node(id, key));
}
return &(it->second);
}
template<class T> template<class... Args>
void SeedGraph<T>::printNodesInDot(std::ofstream& fout,
std::string (*decode)(const T&, Args...), Args... args) const{
for(const auto& it : nodes){
fout << it.second.toString(false, decode, args...);
}
}