xicfilterdetectmatch.cpp 6.7 KB
Newer Older
Olivier Langella's avatar
Olivier Langella committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/*******************************************************************************
 * Copyright (c) 2016 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
 *
 * This file is part of the MassChroQ software.
 *
 *     MassChroQ is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     MassChroQ is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with MassChroQ.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and implementation
 ******************************************************************************/

#include "xicfilterdetectmatch.h"
#include <pappsomspp/xic/detection/xicdetectioninterface.h>
26
#include <pappsomspp/xic/xicpeak.h>
Olivier Langella's avatar
Olivier Langella committed
27
#include "../consoleout.h"
28
#include <cmath>
Olivier Langella's avatar
Olivier Langella committed
29

30
XicFilterDetectMatch::XicFilterDetectMatch(XicTraceBase * p_trace, MonitorSpeedInterface & monitor, const pappso::XicFilterInterface * p_xic_filter,const pappso::XicDetectionInterface * p_detection, McqMatchingMode matching_mode)
31
    :_monitor(monitor)
Olivier Langella's avatar
Olivier Langella committed
32
33
{
    _p_detection = p_detection;
Olivier Langella's avatar
Olivier Langella committed
34
    _p_xic_filter = p_xic_filter;
Olivier Langella's avatar
Olivier Langella committed
35
    _p_trace = p_trace;
36
    _matching_mode = matching_mode;
37
    if (_matching_mode == McqMatchingMode::post_matching) {
38
        _thin_peak_rt_tolerance = 0;
39
    } else {
40
        _thin_peak_rt_tolerance = 5;
41
    }
Olivier Langella's avatar
Olivier Langella committed
42
43
44
45
46
47
48
49
}

XicFilterDetectMatch::~XicFilterDetectMatch()
{

}

void XicFilterDetectMatch::setXicPeak (pappso::XicPeak & xic_peak) {
50
    //qDebug() << "XicFilterDetectMatch::setXicPeak begin" ;
Olivier Langella's avatar
Olivier Langella committed
51
52
53
54

    if (_p_trace != nullptr) {
        _p_trace->addXicPeak(xic_peak);
    }
55
    if (_peak_detected) return;
56
57

    if (_matching_mode == McqMatchingMode::post_matching) {
Olivier Langella's avatar
Olivier Langella committed
58
        //AlignedXicPeak aligned_peak(xic_peak, _p_current_msrun);
59
        AlignedXicPeakSp aligned_peak_sp = std::make_shared<const AlignedXicPeak>(AlignedXicPeak(xic_peak, _p_current_msrun));
Olivier Langella's avatar
Olivier Langella committed
60
        _p_current_search_item->storeAlignedXicPeakForPostMatching(_p_current_msrun, aligned_peak_sp);
61
62
63
64
        if (_rt_target == -1) {
            //the peptide was not observed in MSMS : we only need to store detected peaks to postmatch them
            return;
        }
65
    }
Olivier Langella's avatar
Olivier Langella committed
66
    if (xic_peak.containsRt(_rt_target)) {
67

Olivier Langella's avatar
Olivier Langella committed
68
69
70
71
        // qDebug() << "XicFilterDetectMatch::setXicPeak writeMatchedPeak" ;
        AlignedXicPeak aligned_peak(xic_peak, _p_current_msrun);
        _monitor.writeMatchedPeak(_p_current_msrun, _p_current_search_item, &aligned_peak);

72
        _peak_detected = true;
Olivier Langella's avatar
Olivier Langella committed
73
    }
74
75
    else {

76
77
78
79
80
81
82
83
        if (_matching_mode != McqMatchingMode::post_matching) {
            if ((xic_peak.getLeftBoundary().rt - _rt_target) < 10) {
                unsigned int distance_point = _p_current_xic->getMsPointDistance(_rt_target, xic_peak.getLeftBoundary().rt);
                if(distance_point == 0) {
                    AlignedXicPeak aligned_peak(xic_peak, _p_current_msrun);
                    _monitor.writeMatchedPeak(_p_current_msrun, _p_current_search_item, &aligned_peak);
                    _peak_detected = true;
                }
84

85
            }
86
87
        }
    }
Olivier Langella's avatar
Olivier Langella committed
88
89
90
    if (_peak_detected) {
        if (_p_trace != nullptr) {
            _p_trace->setMatchedPeak(xic_peak);
91
92
93
            AlignedXicPeak aligned_peak(xic_peak, _p_current_msrun);
            _p_trace->setAlignedMatchedPeak(aligned_peak);
        }
94
        if (_matching_mode == McqMatchingMode::post_matching) {
95
            AlignedXicPeak aligned_peak(xic_peak, _p_current_msrun);
96
            //store aligned rt of this matched peak
97
98
            _p_current_search_item->getPeptideRtSp().get()->postMatchingAlignedPeak(aligned_peak);
        }
99
100
    }
    else {
101
        if (std::abs(xic_peak.getMaxXicElement().rt - _rt_target) < _thin_peak_rt_tolerance) {
102
            //if this peak is nearby, in a 5 second range
Olivier Langella's avatar
Olivier Langella committed
103
            _p_log_peaks.push_back(xic_peak.makeXicPeakSp());
Olivier Langella's avatar
Olivier Langella committed
104
105
        }
    }
106

Olivier Langella's avatar
Olivier Langella committed
107
    // qDebug() << "XicFilterDetectMatch::setXicPeak end" ;
Olivier Langella's avatar
Olivier Langella committed
108
109
}

