void ElutionPeakDetection::smoothData(MassTrace& mt, int win_size) const
  {
    // alternative smoothing using SavitzkyGolay
    // looking at the unit test, this method gives better fits than lowess smoothing
    // reference paper uses lowess smoothing

    MSSpectrum<PeakType> spectrum;
    spectrum.insert(spectrum.begin(), mt.begin(), mt.end());
    SavitzkyGolayFilter sg;
    Param param;
    param.setValue("polynomial_order", 2);
    param.setValue("frame_length", std::max(3, win_size)); // frame length must be at least polynomial_order+1, otherwise SG will fail
    sg.setParameters(param);
    sg.filter(spectrum);
    MSSpectrum<PeakType>::iterator iter = spectrum.begin();
    std::vector<double> smoothed_intensities;
    for (; iter != spectrum.end(); ++iter)
    {
      smoothed_intensities.push_back(iter->getIntensity());
    }
    mt.setSmoothedIntensities(smoothed_intensities);
    //alternative end

    // std::cout << "win_size elution: " << scan_time << " " << win_size << std::endl;

    // if there is no previous FWHM estimation... do it now
    //    if (win_size == 0)
    //    {
    //        mt.estimateFWHM(false); // estimate FWHM
    //        win_size = mt.getFWHMScansNum();
    //    }

    // use one global window size for all mass traces to smooth
    //  std::vector<double> rts, ints;
    //
    //  for (MassTrace::const_iterator c_it = mt.begin(); c_it != mt.end(); ++c_it)
    //  {
    //      rts.push_back(c_it->getRT());
    //      ints.push_back(c_it->getIntensity());
    //  }
    //  LowessSmoothing lowess_smooth;
    //  Param lowess_params;
    //  lowess_params.setValue("window_size", win_size);
    //  lowess_smooth.setParameters(lowess_params);
    //  std::vector<double> smoothed_data;
    //  lowess_smooth.smoothData(rts, ints, smoothed_data);
    //  mt.setSmoothedIntensities(smoothed_data);
  }
