45 #define ALIGNMENT_DEBUG
46 #undef ALIGNMENT_DEBUG
87 template <
typename SpectrumType1,
typename SpectrumType2>
88 void getSpectrumAlignment(std::vector<std::pair<Size, Size> >& alignment,
const SpectrumType1& s1,
const SpectrumType2& s2)
const
90 if (!s1.isSorted() || !s2.isSorted())
97 double tolerance = (double)param_.getValue(
"tolerance");
99 if (!param_.getValue(
"is_relative_tolerance").toBool() )
101 std::map<Size, std::map<Size, std::pair<Size, Size> > > traceback;
102 std::map<Size, std::map<Size, double> > matrix;
106 for (
Size i = 1; i <= s1.size(); ++i)
108 matrix[i][0] = i * tolerance;
109 traceback[i][0] = std::make_pair(i - 1, 0);
111 for (
Size j = 1; j <= s2.size(); ++j)
113 matrix[0][j] = j * tolerance;
114 traceback[0][j] = std::make_pair(0, j - 1);
119 Size last_i(0), last_j(0);
122 for (
Size i = 1; i <= s1.size(); ++i)
124 double pos1(s1[i - 1].getMZ());
126 for (
Size j = left_ptr; j <= s2.size(); ++j)
128 bool off_band(
false);
130 double pos2(s2[j - 1].getMZ());
131 double diff_align = fabs(pos1 - pos2);
134 if (pos2 > pos1 && diff_align > tolerance)
136 if (i < s1.size() && j < s2.size() && s1[i].getMZ() < pos2)
143 if (pos1 > pos2 && diff_align > tolerance && j > left_ptr + 1)
148 double score_align = diff_align;
150 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j - 1) != matrix[i - 1].end())
152 score_align += matrix[i - 1][j - 1];
156 score_align += (i - 1 + j - 1) * tolerance;
159 double score_up = tolerance;
160 if (matrix.find(i) != matrix.end() && matrix[i].find(j - 1) != matrix[i].end())
162 score_up += matrix[i][j - 1];
166 score_up += (i + j - 1) * tolerance;
169 double score_left = tolerance;
170 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j) != matrix[i - 1].end())
172 score_left += matrix[i - 1][j];
176 score_left += (i - 1 + j) * tolerance;
179 #ifdef ALIGNMENT_DEBUG
180 cerr << i <<
" " << j <<
" " << left_ptr <<
" " << pos1 <<
" " << pos2 <<
" " << score_align <<
" " << score_left <<
" " << score_up << endl;
183 if (score_align <= score_up && score_align <= score_left && diff_align <= tolerance)
185 matrix[i][j] = score_align;
186 traceback[i][j] = std::make_pair(i - 1, j - 1);
192 if (score_up <= score_left)
194 matrix[i][j] = score_up;
195 traceback[i][j] = std::make_pair(i, j - 1);
199 matrix[i][j] = score_left;
200 traceback[i][j] = std::make_pair(i - 1, j);
216 #ifdef ALIGNMENT_DEBUG
218 cerr <<
"TheMatrix: " << endl <<
" \t \t";
219 for (
Size j = 0; j != s2.size(); ++j)
221 cerr << s2[j].getPosition()[0] <<
" \t";
224 for (
Size i = 0; i <= s1.size(); ++i)
228 cerr << s1[i - 1].getPosition()[0] <<
" \t";
234 for (
Size j = 0; j <= s2.size(); ++j)
236 if (matrix.has(i) && matrix[i].has(j))
238 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
244 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j)
254 cerr << matrix[i][j] <<
" \t";
270 while (i >= 1 && j >= 1)
272 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
274 alignment.push_back(std::make_pair(i - 1, j - 1));
276 Size new_i = traceback[i][j].first;
277 Size new_j = traceback[i][j].second;
283 std::reverse(alignment.begin(), alignment.end());
285 #ifdef ALIGNMENT_DEBUG
288 cerr <<
"Alignment (size=" << alignment.size() <<
"): " << endl;
290 Size i_s1(0), i_s2(0);
291 for (vector<pair<Size, Size> >::const_reverse_iterator it = alignment.rbegin(); it != alignment.rend(); ++it, ++i_s1, ++i_s2)
293 while (i_s1 < it->first - 1)
295 cerr << i_s1 <<
" " << s1[i_s1].getPosition()[0] <<
" " << s1[i_s1].getIntensity() << endl;
298 while (i_s2 < it->second - 1)
300 cerr <<
" \t " << i_s2 <<
" " << s2[i_s2].getPosition()[0] <<
" " << s2[i_s2].getIntensity() << endl;
303 cerr <<
"(" << s1[it->first - 1].getPosition()[0] <<
" <-> " << s2[it->second - 1].getPosition()[0] <<
") ("
304 << it->first <<
"|" << it->second <<
") (" << s1[it->first - 1].getIntensity() <<
"|" << s2[it->second - 1].getIntensity() <<
")" << endl;
313 for (; it != it.
end(); ++it) alignment.emplace_back(it.
refIdx(), it.
tgtIdx());
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
A method or algorithm argument contains illegal values.
Definition: Exception.h:656
For each element in the reference container the closest peak in the target will be searched....
Definition: MatchedIterator.h:72
size_t refIdx() const
index into reference container
Definition: MatchedIterator.h:188
size_t tgtIdx() const
index into target container
Definition: MatchedIterator.h:194
static MatchedIterator end()
the end iterator
Definition: MatchedIterator.h:222
Aligns the peaks of two sorted spectra Method 1: Using a banded (width via 'tolerance' parameter) ali...
Definition: SpectrumAlignment.h:69
void getSpectrumAlignment(std::vector< std::pair< Size, Size > > &alignment, const SpectrumType1 &s1, const SpectrumType2 &s2) const
Definition: SpectrumAlignment.h:88
SpectrumAlignment & operator=(const SpectrumAlignment &source)
assignment operator
SpectrumAlignment(const SpectrumAlignment &source)
copy constructor
SpectrumAlignment()
default constructor
~SpectrumAlignment() override
destructor
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47