Debian Codegraph @

src/poa/SparsePoa.cpp

raw
1 #include <pbcopper/poa/SparsePoa.h>
2
3 #include <pbcopper/align/AlignConfig.h>
4 #include <pbcopper/align/ChainSeeds.h>
5 #include <pbcopper/align/ChainSeedsConfig.h>
6 #include <pbcopper/align/FindSeeds.h>
7 #include <pbcopper/align/Seed.h>
8 #include <pbcopper/align/Seeds.h>
9 #include <pbcopper/align/SparseAlignment.h>
10 #include <pbcopper/logging/Logging.h>
11 #include <pbcopper/poa/PoaGraph.h>
12 #include <pbcopper/utility/SequenceUtils.h>
13
14 #include <algorithm>
15 #include <iostream>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 #include <cstddef>
21
22 namespace PacBio {
23 namespace Poa {
24 namespace {
25 std::vector<Align::Seed> SparseAlignSeedsUnySpecific(const size_t qGramSize,
26 const std::string& seq1,
27 const std::string& seq2)
28 {
29 const auto config = Align::ChainSeedsConfig{1, 1, 3, -1, -1, -1, INT_MAX};
30 const auto seeds = Align::FindSeeds(qGramSize, seq1, seq2, true);
31 const auto chains = Align::ChainSeeds(seeds, config);
32 if (chains.empty()) {
33 return std::vector<Align::Seed>{};
34 }
35 return chains[0];
36 }
37
38 std::vector<std::pair<size_t, size_t>> SparseAlignUnySpecific(const size_t qGramSize,
39 const std::string& seq1,
40 const std::string& seq2)
41 {
42 std::vector<std::pair<size_t, size_t>> result;
43 const auto chain = SparseAlignSeedsUnySpecific(qGramSize, seq1, seq2);
44 for (const auto& s : chain) {
45 result.emplace_back(s.BeginPositionH(), s.BeginPositionV());
46 }
47 return result;
48 }
49 } // namespace
50
51 PoaAlignmentOptions::PoaAlignmentOptions()
52 : alignConfig(Poa::DefaultPoaConfig(Align::AlignMode::LOCAL)), minScoreToAdd(0.0f)
53 {}
54
55 PoaAlignmentOptions::PoaAlignmentOptions(Align::AlignMode alignMode, float newMinScoreToAdd)
56 : alignConfig(Poa::DefaultPoaConfig(alignMode)), minScoreToAdd(newMinScoreToAdd)
57 {}
58
59 PoaAlignmentOptions::PoaAlignmentOptions(const Align::AlignConfig& newAlignConfig,
60 float newMinScoreToAdd)
61 : alignConfig(newAlignConfig), minScoreToAdd(newMinScoreToAdd)
62 {}
63
64 detail::SdpAnchorVector SdpRangeFinder::FindAnchors(const std::string& consensusSequence,
65 const std::string& readSequence) const
66 {
67 return SparseAlignUnySpecific(6, consensusSequence, readSequence);
68 }
69
70 SparsePoa::SparsePoa()
71 : graph_(std::make_unique<PoaGraph>())
72 , readPaths_()
73 , reverseComplemented_()
74 , rangeFinder_(std::make_unique<SdpRangeFinder>())
75 {}
76
77 SparsePoa::~SparsePoa() = default;
78
79 SparsePoa::ReadKey SparsePoa::AddRead(const std::string& readSequence,
80 const PoaAlignmentOptions& alnOptions)
81 {
82 Path outputPath;
83 ReadKey key = -1;
84
85 if (graph_->NumReads() == 0) {
86 graph_->AddFirstRead(readSequence, &outputPath);
87 readPaths_.push_back(outputPath);
88 reverseComplemented_.push_back(false);
89 key = graph_->NumReads() - 1;
90 } else {
91 std::unique_ptr<Poa::PoaAlignmentMatrix> c{
92 graph_->TryAddRead(readSequence, alnOptions.alignConfig, rangeFinder_.get())};
93
94 if (c->Score() >= alnOptions.minScoreToAdd) {
95 graph_->CommitAdd(c.get(), &outputPath);
96 readPaths_.push_back(outputPath);
97 reverseComplemented_.push_back(false);
98 key = graph_->NumReads() - 1;
99 }
100 }
101
102 return key;
103 }
104
105 namespace TEST {
106
107 char Complement(const char base)
108 {
109 constexpr std::array<char, 256> LOOKUP_TABLE{
110 {/* 0 - 7: */ 0, 0, 0, 0, 0, 0, 0, 0,
111 /* 8 - 15: */ 0, 0, 0, 0, 0, 0, 0, 0,
112 /* 16 - 23: */ 0, 0, 0, 0, 0, 0, 0, 0,
113 /* 24 - 31: */ 0, 0, 0, 0, 0, 0, 0, 0,
114 /* 32 - 39: */ 0, 0, 0, 0, 0, 0, 0, 0,
115 /* 40 - 47: */ 0, 0, 0, 0, 0, '-', 0, 0,
116 /* 48 - 55: */ 0, 0, 0, 0, 0, 0, 0, 0,
117 /* 56 - 63: */ 0, 0, 0, 0, 0, 0, 0, 0,
118
119 /* 64 - 71: */ 0, 'T', 'V', 'G', 'H', 0, 0, 'C',
120 /* 72 - 79: */ 'D', 0, 0, 'M', 0, 'K', 'N', 0,
121 /* 80 - 87: */ 0, 0, 'Y', 'S', 'A', 0, 'B', 'W',
122 /* 88 - 95: */ 0, 'R', 0, 0, 0, 0, 0, 0,
123
124 /* 96 - 103: */ 0, 't', 'v', 'g', 'h', 0, 0, 'c',
125 /* 104 - 111: */ 'd', 0, 0, 'm', 0, 'k', 'n', 0,
126 /* 112 - 119: */ 0, 0, 'y', 's', 'a', 0, 'b', 'w',
127 /* 120 - 127: */ 0, 'r', 0, 0, 0, 0, 0, 0,
128
129 /* 128 - 135: */ 0, 0, 0, 0, 0, 0, 0, 0,
130 /* 136 - 143: */ 0, 0, 0, 0, 0, 0, 0, 0,
131 /* 144 - 151: */ 0, 0, 0, 0, 0, 0, 0, 0,
132 /* 152 - 159: */ 0, 0, 0, 0, 0, 0, 0, 0,
133 /* 160 - 167: */ 0, 0, 0, 0, 0, 0, 0, 0,
134 /* 168 - 175: */ 0, 0, 0, 0, 0, 0, 0, 0,
135 /* 176 - 183: */ 0, 0, 0, 0, 0, 0, 0, 0,
136 /* 184 - 191: */ 0, 0, 0, 0, 0, 0, 0, 0,
137 /* 192 - 199: */ 0, 0, 0, 0, 0, 0, 0, 0,
138 /* 200 - 207: */ 0, 0, 0, 0, 0, 0, 0, 0,
139 /* 208 - 215: */ 0, 0, 0, 0, 0, 0, 0, 0,
140 /* 216 - 223: */ 0, 0, 0, 0, 0, 0, 0, 0,
141 /* 224 - 231: */ 0, 0, 0, 0, 0, 0, 0, 0,
142 /* 232 - 239: */ 0, 0, 0, 0, 0, 0, 0, 0,
143 /* 240 - 247: */ 0, 0, 0, 0, 0, 0, 0, 0,
144 /* 248 - 255: */ 0, 0, 0, 0, 0, 0, 0, 0}};
145
146 const char result = LOOKUP_TABLE[static_cast<unsigned char>(base)];
147
148 if (result == 0) {
149 throw std::invalid_argument(base + std::string{" is an invalid base!"});
150 }
151
152 return result;
153 }
154
155 std::string Complement(const std::string& input)
156 {
157 std::string output;
158 output.reserve(input.length());
159 for (const char b : input) {
160 output.push_back(Complement(b));
161 }
162 return output;
163 }
164
165 std::string Reverse(const std::string& input)
166 {
167 std::string output;
168 output.reserve(input.length());
169 for (auto it = input.crbegin(); it != input.crend(); ++it) {
170 output.push_back(*it);
171 }
172 return output;
173 }
174
175 std::string ReverseComplement(const std::string& input)
176 {
177 std::string output;
178 output.reserve(input.length());
179 for (auto it = input.crbegin(); it != input.crend(); ++it) {
180 output.push_back(Complement(*it));
181 }
182 return output;
183 }
184 } // namespace TEST
185
186 SparsePoa::ReadKey SparsePoa::OrientAndAddRead(const std::string& readSequence,
187 const PoaAlignmentOptions& alnOptions)
188 {
189 Path outputPath;
190 ReadKey key;
191
192 if (graph_->NumReads() == 0) {
193 graph_->AddFirstRead(readSequence, &outputPath);
194 readPaths_.push_back(outputPath);
195 reverseComplemented_.push_back(false);
196 key = graph_->NumReads() - 1;
197 } else {
198 std::unique_ptr<Poa::PoaAlignmentMatrix> c1{
199 graph_->TryAddRead(readSequence, alnOptions.alignConfig, rangeFinder_.get())};
200 std::unique_ptr<Poa::PoaAlignmentMatrix> c2{graph_->TryAddRead(
201 TEST::ReverseComplement(readSequence), alnOptions.alignConfig, rangeFinder_.get())};
202
203 if (c1->Score() >= c2->Score() && c1->Score() >= alnOptions.minScoreToAdd) {
204 graph_->CommitAdd(c1.get(), &outputPath);
205 readPaths_.push_back(outputPath);
206 reverseComplemented_.push_back(false);
207 key = graph_->NumReads() - 1;
208 } else if (c2->Score() >= c1->Score() && c2->Score() >= alnOptions.minScoreToAdd) {
209 graph_->CommitAdd(c2.get(), &outputPath);
210 readPaths_.push_back(outputPath);
211 reverseComplemented_.push_back(true);
212 key = graph_->NumReads() - 1;
213 } else {
214 key = -1;
215 }
216 }
217 return key;
218 }
219
220 std::shared_ptr<const PoaConsensus> SparsePoa::FindConsensus(
221 int minCoverage, std::vector<PoaAlignmentSummary>* summaries) const
222 {
223 Align::AlignConfig config = DefaultPoaConfig(Align::AlignMode::LOCAL);
224 return FindConsensus(minCoverage, summaries, config);
225 }
226
227 std::shared_ptr<const PoaConsensus> SparsePoa::FindConsensus(
228 int minCoverage, std::vector<PoaAlignmentSummary>* summaries,
229 const Align::AlignConfig& config) const
230 {
231 std::shared_ptr<const PoaConsensus> pc(graph_->FindConsensus(config, minCoverage));
232
233 if (summaries != nullptr) {
234 summaries->clear();
235
236 // digest the consensus path consensus into map(vtx, pos)
237 // the fold over the readPaths
238 std::map<PoaGraph::Vertex, size_t> cssPosition;
239
240 int i = 0;
241 for (PoaGraph::Vertex v : pc->Path) {
242 cssPosition[v] = i;
243 i++;
244 }
245
246 for (size_t readId = 0; readId < graph_->NumReads(); readId++) {
247 size_t readS = 0;
248 size_t readE = 0;
249 size_t cssS = 0;
250 size_t cssE = 0;
251 bool foundStart = false;
252 size_t nErr = 0;
253
254 const std::vector<PoaGraph::Vertex>& readPath = readPaths_[readId];
255
256 for (size_t readPos = 0; readPos < readPath.size(); readPos++) {
257 PoaGraph::Vertex v = readPath[readPos];
258 if (cssPosition.find(v) != cssPosition.end()) {
259 if (!foundStart) {
260 cssS = cssPosition[v];
261 readS = readPos;
262 foundStart = true;
263 }
264
265 cssE = cssPosition[v] + 1;
266 readE = readPos + 1;
267 } else {
268 nErr += 1;
269 }
270 }
271
272 Interval readExtent(readS, readE);
273 Interval cssExtent(cssS, cssE);
274
275 PoaAlignmentSummary summary;
276 summary.ReverseComplementedRead = reverseComplemented_[readId];
277 summary.ExtentOnRead = readExtent;
278 summary.ExtentOnConsensus = cssExtent;
279 summary.AlignmentIdentity = std::max(0.0F, 1.0F - 1.0F * nErr / cssPosition.size());
280
281 (*summaries).push_back(summary);
282 }
283 }
284
285 return pc;
286 }
287
288 std::string SparsePoa::ToGraphViz(int flags, const PoaConsensus* pc) const
289 {
290 return graph_->ToGraphViz(flags, pc);
291 }
292
293 void SparsePoa::WriteGraphVizFile(const std::filesystem::path& filename, int flags,
294 const PoaConsensus* pc) const
295 {
296 graph_->WriteGraphVizFile(filename, flags, pc);
297 }
298
299 void SparsePoa::WriteGraphCsvFile(const std::filesystem::path& filename) const
300 {
301 graph_->WriteGraphCsvFile(filename);
302 }
303
304 void SparsePoa::PruneGraph(const int minCoverage) { graph_->PruneGraph(minCoverage); }
305
306 void SparsePoa::repCheck()
307 {
308 assert(graph_->NumReads() == readPaths_.size());
309 assert(graph_->NumReads() == reverseComplemented_.size());
310 }
311
312 } // namespace Poa
313 } // namespace PacBio