00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00028 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00029
00030 #include <OpenMS/KERNEL/MSExperiment.h>
00031 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00032 #include <OpenMS/CONCEPT/ProgressLogger.h>
00033
00034 #include <limits>
00035 #include <cmath>
00036
00037 namespace OpenMS
00038 {
00053 class LinearResampler
00054 : public DefaultParamHandler,
00055 public ProgressLogger
00056 {
00057
00058 public:
00059
00061 LinearResampler()
00062 : DefaultParamHandler("LinearResampler")
00063 {
00064 defaults_.setValue("spacing",0.05,"Spacing of the resampled output peaks.");
00065 defaultsToParam_();
00066 }
00067
00069 ~LinearResampler()
00070 {
00071 }
00072
00085 template < typename InputPeakIterator, typename OutputPeakContainer >
00086 void raster(InputPeakIterator first, InputPeakIterator last, OutputPeakContainer& resampled_peak_container)
00087 {
00088 double end_pos = (last-1)->getMZ();
00089 double start_pos = first->getMZ();
00090 int number_raw_points = distance(first,last);
00091 int number_resampled_points = (int)(ceil((end_pos -start_pos) / spacing_ + 1));
00092
00093 resampled_peak_container.resize(number_resampled_points);
00094
00095
00096 typename OutputPeakContainer::iterator it = resampled_peak_container.begin();
00097 for (int i=0; i < number_resampled_points; ++i)
00098 {
00099 it->setMZ( start_pos + i*spacing_);
00100 ++it;
00101 }
00102
00103
00104
00105 double distance_left = 0.;
00106 double distance_right = 0.;
00107 int left_index = 0;
00108 int right_index = 0;
00109
00110 it = resampled_peak_container.begin();
00111 for (int i=0; i < number_raw_points ; ++i)
00112 {
00113 left_index = (int)floor(((first+i)->getMZ() - start_pos) / spacing_);
00114
00115 right_index = left_index + 1;
00116
00117
00118
00119
00120
00121
00122
00123 distance_left = fabs((first+i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
00124
00125
00126 distance_right = fabs((first+i)->getMZ() - (it + right_index)->getMZ());
00127
00128
00129
00130 DoubleReal intensity = (it + left_index)->getIntensity();
00131 intensity += (first+i)->getIntensity()*distance_right / spacing_;
00132 (it + left_index)->setIntensity(intensity);
00133 intensity = (it + right_index)->getIntensity();
00134 intensity += (first+i)->getIntensity()*distance_left;
00135 (it + right_index)->setIntensity(intensity);
00136 }
00137 }
00138
00139
00152 template <typename InputPeakContainer, typename OutputPeakContainer >
00153 void raster(const InputPeakContainer& input_peak_container, OutputPeakContainer& baseline_filtered_container)
00154 {
00155
00156 static_cast<SpectrumSettings&>(baseline_filtered_container) = input_peak_container;
00157
00158 raster(input_peak_container.begin(), input_peak_container.end(), baseline_filtered_container);
00159 }
00160
00161
00173 template <typename InputSpectrumIterator, typename OutputPeakType >
00174 void rasterExperiment(InputSpectrumIterator first, InputSpectrumIterator last, MSExperiment<OutputPeakType>& ms_exp_filtered)
00175 {
00176 UInt n = distance(first,last);
00177 ms_exp_filtered.reserve(n);
00178 startProgress(0,n,"resampling of data");
00179
00180 for (UInt i = 0; i < n; ++i)
00181 {
00182 MSSpectrum< OutputPeakType > spectrum;
00183 InputSpectrumIterator input_it(first+i);
00184
00185
00186 raster(*input_it,spectrum);
00187 setProgress(i);
00188
00189
00190 if (spectrum.size() > 0)
00191 {
00192
00193 static_cast<SpectrumSettings&>(spectrum) = *input_it;
00194 spectrum.setType(SpectrumSettings::RAWDATA);
00195
00196
00197 spectrum.getPrecursorPeak() = input_it->getPrecursorPeak();
00198 spectrum.setRT(input_it->getRT());
00199 spectrum.setMSLevel(input_it->getMSLevel());
00200 spectrum.getName() = input_it->getName();
00201
00202 ms_exp_filtered.push_back(spectrum);
00203 }
00204 }
00205 endProgress();
00206 }
00207
00208
00209
00219 template <typename InputPeakType, typename OutputPeakType >
00220 void rasterExperiment(const MSExperiment< InputPeakType >& ms_exp_raw, MSExperiment<OutputPeakType>& ms_exp_filtered)
00221 {
00222
00223 static_cast<ExperimentalSettings&>(ms_exp_filtered) = ms_exp_raw;
00224
00225 rasterExperiment(ms_exp_raw.begin(), ms_exp_raw.end(), ms_exp_filtered);
00226 }
00227
00228 protected:
00230 double spacing_;
00231
00232 virtual void updateMembers_()
00233 {
00234 spacing_ = param_.getValue("spacing");
00235 }
00236 };
00237
00238
00239 }
00240
00241 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H