~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/APPLICATIONS/TOPP/MassTraceExtractor.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Erhan Kenar $
 
25
// $Authors: Erhan Kenar, Holger Franken $
 
26
// --------------------------------------------------------------------------
 
27
#include <OpenMS/FORMAT/MzMLFile.h>
 
28
#include <OpenMS/FORMAT/PeakFileOptions.h>
 
29
#include <OpenMS/FORMAT/FeatureXMLFile.h>
 
30
#include <OpenMS/KERNEL/MSExperiment.h>
 
31
#include <OpenMS/KERNEL/FeatureMap.h>
 
32
#include <OpenMS/KERNEL/MassTrace.h>
 
33
#include <OpenMS/FILTERING/DATAREDUCTION/MassTraceDetection.h>
 
34
#include <OpenMS/FILTERING/DATAREDUCTION/ElutionPeakDetection.h>
 
35
#include <OpenMS/APPLICATIONS/TOPPBase.h>
 
36
 
 
37
using namespace OpenMS;
 
38
using namespace std;
 
39
 
 
40
//-------------------------------------------------------------
 
41
//Doxygen docu
 
42
//-------------------------------------------------------------
 
43
 
 
44
/**
 
45
        @page TOPP_MassTraceExtractor MassTraceExtractor
 
46
 
 
47
        @brief MassTraceExtractor extracts mass traces from a @ref MSExperiment map and stores them into a @ref FeatureXMLFile.
 
48
 
 
49
        <CENTER>
 
50
        <table>
 
51
        <tr>
 
52
        <td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. predecessor tools </td>
 
53
        <td VALIGN="middle" ROWSPAN=3> \f$ \longrightarrow \f$ MassTraceExtractor \f$ \longrightarrow \f$</td>
 
54
        <td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. successor tools </td>
 
55
        </tr>
 
56
        <tr>
 
57
        <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_PeakPickerHiRes </td>
 
58
        <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_FeatureFinderMetabo</td>
 
59
        </tr>
 
60
        <tr>
 
61
        <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_PeakPickerWavelet </td>
 
62
        <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_TextExporter </td>
 
63
        </tr>
 
64
        </table>
 
65
        </CENTER>
 
66
 
 
67
 
 
68
        This TOPP tool detects mass traces in centroided LC-MS maps and stores them as features in
 
69
        a @ref FeatureMap. These features may be either used directly as input for an metabolite ID approach or further
 
70
        be assembled to aggregate features according to a theoretical isotope pattern. For metabolomics experiments,
 
71
        the @ref TOPP_FeatureFinderMetabo tool offers both mass trace extraction and isotope pattern assembly.
 
72
        For proteomics data, please refer to the @ref TOPP_FeatureFinderCentroided tool.
 
73
 
 
74
        <B>The command line parameters of this tool are:</B>
 
75
        @verbinclude TOPP_MassTraceExtractor.cli
 
76
*/
 
77
 
 
78
// We do not want this class to show up in the docu:
 
79
/// @cond TOPPCLASSES
 
80
 
 
81
class TOPPMassTraceExtractor
 
82
        : public TOPPBase
 
