Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

LinearResamplerAlign.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry               
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 // 
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution 
00014 //    may be used to endorse or promote products derived from this software 
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS. 
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING 
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 // 
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Hannes Roest $
00032 // $Authors: Hannes Roest $
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     //return if nothing to do
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     // generate the resampled peaks at positions origin+i*spacing_
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     //return if nothing to do
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     // get the iterators just before / after the two points start_pos / end_pos
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     // generate the resampled peaks at positions origin+i*spacing_
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     // need to get the raw iterator between two resampled iterators of the raw data
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       //advance the resample iterator until our raw point is between two resampled iterators 
00155       while(resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++;} 
00156       if (resample_it != resample_start) {resample_it--;}
00157 
00158       // if we have the last datapoint we break
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       // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
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     // add the final intensity to the right
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     // need to get the resampled iterator between two iterators of the raw data
00188     while(it != resampled_end && it->getMZ() < raw_it->getMZ()) {it++;} 
00189 
00190     while(it != resampled_end)
00191     {
00192       //advance the raw_iterator until our current point we want to interpolate is between them
00193       while(raw_it != raw_end && raw_it->getMZ() < it->getMZ() ) {raw_it++;} 
00194       if (raw_it != raw_start) {raw_it--;}
00195 
00196       // if we have the last datapoint we break
00197       if ((raw_it+1) == raw_end) {break;}
00198 
00199       // use a linear interpolation between raw_it and raw_it+1
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

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:40 using doxygen 1.7.1