Go to the documentation of this file.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
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
00036 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
00037
00038 #include <OpenMS/FILTERING/TRANSFORMERS/LinearResampler.h>
00039
00040 namespace OpenMS
00041 {
00042
00058 class LinearResamplerAlign
00059 : public LinearResampler
00060
00061 {
00062
00063 public:
00064
00068 template <template <typename> class SpecT, typename PeakType>
00069 void raster(SpecT<PeakType>& spectrum)
00070 {
00071
00072 if (spectrum.empty()) return;
00073
00074 typename SpecT<PeakType>::iterator first = spectrum.begin();
00075 typename SpecT<PeakType>::iterator last = spectrum.end();
00076
00077 double end_pos = (last-1)->getMZ();
00078 double start_pos = first->getMZ();
00079 int number_resampled_points = (int)(ceil((end_pos -start_pos) / spacing_ + 1));
00080
00081 typename std::vector<PeakType> resampled_peak_container;
00082 resampled_peak_container.resize(number_resampled_points);
00083
00084
00085 typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
00086 for (int i=0; i < number_resampled_points; ++i)
00087 {
00088 it->setMZ( start_pos + i*spacing_);
00089 ++it;
00090 }
00091
00092 raster(spectrum.begin(), spectrum.end(), resampled_peak_container.begin(), resampled_peak_container.end());
00093
00094 resampled_peak_container.swap(spectrum);
00095 }
00096
00100 template <template <typename> class SpecT, typename PeakType>
00101 void raster_align(SpecT<PeakType>& spectrum, double start_pos, double end_pos)
00102 {
00103
00104 if (spectrum.empty()) return;
00105 if (end_pos < start_pos)
00106 {
00107 SpecT<PeakType> empty;
00108 empty.swap(spectrum);
00109 return;
00110 }
00111
00112 typename SpecT<PeakType>::iterator first = spectrum.begin();
00113 typename SpecT<PeakType>::iterator last = spectrum.end();
00114
00115
00116 while (first != spectrum.end() && (first)->getMZ() < start_pos) {++first;}
00117 while (last != first && (last-1)->getMZ() > end_pos) {--last;}
00118
00119 int number_resampled_points = (int)(ceil((end_pos -start_pos) / spacing_ + 1));
00120
00121 typename std::vector<PeakType> resampled_peak_container;
00122 resampled_peak_container.resize(number_resampled_points);
00123
00124
00125 typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
00126 for (int i=0; i < number_resampled_points; ++i)
00127 {
00128 it->setMZ( start_pos + i*spacing_);
00129 ++it;
00130 }
00131
00132 raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
00133
00134 resampled_peak_container.swap(spectrum);
00135 }
00136
00140 template < typename PeakTypeIterator, typename ConstPeakTypeIterator>
00141 void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
00142 {
00143 PeakTypeIterator resample_start = resample_it;
00144
00145
00146 while(raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ())
00147 {
00148 resample_it->setIntensity( resample_it->getIntensity() + raw_it->getIntensity() );
00149 raw_it++;
00150 }
00151
00152 while(raw_it != raw_end)
00153 {
00154
00155 while(resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++;}
00156 if (resample_it != resample_start) {resample_it--;}
00157
00158
00159 if ((resample_it+1) == resample_end) {break;}
00160
00161 double dist_left = fabs(raw_it->getMZ() - resample_it->getMZ());
00162 double dist_right = fabs(raw_it->getMZ() - (resample_it+1)->getMZ());
00163
00164
00165 resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left+dist_right));
00166 (resample_it+1)->setIntensity((resample_it+1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left+dist_right));
00167
00168 raw_it++;
00169 }
00170
00171
00172 while(raw_it != raw_end)
00173 {
00174 resample_it->setIntensity( resample_it->getIntensity() + raw_it->getIntensity() );
00175 raw_it++;
00176 }
00177 }
00178
00182 template < typename PeakTypeIterator>
00183 void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator it, PeakTypeIterator resampled_end)
00184 {
00185 PeakTypeIterator raw_start = raw_it;
00186
00187
00188 while(it != resampled_end && it->getMZ() < raw_it->getMZ()) {it++;}
00189
00190 while(it != resampled_end)
00191 {
00192
00193 while(raw_it != raw_end && raw_it->getMZ() < it->getMZ() ) {raw_it++;}
00194 if (raw_it != raw_start) {raw_it--;}
00195
00196
00197 if ((raw_it+1) == raw_end) {break;}
00198
00199
00200 double m = ((raw_it+1)->getIntensity() - raw_it->getIntensity() ) / ((raw_it+1)->getMZ() - raw_it->getMZ() );
00201 it->setIntensity( raw_it->getIntensity() + (it->getMZ() - raw_it->getMZ())*m );
00202 it++;
00203 }
00204
00205 }
00206
00207 };
00208
00209 }
00210
00211 #endif