44 #define ALIGNMENT_DEBUG 45 #undef ALIGNMENT_DEBUG 85 template <
typename SpectrumType1,
typename SpectrumType2>
86 void getSpectrumAlignment(std::vector<std::pair<Size, Size> > & alignment,
const SpectrumType1 & s1,
const SpectrumType2 & s2)
const 88 if (!s1.isSorted() || !s2.isSorted())
95 double tolerance = (
double)param_.getValue(
"tolerance");
97 if (!param_.getValue(
"is_relative_tolerance").toBool() )
99 std::map<Size, std::map<Size, std::pair<Size, Size> > > traceback;
100 std::map<Size, std::map<Size, double> > matrix;
104 for (
Size i = 1; i <= s1.size(); ++i)
106 matrix[i][0] = i * tolerance;
107 traceback[i][0] = std::make_pair(i - 1, 0);
109 for (
Size j = 1; j <= s2.size(); ++j)
111 matrix[0][j] = j * tolerance;
112 traceback[0][j] = std::make_pair(0, j - 1);
117 Size last_i(0), last_j(0);
120 for (
Size i = 1; i <= s1.size(); ++i)
122 double pos1(s1[i - 1].getMZ());
124 for (
Size j = left_ptr; j <= s2.size(); ++j)
126 bool off_band(
false);
128 double pos2(s2[j - 1].getMZ());
129 double diff_align = fabs(pos1 - pos2);
132 if (pos2 > pos1 && diff_align > tolerance)
134 if (i < s1.size() && j < s2.size() && s1[i].getMZ() < pos2)
141 if (pos1 > pos2 && diff_align > tolerance && j > left_ptr + 1)
146 double score_align = diff_align;
148 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j - 1) != matrix[i - 1].end())
150 score_align += matrix[i - 1][j - 1];
154 score_align += (i - 1 + j - 1) * tolerance;
157 double score_up = tolerance;
158 if (matrix.find(i) != matrix.end() && matrix[i].find(j - 1) != matrix[i].end())
160 score_up += matrix[i][j - 1];
164 score_up += (i + j - 1) * tolerance;
167 double score_left = tolerance;
168 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j) != matrix[i - 1].end())
170 score_left += matrix[i - 1][j];
174 score_left += (i - 1 + j) * tolerance;
177 #ifdef ALIGNMENT_DEBUG 178 cerr << i <<
" " << j <<
" " << left_ptr <<
" " << pos1 <<
" " << pos2 <<
" " << score_align <<
" " << score_left <<
" " << score_up << endl;
181 if (score_align <= score_up && score_align <= score_left && diff_align <= tolerance)
183 matrix[i][j] = score_align;
184 traceback[i][j] = std::make_pair(i - 1, j - 1);
190 if (score_up <= score_left)
192 matrix[i][j] = score_up;
193 traceback[i][j] = std::make_pair(i, j - 1);
197 matrix[i][j] = score_left;
198 traceback[i][j] = std::make_pair(i - 1, j);
214 #ifdef ALIGNMENT_DEBUG 216 cerr <<
"TheMatrix: " << endl <<
" \t \t";
217 for (
Size j = 0; j != s2.size(); ++j)
219 cerr << s2[j].getPosition()[0] <<
" \t";
222 for (
Size i = 0; i <= s1.size(); ++i)
226 cerr << s1[i - 1].getPosition()[0] <<
" \t";
232 for (
Size j = 0; j <= s2.size(); ++j)
234 if (matrix.has(i) && matrix[i].has(j))
236 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
242 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j)
252 cerr << matrix[i][j] <<
" \t";
268 while (i >= 1 && j >= 1)
270 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
272 alignment.push_back(std::make_pair(i - 1, j - 1));
274 Size new_i = traceback[i][j].first;
275 Size new_j = traceback[i][j].second;
281 std::reverse(alignment.begin(), alignment.end());
283 #ifdef ALIGNMENT_DEBUG 286 cerr <<
"Alignment (size=" << alignment.size() <<
"): " << endl;
288 Size i_s1(0), i_s2(0);
289 for (vector<pair<Size, Size> >::const_reverse_iterator it = alignment.rbegin(); it != alignment.rend(); ++it, ++i_s1, ++i_s2)
291 while (i_s1 < it->first - 1)
293 cerr << i_s1 <<
" " << s1[i_s1].getPosition()[0] <<
" " << s1[i_s1].getIntensity() << endl;
296 while (i_s2 < it->second - 1)
298 cerr <<
" \t " << i_s2 <<
" " << s2[i_s2].getPosition()[0] <<
" " << s2[i_s2].getIntensity() << endl;
301 cerr <<
"(" << s1[it->first - 1].getPosition()[0] <<
" <-> " << s2[it->second - 1].getPosition()[0] <<
") (" 302 << it->first <<
"|" << it->second <<
") (" << s1[it->first - 1].getIntensity() <<
"|" << s2[it->second - 1].getIntensity() <<
")" << endl;
309 for (
Size i = 0; i != s1.size(); ++i)
311 const double& theo_mz = s1[i].getMZ();
312 double max_dist_dalton = theo_mz * tolerance * 1e-6;
315 Size j = s2.findNearest(theo_mz);
316 double exp_mz = s2[j].getMZ();
319 if (std::abs(theo_mz - exp_mz) < max_dist_dalton)
321 alignment.push_back(std::make_pair(i, j));
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
A method or algorithm argument contains illegal values.
Definition: Exception.h:648
void getSpectrumAlignment(std::vector< std::pair< Size, Size > > &alignment, const SpectrumType1 &s1, const SpectrumType2 &s2) const
Definition: SpectrumAlignment.h:86
Aligns the peaks of two sorted spectra Method 1: Using a banded (width via 'tolerance' parameter) ali...
Definition: SpectrumAlignment.h:65
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91