35 #ifndef OPENMS_COMPARISON_SPECTRA_SPECTRUMALIGNMENT_H 36 #define OPENMS_COMPARISON_SPECTRA_SPECTRUMALIGNMENT_H 45 #define ALIGNMENT_DEBUG 46 #undef ALIGNMENT_DEBUG 86 template <
typename SpectrumType1,
typename SpectrumType2>
87 void getSpectrumAlignment(std::vector<std::pair<Size, Size> > & alignment,
const SpectrumType1 & s1,
const SpectrumType2 & s2)
const 89 if (!s1.isSorted() || !s2.isSorted())
96 double tolerance = (
double)param_.getValue(
"tolerance");
98 if (!param_.getValue(
"is_relative_tolerance").toBool() )
100 std::map<Size, std::map<Size, std::pair<Size, Size> > > traceback;
101 std::map<Size, std::map<Size, double> > matrix;
105 for (
Size i = 1; i <= s1.size(); ++i)
107 matrix[i][0] = i * tolerance;
108 traceback[i][0] = std::make_pair(i - 1, 0);
110 for (
Size j = 1; j <= s2.size(); ++j)
112 matrix[0][j] = j * tolerance;
113 traceback[0][j] = std::make_pair(0, j - 1);
118 Size last_i(0), last_j(0);
121 for (
Size i = 1; i <= s1.size(); ++i)
123 double pos1(s1[i - 1].getMZ());
125 for (
Size j = left_ptr; j <= s2.size(); ++j)
127 bool off_band(
false);
129 double pos2(s2[j - 1].getMZ());
130 double diff_align = fabs(pos1 - pos2);
133 if (pos2 > pos1 && diff_align > tolerance)
135 if (i < s1.size() && j < s2.size() && s1[i].getMZ() < pos2)
142 if (pos1 > pos2 && diff_align > tolerance && j > left_ptr + 1)
147 double score_align = diff_align;
149 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j - 1) != matrix[i - 1].end())
151 score_align += matrix[i - 1][j - 1];
155 score_align += (i - 1 + j - 1) * tolerance;
158 double score_up = tolerance;
159 if (matrix.find(i) != matrix.end() && matrix[i].find(j - 1) != matrix[i].end())
161 score_up += matrix[i][j - 1];
165 score_up += (i + j - 1) * tolerance;
168 double score_left = tolerance;
169 if (matrix.find(i - 1) != matrix.end() && matrix[i - 1].find(j) != matrix[i - 1].end())
171 score_left += matrix[i - 1][j];
175 score_left += (i - 1 + j) * tolerance;
178 #ifdef ALIGNMENT_DEBUG 179 cerr << i <<
" " << j <<
" " << left_ptr <<
" " << pos1 <<
" " << pos2 <<
" " << score_align <<
" " << score_left <<
" " << score_up << endl;
182 if (score_align <= score_up && score_align <= score_left && diff_align <= tolerance)
184 matrix[i][j] = score_align;
185 traceback[i][j] = std::make_pair(i - 1, j - 1);
191 if (score_up <= score_left)
193 matrix[i][j] = score_up;
194 traceback[i][j] = std::make_pair(i, j - 1);
198 matrix[i][j] = score_left;
199 traceback[i][j] = std::make_pair(i - 1, j);
215 #ifdef ALIGNMENT_DEBUG 217 cerr <<
"TheMatrix: " << endl <<
" \t \t";
218 for (
Size j = 0; j != s2.size(); ++j)
220 cerr << s2[j].getPosition()[0] <<
" \t";
223 for (
Size i = 0; i <= s1.size(); ++i)
227 cerr << s1[i - 1].getPosition()[0] <<
" \t";
233 for (
Size j = 0; j <= s2.size(); ++j)
235 if (matrix.has(i) && matrix[i].has(j))
237 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
243 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j)
253 cerr << matrix[i][j] <<
" \t";
269 while (i >= 1 && j >= 1)
271 if (traceback[i][j].first == i - 1 && traceback[i][j].second == j - 1)
273 alignment.push_back(std::make_pair(i - 1, j - 1));
275 Size new_i = traceback[i][j].first;
276 Size new_j = traceback[i][j].second;
282 std::reverse(alignment.begin(), alignment.end());
284 #ifdef ALIGNMENT_DEBUG 287 cerr <<
"Alignment (size=" << alignment.size() <<
"): " << endl;
289 Size i_s1(0), i_s2(0);
290 for (vector<pair<Size, Size> >::const_reverse_iterator it = alignment.rbegin(); it != alignment.rend(); ++it, ++i_s1, ++i_s2)
292 while (i_s1 < it->first - 1)
294 cerr << i_s1 <<
" " << s1[i_s1].getPosition()[0] <<
" " << s1[i_s1].getIntensity() << endl;
297 while (i_s2 < it->second - 1)
299 cerr <<
" \t " << i_s2 <<
" " << s2[i_s2].getPosition()[0] <<
" " << s2[i_s2].getIntensity() << endl;
302 cerr <<
"(" << s1[it->first - 1].getPosition()[0] <<
" <-> " << s2[it->second - 1].getPosition()[0] <<
") (" 303 << it->first <<
"|" << it->second <<
") (" << s1[it->first - 1].getIntensity() <<
"|" << s2[it->second - 1].getIntensity() <<
")" << endl;
310 for (
Size i = 0; i != s1.size(); ++i)
312 const double& theo_mz = s1[i].getMZ();
313 double max_dist_dalton = theo_mz * tolerance * 1e-6;
316 Size j = s2.findNearest(theo_mz);
317 double exp_mz = s2[j].getMZ();
320 if (std::abs(theo_mz - exp_mz) < max_dist_dalton)
322 alignment.push_back(std::make_pair(i, j));
329 #endif //OPENMS_COMPARISON_SPECTRA_SPECTRUMALIGNMENT_H
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
A method or algorithm argument contains illegal values.
Definition: Exception.h:649
void getSpectrumAlignment(std::vector< std::pair< Size, Size > > &alignment, const SpectrumType1 &s1, const SpectrumType2 &s2) const
Definition: SpectrumAlignment.h:87
Aligns the peaks of two sorted spectra Method 1: Using a banded (width via 'tolerance' parameter) ali...
Definition: SpectrumAlignment.h:66
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92