Debian Codegraph @

src/C++/Quiver/MutationEnumerator.cpp

raw
1 // Author: David Alexander
2
3 #include <ConsensusCore/Mutation.hpp>
4 #include <ConsensusCore/Quiver/MutationEnumerator.hpp>
5 #include <ConsensusCore/Types.hpp>
6 #include <ConsensusCore/Utils.hpp>
7
8 #include <boost/range/as_array.hpp>
9 #include <boost/tuple/tuple.hpp>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 namespace ConsensusCore {
15 namespace { // PRIVATE
16 const char BASES[] = {'A', 'C', 'G', 'T'};
17
18 inline int BoundPosition(const std::string& tpl, int pos)
19 {
20 return pos < 0 ? 0 : (pos > static_cast<int>(tpl.length()) ? tpl.length() : pos);
21 }
22
23 inline std::pair<int, int> BoundInterval(const std::string& tpl, int beginPos, int endPos)
24 {
25 return std::make_pair(BoundPosition(tpl, beginPos), BoundPosition(tpl, endPos));
26 }
27 } // PRIVATE
28
29 namespace detail {
30
31 AbstractMutationEnumerator::AbstractMutationEnumerator(const std::string& tpl) : tpl_(tpl) {}
32
33 AbstractMutationEnumerator::~AbstractMutationEnumerator() {}
34 } // detail
35
36 AllSingleBaseMutationEnumerator::AllSingleBaseMutationEnumerator(const std::string& tpl)
37 : detail::AbstractMutationEnumerator(tpl)
38 {
39 }
40
41 std::vector<Mutation> AllSingleBaseMutationEnumerator::Mutations() const
42 {
43 return Mutations(0, tpl_.length());
44 }
45
46 std::vector<Mutation> AllSingleBaseMutationEnumerator::Mutations(int beginPos, int endPos) const
47 {
48 std::vector<Mutation> result;
49 boost::tie(beginPos, endPos) = BoundInterval(tpl_, beginPos, endPos);
50 for (int pos = beginPos; pos < endPos; pos++) {
51 foreach (char base, boost::as_array(BASES)) {
52 if (base != tpl_[pos]) {
53 result.push_back(Mutation(SUBSTITUTION, pos, base));
54 }
55 }
56 foreach (char base, boost::as_array(BASES)) {
57 result.push_back(Mutation(INSERTION, pos, base));
58 }
59 result.push_back(Mutation(DELETION, pos, '-'));
60 }
61 return result;
62 }
63
64 UniqueSingleBaseMutationEnumerator::UniqueSingleBaseMutationEnumerator(const std::string& tpl)
65 : detail::AbstractMutationEnumerator(tpl)
66 {
67 }
68
69 std::vector<Mutation> UniqueSingleBaseMutationEnumerator::Mutations() const
70 {
71 return Mutations(0, tpl_.length());
72 }
73
74 std::vector<Mutation> UniqueSingleBaseMutationEnumerator::Mutations(int beginPos, int endPos) const
75 {
76 std::vector<Mutation> result;
77 boost::tie(beginPos, endPos) = BoundInterval(tpl_, beginPos, endPos);
78 for (int pos = beginPos; pos < endPos; pos++) {
79 char prevTplBase = pos > 0 ? tpl_[pos - 1] : '-';
80 foreach (char base, boost::as_array(BASES)) {
81 if (base != tpl_[pos]) {
82 result.push_back(Mutation(SUBSTITUTION, pos, base));
83 }
84 }
85 // Insertions only allowed at the beginning of homopolymers
86 foreach (char base, boost::as_array(BASES)) {
87 if (base != prevTplBase) {
88 result.push_back(Mutation(INSERTION, pos, base));
89 }
90 }
91 // Deletions only allowed at the beginning of homopolymers
92 if (tpl_[pos] != prevTplBase) {
93 result.push_back(Mutation(DELETION, pos, '-'));
94 }
95 }
96 return result;
97 }
98
99 DinucleotideRepeatMutationEnumerator::DinucleotideRepeatMutationEnumerator(
100 const std::string& tpl, int minDinucRepeatElements)
101 : detail::AbstractMutationEnumerator(tpl), minDinucRepeatElements_(minDinucRepeatElements)
102 {
103 }
104
105 std::vector<Mutation> DinucleotideRepeatMutationEnumerator::Mutations() const
106 {
107 return Mutations(0, tpl_.length());
108 }
109
110 std::vector<Mutation> DinucleotideRepeatMutationEnumerator::Mutations(int beginPos,
111 int endPos) const
112 {
113 std::vector<Mutation> result;
114
115 if (minDinucRepeatElements_ <= 0) return result;
116
117 //
118 // Consider all dinucleotide repeats that _start_ in the window
119 // and don't increment pos here, that happens later
120 //
121 boost::tie(beginPos, endPos) = BoundInterval(tpl_, beginPos, endPos);
122 for (int pos = beginPos; pos + 1 < endPos;) {
123 char x = tpl_[pos];
124 char y = tpl_[pos + 1];
125 int numElements = 1;
126
127 for (int i = pos + 2; i + 1 < static_cast<int>(tpl_.length()); i += 2) {
128 //
129 // if we're beyond the window and have enough elements, stop
130 //
131 if (numElements >= minDinucRepeatElements_ && i >= endPos)
132 break;
133
134 else if (tpl_[i] == x && tpl_[i + 1] == y)
135 numElements++;
136 else
137 break;
138 }
139
140 if (numElements >= minDinucRepeatElements_) {
141 std::string dinuc;
142 dinuc.push_back(x);
143 dinuc.push_back(y);
144 result.push_back(Mutation(INSERTION, pos, pos, dinuc));
145 result.push_back(Mutation(DELETION, pos, pos + 2, std::string()));
146 }
147
148 //
149 // Increment by the number of repeats found (if > 1),
150 // less 1 so that we resume on the last base of the repeat,
151 // otherwise just move ahead by 1
152 //
153 if (numElements > 1)
154 pos += 2 * numElements - 1;
155 else
156 pos++;
157 }
158
159 return result;
160 }
161 }