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

MRMFeatureFinderScoring.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_ANALYSIS_OPENSWATH_MRMFEATUREFINDERSCORING_H
00036 #define OPENMS_ANALYSIS_OPENSWATH_MRMFEATUREFINDERSCORING_H
00037 
00038 #define run_identifier "unique_run_identifier"
00039 #define USE_SP_INTERFACE
00040 
00041 // move to TOPPTool
00042 #include <OpenMS/FORMAT/MzMLFile.h>
00043 #include <OpenMS/FORMAT/FeatureXMLFile.h>
00044 #include <OpenMS/FORMAT/TransformationXMLFile.h>
00045 #include <OpenMS/FORMAT/TraMLFile.h>
00046 
00047 #include <OpenMS/KERNEL/MRMTransitionGroup.h>
00048 #include <OpenMS/KERNEL/MRMFeature.h>
00049 #include <OpenMS/KERNEL/MSSpectrum.h>
00050 #include <OpenMS/KERNEL/MSChromatogram.h>
00051 
00052 // peak picking & noise estimation
00053 #include <OpenMS/FILTERING/NOISEESTIMATION/SignalToNoiseEstimatorMedian.h>
00054 #include <OpenMS/ANALYSIS/OPENSWATH/MRMTransitionGroupPicker.h>
00055 
00056 // data access
00057 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/DataStructures.h>
00058 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h>
00059 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/DATAACCESS/TransitionExperiment.h>
00060 #include <OpenMS/ANALYSIS/OPENSWATH/DATAACCESS/MRMFeatureAccessOpenMS.h>
00061 
00062 // scoring
00063 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/EmgScoring.h>
00064 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/ALGO/Scoring.h>
00065 #include <OpenMS/ANALYSIS/OPENSWATH/OPENSWATHALGO/ALGO/MRMScoring.h>
00066 #include <OpenMS/ANALYSIS/OPENSWATH/DIAScoring.h>
00067 
00068 // auxiliary
00069 #include <OpenMS/ANALYSIS/OPENSWATH/SpectrumAddition.h>
00070 
00071 #ifdef _OPENMP
00072 #include <omp.h>
00073 #endif
00074 
00075 bool SortDoubleDoublePairFirst(const std::pair<double, double>& left, const std::pair<double, double>& right);
00076 
00077 namespace OpenMS
00078 {
00079 
00084   struct OpenSwath_Scores
00085   {
00086     double elution_model_fit_score;
00087     double library_corr;
00088     double library_rmsd;
00089     double norm_rt_score;
00090     double isotope_correlation;
00091     double isotope_overlap;
00092     double massdev_score;
00093     double xcorr_coelution_score;
00094     double xcorr_shape_score;
00095     double yseries_score;
00096     double log_sn_score;
00097 
00098     double get_quick_lda_score(double library_corr, double library_rmsd, double norm_rt_score, double xcorr_coelution_score,
00099                                double xcorr_shape_score, double log_sn_score)
00100     {
00101       // some scores based on manual evaluation of 80 chromatograms
00102       // quick LDA average model on 100 2xCrossvalidated runs (0.85 TPR/0.17 FDR)
00103       // true: mean 4.2 with sd 1.055
00104       // false: mean -0.07506772  with sd 1.055
00105       // below -0.5 removes around 30% of the peaks
00106       // below 0    removes around 50% of the peaks
00107       // below 0.5  removes around 70% of the peaks
00108       // below 1.0  removes around 85% of the peaks
00109       // below 1.5  removes around 93% of the peaks
00110       // below 2.0  removes around 97% of the peaks
00111       double lda_quick_score =
00112         library_corr                    * -0.5319046 +
00113         library_rmsd                    *  2.1643962 +
00114         norm_rt_score                   *  8.0353047 +
00115         xcorr_coelution_score           *  0.1458914 +
00116         xcorr_shape_score               * -1.6901925 +
00117         log_sn_score                    * -0.8002824;
00118       return lda_quick_score;
00119     }
00120 
00121     double calculate_lda_prescore(OpenSwath_Scores scores)
00122     {
00123 
00124       // LDA average model on 100 2xCrossvalidated runs (0.91 TPR/0.20 FDR)
00125       /*
00126       double xx_old_lda_prescore =
00127       intensity_score       * -2.296679          +
00128       library_corr          * -0.1223876         +
00129       library_rmsd          *  2.013638          +
00130       nr_peaks_score        *  0.01683357        +
00131       rt_score              *  0.00143999        +
00132       sn_score              * -0.1619762         +
00133       total_xic_score       *  0.00000003697898  +
00134       xcorr_coelution_score *  0.05909583        +
00135       xcorr_shape_score     * -0.4699841;
00136       */
00137 
00138       return scores.library_corr                     * -0.34664267 +
00139              scores.library_rmsd                     *  2.98700722 +
00140              scores.norm_rt_score                    *  7.05496384 +
00141              scores.xcorr_coelution_score            *  0.09445371 +
00142              scores.xcorr_shape_score                * -5.71823862 +
00143              scores.log_sn_score                     * -0.72989582 +
00144              scores.elution_model_fit_score          *  1.88443209;
00145     }
00146 
00147     double calculate_swath_lda_prescore(OpenSwath_Scores scores)
00148     {
00149 
00150       // Swath - LDA average model on 100 2xCrossvalidated runs (0.76 TPR/0.20 FDR) [without elution model]
00151       /*
00152       double xx_old_swath_prescore =
00153       intensity_score              * -3.148838e+00  +
00154       library_corr                 * -7.562403e-02  +
00155       library_rmsd                 *  1.786286e+00  +
00156       nr_peaks_score               * -7.674263e-03  +
00157       rt_score                     *  1.748377e-03  +
00158       sn_score                     * -1.372636e-01  +
00159       total_xic_score              *  7.278437e-08  +
00160       xcorr_coelution_score        *  1.181813e-01  +
00161       weighted_coelution_score     * -7.661783e-02  +
00162       xcorr_shape_score            * -6.903933e-02  +
00163       weighted_xcorr_shape         * -4.234820e-01  +
00164       bseries_score                * -2.022380e-02  +
00165       massdev_score                *  2.844948e-02  +
00166       massdev_score_weighted       *  1.133209e-02  +
00167       yseries_score                * -9.510874e-02  +
00168       isotope_corr                 * -1.619902e+00  +
00169       isotope_overlap              *  2.890688e-01  ;
00170       */
00171 
00172       return scores.library_corr              * -0.19011762 +
00173              scores.library_rmsd              *  2.47298914 +
00174              scores.norm_rt_score             *  5.63906731 +
00175              scores.isotope_correlation       * -0.62640133 +
00176              scores.isotope_overlap           *  0.36006925 +
00177              scores.massdev_score             *  0.08814003 +
00178              scores.xcorr_coelution_score     *  0.13978311 +
00179              scores.xcorr_shape_score         * -1.16475032 +
00180              scores.yseries_score             * -0.19267813 +
00181              scores.log_sn_score              * -0.61712054;
00182     }
00183 
00184   };
00185 
00199   class OPENMS_DLLAPI MRMFeatureFinderScoring :
00200     public DefaultParamHandler,
00201     public ProgressLogger
00202   {
00203 
00204 public:
00205 
00207 
00208 
00209     // All the filters expect MSSpectrum<PeakT>, thus we give it an "MSSpectrum"
00210     // but filled with Chromatogram Peaks.
00211     typedef MSSpectrum<ChromatogramPeak> RichPeakChromatogram; // this is the type in which we store the chromatograms for this analysis
00212     typedef OpenSwath::LightTransition TransitionType;
00213     typedef OpenSwath::LightTargetedExperiment TargetedExpType;
00214     typedef OpenSwath::LightPeptide PeptideType;
00215     typedef OpenSwath::LightProtein ProteinType;
00216     typedef OpenSwath::LightModification ModificationType;
00217     typedef MRMTransitionGroup<MSSpectrum <ChromatogramPeak>, TransitionType> MRMTransitionGroupType; // a transition group holds the MSSpectra with the Chromatogram peaks from above
00218     typedef std::map<String, MRMTransitionGroupType> TransitionGroupMapType;
00220 
00222     MRMFeatureFinderScoring();
00223 
00225     ~MRMFeatureFinderScoring();
00226 
00227     // pick features in one experiment containing chromatograms
00228     void pickExperiment(OpenSwath::SpectrumAccessPtr input, FeatureMap<Feature>& output, OpenSwath::LightTargetedExperiment& transition_exp,
00229                         TransformationDescription trafo, OpenSwath::SpectrumAccessPtr swath_map, TransitionGroupMapType& transition_group_map)
00230     {
00231       updateMembers_();
00232 
00233       //
00234       // Step 1
00235       //
00236       // Store the peptide retention times in an intermediate map
00237       PeptideRTMap_.clear();
00238       for (Size i = 0; i < transition_exp.getPeptides().size(); i++)
00239       {
00240         PeptideType pep = transition_exp.getPeptides()[i];
00241         PeptideRTMap_[pep.id] = pep.rt;
00242         PeptideRefMap_[pep.id] = &transition_exp.getPeptides()[i];
00243       }
00244 
00245       // Store the proteins from the input in the output feature map
00246       std::vector<ProteinHit> protein_hits;
00247       for (Size i = 0; i < transition_exp.getProteins().size(); i++)
00248       {
00249         const ProteinType& prot = transition_exp.getProteins()[i];
00250         ProteinRefMap_[transition_exp.getProteins()[i].id] = &transition_exp.getProteins()[i];
00251         ProteinHit prot_hit = ProteinHit();
00252         prot_hit.setSequence(prot.sequence);
00253         prot_hit.setAccession(prot.id);
00254         protein_hits.push_back(prot_hit);
00255       }
00256 
00257       ProteinIdentification prot_id = ProteinIdentification();
00258       prot_id.setHits(protein_hits);
00259       prot_id.setIdentifier(run_identifier);
00260       output.getProteinIdentifications().push_back(prot_id);
00261 
00262       //
00263       // Step 2
00264       //
00265       // Create all MRM transition groups from the individual transitions.
00266       mapExperimentToTransitionList(input, transition_exp, transition_group_map, trafo, rt_extraction_window_);
00267       int counter = 0;
00268       for (TransitionGroupMapType::iterator trgroup_it = transition_group_map.begin(); trgroup_it != transition_group_map.end(); trgroup_it++)
00269       {
00270         if (trgroup_it->second.getChromatograms().size() > 0) {counter++; }
00271       }
00272       std::cout << "Will analyse " << counter << " peptides with a total of " << transition_exp.getTransitions().size() << " transitions " << std::endl;
00273 
00274       //
00275       // Step 3
00276       //
00277       // Go through all transition groups: first create consensus features, then score them
00278       Size progress = 0;
00279       startProgress(0, transition_group_map.size(), "picking peaks");
00280       for (TransitionGroupMapType::iterator trgroup_it = transition_group_map.begin(); trgroup_it != transition_group_map.end(); trgroup_it++)
00281       {
00282 
00283         setProgress(++progress);
00284         MRMTransitionGroupType& transition_group = trgroup_it->second;
00285         if (transition_group.getChromatograms().size() == 0 || transition_group.getTransitions().size() == 0)
00286         {
00287           continue;
00288         }
00289 
00290         MRMTransitionGroupPicker trgroup_picker;
00291         trgroup_picker.setParameters(param_.copy("TransitionGroupPicker:", true));
00292         trgroup_picker.pickTransitionGroup(transition_group);
00293         scorePeakgroups_(trgroup_it->second, trafo, swath_map, output);
00294 
00295       }
00296       endProgress();
00297 
00298       //output.sortByPosition(); // if the exact same order is needed
00299       return;
00300     }
00301 
00302     // Map an input experiment (mzML) and transition list (TraML) onto each other
00303     // when they share identifiers, e.g. if the transition id is the same as the
00304     // chromatogram native id.
00305     void mapExperimentToTransitionList(OpenSwath::SpectrumAccessPtr input, OpenSwath::LightTargetedExperiment& transition_exp,
00306                                        TransitionGroupMapType& transition_group_map, TransformationDescription trafo, double rt_extraction_window);
00307 
00308     void setStrictFlag(bool f)
00309     {
00310       strict_ = f;
00311     }
00312 
00313 private:
00314 
00316     template <typename SpectrumT, typename TransitionT>
00317     void scorePeakgroups_(MRMTransitionGroup<SpectrumT, TransitionT> & transition_group, TransformationDescription & trafo,
00318                          OpenSwath::SpectrumAccessPtr  swath_map, FeatureMap<Feature>& output)
00319     {
00320       //std::vector<SignalToNoiseEstimatorMedian<RichPeakChromatogram> > signal_noise_estimators;
00321       typedef typename MRMTransitionGroup<SpectrumT, TransitionT>::PeakType PeakT;
00322       std::vector<OpenSwath::ISignalToNoisePtr> signal_noise_estimators;
00323       std::vector<MRMFeature> feature_list;
00324 
00325 
00326       DoubleReal sn_win_len_ = (DoubleReal)param_.getValue("TransitionGroupPicker:sn_win_len");
00327       DoubleReal sn_bin_count_ = (DoubleReal)param_.getValue("TransitionGroupPicker:sn_bin_count");
00328       for (Size k = 0; k < transition_group.getChromatograms().size(); k++)
00329       {
00330         OpenSwath::ISignalToNoisePtr snptr(new OpenMS::SignalToNoiseOpenMS< PeakT >(transition_group.getChromatograms()[k], sn_win_len_, sn_bin_count_));
00331         signal_noise_estimators.push_back(snptr);
00332       }
00333 
00334       // get the expected rt value for this peptide
00335       double expected_rt = PeptideRTMap_[transition_group.getTransitionGroupID()];
00336       TransformationDescription newtr = trafo;
00337       newtr.invert();
00338       expected_rt = newtr.apply(expected_rt);
00339 
00340       // Go through all peak groups (found MRM features) and score them
00341       for (std::vector<MRMFeature>::iterator mrmfeature = transition_group.getFeaturesMuteable().begin();
00342            mrmfeature != transition_group.getFeaturesMuteable().end(); mrmfeature++)
00343       {
00344 
00345         OpenSwath::IMRMFeature* imrmfeature;
00346         imrmfeature = new MRMFeatureOpenMS(*mrmfeature);
00347 
00348         OpenSwath::ITransitionGroup* itransition_group;
00349         itransition_group = new TransitionGroupOpenMS<SpectrumT, TransitionT>(transition_group);
00350 
00351 #ifdef DEBUG_MRMPEAKPICKER
00352         std::cout << "000000000000000000000000000000000000000000000000000000000000000000000000000 " << std::endl;
00353         std::cout << "scoring feature " << (*mrmfeature) << " == " << mrmfeature->getMetaValue("PeptideRef") <<
00354         "[ expected RT " << PeptideRTMap_[mrmfeature->getMetaValue("PeptideRef")] << " / " << expected_rt << " ]" <<
00355         " with " << transition_group.size()  << " nr transitions and nr chromats " << transition_group.getChromatograms().size() << std::endl;
00356 #endif
00357 
00358         int group_size = boost::numeric_cast<int>(transition_group.size());
00359         if (group_size == 0)
00360         {
00361           throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__,
00362                                            "Error: Transition group " + transition_group.getTransitionGroupID() + " has no chromatograms.");
00363         }
00364         if (group_size < 2)
00365         {
00366           std::cerr << "Error: transition group " << transition_group.getTransitionGroupID() << " has less than 2 chromatograms. It has " << group_size << std::endl;
00367           continue;
00368           //throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Error: transition group " + transition_group.getTransitionGroupID() + " has less than 2 chromatograms.");
00369         }
00370 
00371         // calculate the normalized library intensity (expected value of the intensities)
00372         std::vector<double> normalized_library_intensity;
00373         transition_group.getLibraryIntensity(normalized_library_intensity);
00374         OpenSwath::Scoring::normalize_sum(&normalized_library_intensity[0], boost::numeric_cast<int>(normalized_library_intensity.size()));
00375 
00376         // calcxcorr -> for each lag do the correlation, normally use lag 0
00377         // xcorr_matrix  => correlate chromatogram i with chromatogram j
00378         bool normalize = true;
00379         mrmscore_.initializeXCorrMatrix(imrmfeature, itransition_group, normalize);
00380 
00381         // XCorr score (coelution)
00382         double xcorr_coelution_score = 0;
00383         if (use_coelution_score_)
00384         {
00385           xcorr_coelution_score = mrmscore_.calcXcorrCoelutionScore();
00386           mrmfeature->addScore("var_xcorr_coelution", xcorr_coelution_score);
00387         }
00388 
00389         double weighted_coelution_score = 0;
00390         if (use_coelution_score_)
00391         {
00392           weighted_coelution_score = mrmscore_.calcXcorrCoelutionScore_weighted(normalized_library_intensity);
00393           mrmfeature->addScore("var_xcorr_coelution_weighted ", weighted_coelution_score);
00394         }
00395 
00396         // XCorr score (shape)
00397         // mean over the intensities at the max of the crosscorrelation
00398         // FEATURE : weigh by the intensity as done by mQuest
00399         // FEATURE : normalize with the intensity at the peak group apex?
00400         double xcorr_shape_score = 0;
00401         if (use_shape_score_)
00402         {
00403           xcorr_shape_score = mrmscore_.calcXcorrShape_score();
00404           mrmfeature->addScore("var_xcorr_shape", xcorr_shape_score);
00405         }
00406 
00407         double weighted_xcorr_shape = 0;
00408         if (use_shape_score_)
00409         {
00410           weighted_xcorr_shape = mrmscore_.calcXcorrShape_score_weighted(normalized_library_intensity);
00411           mrmfeature->addScore("var_xcorr_shape_weighted", weighted_xcorr_shape);
00412         }
00413 
00414         // FEATURE : how should we best calculate correlation between library and experiment?
00415         // FEATURE : spectral angle
00416         double library_corr = 0, library_rmsd = 0;
00417         double library_manhattan, library_dotprod;
00418         if (use_library_score_)
00419         {
00420           mrmscore_.calcLibraryScore(imrmfeature, transition_group.getTransitions(), library_corr, library_rmsd, library_manhattan, library_dotprod);
00421           mrmfeature->addScore("var_library_corr", library_corr);
00422           mrmfeature->addScore("var_library_rmsd", library_rmsd);
00423           mrmfeature->addScore("var_library_manhattan", library_manhattan); // new score
00424           mrmfeature->addScore("var_library_dotprod", library_dotprod); // new score
00425         }
00426 
00427         // Retention time score
00428         double rt_score = 0, norm_rt_score = 0;
00429         if (use_rt_score_)
00430         {
00431           // get the id, then get the expected and the experimental retention time
00432           String native_id = transition_group.getChromatograms()[0].getNativeID();
00433           TransitionType tr = transition_group.getTransition(native_id);
00434           const PeptideType* pep = PeptideRefMap_[tr.getPeptideRef()];
00435           double experimental_rt = mrmfeature->getFeature(native_id).getRT();
00436           double normalized_experimental_rt = trafo.apply(experimental_rt);
00437           // rt score is delta iRT
00438           rt_score = mrmscore_.calcRTScore(*pep, normalized_experimental_rt);
00439           norm_rt_score = rt_score / rt_normalization_factor_;
00440           mrmfeature->addScore("delta_rt", mrmfeature->getRT() - expected_rt);
00441           mrmfeature->addScore("assay_rt", expected_rt);
00442           mrmfeature->addScore("norm_RT", normalized_experimental_rt);
00443           mrmfeature->addScore("rt_score", rt_score);
00444           mrmfeature->addScore("var_norm_rt_score", norm_rt_score);
00445         }
00446 
00447         // Intensity score
00448         double intensity_score = 0;
00449         if (use_intensity_score_)
00450         {
00451           intensity_score = mrmfeature->getIntensity() / (double)mrmfeature->getMetaValue("total_xic");
00452           mrmfeature->addScore("var_intensity_score", intensity_score);
00453         }
00454 
00455         double total_xic_score = 0;
00456         if (use_total_xic_score_)
00457         {
00458           total_xic_score = (double)mrmfeature->getMetaValue("total_xic");
00459           mrmfeature->addScore("total_xic", total_xic_score);
00460         }
00461 
00462         double nr_peaks_score = 0;
00463         if (use_nr_peaks_score_)
00464         {
00465           nr_peaks_score = group_size;
00466           mrmfeature->addScore("nr_peaks", nr_peaks_score);
00467         }
00468 
00469         double sn_score = 0, log_sn_score = 0;
00470         if (use_sn_score_)
00471         {
00472           sn_score = mrmscore_.calcSNScore(imrmfeature, signal_noise_estimators);
00473           if (sn_score < 1) // fix to make sure, that log(sn_score = 0) = -inf does not occur
00474           {
00475             log_sn_score = 0;
00476           }
00477           else
00478           {
00479             log_sn_score = std::log(sn_score);
00480           }
00481           mrmfeature->addScore("sn_ratio", sn_score);
00482           mrmfeature->addScore("var_log_sn_score", log_sn_score);
00483         }
00484 
00485         OpenSwath_Scores scores;
00486         double quick_lda_dismiss = 0;
00487         double lda_quick_score = -scores.get_quick_lda_score(library_corr, library_rmsd, norm_rt_score, xcorr_coelution_score, xcorr_shape_score, log_sn_score);
00488 
00489         if (lda_quick_score < quick_lda_dismiss)
00490         {
00491           // continue;
00492         }
00493 
00494         double elution_model_fit_score = 0;
00495         if (use_elution_model_score_)
00496         {
00497           elution_model_fit_score = emgscoring_.calcElutionFitScore((*mrmfeature), transition_group);
00498           mrmfeature->addScore("var_elution_model_fit_score", elution_model_fit_score);
00499         }
00500 
00501         double xx_lda_prescore;
00502         scores.library_corr              = library_corr;
00503         scores.library_rmsd              = library_rmsd;
00504         scores.norm_rt_score             = norm_rt_score;
00505         scores.elution_model_fit_score   = elution_model_fit_score;
00506         scores.log_sn_score              = log_sn_score;
00507         scores.xcorr_coelution_score     = xcorr_coelution_score;
00508         scores.xcorr_shape_score         = xcorr_shape_score;
00509         xx_lda_prescore = -scores.calculate_lda_prescore(scores);
00510 
00511         bool swath_present = (swath_map->getNrSpectra() > 0);
00512         if (!swath_present)
00513         {
00514           mrmfeature->addScore("main_var_xx_lda_prelim_score", xx_lda_prescore);
00515           mrmfeature->setOverallQuality(xx_lda_prescore);
00516         }
00517         else
00518         {
00519           mrmfeature->addScore("xx_lda_prelim_score", xx_lda_prescore);
00520         }
00521 
00522         if (swath_present)
00523         {
00524           calculateSwathScores_(transition_group, *mrmfeature, swath_map, normalized_library_intensity, scores);
00525         }
00526 
00527 #if 0
00528         if (do_local_fdr_)
00529         {
00530           calculate_local_fdr_scores(transition_group, *mrmfeature, trafo);
00531         }
00532 #endif
00533 
00535         // add the peptide hit information to the feature
00537 
00538         const PeptideType* pep = PeptideRefMap_[transition_group.getTransitions()[0].getPeptideRef()];
00539         const ProteinType* prot = ProteinRefMap_[pep->protein_ref];
00540 
00541         PeptideIdentification pep_id_ = PeptideIdentification();
00542         PeptideHit pep_hit_ = PeptideHit();
00543 
00544         if (pep->getChargeState() != -1)
00545         {
00546           pep_hit_.setCharge(pep->getChargeState());
00547         }
00548         pep_hit_.setScore(xx_lda_prescore);
00549         if (swath_present)
00550         {
00551           pep_hit_.setScore(mrmfeature->getScore("xx_swath_prelim_score"));
00552         }
00553         pep_hit_.setSequence((String)pep->sequence);
00554         pep_hit_.addProteinAccession(prot->id);
00555         pep_id_.insertHit(pep_hit_);
00556         pep_id_.setIdentifier(run_identifier);
00557 
00558         mrmfeature->getPeptideIdentifications().push_back(pep_id_);
00559         mrmfeature->ensureUniqueId();
00560         mrmfeature->setMetaValue("PrecursorMZ", transition_group.getTransitions()[0].getPrecursorMZ());
00561         mrmfeature->setSubordinates(mrmfeature->getFeatures()); // add all the subfeatures as subordinates
00562         double total_intensity = 0, total_peak_apices = 0;
00563         for (std::vector<Feature>::iterator sub_it = mrmfeature->getSubordinates().begin(); sub_it != mrmfeature->getSubordinates().end(); sub_it++)
00564         {
00565           if (!write_convex_hull_) {sub_it->getConvexHulls().clear(); }
00566           sub_it->ensureUniqueId();
00567           if (sub_it->getMZ() > quantification_cutoff_)
00568           {
00569             total_intensity += sub_it->getIntensity();
00570             total_peak_apices += (DoubleReal)sub_it->getMetaValue("peak_apex_int");
00571           }
00572         }
00573         // overwrite the reported intensities with those above the m/z cutoff
00574         mrmfeature->setIntensity(total_intensity);
00575         mrmfeature->setMetaValue("peak_apices_sum", total_peak_apices);
00576         feature_list.push_back((*mrmfeature));
00577 
00578         delete imrmfeature;
00579         delete itransition_group;
00580       }
00581 
00582       // Order by quality
00583       std::sort(feature_list.begin(), feature_list.end(), OpenMS::Feature::OverallQualityLess());
00584       std::reverse(feature_list.begin(), feature_list.end());
00585 
00586       for (Size i = 0; i < feature_list.size(); i++)
00587       {
00588         if (stop_report_after_feature_ >= 0 && i >= (Size)stop_report_after_feature_) {break; }
00589         output.push_back(feature_list[i]);
00590       }
00591     }
00592 
00594     OpenSwath::SpectrumPtr getAddedSpectra_(OpenSwath::SpectrumAccessPtr swath_map, double RT, int nr_spectra_to_add)
00595     {
00596       std::vector<std::size_t> indices = swath_map->getSpectraByRT(RT, 0.0);
00597       int closest_idx = boost::numeric_cast<int>(indices[0]);
00598       if (indices[0] != 0 &&
00599           std::fabs(swath_map->getSpectrumMetaById(boost::numeric_cast<int>(indices[0]) - 1).RT - RT) <
00600           std::fabs(swath_map->getSpectrumMetaById(boost::numeric_cast<int>(indices[0])).RT - RT))
00601       {
00602         closest_idx--;
00603       }
00604 
00605       if (nr_spectra_to_add == 1)
00606       {
00607         OpenSwath::SpectrumPtr spectrum_ = swath_map->getSpectrumById(closest_idx);
00608         return spectrum_;
00609       }
00610       else
00611       {
00612         std::vector<OpenSwath::SpectrumPtr> all_spectra;
00613         // always add the spectrum 0, then add those right and left
00614         all_spectra.push_back(swath_map->getSpectrumById(closest_idx));
00615         for (int i = 1; i <= nr_spectra_to_add / 2; i++) // cast to int is intended!
00616         {
00617           all_spectra.push_back(swath_map->getSpectrumById(closest_idx - i));
00618           all_spectra.push_back(swath_map->getSpectrumById(closest_idx + i));
00619         }
00620         OpenSwath::SpectrumPtr spectrum_ = SpectrumAddition::addUpSpectra(all_spectra, spacing_for_spectra_resampling_, true);
00621         return spectrum_;
00622       }
00623     }
00624 
00625     template <typename SpectrumT, typename TransitionT>
00626     void calculateSwathScores_(MRMTransitionGroup<SpectrumT, TransitionT> & transition_group, MRMFeature & mrmfeature_,
00627                                 OpenSwath::SpectrumAccessPtr swath_map, std::vector<double>& normalized_library_intensity, OpenSwath_Scores scores)
00628     {
00629       MRMFeature* mrmfeature = &mrmfeature_;
00630 
00631       // parameters
00632       int by_charge_state = 1; // for which charge states should we check b/y series
00633 
00634       // find spectrum that is closest to the apex of the peak using binary search
00635       OpenSwath::SpectrumPtr spectrum_ = getAddedSpectra_(swath_map, mrmfeature->getRT(), add_up_spectra_);
00636       OpenSwath::SpectrumPtr* spectrum = &spectrum_;
00637 
00638       // Isotope correlation / overlap score: Is this peak part of an
00639       // isotopic pattern or is it the monoisotopic peak in an isotopic
00640       // pattern?
00641       OpenSwath::IMRMFeature* imrmfeature = new MRMFeatureOpenMS(*mrmfeature);
00642       double isotope_corr = 0, isotope_overlap = 0;
00643       diascoring_.dia_isotope_scores(transition_group.getTransitions(),
00644                                     (*spectrum), imrmfeature, isotope_corr, isotope_overlap);
00645       // Mass deviation score
00646       double ppm_score = 0, ppm_score_weighted = 0;
00647       diascoring_.dia_massdiff_score(transition_group.getTransitions(),
00648                                     (*spectrum), normalized_library_intensity, ppm_score, ppm_score_weighted);
00649 
00650       // Presence of b/y series score
00651       double bseries_score = 0, yseries_score = 0;
00652       OpenMS::AASequence aas;
00653       OpenSwathDataAccessHelper::convertPeptideToAASequence(*PeptideRefMap_[transition_group.getTransitions()[0].getPeptideRef()], aas);
00654       diascoring_.dia_by_ion_score((*spectrum), aas, by_charge_state, bseries_score, yseries_score);
00655       mrmfeature->addScore("var_isotope_correlation_score", isotope_corr);
00656       mrmfeature->addScore("var_isotope_overlap_score", isotope_overlap);
00657 #ifdef DEBUG_MRMPEAKPICKER
00658       cout << "added corr isotope_score " << isotope_corr << endl;
00659       cout << "added overlap isotope_score " << isotope_overlap << endl;
00660 #endif
00661 
00662       // FEATURE we should not punish so much when one transition is missing!
00663       double massdev_score = ppm_score / transition_group.size();
00664       double massdev_score_weighted = ppm_score_weighted;
00665       mrmfeature->addScore("var_massdev_score", massdev_score);
00666       mrmfeature->addScore("var_massdev_score_weighted", massdev_score_weighted);
00667 #ifdef DEBUG_MRMPEAKPICKER
00668       cout << "added score massdev_score " << massdev_score << endl;
00669       cout << "added score weighted massdev_score " << massdev_score_weighted << endl;
00670 #endif
00671 
00672       mrmfeature->addScore("var_bseries_score", bseries_score);
00673       mrmfeature->addScore("var_yseries_score", yseries_score);
00674 #ifdef DEBUG_MRMPEAKPICKER
00675       cout << "added score bseries_score " << bseries_score << endl;
00676       cout << "added score yseries_score " << yseries_score << endl;
00677 #endif
00678 
00679       double dotprod_score_dia;
00680       double manhatt_score_dia;
00681 
00682       diascoring_.score_with_isotopes((*spectrum), transition_group.getTransitions(), dotprod_score_dia, manhatt_score_dia);
00683 
00684       mrmfeature->addScore("var_dotprod_score", dotprod_score_dia);
00685       mrmfeature->addScore("var_manhatt_score", manhatt_score_dia);
00686 
00687       scores.yseries_score             = yseries_score;
00688       scores.isotope_correlation       = isotope_corr;
00689       scores.isotope_overlap           = isotope_overlap;
00690       scores.massdev_score             = massdev_score;
00691       double xx_swath_prescore = -scores.calculate_swath_lda_prescore(scores);
00692       mrmfeature->addScore("main_var_xx_swath_prelim_score", xx_swath_prescore);
00693       mrmfeature->setOverallQuality(xx_swath_prescore);
00694 #ifdef DEBUG_MRMPEAKPICKER
00695       cout << "added xx_swath_prescore (everything above 2 is good) " << xx_swath_prescore << endl;
00696 #endif
00697       delete imrmfeature;
00698     }
00699 
00700     // void handle_params();
00701 
00703     void updateMembers_();
00704 
00705     // Variables
00706     DoubleReal rt_extraction_window_;
00707     DoubleReal quantification_cutoff_;
00708 
00709     // Which scores to use
00710     bool use_coelution_score_;
00711     bool use_shape_score_;
00712     bool use_rt_score_;
00713     bool use_library_score_;
00714     bool use_elution_model_score_;
00715     bool use_intensity_score_;
00716     bool use_total_xic_score_;
00717     bool use_nr_peaks_score_;
00718     bool use_sn_score_;
00719 
00720     int stop_report_after_feature_;
00721     int add_up_spectra_;
00722     DoubleReal spacing_for_spectra_resampling_;
00723 
00724     // bool do_local_fdr_;
00725     bool write_convex_hull_;
00726     bool strict_;
00727 
00728     DoubleReal rt_normalization_factor_;
00729 
00730     std::map<OpenMS::String, double> PeptideRTMap_;
00731     std::map<OpenMS::String, const PeptideType*> PeptideRefMap_;
00732     std::map<OpenMS::String, const ProteinType*> ProteinRefMap_;
00733 
00734     OpenSwath::MRMScoring mrmscore_;
00735     OpenMS::DIAScoring diascoring_;
00736     OpenMS::EmgScoring emgscoring_;
00737   };
00738 }
00739 
00740 #undef run_identifier
00741 #endif

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