Beispiel #2
0
void ElutionPeakDetection::detectElutionPeaks_(MassTrace & mt, std::vector<MassTrace> & single_mtraces)
{
    std::vector<DoubleReal> rts, ints;

    for (MassTrace::const_iterator c_it = mt.begin(); c_it != mt.end(); ++c_it)
    {
        rts.push_back(c_it->getRT());
        ints.push_back(c_it->getIntensity());
    }

    std::vector<DoubleReal> smoothed_data;


    LowessSmoothing lowess_smooth;
    Param lowess_params;

    // use dynamically computed window sizes

    // Size win_size = mt.getFWHMScansNum();

    // use one global window size for all mass traces to smooth
    DoubleReal scan_time(mt.getScanTime());
    Size win_size = std::ceil(chrom_fwhm_ / scan_time);

    // std::cout << "win_size elution: " << scan_time << " " << win_size << std::endl;

    // if there is no previous FWHM estimation... do it now
    //    if (win_size == 0)
    //    {
    //        mt.estimateFWHM(false); // estimate FWHM
    //        win_size = mt.getFWHMScansNum();
    //    }

    lowess_params.setValue("window_size", win_size);
    lowess_smooth.setParameters(lowess_params);

    lowess_smooth.smoothData(rts, ints, smoothed_data);

    mt.setSmoothedIntensities(smoothed_data);

    // debug intensities

    // Size i = 0;

    //    std::cout << "*****" << std::endl;
    //    for (MassTrace::const_iterator mt_it = mt.begin(); mt_it != mt.end(); ++mt_it)
    //    {
    //        std::cout << mt_it->getIntensity() << " " << smoothed_data[i] << std::endl;
    //        ++i;
    //    }
    //std::cout << "*****" << std::endl;

    std::vector<Size> maxes, mins;

    // mt.findLocalExtrema(win_size / 2, maxes, mins);

    findLocalExtrema(mt, win_size/2, maxes, mins);

    // if only one maximum exists: finished!
    if (maxes.size() == 1)
    {
        bool pw_ok = true;
        bool snr_ok = true;

        // check mass trace filter criteria (if enabled)
        if (pw_filtering_ == "fixed")
        {
            DoubleReal act_fwhm(mt.estimateFWHM(true));

            // std::cout << "act_fwhm: " << act_fwhm << " ";

            if (act_fwhm < min_fwhm_ || act_fwhm > max_fwhm_)
            {
                pw_ok = false;
            }

            // std::cout << pw_ok << std::endl;
        }

        if (mt_snr_filtering_)
        {
            if (computeApexSNR(mt) < chrom_peak_snr_)
            {
                snr_ok = false;
            }
        }


        if (pw_ok && snr_ok)
        {
            mt.updateSmoothedMaxRT();

            if (pw_filtering_ != "fixed")
            {
                mt.estimateFWHM(true);
            }

            // check for minimum/maximum trace length
            //          DoubleReal mt_length(std::fabs(mt.rbegin()->getRT() - mt.begin()->getRT()));

            //        if ((mt_length >= min_trace_length_) && (mt_length <= max_trace_length_))
            // if (mt_quality >= 1.2)
            //      {
#ifdef _OPENMP
#pragma omp critical
#endif
            single_mtraces.push_back(mt);

        }
    }
    else if (maxes.empty())
    {
        return;
    }
    else // split mt to subtraces
    {
        MassTrace::const_iterator cp_it = mt.begin();
        Size last_idx(0);

        for (Size min_idx = 0; min_idx < mins.size(); ++min_idx)
        {
            // copy subtrace between cp_it and splitpoint
            std::vector<PeakType> tmp_mt;
            std::vector<DoubleReal> smoothed_tmp;

            while (last_idx <= mins[min_idx])
            {
                tmp_mt.push_back(*cp_it);
                smoothed_tmp.push_back(mt.getSmoothedIntensities()[last_idx]);
                ++cp_it;
                ++last_idx;
            }

            // check if

//            if (tmp_mt.size() >= win_size / 2)
//            {
                DoubleReal scantime(mt.getScanTime());
                MassTrace new_mt(tmp_mt, scantime);

                // copy smoothed ints
                new_mt.setSmoothedIntensities(smoothed_tmp);


                // check filter criteria
                bool pw_ok = true;
                bool snr_ok = true;

                // check mass trace filter criteria (if enabled)
                if (pw_filtering_ == "fixed")
                {
                    DoubleReal act_fwhm(new_mt.estimateFWHM(true));

                    // std::cout << "act_fwhm: " << act_fwhm << " ";

                    if (act_fwhm < min_fwhm_ || act_fwhm > max_fwhm_)
                    {
                        pw_ok = false;
                    }

                    // std::cout << pw_ok << std::endl;
                }

                if (mt_snr_filtering_)
                {
                    if (computeApexSNR(mt) < chrom_peak_snr_)
                    {
                        snr_ok = false;
                    }
                }


                if (pw_ok && snr_ok)
                {

                    // set label of subtrace
                    String tr_num;
                    std::stringstream read_in;
                    read_in << (min_idx + 1);
                    tr_num = "." + read_in.str();

                    new_mt.setLabel(mt.getLabel() + tr_num);
                    //new_mt.updateWeightedMeanRT();
                    new_mt.updateSmoothedMaxRT();
                    //new_mt.updateSmoothedWeightedMeanRT();
                    new_mt.updateWeightedMeanMZ();
                    new_mt.updateWeightedMZsd();

                    if (pw_filtering_ != "fixed")
                    {
                        new_mt.estimateFWHM(true);
                    }
                    // DoubleReal mt_quality(computeApexSNR(new_mt));

                    // DoubleReal new_mt_length(std::fabs(new_mt.rbegin()->getRT() - new_mt.begin()->getRT()));

                    // if ((new_mt_length >= min_trace_length_) && (new_mt_length <= max_trace_length_))
                    //{
#ifdef _OPENMP
#pragma omp critical
#endif
                    single_mtraces.push_back(new_mt);
                }
          //  }
        }

        // don't forget the trailing trace
        std::vector<PeakType> tmp_mt;

        std::vector<DoubleReal> smoothed_tmp;

        while (last_idx < mt.getSize())
        {
            tmp_mt.push_back(*cp_it);
            smoothed_tmp.push_back(mt.getSmoothedIntensities()[last_idx]);
            ++cp_it;
            ++last_idx;
        }

//        if (tmp_mt.size() >= win_size / 2)
//        {
            DoubleReal scantime(mt.getScanTime());
            MassTrace new_mt(tmp_mt, scantime);

            // copy smoothed ints
            new_mt.setSmoothedIntensities(smoothed_tmp);

            // check filter criteria
            bool pw_ok = true;
            bool snr_ok = true;

            // check mass trace filter criteria (if enabled)
            if (pw_filtering_ == "fixed")
            {
                DoubleReal act_fwhm(new_mt.estimateFWHM(true));

                // std::cout << "act_fwhm: " << act_fwhm << " ";

                if (act_fwhm < min_fwhm_ || act_fwhm > max_fwhm_)
                {
                    pw_ok = false;
                }

                // std::cout << pw_ok << std::endl;
            }

            if (mt_snr_filtering_)
            {
                if (computeApexSNR(mt) < chrom_peak_snr_)
                {
                    snr_ok = false;
                }
            }


            if (pw_ok && snr_ok)
            {
                // set label of subtrace
                String tr_num;
                std::stringstream read_in;
                read_in << (mins.size() + 1);
                tr_num = "." + read_in.str();

                new_mt.setLabel(mt.getLabel() + tr_num);
                new_mt.updateSmoothedMaxRT();
                new_mt.updateWeightedMeanMZ();
                new_mt.updateWeightedMZsd();

                if (pw_filtering_ != "fixed")
                {
                    new_mt.estimateFWHM(true);
                }
                // DoubleReal mt_quality(computeApexSNR(new_mt));

                //                DoubleReal mt_length(std::fabs(new_mt.rbegin()->getRT() - new_mt.begin()->getRT()));

                //                if ((mt_length >= min_trace_length_) && (mt_length <= max_trace_length_))
                //                {
#ifdef _OPENMP
#pragma omp critical
#endif
                single_mtraces.push_back(new_mt);
            }
     //   }
    }
    return;
}