OpenMS
Loading...
Searching...
No Matches
MapAlignerBase.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Hendrik Weisser $
6// $Authors: Marc Sturm, Clemens Groepl, Hendrik Weisser $
7// --------------------------------------------------------------------------
8
9#pragma once
10
13
16
21
24
25#include <vector>
26
27//-------------------------------------------------------------
28// Doxygen docu
29//-------------------------------------------------------------
30
37// We do not want this class to show up in the docu:
39
40namespace OpenMS
41{
42 // @brief stores model defaults for map aligner algorithms
43 struct MapAlignerBase
44 {
45 // "public" so it can be used in DefaultParamHandlerDocumenter to get docu
46 static Param getModelDefaults(const String& default_model)
47 {
48 Param params;
49 params.setValue("type", default_model, "Type of model");
50 // TODO: avoid referring to each TransformationModel subclass explicitly
51 std::vector<std::string> model_types = {"linear","b_spline","lowess","interpolated"};
52 if (!ListUtils::contains(model_types, default_model))
53 {
54 model_types.insert(model_types.begin(), default_model);
55 }
56 params.setValidStrings("type", model_types);
57
58 Param model_params;
59 TransformationModelLinear::getDefaultParameters(model_params);
60 params.insert("linear:", model_params);
61 params.setSectionDescription("linear", "Parameters for 'linear' model");
62
63 TransformationModelBSpline::getDefaultParameters(model_params);
64 params.insert("b_spline:", model_params);
65 params.setSectionDescription("b_spline", "Parameters for 'b_spline' model");
66
67 TransformationModelLowess::getDefaultParameters(model_params);
68 params.insert("lowess:", model_params);
69 params.setSectionDescription("lowess", "Parameters for 'lowess' model");
70
71 TransformationModelInterpolated::getDefaultParameters(model_params);
72 params.insert("interpolated:", model_params);
73 params.setSectionDescription("interpolated",
74 "Parameters for 'interpolated' model");
75 return params;
76 }
77 };
78
79
80class TOPPMapAlignerBase :
81 public TOPPBase, public MapAlignerBase
82{
83
84public:
85 TOPPMapAlignerBase(String name, String description, bool official = true) :
86 TOPPBase(name, description, official), ref_params_(REF_NONE)
87 {
88 }
89
90protected:
91
92 // Kind of reference parameters that the tool offers:
93 // - REF_NONE: no reference
94 // - REF_RESTRICTED: reference file must have same type as input files
95 // - REF_FLEXIBLE: reference file can have any supported file type
96 enum ReferenceParameterKind { REF_NONE, REF_RESTRICTED, REF_FLEXIBLE }
97 ref_params_;
98
99 void registerOptionsAndFlagsMapAligners_(const String& file_formats,
100 enum ReferenceParameterKind ref_params)
101 {
102 registerInputFileList_("in", "<files>", StringList(), "Input files to align (all must have the same file type)", true);
103 setValidFormats_("in", ListUtils::create<String>(file_formats));
104 registerOutputFileList_("out", "<files>", StringList(), "Output files (same file type as 'in'). This option or 'trafo_out' has to be provided; they can be used together.", false);
105 setValidFormats_("out", ListUtils::create<String>(file_formats));
106 registerOutputFileList_("trafo_out", "<files>", StringList(), "Transformation output files. This option or 'out' has to be provided; they can be used together.", false);
107 setValidFormats_("trafo_out", ListUtils::create<String>("trafoXML"));
108
109 // Optional spectra files for transformation
110 registerInputFileList_("in_spectra_files", "<files>", StringList(), "Optional input spectra files (mzML) that will be transformed along with the alignment. Size must match the number of input files.", false);
111 setValidFormats_("in_spectra_files", ListUtils::create<String>("mzML"));
112 registerOutputFileList_("out_spectra_files", "<files>", StringList(), "Optional output spectra files (mzML) corresponding to transformed in_spectra_files. Size must match in_spectra_files.", false);
113 setValidFormats_("out_spectra_files", ListUtils::create<String>("mzML"));
114
115 if (ref_params != REF_NONE)
116 {
117 registerTOPPSubsection_("reference", "Options to define a reference file (use either 'file' or 'index', not both)");
118 String description = "File to use as reference";
119 if (ref_params == REF_RESTRICTED)
120 {
121 description += " (same file format as input files required)";
122 }
123 registerInputFile_("reference:file", "<file>", "", description, false);
124 setValidFormats_("reference:file", ListUtils::create<String>(file_formats));
125 registerIntOption_("reference:index", "<number>", 0, "Use one of the input files as reference ('1' for the first file, etc.).\nIf '0', no explicit reference is set - the algorithm will select a reference.", false);
126 setMinInt_("reference:index", 0);
127 }
128 ref_params_ = ref_params;
129 }
130
131 ExitCodes checkParameters_()
132 {
133 //-------------------------------------------------------------
134 // parameter handling
135 //-------------------------------------------------------------
136 StringList ins = getStringList_("in");
137 StringList outs = getStringList_("out");
138 StringList trafos = getStringList_("trafo_out");
139 StringList in_spectra = getStringList_("in_spectra_files");
140 StringList out_spectra = getStringList_("out_spectra_files");
141
142 //-------------------------------------------------------------
143 // check for valid input
144 //-------------------------------------------------------------
145 // check whether some kind of output file is given:
146 if (outs.empty() && trafos.empty())
147 {
148 writeLogError_("Error: Data output or transformation output files have to be provided (parameters 'out'/'trafo_out')");
149 return ILLEGAL_PARAMETERS;
150 }
151 // check whether number of input files equals number of output files:
152 if (!outs.empty() && (ins.size() != outs.size()))
153 {
154 writeLogError_("Error: The number of data input and output files has to be equal (parameters 'in'/'out')");
155 return ILLEGAL_PARAMETERS;
156 }
157 if (!trafos.empty() && (ins.size() != trafos.size()))
158 {
159 writeLogError_("Error: The number of data input and transformation output files has to be equal (parameters 'in'/'trafo_out')");
160 return ILLEGAL_PARAMETERS;
161 }
162 // check whether all input files have the same type (this type is used to store the output type too):
163 FileTypes::Type in_type = FileHandler::getType(ins[0]);
164 for (Size i = 1; i < ins.size(); ++i)
165 {
166 if (FileHandler::getType(ins[i]) != in_type)
167 {
168 writeLogError_("Error: All input files (parameter 'in') must have the same format!");
169 return ILLEGAL_PARAMETERS;
170 }
171 }
172
173 // check optional spectra files
174 if (!in_spectra.empty() || !out_spectra.empty())
175 {
176 if (in_spectra.size() != ins.size())
177 {
178 writeLogError_("Error: The number of spectra input files has to be equal to the number of main input files (parameters 'in_spectra_files'/'in')");
179 return ILLEGAL_PARAMETERS;
180 }
181 if (out_spectra.size() != in_spectra.size())
182 {
183 writeLogError_("Error: The number of spectra input and output files has to be equal (parameters 'in_spectra_files'/'out_spectra_files')");
184 return ILLEGAL_PARAMETERS;
185 }
186 }
187
188 if (ref_params_ != REF_NONE) // a valid ref. index OR file should be given
189 {
190 Size reference_index = getIntOption_("reference:index");
191 String reference_file = getStringOption_("reference:file");
192 if (reference_index > ins.size())
193 {
194 writeLogError_("Error: Value of parameter 'reference:index' must not be higher than the number of input files");
195 return ILLEGAL_PARAMETERS;
196 }
197 if (reference_index && !reference_file.empty())
198 {
199 writeLogError_("Error: Parameters 'reference:index' and 'reference:file' cannot be used together");
200 return ILLEGAL_PARAMETERS;
201 }
202
203 if ((ref_params_ == REF_RESTRICTED) && !reference_file.empty() &&
204 (FileHandler::getType(reference_file) != in_type))
205 {
206 writeLogError_("Error: Reference file must have the same format as other input files (parameters 'reference:file'/'in')");
207 return ILLEGAL_PARAMETERS;
208 }
209 }
210
211 return EXECUTION_OK;
212 }
213
214 void transformSpectraFiles_(const StringList& in_spectra_files,
215 const StringList& out_spectra_files,
216 const std::vector<TransformationDescription>& transformations,
217 bool store_original_rt)
218 {
219 if (in_spectra_files.empty() || out_spectra_files.empty())
220 {
221 return; // Nothing to do
222 }
223
224 OPENMS_PRECONDITION(in_spectra_files.size() == transformations.size(),
225 "Number of spectra files must match number of transformations");
226 OPENMS_PRECONDITION(in_spectra_files.size() == out_spectra_files.size(),
227 "Number of input and output spectra files must match");
228
229 ProgressLogger progresslogger;
230 progresslogger.setLogType(log_type_);
231 progresslogger.startProgress(0, in_spectra_files.size(), "transforming spectra files");
232
233 for (Size i = 0; i < in_spectra_files.size(); ++i)
234 {
235 progresslogger.setProgress(i);
236
237 PeakMap exp;
238 FileHandler().loadExperiment(in_spectra_files[i], exp, {FileTypes::MZML}, log_type_);
239
240 MapAlignmentTransformer::transformRetentionTimes(exp, transformations[i], store_original_rt);
241
242 addDataProcessing_(exp, getProcessingInfo_(DataProcessing::ALIGNMENT));
243 FileHandler().storeExperiment(out_spectra_files[i], exp, {FileTypes::MZML}, log_type_);
244 }
245
246 progresslogger.endProgress();
247 }
248
249};
250
251}
252
254
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition openms/include/OpenMS/CONCEPT/Macros.h:94
std::vector< String > StringList
Vector of String.
Definition ListUtils.h:44
MSExperiment PeakMap
Two-dimensional map of raw data points or peaks.
Definition StandardTypes.h:35
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19