OpenMS
MapAlignerBase.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- 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 
23 
24 //-------------------------------------------------------------
25 // Doxygen docu
26 //-------------------------------------------------------------
27 
34 // We do not want this class to show up in the docu:
36 
37 namespace OpenMS
38 {
39  // @brief stores model defaults for map aligner algorithms
40  struct MapAlignerBase
41  {
42  // "public" so it can be used in DefaultParamHandlerDocumenter to get docu
43  static Param getModelDefaults(const String& default_model)
44  {
45  Param params;
46  params.setValue("type", default_model, "Type of model");
47  // TODO: avoid referring to each TransformationModel subclass explicitly
48  std::vector<std::string> model_types = {"linear","b_spline","lowess","interpolated"};
49  if (!ListUtils::contains(model_types, default_model))
50  {
51  model_types.insert(model_types.begin(), default_model);
52  }
53  params.setValidStrings("type", model_types);
54 
55  Param model_params;
57  params.insert("linear:", model_params);
58  params.setSectionDescription("linear", "Parameters for 'linear' model");
59 
61  params.insert("b_spline:", model_params);
62  params.setSectionDescription("b_spline", "Parameters for 'b_spline' model");
63 
65  params.insert("lowess:", model_params);
66  params.setSectionDescription("lowess", "Parameters for 'lowess' model");
67 
69  params.insert("interpolated:", model_params);
70  params.setSectionDescription("interpolated",
71  "Parameters for 'interpolated' model");
72  return params;
73  }
74  };
75 
76 
77 class TOPPMapAlignerBase :
78  public TOPPBase, public MapAlignerBase
79 {
80 
81 public:
82  TOPPMapAlignerBase(String name, String description, bool official = true) :
83  TOPPBase(name, description, official), ref_params_(REF_NONE)
84  {
85  }
86 
87 protected:
88 
89  // Kind of reference parameters that the tool offers:
90  // - REF_NONE: no reference
91  // - REF_RESTRICTED: reference file must have same type as input files
92  // - REF_FLEXIBLE: reference file can have any supported file type
93  enum ReferenceParameterKind { REF_NONE, REF_RESTRICTED, REF_FLEXIBLE }
94  ref_params_;
95 
96  void registerOptionsAndFlagsMapAligners_(const String& file_formats,
97  enum ReferenceParameterKind ref_params)
98  {
99  registerInputFileList_("in", "<files>", StringList(), "Input files to align (all must have the same file type)", true);
100  setValidFormats_("in", ListUtils::create<String>(file_formats));
101  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);
102  setValidFormats_("out", ListUtils::create<String>(file_formats));
103  registerOutputFileList_("trafo_out", "<files>", StringList(), "Transformation output files. This option or 'out' has to be provided; they can be used together.", false);
104  setValidFormats_("trafo_out", ListUtils::create<String>("trafoXML"));
105 
106  if (ref_params != REF_NONE)
107  {
108  registerTOPPSubsection_("reference", "Options to define a reference file (use either 'file' or 'index', not both)");
109  String description = "File to use as reference";
110  if (ref_params == REF_RESTRICTED)
111  {
112  description += " (same file format as input files required)";
113  }
114  registerInputFile_("reference:file", "<file>", "", description, false);
115  setValidFormats_("reference:file", ListUtils::create<String>(file_formats));
116  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);
117  setMinInt_("reference:index", 0);
118  }
119  ref_params_ = ref_params;
120  }
121 
122  ExitCodes checkParameters_()
123  {
124  //-------------------------------------------------------------
125  // parameter handling
126  //-------------------------------------------------------------
127  StringList ins = getStringList_("in");
128  StringList outs = getStringList_("out");
129  StringList trafos = getStringList_("trafo_out");
130 
131  //-------------------------------------------------------------
132  // check for valid input
133  //-------------------------------------------------------------
134  // check whether some kind of output file is given:
135  if (outs.empty() && trafos.empty())
136  {
137  writeLogError_("Error: Data output or transformation output files have to be provided (parameters 'out'/'trafo_out')");
138  return ILLEGAL_PARAMETERS;
139  }
140  // check whether number of input files equals number of output files:
141  if (!outs.empty() && (ins.size() != outs.size()))
142  {
143  writeLogError_("Error: The number of data input and output files has to be equal (parameters 'in'/'out')");
144  return ILLEGAL_PARAMETERS;
145  }
146  if (!trafos.empty() && (ins.size() != trafos.size()))
147  {
148  writeLogError_("Error: The number of data input and transformation output files has to be equal (parameters 'in'/'trafo_out')");
149  return ILLEGAL_PARAMETERS;
150  }
151  // check whether all input files have the same type (this type is used to store the output type too):
152  FileTypes::Type in_type = FileHandler::getType(ins[0]);
153  for (Size i = 1; i < ins.size(); ++i)
154  {
155  if (FileHandler::getType(ins[i]) != in_type)
156  {
157  writeLogError_("Error: All input files (parameter 'in') must have the same format!");
158  return ILLEGAL_PARAMETERS;
159  }
160  }
161 
162  if (ref_params_ != REF_NONE) // a valid ref. index OR file should be given
163  {
164  Size reference_index = getIntOption_("reference:index");
165  String reference_file = getStringOption_("reference:file");
166  if (reference_index > ins.size())
167  {
168  writeLogError_("Error: Value of parameter 'reference:index' must not be higher than the number of input files");
169  return ILLEGAL_PARAMETERS;
170  }
171  if (reference_index && !reference_file.empty())
172  {
173  writeLogError_("Error: Parameters 'reference:index' and 'reference:file' cannot be used together");
174  return ILLEGAL_PARAMETERS;
175  }
176 
177  if ((ref_params_ == REF_RESTRICTED) && !reference_file.empty() &&
178  (FileHandler::getType(reference_file) != in_type))
179  {
180  writeLogError_("Error: Reference file must have the same format as other input files (parameters 'reference:file'/'in')");
181  return ILLEGAL_PARAMETERS;
182  }
183  }
184 
185  return EXECUTION_OK;
186  }
187 
188 };
189 
190 }
191 
193 
static FileTypes::Type getType(const String &filename)
Tries to determine the file type (by name or content)
static bool contains(const std::vector< T > &container, const E &elem)
Checks whether the element elem is contained in the given container.
Definition: ListUtils.h:137
static void getDefaultParameters(Param &params)
Gets the default parameters.
static void getDefaultParameters(Param &params)
Gets the default parameters.
static void getDefaultParameters(Param &params)
Gets the default parameters.
static void getDefaultParameters(Param &params)
Gets the default parameters.
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Type
Actual file types enum.
Definition: FileTypes.h:31