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

« back to all changes in this revision

Viewing changes to source/APPLICATIONS/TOPP/NoiseFilterSGolay.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: Alexandra Zerck $
 
25
// $Authors: Eva Lange $
 
26
// --------------------------------------------------------------------------
 
27
#include <OpenMS/config.h>
 
28
 
 
29
#include <OpenMS/FILTERING/SMOOTHING/SavitzkyGolayFilter.h>
 
30
#include <OpenMS/FORMAT/MzMLFile.h>
 
31
#include <OpenMS/KERNEL/MSExperiment.h>
 
32
#include <OpenMS/APPLICATIONS/TOPPBase.h>
 
33
#include <OpenMS/FORMAT/PeakTypeEstimator.h>
 
34
#include <OpenMS/DATASTRUCTURES/StringList.h>
 
35
 
 
36
using namespace OpenMS;
 
37
using namespace std;
 
38
 
 
39
//-------------------------------------------------------------
 
40
//Doxygen docu
 
41
//-------------------------------------------------------------
 
42
 
 
43
/**
 
44
        @page TOPP_NoiseFilterSGolay NoiseFilterSGolay
 
45
 
 
46
        @brief  Executes a Savitzky Golay filter to reduce the noise in an MS experiment.
 
47
 
 
48
<CENTER>
 
49
        <table>
 
50
                <tr>
 
51
                        <td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. predecessor tools </td>
 
52
      <td VALIGN="middle" ROWSPAN=4> \f$ \longrightarrow \f$ NoiseFilterSGolay \f$ \longrightarrow \f$</td>
 
53
                        <td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. successor tools </td>
 
54
                </tr>
 
55
                <tr>
 
56
                        <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_FileConverter </td>
 
57
      <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_PeakPickerWavelet</td>
 
58
                </tr>
 
59
    <tr>
 
60
      <td VALIGN="middle" ALIGN = "center" ROWSPAN=2> @ref TOPP_Resampler </td>
 
61
      <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_PeakPickerHiRes</td>
 
62
    </tr>
 
63
    <tr>
 
64
      <td VALIGN="middle" ALIGN = "center" ROWSPAN=1> @ref TOPP_BaselineFilter</td>
 
65
    </tr>
 
66
        </table>
 
67
</CENTER>
 
68
 
 
69
 
 
70
        The idea of the Savitzky Golay filter is to find filter-coefficients
 
71
        that preserve higher moments, which means to approximate the underlying
 
72
        function within the moving window by a polynomial of higher order
 
73
        (typically quadratic or quartic) (see A. Savitzky and M. J. E. Golay,
 
74
        ''Smoothing and Differentiation of Data by Simplified Least Squares Procedures'').
 
75
 
 
76
        @note The Savitzky Golay filter works only on uniform data (to generate equally spaced data use the @ref TOPP_Resampler tool).
 
77
 
 
78
        <B>The command line parameters of this tool are:</B>
 
79
  @verbinclude TOPP_NoiseFilterSGolay.cli
 
80
 
 
81
        <B>The algorithm parameters for the Savitzky Golay filter are:</B>
 
82
  @htmlinclude OpenMS_SavitzkyGolayFilter.parameters
 
83
*/
 
84
 
 
85
// We do not want this class to show up in the docu:
 
86
/// @cond TOPPCLASSES
 
87
 
 
88
 
 
89
class TOPPNoiseFilterSGolay
 
90
      : public TOPPBase
 
91
{
 
92
  public:
 
93
    TOPPNoiseFilterSGolay()
 
94
        : TOPPBase("NoiseFilterSGolay","Removes noise from profile spectra by using a Savitzky Golay filter.")
 
95
    {
 
96
    }
 
97
 
 
98
    void registerOptionsAndFlags_()
 
99
    {
 
100
                registerInputFile_("in","<file>","","input raw data file ");
 
101
                        setValidFormats_("in",StringList::create("mzML"));
 
102
                        registerOutputFile_("out","<file>","","output raw data file ");
 
103
                setValidFormats_("out",StringList::create("mzML"));
 
104
                        addEmptyLine_();
 
105
                addText_("Parameters for the algorithms can be given in the INI file only.");
 
106
                        addEmptyLine_();
 
107
                        addText_("Note: The Savitzky Golay filter works only on uniform data (to generate equally spaced data use the Resampler tool).");
 
108
        registerSubsection_("algorithm","Algorithm parameters section");
 
109
    }
 
110
 
 
111
    Param getSubsectionDefaults_(const String& /*section*/) const
 
112
    {
 
113
      return SavitzkyGolayFilter().getDefaults();
 
114
    }
 
115
 
 
116
    ExitCodes main_(int , const char**)
 
117
    {
 
118
      //-------------------------------------------------------------
 
119
      // parameter handling
 
120
      //-------------------------------------------------------------
 
121
      String in = getStringOption_("in");
 
122
      String out = getStringOption_("out");
 
123
 
 
124
      //-------------------------------------------------------------
 
125
      // loading input
 
126
      //-------------------------------------------------------------
 
127
      MzMLFile mz_data_file;
 
128
      mz_data_file.setLogType(log_type_);
 
129
      MSExperiment<Peak1D> exp;
 
130
      mz_data_file.load(in,exp);
 
131
 
 
132
                        if (exp.empty())
 
133
                        {
 
134
                                LOG_WARN << "The given file does not contain any conventional peak data, but might"
 
135
                                                  " contain chromatograms. This tool currently cannot handle them, sorry.";
 
136
                                return INCOMPATIBLE_INPUT_DATA;
 
137
                        }
 
138
                        //check for peak type (profile data required)
 
139
                        if (PeakTypeEstimator().estimateType(exp[0].begin(),exp[0].end())==SpectrumSettings::PEAKS)
 
140
                        {
 
141
                                writeLog_("Warning: OpenMS peak type estimation indicates that this is not profile data!");
 
142
                        }
 
143
 
 
144
                        //check if spectra are sorted
 
145
                        for (Size i=0; i< exp.size(); ++i)
 
146
                        {
 
147
                                if (!exp[i].isSorted())
 
148
                                {
 
149
                                        writeLog_("Error: Not all spectra are sorted according to peak m/z positions. Use FileFilter to sort the input!");
 
150
                                        return INCOMPATIBLE_INPUT_DATA;
 
151
                                }
 
152
                        }
 
153
 
 
154
      //-------------------------------------------------------------
 
155
      // calculations
 
156
      //-------------------------------------------------------------
 
157
        Param filter_param = getParam_().copy("algorithm:",true);
 
158
                        writeDebug_("Parameters passed to filter", filter_param,3);
 
159
 
 
160
      SavitzkyGolayFilter sgolay;
 
161
      sgolay.setLogType(log_type_);
 
162
      sgolay.setParameters( filter_param );
 
163
      sgolay.filterExperiment(exp);
 
164
 
 
165
      //-------------------------------------------------------------
 
166
      // writing output
 
167
      //-------------------------------------------------------------
 
168
      
 
169
      //annotate output with data processing info
 
170
      addDataProcessing_(exp, getProcessingInfo_(DataProcessing::SMOOTHING));
 
171
 
 
172
      mz_data_file.store(out,exp);
 
173
 
 
174
      return EXECUTION_OK;
 
175
    }
 
176
};
 
177
 
 
178
 
 
179
int main( int argc, const char** argv )
 
180
{
 
181
  TOPPNoiseFilterSGolay tool;
 
182
  return tool.main(argc,argv);
 
183
}
 
184
 
 
185
/// @endcond