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

EmgScoring.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_TRANSFORMATIONS_FEATUREFINDER_EMGSCORING_H
00036 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_EMGSCORING_H
00037 
00038 #include <vector>
00039 #include <boost/math/special_functions/fpclassify.hpp> // for isnan
00040 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/EmgFitter1D.h>
00041 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/EmgModel.h>
00042 #include <OpenMS/FILTERING/SMOOTHING/GaussFilter.h>
00043 
00044 #include <OpenMS/KERNEL/MRMFeature.h>
00045 #include <OpenMS/KERNEL/MRMTransitionGroup.h>
00046 
00047 #include <OpenMS/KERNEL/StandardTypes.h>
00048 
00049 
00050 namespace OpenMS
00051 {
00052 
00060   class EmgScoring
00061   {
00062 
00063   public :
00064 
00065     EmgScoring() { }
00066 
00067     ~EmgScoring() { }
00068 
00069     void setFitterParam(Param param)
00070     {
00071       fitter_emg1D_.setParameters(param);
00072     }
00073 
00074     Param getDefaults()
00075     {
00076       return fitter_emg1D_.getDefaults();
00077     }
00078 
00080     template<typename SpectrumType, class TransitionT>
00081     double calcElutionFitScore(MRMFeature & mrmfeature, MRMTransitionGroup<SpectrumType, TransitionT> & transition_group)
00082     {
00083 
00084       std::vector<double> fit_scores;
00085       double avg_score = 0;
00086       bool smooth_data = false;
00087       for (Size k = 0; k < transition_group.size(); k++)
00088       {
00089         // get the id, then find the corresponding transition and features within this peakgroup
00090         String native_id = transition_group.getChromatograms()[k].getNativeID();
00091         Feature f = mrmfeature.getFeature(native_id);
00092         OPENMS_PRECONDITION(f.getConvexHulls().size() == 1, "Convex hulls need to have exactly one hull point structure");
00093 
00094         // TODO what if score is -1 ?? e.g. if it is undefined
00095         double fscore = elutionModelFit(f.getConvexHulls()[0].getHullPoints(), smooth_data);
00096         fit_scores.push_back(fscore);
00097         avg_score += fscore;
00098       }
00099 
00100       avg_score /= transition_group.size();
00101       return avg_score;
00102     }
00103 
00104     // Fxn from FeatureFinderAlgorithmMRM
00105     // TODO: check whether we can leave out some of the steps here, e.g. gaussian smoothing
00106     double elutionModelFit(ConvexHull2D::PointArrayType current_section, bool smooth_data)
00107     {
00108       // We need at least 2 datapoints in order to create a fit
00109       if (current_section.size() < 2)
00110       {
00111         return -1;
00112       }
00113 
00114       // local PeakType is a small hack since here we *need* data of type
00115       // Peak1D, otherwise our fitter will not accept it.
00116       typedef Peak1D LocalPeakType;
00117 
00118 
00119       // -- cut line 301 of FeatureFinderAlgorithmMRM
00120       std::vector<LocalPeakType> data_to_fit;
00121       prepareFit_(current_section, data_to_fit, smooth_data);
00122       InterpolationModel * model_rt = 0;
00123       DoubleReal quality = fitRT_(data_to_fit, model_rt);
00124       // cut line 354 of FeatureFinderAlgorithmMRM
00125       delete model_rt;
00126 
00127       return quality;
00128 
00129     }
00130 
00131   protected:
00132     template<class LocalPeakType>
00133     double fitRT_(std::vector<LocalPeakType> & rt_input_data, InterpolationModel * & model)
00134     {
00135       DoubleReal quality;
00136       //Param param;
00137 
00138       /*EmgFitter
00139        param.setValue( "tolerance_stdev_bounding_box", tolerance_stdev_box_);
00140        param.setValue( "statistics:mean", rt_stat_.mean() );
00141        param.setValue( "statistics:variance", rt_stat_.variance() );
00142        param.setValue( "interpolation_step", interpolation_step_rt_ );
00143        param.setValue( "max_iteration", max_iteration_);
00144        param.setValue( "deltaAbsError", deltaAbsError_);
00145        param.setValue( "deltaRelError", deltaRelError_);
00146        */
00147 
00148       // Set parameter for fitter
00149       //fitter_emg1D.setParameters(param);
00150       // Construct model for rt
00151       quality = fitter_emg1D_.fit1d(rt_input_data, model);
00152 
00153       // Check quality
00154       if (boost::math::isnan(quality)) quality = -1.0;
00155       return quality;
00156     }
00157 
00158     // Fxn from FeatureFinderAlgorithmMRM
00159     // TODO: check whether we can leave out some of the steps here, e.g. gaussian smoothing
00160     template<class LocalPeakType>
00161     void prepareFit_(const ConvexHull2D::PointArrayType & current_section, std::vector<LocalPeakType> & data_to_fit, bool smooth_data)
00162     {
00163       // typedef Peak1D LocalPeakType;
00164       PeakSpectrum filter_spec;
00165       // first smooth the data to prevent outliers from destroying the fit
00166       for (ConvexHull2D::PointArrayType::const_iterator it = current_section.begin(); it != current_section.end(); it++)
00167       {
00168         LocalPeakType p;
00169         p.setMZ(it->getX());
00170         p.setIntensity(it->getY());
00171         filter_spec.push_back(p);
00172       }
00173 
00174       // add two peaks at the beginning and at the end for better fit
00175       // therefore calculate average distance first
00176       std::vector<DoubleReal> distances;
00177       for (Size j = 1; j < filter_spec.size(); ++j)
00178       {
00179         distances.push_back(filter_spec[j].getMZ() - filter_spec[j - 1].getMZ());
00180       }
00181       DoubleReal dist_average = std::accumulate(distances.begin(), distances.end(), 0.0) / (DoubleReal) distances.size();
00182 
00183       // append peaks
00184       Peak1D new_peak;
00185       new_peak.setIntensity(0);
00186       new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
00187       filter_spec.push_back(new_peak);
00188       new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
00189       filter_spec.push_back(new_peak);
00190       new_peak.setMZ(filter_spec.back().getMZ() + dist_average);
00191       filter_spec.push_back(new_peak);
00192 
00193       // prepend peaks
00194       new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
00195       filter_spec.insert(filter_spec.begin(), new_peak);
00196       new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
00197       filter_spec.insert(filter_spec.begin(), new_peak);
00198       new_peak.setMZ(filter_spec.front().getMZ() - dist_average);
00199       filter_spec.insert(filter_spec.begin(), new_peak);
00200 
00201       // To get an estimate of the peak quality, we probably should not smooth
00202       // and/or transform the data.
00203       if (smooth_data)
00204       {
00205         GaussFilter filter;
00206         Param filter_param(filter.getParameters());
00207         filter.setParameters(filter_param);
00208         filter_param.setValue("gaussian_width", 4 * dist_average);
00209         filter.setParameters(filter_param);
00210         filter.filter(filter_spec);
00211       }
00212 
00213       // transform the data for fitting and fit RT profile
00214       for (Size j = 0; j != filter_spec.size(); ++j)
00215       {
00216         LocalPeakType p;
00217         p.setPosition(filter_spec[j].getMZ());
00218         p.setIntensity(filter_spec[j].getIntensity());
00219         data_to_fit.push_back(p);
00220       }
00221     }
00222 
00223     EmgFitter1D fitter_emg1D_;
00224   };
00225 }
00226 
00227 #endif /* EMGSCORING_H_ */

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