83
{
 
84
public:
 
85
    TOPPMassTraceExtractor()
 
86
        : TOPPBase("MassTraceExtractor", "Detects mass traces in centroided LC-MS data.")
 
87
    {
 
88
    }
 
89
 
 
90
protected:
 
91
 
 
92
    void registerOptionsAndFlags_()
 
93
    {
 
94
        registerInputFile_("in","<file>", "", "input centroided mzML file");
 
95
        setValidFormats_("in",StringList::create("mzML"));
 
96
        registerOutputFile_("out", "<file>", "", "output featureXML file with mass traces");
 
97
        setValidFormats_("out",StringList::create("featureXML"));
 
98
 
 
99
        addEmptyLine_();
 
100
        addText_("Parameters for the mass trace detection algorithm can be given in the 'algorithm' part of INI file.");
 
101
        registerSubsection_("algorithm","Algorithm parameters section");
 
102
 
 
103
    }
 
104
 
 
105
    Param getSubsectionDefaults_(const String& /*section*/) const
 
106
    {
 
107
        Param combined;
 
108
 
 
109
        Param p_com;
 
110
        p_com.setValue("chrom_fwhm" , 0.0 , "Allows filtering of mass traces with peak width (in seconds) less than this threshold. Disabled by default (set to 0.0).");
 
111
        combined.insert("common:", p_com);
 
112
 
 
113
        Param p_mtd = MassTraceDetection().getDefaults();
 
114
        p_mtd.remove("chrom_fwhm");
 
115
        combined.insert("mtd:", p_mtd);
 
116
 
 
117
        Param p_epd = ElutionPeakDetection().getDefaults();
 
118
        p_epd.remove("chrom_fwhm");
 
119
        p_epd.setValue("enabled", "true", "Switches on/off the detection of elution peaks");
 
120
        p_epd.setValidStrings("enabled", StringList::create("true,false"));
 
121
        combined.insert("epd:", p_epd);
 
122
 
 
123
        return combined;
 
124
    }
 
125
 
 
126
    ExitCodes main_(int , const char**)
 
127
    {
 
128
 
 
129
        //-------------------------------------------------------------
 
130
        // parameter handling
 
131
        //-------------------------------------------------------------
 
132
 
 
133
        String in = getStringOption_("in");
 
134
        String out = getStringOption_("out");
 
135
 
 
136
        //-------------------------------------------------------------
 
137
        // loading input
 
138
        //-------------------------------------------------------------
 
139
        MzMLFile mz_data_file;
 
140
        mz_data_file.setLogType(log_type_);
 
141
        MSExperiment<Peak1D> ms_peakmap;
 
142
        std::vector<Int> ms_level(1, 1);
 
143
        (mz_data_file.getOptions()).setMSLevels(ms_level);
 
144
        mz_data_file.load(in, ms_peakmap);
 
145
 
 
146
        if (ms_peakmap.size()==0)
 
147
        {
 
148
            LOG_WARN << "The given file does not contain any conventional peak data, but might"
 
149
                        " contain chromatograms. This tool currently cannot handle them, sorry.";
 
150
            return INCOMPATIBLE_INPUT_DATA;
 
151
        }
 
152
 
 
153
 
 
154
        FeatureMap<> ms_feat_map;
 
155
        vector<MassTrace> m_traces;
 
156
 
 
157
        //-------------------------------------------------------------
 
158
        // get params for MTD and EPD algorithms
 
159
        //-------------------------------------------------------------
 
160
        Param com_param = getParam_().copy("algorithm:common:",true);
 
161
        writeDebug_("Common parameters passed to both subalgorithms (mtd and epd)", com_param,3);
 
162
 
 
163
        Param mtd_param = getParam_().copy("algorithm:mtd:",true);
 
164
        writeDebug_("Parameters passed to MassTraceDetection", mtd_param,3);
 
165
 
 
166
        Param epd_param = getParam_().copy("algorithm:epd:",true);
 
167
        writeDebug_("Parameters passed to ElutionPeakDetection", epd_param,3);
 
168
 
 
169
 
 
170
        //-------------------------------------------------------------
 
171
        // configure and run MTD
 
172
        //-------------------------------------------------------------
 
173
 
 
174
        MassTraceDetection mt_ext;
 
175
        mtd_param.insert("", com_param);
 
176
        mt_ext.setParameters(mtd_param);
 
177
        mt_ext.run(ms_peakmap, m_traces);
 
178
        
 
179
        vector<MassTrace> m_traces_final = m_traces;
 
180
 
 
181
        bool use_epd = epd_param.getValue("enabled").toBool();
 
182
 
 
183
        if (use_epd)
 
184
        {
 
185
            ElutionPeakDetection ep_det;
 
186
 
 
187
            epd_param.remove("enabled"); // artificially added above
 
188
            epd_param.insert("", com_param);
 
189
 
 
190
            ep_det.setParameters(epd_param);
 
191
 
 
192
            std::vector<MassTrace> splitted_mtraces;
 
193
 
 
194
            ep_det.detectPeaks(m_traces, splitted_mtraces);
 
195
 
 
196
            if (ep_det.getParameters().getValue("width_filtering").toBool())
 
197
            {
 
198
                m_traces_final.clear();
 
199
                ep_det.filterByPeakWidth(splitted_mtraces, m_traces_final);
 
200
 
 
201
                LOG_INFO << "Notice: " << splitted_mtraces.size() - m_traces_final.size() << " of total " << splitted_mtraces.size() << " were dropped because of too low peak width." << std::endl;
 
202
            }
 
203
            else
 
204
            {
 
205
                m_traces_final = splitted_mtraces;
 
206
            }
 
207
        }
 
208
 
 
209
        //-----------------------------------------------------------
 
210
        // convert mass traces to features
 
211
        //-----------------------------------------------------------
 
212
 
 
213
        for (Size i = 0; i < m_traces_final.size(); ++i)
 
214
        {
 
215
            if (m_traces_final[i].getSize() == 0) continue;
 
216
 
 
217
            Feature f;
 
218
            f.setMetaValue(3, m_traces_final[i].getLabel());
 
219
            f.setCharge(0);
 
220
            f.setMZ(m_traces_final[i].getCentroidMZ());
 
221
            f.setIntensity(m_traces_final[i].computePeakArea());
 
222
            f.setRT(m_traces_final[i].getCentroidRT());
 
223
            f.setWidth(m_traces_final[i].estimateFWHM(use_epd));
 
224
            f.setOverallQuality(1 - (1.0/m_traces_final[i].getSize()));
 
225
            f.getConvexHulls().push_back(m_traces_final[i].getConvexhull());
 
226
 
 
227
            ms_feat_map.push_back(f);
 
228
        }
 
229
 
 
230
        ms_feat_map.applyMemberFunction(&UniqueIdInterface::setUniqueId);
 
231
 
 
232
        //-------------------------------------------------------------
 
233
        // writing output
 
234
        //-------------------------------------------------------------
 
235
 
 
236
        //annotate output with data processing info TODO
 
237
        addDataProcessing_(ms_feat_map, getProcessingInfo_(DataProcessing::QUANTITATION));
 
238
        ms_feat_map.setUniqueId();
 
239
 
 
240
        FeatureXMLFile().store(out, ms_feat_map);
 
241
 
 
242
        return EXECUTION_OK;
 
243
    }
 
244
};
 
245
 
 
246
 
 
247
int main( int argc, const char** argv )
 
248
{
 
249
    TOPPMassTraceExtractor tool;
 
250
    return tool.main(argc, argv);
 
251
}
 
252
 
 
253
/// @endcond