110
void XicFilterDetectMatch::filterDetectQuantify(const pappso::Xic & xic, pappso::pappso_double rt_target, QuantiItemBase * p_currentSearchItem, const Msrun * p_msrun) {
111

112
    qDebug() << "XicFilterDetectMatch::filterDetectQuantify begin" ;
113
    _p_current_search_item = p_currentSearchItem;
Olivier Langella's avatar
Olivier Langella committed
114
115
    _p_current_msrun = p_msrun;
    _rt_target = rt_target;
116
    _peak_detected = false;
117

118
    _p_current_xic = &xic;
119
    pappso::Xic newxic;
120
    if (_p_xic_filter != nullptr) {
121
122

        qDebug() << "XicFilterDetectMatch::filterDetectQuantify filter " << xic.size() << " " << newxic.size();
123
        _p_xic_filter->filter(xic, &newxic);
124
        qDebug() << "XicFilterDetectMatch::filterDetectQuantify filter 2 " << newxic.size();
125
        _p_current_xic = &newxic;
126
    }
127
    qDebug() << "XicFilterDetectMatch::filterDetectQuantify detect " << _rt_target;
128
    _p_detection->detect(*_p_current_xic,  this);
Olivier Langella's avatar
Olivier Langella committed
129
    qDebug() << "XicFilterDetectMatch::filterDetectQuantify after detect" ;
130
    if (_peak_detected == false) {
Olivier Langella's avatar
Olivier Langella committed
131
        if (_p_log_peaks.size() > 0) {
132
            //take the nearest peak : this should be a small peak, that does not cover the RT target
Olivier Langella's avatar
Olivier Langella committed
133
            const pappso::XicPeak * peak_i = _p_log_peaks.at(0).get();
134
            pappso::pappso_double rt_distance = std::abs(peak_i->getMaxXicElement().rt - _rt_target);
Olivier Langella's avatar
Olivier Langella committed
135
            for (unsigned int i=1; i < _p_log_peaks.size(); i++) {
136

Olivier Langella's avatar
Olivier Langella committed
137
                pappso::pappso_double new_rt_distance = std::abs(_p_log_peaks.at(i)->getMaxXicElement().rt - _rt_target);
138
                if (new_rt_distance < rt_distance) {
Olivier Langella's avatar
Olivier Langella committed
139
                    peak_i = _p_log_peaks.at(i).get();
140
141
142
143
                    rt_distance = new_rt_distance;
                }

            }
144

145
146
147
148
149
150
151
            _peak_detected = true;
            AlignedXicPeak aligned_peak(*peak_i, _p_current_msrun);
            _monitor.writeMatchedPeak(_p_current_msrun, _p_current_search_item, &aligned_peak);

        }
    }

152
153
    if (_peak_detected) {
        if (_matching_mode == McqMatchingMode::post_matching) {
154
            //if a peak was detected, that was from an observed peptide in this msrun, we don't need to store it to postmatch it
155
156
            _p_current_search_item->clearAlignedXicPeakForPostMatching(_p_current_msrun);
        }
Olivier Langella's avatar
Olivier Langella committed
157
    } else {
158
159
160
        if (_matching_mode != McqMatchingMode::post_matching) {
            _monitor.writeMatchedPeak(_p_current_msrun, _p_current_search_item, nullptr);
        }
161
    }
Olivier Langella's avatar
Olivier Langella committed
162

Olivier Langella's avatar
Olivier Langella committed
163
    qDebug() << "XicFilterDetectMatch::filterDetectQuantify end" ;
Olivier Langella's avatar
Olivier Langella committed
164
}