-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeding_strategy_safe.txt
More file actions
executable file
·144 lines (118 loc) · 5.08 KB
/
seeding_strategy_safe.txt
File metadata and controls
executable file
·144 lines (118 loc) · 5.08 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by fritsche on 27/08/22.
//
#pragma once
#include "KmerLookup.h"
namespace protal {
struct AlignmentAnchor {
const LookupResult a;
const LookupResult b;
const size_t hit_anchor_count = 0;
AlignmentAnchor(LookupResult& a, LookupResult &b, size_t hit_anchor_count) :
a(a), b(b), hit_anchor_count(hit_anchor_count) {};
AlignmentAnchor(LookupResult&& a, LookupResult &&b, size_t hit_anchor_count) :
a(a), b(b), hit_anchor_count(hit_anchor_count) {};
};
using Seed = LookupResult;
using SeedList = std::vector<Seed>;
using SeedIterator = SeedList::iterator;
using SeedListSet = std::vector<SeedList>;
using SeedListSetIterators = std::vector<SeedIterator>;
using Anchor = AlignmentAnchor;
// using AnchorList = std::vector<Anchor>;
using AnchorList = std::vector<std::pair<LookupResult, LookupResult>>;
class Seeding {
size_t m_seeding_position_count;
SeedListSet m_seedlist_set;
SeedListSetIterators m_seedlist_set_iterators;
AnchorList m_anchor_list;
public:
Seeding(size_t seeding_position_count) : m_seeding_position_count(seeding_position_count) {
m_seedlist_set.resize(seeding_position_count);
m_seedlist_set_iterators.resize(seeding_position_count);
}
void Add(size_t idx, SeedIterator begin, SeedIterator end) {
assert(idx < m_seedlist_set.size());
m_seedlist_set[idx].clear();
while (begin < end) {
m_seedlist_set[idx].emplace_back(*begin);
begin++;
}
m_seedlist_set_iterators[idx] = m_seedlist_set[idx].begin();
}
bool LoopCondition() const {
size_t not_end_count = 0;
for (auto i = 0; i < m_seedlist_set.size(); i++) {
if (m_seedlist_set_iterators[i] != m_seedlist_set[i].end()) not_end_count++;
if (not_end_count >= 2) return true;
}
return false;
}
[[nodiscard]] int MinAnchorIdx() const {
int current_min_idx = 0;
auto current_min = m_seedlist_set_iterators[current_min_idx];
// Loop over "iterators"
for (int i = 1; i < m_seedlist_set_iterators.size(); i++) {
auto& current_element = m_seedlist_set_iterators[i];
auto current_end = m_seedlist_set[i].end();
if (current_element == current_end) continue;
if (*current_element < *current_min) {
current_min = current_element;
current_min_idx = i;
}
};
return current_min_idx;
}
[[nodiscard]] inline bool IsSmallerThanOtherIndices(int idx) const {
auto& current_iterator = m_seedlist_set_iterators[idx];
return std::all_of(m_seedlist_set_iterators.begin(), m_seedlist_set_iterators.end(), [¤t_iterator](SeedIterator const& it) {
return current_iterator == it || *current_iterator < *it;
});
}
inline bool HasIdenticalPair(int idx) {
auto& current_iterator = m_seedlist_set_iterators[idx];
return std::any_of(m_seedlist_set_iterators.begin(), m_seedlist_set_iterators.end(), [¤t_iterator](SeedIterator const& it) {
return current_iterator != it && *current_iterator == *it;
});
}
inline void AdvanceSeedIterator(int idx) {
assert(idx < m_seedlist_set.size());
auto end = m_seedlist_set[idx].end();
do {
std::advance(m_seedlist_set_iterators[idx], 1);
} while (m_seedlist_set_iterators[idx] != end && IsSmallerThanOtherIndices(idx));
}
inline void SortSeedListSet() {
std::cout << "Before sort" << std::endl;
for (auto& seedlist : m_seedlist_set) {
std::cout << "Sort list" << std::endl;
for (auto& seed : seedlist) {
std::cout << seed.ToString() << std::endl;
}
}
std::cout << "Sort lists" << std::endl;
for (auto& seedlist : m_seedlist_set) {
std::cout << "Sort list" << std::endl;
for (auto& seed : seedlist) {
std::cout << seed.ToString() << std::endl;
}
std::sort(seedlist.begin(), seedlist.end(), [](Seed const& a, Seed const& b) {
return a < b;
});
}
}
AnchorList& GetAnchorList() {
return m_anchor_list;
}
void ComputeSeedPairs() {
m_anchor_list.clear();
while (LoopCondition()) {
auto min_anchor_idx = MinAnchorIdx();
if (HasIdenticalPair(min_anchor_idx)) {
m_anchor_list.emplace_back(std::pair<Seed, Seed>(*(m_seedlist_set_iterators[0]), *(m_seedlist_set_iterators[1])));
}
AdvanceSeedIterator(min_anchor_idx);
}
}
};
}