9#include <OpenMS/FORMAT/ConsensusXMLFile.h>
10#include <OpenMS/FORMAT/FeatureXMLFile.h>
11#include <OpenMS/FORMAT/FileHandler.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#include <OpenMS/METADATA/ExperimentalDesign.h>
17#include <OpenMS/FORMAT/ExperimentalDesignFile.h>
19#include <OpenMS/KERNEL/ConversionHelper.h>
21#include <OpenMS/APPLICATIONS/TOPPBase.h>
42class TOPPFeatureLinkerBase :
48 TOPPFeatureLinkerBase(String name, String description,
bool official =
true) :
49 TOPPBase(name, description, official)
54 void registerOptionsAndFlags_() override
56 registerInputFileList_(
"in",
"<files>", ListUtils::create<String>(
""),
"input files separated by blanks",
true);
57 setValidFormats_(
"in", ListUtils::create<String>(
"featureXML,consensusXML"));
58 registerOutputFile_(
"out",
"<file>",
"",
"Output file",
true);
59 setValidFormats_(
"out", ListUtils::create<String>(
"consensusXML"));
60 registerInputFile_(
"design",
"<file>",
"",
"input file containing the experimental design",
false);
61 setValidFormats_(
"design", ListUtils::create<String>(
"tsv"));
63 registerFlag_(
"keep_subelements",
"For consensusXML input only: If set, the sub-features of the inputs are transferred to the output.");
66 ExitCodes common_main_(FeatureGroupingAlgorithm * algorithm,
75 ins.push_back(getStringOption_(
"in"));
79 ins = getStringList_(
"in");
81 String out = getStringOption_(
"out");
87 FileTypes::Type file_type = FileHandler::getType(ins[0]);
88 for (Size i = 0; i < ins.size(); ++i)
90 if (FileHandler::getType(ins[i]) != file_type)
92 writeLogError_(
"Error: All input files must be of the same type!");
93 return ILLEGAL_PARAMETERS;
100 Param algorithm_param = getParam_().copy(
"algorithm:",
true);
101 writeDebug_(
"Used algorithm parameters", algorithm_param, 3);
102 algorithm->setParameters(algorithm_param);
108 ConsensusMap out_map;
109 StringList ms_run_locations;
116 design_file = getStringOption_(
"design");
119 if (file_type == FileTypes::CONSENSUSXML && !design_file.empty())
121 writeLogError_(
"Error: Using fractionated design with consensusXML als input is not supported!");
122 return ILLEGAL_PARAMETERS;
125 if (file_type == FileTypes::FEATUREXML)
127 OPENMS_LOG_INFO <<
"Linking " << ins.size() <<
" featureXMLs." << endl;
134 map<unsigned, vector<String>> frac2files;
136 if (!design_file.empty())
139 ExperimentalDesign ed = ExperimentalDesignFile::load(design_file,
false);
142 frac2files = ed.getFractionToMSFilesMapping();
144 writeDebug_(String(
"Grouping ") + String(ed.getNumberOfFractions()) +
" fractions.", 3);
147 if (!ed.sameNrOfMSFilesPerFraction())
149 writeLogError_(
"Error: Number of runs must match for every fraction!");
150 return ILLEGAL_PARAMETERS;
155 for (Size i = 0; i != ins.size(); ++i)
157 frac2files[1].emplace_back(String(
"file") + String(i));
161 vector<FeatureMap > maps(ins.size());
163 FeatureFileOptions param = f.getOptions();
166 param.setLoadSubordinates(
false);
167 param.setLoadConvexHull(
false);
171 setLogType(ProgressLogger::CMD);
172 startProgress(0, ins.size(),
"reading input");
173 for (Size i = 0; i < ins.size(); ++i)
179 tmp.getPrimaryMSRunPath(ms_runs);
182 if (ms_runs.size() > 1 || ms_runs.empty())
184 OPENMS_LOG_WARN <<
"Exactly one MS run should be associated with a FeatureMap. "
186 <<
" provided." << endl;
190 out_map.getColumnHeaders()[i].filename = ms_runs.front();
192 out_map.getColumnHeaders()[i].size = tmp.size();
193 out_map.getColumnHeaders()[i].unique_id = tmp.getUniqueId();
196 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
199 for (Feature& ft : tmp)
204 if (ft.metaValueExists(Constants::UserParam::DC_CHARGE_ADDUCTS))
206 adduct = ft.getMetaValue(Constants::UserParam::DC_CHARGE_ADDUCTS);
208 if (ft.metaValueExists(Constants::UserParam::ADDUCT_GROUP))
210 group = ft.getMetaValue(Constants::UserParam::ADDUCT_GROUP);
212 ft.getSubordinates().clear();
213 ft.getConvexHulls().clear();
217 ft.setMetaValue(Constants::UserParam::DC_CHARGE_ADDUCTS, adduct);
221 ft.setMetaValue(
"Group", group);
227 maps[i].updateRanges();
229 setProgress(progress++);
236 out_map.getColumnHeaders()[1] = out_map.getColumnHeaders()[0];
237 out_map.getColumnHeaders()[0].label =
"light";
238 out_map.getColumnHeaders()[1].label =
"heavy";
239 ms_run_locations.push_back(ms_run_locations[0]);
245 if (frac2files.size() == 1)
247 algorithm->group(maps, out_map);
251 writeDebug_(String(
"Stored in ") + String(maps.size()) +
" maps.", 3);
252 for (Size i = 1; i <= frac2files.size(); ++i)
254 vector<FeatureMap> fraction_maps;
257 for (
size_t feature_map_index = 0; feature_map_index != frac2files[i].size(); ++feature_map_index)
259 fraction_maps.push_back(maps[feature_map_index]);
261 algorithm->group(fraction_maps, out_map);
269 bool keep_subelements = getFlag_(
"keep_subelements");
270 vector<ConsensusMap> maps(ins.size());
272 for (Size i = 0; i < ins.size(); ++i)
274 f.load(ins[i], maps[i]);
275 maps[i].updateRanges();
278 maps[i].getPrimaryMSRunPath(ms_runs);
279 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
280 if (keep_subelements)
282 auto saveOldMapIndex =
283 [](PeptideIdentification &p)
285 if (p.metaValueExists(
"map_index"))
287 p.setMetaValue(
"old_map_index", p.getMetaValue(
"map_index"));
291 OPENMS_LOG_WARN <<
"Warning: map_index not found in PeptideID. The tool will not be able to assign a"
292 "consistent one. Check the settings of previous tools." << std::endl;
295 maps[i].applyFunctionOnPeptideIDs(saveOldMapIndex,
true);
299 algorithm->group(maps, out_map);
303 if (!keep_subelements)
305 for (Size i = 0; i < ins.size(); ++i)
307 out_map.getColumnHeaders()[i].filename = ins[i];
308 out_map.getColumnHeaders()[i].size = maps[i].size();
309 out_map.getColumnHeaders()[i].unique_id = maps[i].getUniqueId();
316 algorithm->transferSubelements(maps, out_map);
321 out_map.applyMemberFunction(&UniqueIdInterface::setUniqueId);
324 addDataProcessing_(out_map,
325 getProcessingInfo_(DataProcessing::FEATURE_GROUPING));
329 out_map.sortPeptideIdentificationsByMapIndex();
332 ConsensusXMLFile().store(out, out_map);
335 map<Size, UInt> num_consfeat_of_size;
336 for (
const ConsensusFeature& cf : out_map)
338 ++num_consfeat_of_size[cf.size()];
341 OPENMS_LOG_INFO <<
"Number of consensus features:" << endl;
342 for (map<Size, UInt>::reverse_iterator i = num_consfeat_of_size.rbegin();
343 i != num_consfeat_of_size.rend(); ++i)
345 OPENMS_LOG_INFO <<
" of size " << setw(2) << i->first <<
": " << setw(6)
346 << i->second << endl;
348 OPENMS_LOG_INFO <<
" total: " << setw(6) << out_map.size() << endl;
Definition FLASHDeconvWizardBase.cpp:26