35#include <OpenMS/FORMAT/ConsensusXMLFile.h>
36#include <OpenMS/FORMAT/FeatureXMLFile.h>
37#include <OpenMS/FORMAT/FileHandler.h>
38#include <OpenMS/FORMAT/FileTypes.h>
39#include <OpenMS/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithm.h>
40#include <OpenMS/DATASTRUCTURES/ListUtils.h>
41#include <OpenMS/CONCEPT/ProgressLogger.h>
42#include <OpenMS/METADATA/ExperimentalDesign.h>
43#include <OpenMS/FORMAT/ExperimentalDesignFile.h>
45#include <OpenMS/KERNEL/ConversionHelper.h>
47#include <OpenMS/APPLICATIONS/TOPPBase.h>
68class TOPPFeatureLinkerBase :
74 TOPPFeatureLinkerBase(String name, String description,
bool official =
true) :
75 TOPPBase(name, description, official)
80 void registerOptionsAndFlags_() override
82 registerInputFileList_(
"in",
"<files>", ListUtils::create<String>(
""),
"input files separated by blanks",
true);
83 setValidFormats_(
"in", ListUtils::create<String>(
"featureXML,consensusXML"));
84 registerOutputFile_(
"out",
"<file>",
"",
"Output file",
true);
85 setValidFormats_(
"out", ListUtils::create<String>(
"consensusXML"));
86 registerInputFile_(
"design",
"<file>",
"",
"input file containing the experimental design",
false);
87 setValidFormats_(
"design", ListUtils::create<String>(
"tsv"));
89 registerFlag_(
"keep_subelements",
"For consensusXML input only: If set, the sub-features of the inputs are transferred to the output.");
92 ExitCodes common_main_(FeatureGroupingAlgorithm * algorithm,
101 ins.push_back(getStringOption_(
"in"));
105 ins = getStringList_(
"in");
107 String out = getStringOption_(
"out");
113 FileTypes::Type file_type = FileHandler::getType(ins[0]);
114 for (Size i = 0; i < ins.size(); ++i)
116 if (FileHandler::getType(ins[i]) != file_type)
118 writeLog_(
"Error: All input files must be of the same type!");
119 return ILLEGAL_PARAMETERS;
126 Param algorithm_param = getParam_().copy(
"algorithm:",
true);
127 writeDebug_(
"Used algorithm parameters", algorithm_param, 3);
128 algorithm->setParameters(algorithm_param);
134 ConsensusMap out_map;
135 StringList ms_run_locations;
142 design_file = getStringOption_(
"design");
145 if (file_type == FileTypes::CONSENSUSXML && !design_file.empty())
147 writeLog_(
"Error: Using fractionated design with consensusXML als input is not supported!");
148 return ILLEGAL_PARAMETERS;
151 if (file_type == FileTypes::FEATUREXML)
153 OPENMS_LOG_INFO <<
"Linking " << ins.size() <<
" featureXMLs." << endl;
160 map<unsigned, vector<String>> frac2files;
162 if (!design_file.empty())
165 ExperimentalDesign ed = ExperimentalDesignFile::load(design_file,
false);
168 frac2files = ed.getFractionToMSFilesMapping();
170 writeDebug_(String(
"Grouping ") + String(ed.getNumberOfFractions()) +
" fractions.", 3);
173 if (!ed.sameNrOfMSFilesPerFraction())
175 writeLog_(
"Error: Number of runs must match for every fraction!");
176 return ILLEGAL_PARAMETERS;
181 for (Size i = 0; i != ins.size(); ++i)
183 frac2files[1].emplace_back(String(
"file") + String(i));
187 vector<FeatureMap > maps(ins.size());
189 FeatureFileOptions param = f.getOptions();
192 param.setLoadSubordinates(
false);
193 param.setLoadConvexHull(
false);
197 setLogType(ProgressLogger::CMD);
198 startProgress(0, ins.size(),
"reading input");
199 for (Size i = 0; i < ins.size(); ++i)
205 tmp.getPrimaryMSRunPath(ms_runs);
208 if (ms_runs.size() > 1 || ms_runs.empty())
210 OPENMS_LOG_WARN <<
"Exactly one MS run should be associated with a FeatureMap. "
212 <<
" provided." << endl;
216 out_map.getColumnHeaders()[i].filename = ms_runs.front();
218 out_map.getColumnHeaders()[i].size = tmp.size();
219 out_map.getColumnHeaders()[i].unique_id = tmp.getUniqueId();
222 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
225 for (FeatureMap::Iterator it = tmp.begin(); it != tmp.end();
230 if (it->metaValueExists(
"dc_charge_adducts"))
232 adduct = it->getMetaValue(
"dc_charge_adducts");
234 it->getSubordinates().clear();
235 it->getConvexHulls().clear();
239 it->setMetaValue(
"dc_charge_adducts", adduct);
245 maps[i].updateRanges();
247 setProgress(progress++);
254 out_map.getColumnHeaders()[1] = out_map.getColumnHeaders()[0];
255 out_map.getColumnHeaders()[0].label =
"light";
256 out_map.getColumnHeaders()[1].label =
"heavy";
257 ms_run_locations.push_back(ms_run_locations[0]);
263 if (frac2files.size() == 1)
265 algorithm->group(maps, out_map);
269 writeDebug_(String(
"Stored in ") + String(maps.size()) +
" maps.", 3);
270 for (Size i = 1; i <= frac2files.size(); ++i)
272 vector<FeatureMap> fraction_maps;
275 for (
size_t feature_map_index = 0; feature_map_index != frac2files[i].size(); ++feature_map_index)
277 fraction_maps.push_back(maps[feature_map_index]);
279 algorithm->group(fraction_maps, out_map);
287 bool keep_subelements = getFlag_(
"keep_subelements");
288 vector<ConsensusMap> maps(ins.size());
290 for (Size i = 0; i < ins.size(); ++i)
292 f.load(ins[i], maps[i]);
293 maps[i].updateRanges();
296 maps[i].getPrimaryMSRunPath(ms_runs);
297 ms_run_locations.insert(ms_run_locations.end(), ms_runs.begin(), ms_runs.end());
298 if (keep_subelements)
300 auto saveOldMapIndex =
301 [](PeptideIdentification &p)
303 if (p.metaValueExists(
"map_index"))
305 p.setMetaValue(
"old_map_index", p.getMetaValue(
"map_index"));
309 OPENMS_LOG_WARN <<
"Warning: map_index not found in PeptideID. The tool will not be able to assign a"
310 "consistent one. Check the settings of previous tools." << std::endl;
313 maps[i].applyFunctionOnPeptideIDs(saveOldMapIndex,
true);
317 algorithm->group(maps, out_map);
321 if (!keep_subelements)
323 for (Size i = 0; i < ins.size(); ++i)
325 out_map.getColumnHeaders()[i].filename = ins[i];
326 out_map.getColumnHeaders()[i].size = maps[i].size();
327 out_map.getColumnHeaders()[i].unique_id = maps[i].getUniqueId();
334 algorithm->transferSubelements(maps, out_map);
339 out_map.applyMemberFunction(&UniqueIdInterface::setUniqueId);
342 addDataProcessing_(out_map,
343 getProcessingInfo_(DataProcessing::FEATURE_GROUPING));
347 out_map.sortPeptideIdentificationsByMapIndex();
350 ConsensusXMLFile().store(out, out_map);
353 map<Size, UInt> num_consfeat_of_size;
354 for (ConsensusMap::const_iterator cmit = out_map.begin();
355 cmit != out_map.end(); ++cmit)
357 ++num_consfeat_of_size[cmit->size()];
360 OPENMS_LOG_INFO <<
"Number of consensus features:" << endl;
361 for (map<Size, UInt>::reverse_iterator i = num_consfeat_of_size.rbegin();
362 i != num_consfeat_of_size.rend(); ++i)
364 OPENMS_LOG_INFO <<
" of size " << setw(2) << i->first <<
": " << setw(6)
365 << i->second << endl;
367 OPENMS_LOG_INFO <<
" total: " << setw(6) << out_map.size() << endl;
Definition INIFileEditorWindow.cpp:53