OpenMS
Loading...
Searching...
No Matches
FeatureLinkerBase.cpp
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: Timo Sachsenberg $
6// $Authors: Marc Sturm, Clemens Groepl, Steffen Sass $
7// --------------------------------------------------------------------------
8
9#include <OpenMS/FORMAT/FileHandler.h>
10#include <OpenMS/CONCEPT/LogStream.h>
11#include <OpenMS/KERNEL/ConsensusMap.h>
12#include <OpenMS/FORMAT/FileTypes.h>
13#include <OpenMS/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithm.h>
14#include <OpenMS/DATASTRUCTURES/ListUtils.h>
15#include <OpenMS/CONCEPT/ProgressLogger.h>
16
17#include <OpenMS/KERNEL/ConversionHelper.h>
18
19#include <OpenMS/APPLICATIONS/TOPPBase.h>
20
21#include <iomanip> // setw
22
23using namespace OpenMS;
24using namespace std;
25
26//-------------------------------------------------------------
27//Doxygen docu
28//-------------------------------------------------------------
29
36
37// We do not want this class to show up in the docu:
39
40// Output is always a ConsensusMap. The serialization track follows the input:
41// parquet inputs → consensusparquet; XML (or other) inputs → consensusXML.
42static FileTypes::Type consensusOutTypeFor(FileTypes::Type in_type)
43{
44 switch (in_type)
45 {
46 case FileTypes::FEATUREPARQUET:
47 case FileTypes::CONSENSUSPARQUET:
48 return FileTypes::CONSENSUSPARQUET;
49 default:
50 return FileTypes::CONSENSUSXML;
51 }
52}
53
54class TOPPFeatureLinkerBase :
55 public TOPPBase,
56 public ProgressLogger
57{
58
59public:
60 TOPPFeatureLinkerBase(std::string name, std::string description) :
61 TOPPBase(name, description)
62 {
63 }
64
65protected:
66 void registerOptionsAndFlags_() override // only for "unlabeled" algorithms!
67 {
68 registerInputFileList_("in", "<files>", ListUtils::create<std::string>(""), "input files separated by blanks", true);
69 setValidFormats_("in", ListUtils::create<std::string>("featureXML,consensusXML,featureparquet,consensusparquet"));
70 registerOutputFile_("out", "<file>", "", "Output file", true);
71 setValidFormats_("out", ListUtils::create<std::string>("consensusXML,consensusparquet"));
72 addEmptyLine_();
73 registerFlag_("keep_subelements", "For consensusXML/consensusparquet input only: If set, the sub-features of the inputs are transferred to the output.");
74 }
75
76 ExitCodes common_main_(FeatureGroupingAlgorithm * algorithm,
77 bool labeled = false)
78 {
79 //-------------------------------------------------------------
80 // parameter handling
81 //-------------------------------------------------------------
82 StringList ins;
83 if (labeled)
84 {
85 ins.push_back(getStringOption_("in"));
86 }
87 else
88 {
89 ins = getStringList_("in");
90 }
91 std::string out = getStringOption_("out");
92
93 //-------------------------------------------------------------
94 // check for valid input
95 //-------------------------------------------------------------
96 // check if all input files have the correct type
97 FileTypes::Type file_type = FileHandler::getType(ins[0]);
98 for (Size i = 0; i < ins.size(); ++i)
99 {
100 if (FileHandler::getType(ins[i]) != file_type)
101 {
102 writeLogError_("Error: All input files must be of the same type!");
103 return ILLEGAL_PARAMETERS;
104 }
105 }
106
107 //-------------------------------------------------------------
108 // set up algorithm
109 //-------------------------------------------------------------
110 Param algorithm_param = getParam_().copy("algorithm:", true);
111 writeDebug_("Used algorithm parameters", algorithm_param, 3);
112 algorithm->setParameters(algorithm_param);
113
114 //-------------------------------------------------------------
115 // perform grouping
116 //-------------------------------------------------------------
117 // load input
118 ConsensusMap out_map;
119 StringList ms_run_locations;
120
121 if (file_type == FileTypes::FEATUREXML || file_type == FileTypes::FEATUREPARQUET)
122 {
123 OPENMS_LOG_INFO << "Linking " << ins.size() << " feature maps." << endl;
124
125 vector<FeatureMap > maps(ins.size());
126 FileHandler f;
127 FeatureFileOptions param = f.getFeatOptions();
128
129 // to save memory don't load convex hulls and subordinates
130 param.setLoadSubordinates(false);
131 param.setLoadConvexHull(false);
132 f.setFeatOptions(param);
133
134 Size progress = 0;
135 setLogType(ProgressLogger::CMD);
136 startProgress(0, ins.size(), "reading input");
137 for (Size i = 0; i < ins.size(); ++i)
138 {
139 FeatureMap tmp;
140 f.loadFeatures(ins[i], tmp, {FileTypes::FEATUREXML, FileTypes::FEATUREPARQUET});
141
142 StringList ms_runs;
143 tmp.getPrimaryMSRunPath(ms_runs);
144
145 // associate mzML file with map i in consensusXML
146 if (ms_runs.size() > 1 || ms_runs.empty())
147 {
148 OPENMS_LOG_WARN << "Exactly one MS run should be associated with a FeatureMap. "
149 << ms_runs.size()
150 << " provided." << endl;
151 }
152 else
153 {
154 out_map.getColumnHeaders()[i].filename = ms_runs.front();
155 }
156 out_map.getColumnHeaders()[i].size = tmp.size();
157 out_map.getColumnHeaders()[i].unique_id = tmp.getUniqueId();
158
159 // copy over information on the primary MS run
160 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
161
162 // to save memory, remove convex hulls, subordinates:
163 for (Feature& ft : tmp)
164 {
165 std::string adduct;
166 std::string group;
167 //exception: addduct information
168 if (ft.metaValueExists(Constants::UserParam::DC_CHARGE_ADDUCTS))
169 {
170 adduct = ft.getMetaValue(Constants::UserParam::DC_CHARGE_ADDUCTS).toString();
171 }
172 if (ft.metaValueExists(Constants::UserParam::ADDUCT_GROUP))
173 {
174 group = ft.getMetaValue(Constants::UserParam::ADDUCT_GROUP).toString();
175 }
176 ft.getSubordinates().clear();
177 ft.getConvexHulls().clear();
178 ft.clearMetaInfo();
179 if (!adduct.empty())
180 {
181 ft.setMetaValue(Constants::UserParam::DC_CHARGE_ADDUCTS, adduct);
182 }
183 if (!group.empty())
184 {
185 ft.setMetaValue("Group", group);
186 }
187
188 }
189
190 maps[i] = tmp;
191 maps[i].updateRanges();
192
193 setProgress(progress++);
194 }
195 endProgress();
196
197 // exception for "labeled" algorithms: copy file descriptions
198 if (labeled)
199 {
200 out_map.getColumnHeaders()[1] = out_map.getColumnHeaders()[0];
201 out_map.getColumnHeaders()[0].label = "light";
202 out_map.getColumnHeaders()[1].label = "heavy";
203 ms_run_locations.push_back(ms_run_locations[0]);
204 }
205
207 // invoke feature grouping algorithm
208
209 algorithm->group(maps, out_map);
210 }
211 else
212 {
213 //TODO isn't it better to have this option/functionality in the FeatureGroupingAlgorithm class?
214 // Otherwise everyone has to remember e.g. to annotate the old map_index etc.
215 bool keep_subelements = getFlag_("keep_subelements");
216 vector<ConsensusMap> maps(ins.size());
217 FileHandler f;
218 for (Size i = 0; i < ins.size(); ++i)
219 {
220 f.loadConsensusFeatures(ins[i], maps[i], {FileTypes::CONSENSUSXML, FileTypes::CONSENSUSPARQUET});
221 maps[i].updateRanges();
222 // copy over information on the primary MS run
223 StringList ms_runs;
224 maps[i].getPrimaryMSRunPath(ms_runs);
225 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
226 if (keep_subelements)
227 {
228 auto saveOldMapIndex =
229 [](PeptideIdentification &p)
230 {
231 if (p.metaValueExists("map_index"))
232 {
233 p.setMetaValue("old_map_index", p.getMetaValue("map_index"));
234 }
235 else
236 {
237 OPENMS_LOG_WARN << "Warning: map_index not found in PeptideID. The tool will not be able to assign a"
238 "consistent one. Check the settings of previous tools." << std::endl;
239 }
240 };
241 maps[i].applyFunctionOnPeptideIDs(saveOldMapIndex, true);
242 }
243 }
244 // group
245 algorithm->group(maps, out_map);
246
247 // set file descriptions:
248
249 if (!keep_subelements)
250 {
251 for (Size i = 0; i < ins.size(); ++i)
252 {
253 out_map.getColumnHeaders()[i].filename = ins[i];
254 out_map.getColumnHeaders()[i].size = maps[i].size();
255 out_map.getColumnHeaders()[i].unique_id = maps[i].getUniqueId();
256 }
257 }
258 else
259 {
260 // components of the output map are not the input maps themselves, but
261 // the components of the input maps:
262 algorithm->transferSubelements(maps, out_map);
263 }
264 }
265
266 // assign unique ids
267 out_map.applyMemberFunction(&UniqueIdInterface::setUniqueId);
268
269 // annotate output with data processing info
270 addDataProcessing_(out_map,
271 getProcessingInfo_(DataProcessing::FEATURE_GROUPING));
272
273
274 // sort list of peptide identifications in each consensus feature by map index
275 out_map.sortPeptideIdentificationsByMapIndex();
276
277 // write output
278 FileHandler().storeConsensusFeatures(out, out_map, {consensusOutTypeFor(file_type)});
279
280 // some statistics
281 map<Size, UInt> num_consfeat_of_size;
282 for (const ConsensusFeature& cf : out_map)
283 {
284 ++num_consfeat_of_size[cf.size()];
285 }
286
287 OPENMS_LOG_INFO << "Number of consensus features:" << endl;
288 for (map<Size, UInt>::reverse_iterator i = num_consfeat_of_size.rbegin();
289 i != num_consfeat_of_size.rend(); ++i)
290 {
291 OPENMS_LOG_INFO << " of size " << setw(2) << i->first << ": " << setw(6)
292 << i->second << endl;
293 }
294 OPENMS_LOG_INFO << " total: " << setw(6) << out_map.size() << endl;
295
296 return EXECUTION_OK;
297 }
298
299};
300
Definition CometModification.cpp:12