1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Chris Bielow $
// $Authors: Marc Sturm, Chris Bielow $
// --------------------------------------------------------------------------
#include <OpenMS/FORMAT/TextFile.h>
#include <OpenMS/FORMAT/FileHandler.h>
#include <OpenMS/FORMAT/FileTypes.h>
#include <OpenMS/FORMAT/MzMLFile.h>
#include <OpenMS/FORMAT/TraMLFile.h>
#include <OpenMS/FORMAT/FeatureXMLFile.h>
#include <OpenMS/FORMAT/ConsensusXMLFile.h>
#include <OpenMS/DATASTRUCTURES/StringList.h>
#include <OpenMS/APPLICATIONS/TOPPBase.h>
using namespace OpenMS;
using namespace std;
//-------------------------------------------------------------
//Doxygen docu
//-------------------------------------------------------------
/**
@page TOPP_FileMerger FileMerger
@brief Merges several files. Multiple output format supported, depending on input format.
<center>
<table>
<tr>
<td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. predecessor tools </td>
<td VALIGN="middle" ROWSPAN=2> \f$ \longrightarrow \f$ FileMerger \f$ \longrightarrow \f$</td>
<td ALIGN = "center" BGCOLOR="#EBEBEB"> pot. successor tools </td>
</tr>
<tr>
<td VALIGN="middle" ALIGN = "center" ROWSPAN=1> any tool/instrument producing merge able files </td>
<td VALIGN="middle" ALIGN = "center" ROWSPAN=1> any tool operating merged files (e.g. @ref TOPP_XTandemAdapter) </td>
</tr>
</table>
</center>
The meta information that is valid for the whole experiment (e.g. MS instrument and sample)
is taken from the first file.
The retention times for the individual scans are taken from either:
<ul>
<li>the input file meta data (e.g. mzML)
<li>from the input file names (name must contain 'rt' directly followed by a number, e.g. 'myscan_rt3892.98_MS2.dta')
<li>as a list (one RT for each file)
<li>or are auto-generated (starting at 1 with 1 second increment).
</ul>
<B>The command line parameters of this tool are:</B>
@verbinclude TOPP_FileMerger.cli
<B>INI file documentation of this tool:</B>
@htmlinclude TOPP_FileMerger.html
*/
// We do not want this class to show up in the docu:
/// @cond TOPPCLASSES
class TOPPFileMerger :
public TOPPBase
{
public:
TOPPFileMerger() :
TOPPBase("FileMerger", "Merges several MS files into one file.")
{
}
protected:
void registerOptionsAndFlags_()
{
StringList valid_in = StringList::create("mzData,mzXML,mzML,dta,dta2d,mgf,featureXML,consensusXML,fid,traML");
registerInputFileList_("in", "<files>", StringList(), "Input files separated by blank");
setValidFormats_("in", valid_in);
registerStringOption_("in_type", "<type>", "", "Input file type (default: determined from file extension or content)", false);
setValidStrings_("in_type", valid_in);
registerOutputFile_("out", "<file>", "", "Output file");
setValidFormats_("out", StringList::create("mzML,featureXML,consensusXML,traML"));
registerFlag_("annotate_file_origin", "Store the original filename in each feature using meta value \"file_origin\" (for featureXML and consensusXML only).");
addEmptyLine_();
registerTOPPSubsection_("raw", "Flags for non-featureXML input/output");
registerFlag_("raw:rt_auto", "Assign retention times automatically (integers starting at 1)");
registerDoubleList_("raw:rt_custom", "<rt>", DoubleList(), "List of custom retention times that are assigned to the files. The number of given retention times must be equal to the number of given input file.", false);
registerFlag_("raw:rt_filename", "If this flag is set FileMerger tries to guess the rt of the file name.\n"
"This option is useful for merging DTA file, which should contain the string\n"
"'rt' directly followed by a floating point number:\n"
"i.e. my_spectrum_rt2795.15.dta");
registerIntOption_("raw:ms_level", "<num>", 2, "This option is useful for use with DTA files which does not contain MS level information. The given level is assigned to the spectra.", false);
registerFlag_("raw:user_ms_level", "If this flag is set, the MS level given above is used");
}
ExitCodes main_(int, const char**)
{
//-------------------------------------------------------------
// parameter handling
//-------------------------------------------------------------
//file list
StringList file_list = getStringList_("in");
//file type
FileHandler fh;
FileTypes::Type force_type;
if (getStringOption_("in_type").size() > 0)
{
force_type = FileTypes::nameToType(getStringOption_("in_type"));
}
else
{
force_type = fh.getType(file_list[0]);
}
//output file names and types
String out_file = getStringOption_("out");
//-------------------------------------------------------------
// calculations
//-------------------------------------------------------------
bool annotate_file_origin = getFlag_("annotate_file_origin");
if (force_type == FileTypes::FEATUREXML)
{
FeatureMap<> out;
for (Size i = 0; i < file_list.size(); ++i)
{
FeatureMap<> map;
FeatureXMLFile fh;
fh.load(file_list[i], map);
if (annotate_file_origin)
{
for (FeatureMap<>::iterator it = map.begin(); it != map.end(); ++it)
{
it->setMetaValue("file_origin", DataValue(file_list[i]));
}
}
out += map;
}
//-------------------------------------------------------------
// writing output
//-------------------------------------------------------------
//annotate output with data processing info
addDataProcessing_(out, getProcessingInfo_(DataProcessing::FORMAT_CONVERSION));
FeatureXMLFile f;
f.store(out_file, out);
}
else if (force_type == FileTypes::CONSENSUSXML)
{
ConsensusMap out;
ConsensusXMLFile fh;
fh.load(file_list[0], out);
//skip first file
for (Size i = 1; i < file_list.size(); ++i)
{
ConsensusMap map;
ConsensusXMLFile fh;
fh.load(file_list[i], map);
if (annotate_file_origin)
{
for (ConsensusMap::iterator it = map.begin(); it != map.end(); ++it)
{
it->setMetaValue("file_origin", DataValue(file_list[i]));
}
}
out += map;
}
//-------------------------------------------------------------
// writing output
//-------------------------------------------------------------
//annotate output with data processing info
addDataProcessing_(out, getProcessingInfo_(DataProcessing::FORMAT_CONVERSION));
ConsensusXMLFile f;
f.store(out_file, out);
}
else if (force_type == FileTypes::TRAML)
{
TargetedExperiment out;
for (Size i = 0; i < file_list.size(); ++i)
{
TargetedExperiment map;
TraMLFile fh;
fh.load(file_list[i], map);
out += map;
}
//-------------------------------------------------------------
// writing output
//-------------------------------------------------------------
//annotate output with data processing info
Software software;
software.setName("FileMerger");
software.setVersion(VersionInfo::getVersion());
out.addSoftware(software);
TraMLFile f;
f.store(out_file, out);
}
else
{
// we might want to combine different types, thus we only
// query in_type (which applies to all files)
// and not the suffix or content of a single file
force_type = FileTypes::nameToType(getStringOption_("in_type"));
//rt
bool rt_auto_number = getFlag_("raw:rt_auto");
bool rt_filename = getFlag_("raw:rt_filename");
bool rt_custom = false;
DoubleList custom_rts = getDoubleList_("raw:rt_custom");
if (custom_rts.size() != 0)
{
rt_custom = true;
if (custom_rts.size() != file_list.size())
{
writeLog_("Custom retention time list must have as many elements as there are input files!");
printUsage_();
return ILLEGAL_PARAMETERS;
}
}
//ms level
bool user_ms_level = getFlag_("raw:user_ms_level");
MSExperiment<> out;
out.reserve(file_list.size());
UInt rt_auto = 0;
UInt native_id = 0;
std::vector<MSChromatogram<ChromatogramPeak> > all_chromatograms;
for (Size i = 0; i < file_list.size(); ++i)
{
String filename = file_list[i];
//load file
MSExperiment<> in;
fh.loadExperiment(filename, in, force_type, log_type_);
if (in.empty() && in.getChromatograms().empty())
{
writeLog_(String("Warning: Empty file '") + filename + "'!");
continue;
}
out.reserve(out.size() + in.size());
//warn if custom RT and more than one scan in input file
if (rt_custom && in.size() > 1)
{
writeLog_(String("Warning: More than one scan in file '") + filename + "'! All scans will have the same retention time!");
}
for (MSExperiment<>::const_iterator it2 = in.begin(); it2 != in.end(); ++it2)
{
//handle rt
Real rt_final = it2->getRT();
if (rt_auto_number)
{
rt_final = ++rt_auto;
}
else if (rt_custom)
{
rt_final = custom_rts[i];
}
else if (rt_filename)
{
if (!filename.hasSubstring("rt"))
{
writeLog_(String("Warning: cannot guess retention time from filename as it does not contain 'rt'"));
}
for (Size i = 0; i < filename.size(); ++i)
{
if (filename[i] == 'r' && ++i != filename.size() && filename[i] == 't' && ++i != filename.size() && isdigit(filename[i]))
{
String rt;
while (i != filename.size() && (filename[i] == '.' || isdigit(filename[i])))
{
rt += filename[i++];
}
if (rt.size() > 0)
{
// remove dot from rt3892.98.dta
// ^
if (rt[rt.size() - 1] == '.')
{
// remove last character
rt.erase(rt.end() - 1);
}
}
try
{
float tmp = rt.toFloat();
rt_final = tmp;
}
catch (Exception::ConversionError)
{
writeLog_(String("Warning: cannot convert the found retention time in a value '" + rt + "'."));
}
}
}
}
// none of the rt methods were successful
if (rt_final == -1)
{
writeLog_(String("Warning: No valid retention time for output scan '") + rt_auto + "' from file '" + filename + "'");
}
out.addSpectrum(*it2);
out.getSpectra().back().setRT(rt_final);
out.getSpectra().back().setNativeID(native_id);
if (user_ms_level)
{
out.getSpectra().back().setMSLevel((int)getIntOption_("raw:ms_level"));
}
++native_id;
}
// if we had only one spectrum, we can annotate it directly, for more spectra, we just name the source file leaving the spectra unannotated (to avoid a long and redundant list of sourceFiles)
if (in.size() == 1)
{
out.getSpectra().back().setSourceFile(in.getSourceFiles()[0]);
in.getSourceFiles().clear(); // delete source file annotated from source file (its in the spectrum anyways)
}
// copy experimental settings from first file
if (i == 0)
{
out.ExperimentalSettings::operator=(in);
}
else // otherwise append
{
out.getSourceFiles().insert(out.getSourceFiles().end(), in.getSourceFiles().begin(), in.getSourceFiles().end()); // could be emtpty if spectrum was annotated above, but that's ok then
}
// also add the chromatograms
for (std::vector<MSChromatogram<ChromatogramPeak> >::const_iterator it2 = in.getChromatograms().begin(); it2 != in.getChromatograms().end(); ++it2)
{
all_chromatograms.push_back(*it2);
}
}
// set the chromatograms
out.setChromatograms(all_chromatograms);
//-------------------------------------------------------------
// writing output
//-------------------------------------------------------------
//annotate output with data processing info
addDataProcessing_(out, getProcessingInfo_(DataProcessing::FORMAT_CONVERSION));
MzMLFile f;
f.setLogType(log_type_);
f.store(out_file, out);
}
return EXECUTION_OK;
}
};
int main(int argc, const char** argv)
{
TOPPFileMerger tool;
return tool.main(argc, argv);
}
/// @endcond
|