Debian Codegraph @

src/align/GlobalLocalAlignment.cpp

raw
1 #include <pbcopper/align/GlobalLocalAlignment.h>
2
3 #include <pbcopper/utility/Ssize.h>
4
5 #include <algorithm>
6
7 namespace PacBio {
8 namespace Align {
9 namespace {
10 /// \brief Traverse the last row of a DP matrix (i.e. representing
11 /// alignments terminating with the last base of the query
12 /// sequence) and return the max score and it's position
13 GlobalLocalResult GlobalLocalLastRowMax(const std::vector<int32_t>& lastRow) noexcept
14 {
15 const auto maxElementIt = std::max_element(std::cbegin(lastRow), std::cend(lastRow));
16 const int32_t maxElementPos = maxElementIt - std::cbegin(lastRow);
17 return {*maxElementIt, maxElementPos};
18 }
19 } // namespace
20
21 GlobalLocalResult GlobalLocalAlign(const std::string& query, const std::string& read,
22 const GlobalLocalParameters& parameters) noexcept
23 {
24 GlobalLocalStorage storage{};
25 return GlobalLocalAlign(query.c_str(), query.size(), read.c_str(), read.size(), parameters,
26 storage);
27 }
28
29 GlobalLocalResult GlobalLocalAlign(const char* const query, const int32_t queryLength,
30 const char* const read, const int32_t readLength,
31 const GlobalLocalParameters& parameters) noexcept
32 {
33 GlobalLocalStorage storage{};
34 return GlobalLocalAlign(query, queryLength, read, readLength, parameters, storage);
35 }
36
37 GlobalLocalResult GlobalLocalAlign(const std::string& query, const std::string& read,
38 const GlobalLocalParameters& parameters,
39 GlobalLocalStorage& storage) noexcept
40 {
41 return GlobalLocalAlign(query.c_str(), query.size(), read.c_str(), read.size(), parameters,
42 storage);
43 }
44
45 GlobalLocalResult GlobalLocalAlign(const char* const query, const int32_t queryLength,
46 const char* const read, const int32_t readLength,
47 const GlobalLocalParameters& parameters,
48 GlobalLocalStorage& storage) noexcept
49 {
50 // The lastRow has m rows for the query and
51 // n column for the read
52 const int32_t m = queryLength + 1;
53 const int32_t n = readLength + 1;
54
55 std::vector<int32_t> lastRow(readLength);
56
57 // We only store two columns as we do not compute the traceback.
58 // Both columns are stored in one contiguous memory block.
59 // Offsets determine where columns start.
60 // Columns are swapped after each outer loop of read bases.
61 if (Utility::Ssize(storage.Columns) < m * 2) {
62 storage.Columns.resize(m * 2, 0);
63 }
64 auto* const col = storage.Columns.data();
65 int32_t prevOffset = 0;
66 int32_t curOffset = m;
67
68 // Initialize previous column, after swapping, to zero
69 std::fill_n(col + curOffset, m, 0);
70
71 // Deltas are used to reduce number of instructions in the inner loop
72 const int32_t mismatchDelta = parameters.MatchScore - parameters.MismatchPenalty;
73 const int32_t insertionDelta = parameters.BranchPenalty - parameters.InsertionPenalty;
74 const int32_t deletionDelta = parameters.MergePenalty - parameters.DeletionPenalty;
75
76 // The matrix has i for rows, query bases, and j for columns, read bases.
77 // Outer loop is over read bases und inner loop over query bases.
78 // Insertion and deletions are with respect to the read that is horizontally.
79 for (int32_t j = 1; j < n; ++j) { // rows
80 std::swap(prevOffset, curOffset);
81 const char prevRead = read[j - 1];
82 const int32_t readMismatch = (prevRead != read[j]);
83 for (int32_t i = 1; i < m; ++i) { // cols
84
85 // Match/mismatch
86 const int32_t a{col[i - 1 + prevOffset] + parameters.MatchScore -
87 (mismatchDelta * (prevRead != query[i - 1]))};
88
89 // Insertion in the read or branch if the current and last read sequence are identical
90 const int32_t b{col[i + prevOffset] + parameters.BranchPenalty -
91 (insertionDelta * readMismatch)};
92
93 // Deletion in the read or merge if the current and last read sequence are identical
94 const int32_t c{col[i - 1 + curOffset] + parameters.MergePenalty -
95 (deletionDelta * (query[i - 1] != query[i]))};
96
97 col[i + curOffset] = std::max(a, std::max(b, c));
98 }
99 lastRow[j - 1] = col[curOffset + m - 1];
100 }
101
102 return GlobalLocalLastRowMax(lastRow);
103 }
104
105 } // namespace Align
106 } // namespace PacBio