File: idconvert.cpp

package info (click to toggle)
libpwiz 3.0.18342-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,888 kB
  • sloc: cpp: 157,552; sh: 4,182; makefile: 317
file content (547 lines) | stat: -rw-r--r-- 17,225 bytes parent folder | download | duplicates (3)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//
// $Id$
//
//
// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
//
// Copyright 2011 Vanderbilt University - Nashville, TN 37232
//
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
//


#include "pwiz/utility/misc/Std.hpp"
#include "pwiz/utility/misc/Filesystem.hpp"
#include "pwiz/data/identdata/DefaultReaderList.hpp"
#include "pwiz/data/identdata/IdentDataFile.hpp"
//#include "pwiz/data/identdata/IO.hpp"
#include "pwiz/data/identdata/Version.hpp"
#include "pwiz/Version.hpp"
#include "boost/program_options.hpp"

using namespace pwiz::cv;
using namespace pwiz::data;
using namespace pwiz::identdata;
using namespace pwiz::util;


struct Config
{
    vector<string> filenames;
    //vector<string> filters;
    string outputPath;
    string extension;
    bool verbose;
    IdentDataFile::WriteConfig writeConfig;
    //string contactFilename;
    //bool merge;

    Config()
    :   outputPath("."), verbose(false)//, merge(false)
    {}

    string outputFilename(const string& inputFilename, const IdentData& inputIdentData, bool multipleOutputs) const;
};


string Config::outputFilename(const string& filename, const IdentData& idd, bool multipleOutputs) const
{
    if (idd.dataCollection.inputs.spectraData.empty())
        throw runtime_error("[Config::outputFilename] no spectraData elements");

    string sourceName;
    if (multipleOutputs)
        sourceName = idd.id;
    if (sourceName.empty())
        sourceName = bfs::basename(idd.dataCollection.inputs.spectraData[0]->name);
    if (sourceName.empty())
        sourceName = bfs::basename(filename);
    
    // this list is for Windows; it's a superset of the POSIX list
    string illegalFilename = "\\/*:?<>|\"";
    BOOST_FOREACH(char& c, sourceName)
        if (illegalFilename.find(c) != string::npos)
            c = '_';

    bfs::path fullPath = bfs::path(outputPath) / (sourceName + extension);
    return fullPath.string(); 
}


ostream& operator<<(ostream& os, const Config& config)
{
    os << "format: " << config.writeConfig << endl;
    os << "outputPath: " << config.outputPath << endl;
    os << "extension: " << config.extension << endl; 
    //os << "contactFilename: " << config.contactFilename << endl;
    os << endl;

    /*os << "filters:\n  ";
    copy(config.filters.begin(), config.filters.end(), ostream_iterator<string>(os,"\n  "));
    os << endl;*/

    os << "filenames:\n  ";
    copy(config.filenames.begin(), config.filenames.end(), ostream_iterator<string>(os,"\n  "));
    os << endl;

    return os;
}


Config parseCommandLine(int argc, const char* argv[])
{
    namespace po = boost::program_options;

    ostringstream usage;
    usage << "Usage: idconvert [options] [filemasks]\n"
          << "Convert mass spec identification file formats.\n"
          << "\n"
          << "Return value: # of failed files.\n"
          << "\n";
        
    Config config;
    string filelistFilename;
    string configFilename;

    bool format_text = false;
    bool format_mzIdentML = false;
    bool format_pepXML = false;
    //bool gzip = false;

    po::options_description od_config("Options");
    od_config.add_options()
        ("filelist,f",
            po::value<string>(&filelistFilename),
            ": specify text file containing filenames")
        ("outdir,o",
            po::value<string>(&config.outputPath)->default_value(config.outputPath),
            ": set output directory ('-' for stdout) [.]")
        ("config,c", 
            po::value<string>(&configFilename),
            ": configuration file (optionName=value)")
        ("ext,e",
            po::value<string>(&config.extension)->default_value(config.extension),
            ": set extension for output files [mzid|pepXML|txt]")
        ("mzIdentML",
            po::value<bool>(&format_mzIdentML)->zero_tokens(),
            ": write mzIdentML format [default]")
        ("pepXML",
            po::value<bool>(&format_pepXML)->zero_tokens(),
            ": write pepXML format")
        ("text",
            po::value<bool>(&format_text)->zero_tokens(),
            ": write hierarchical text format")
        ("verbose,v",
            po::value<bool>(&config.verbose)->zero_tokens(),
            ": display detailed progress information")
        /*("contactInfo,i",
            po::value<string>(&config.contactFilename),
            ": filename for contact info")
        ("gzip,g",
            po::value<bool>(&gzip)->zero_tokens(),
            ": gzip entire output file (adds .gz to filename)")
        ("filter",
            po::value< vector<string> >(&config.filters),
            ": add a spectrum list filter")
        ("merge",
            po::value<bool>(&config.merge)->zero_tokens(),
            ": create a single output file from multiple input files by merging file-level metadata and concatenating spectrum lists")*/
        ;

    // append options description to usage string

    usage << od_config;

    // extra usage

    usage << "Examples:\n"
          << endl
          << "# convert sequest.pepXML to sequest.mzid\n"
          << "idconvert sequest.pepXML\n"
          << endl
          << "# convert sequest.protXML to sequest.mzid\n"
          << "# Also reads any pepXML file referenced in the \n"
          << "# protXML file if available.  If the protXML \n"
          << "# file has been moved from its original location, \n"
          << "# the pepXML will still be found if it has also \n"
          << "# been moved to the same position relative to the \n"
          << "# protXML file. This relative position is determined \n"
          << "# by reading the protXML protein_summary:summary_xml \n"
          << "# and protein_summary_header:source_files values.\n"
          << "idconvert sequest.protXML\n"
          << endl
          << "# convert mascot.mzid to mascot.pepXML\n"
          << "idconvert mascot.mzid --pepXML\n"
          << endl
          << "# put output file in my_output_dir\n"
          << "idconvert omssa.pepXML -o my_output_dir\n"
          << endl
          << "# use a configuration file\n"
          << "idconvert xtandem.pep.xml -c config.txt\n"
          << endl
          << "# example configuration file\n"
          << "pepXML=true\n"
          //<< "gzip=true\n"
          << endl
          << endl

          << "Questions, comments, and bug reports:\n"
          << "https://github.com/ProteoWizard\n"
          << "support@proteowizard.org\n"
          << "\n"
          << "ProteoWizard release: " << pwiz::Version::str() << " (" << pwiz::Version::LastModified() << ")" << endl
          << "ProteoWizard IdentData: " << pwiz::identdata::Version::str() << " (" << pwiz::identdata::Version::LastModified() << ")" << endl
          << "Build date: " << __DATE__ << " " << __TIME__ << endl;

    if (argc <= 1)
        throw usage_exception(usage.str());

    // handle positional arguments

    const char* label_args = "args";

    po::options_description od_args;
    od_args.add_options()(label_args, po::value< vector<string> >(), "");

    po::positional_options_description pod_args;
    pod_args.add(label_args, -1);
   
    po::options_description od_parse;
    od_parse.add(od_config).add(od_args);

    // parse command line

    po::variables_map vm;
    po::store(po::command_line_parser(argc, (char**)argv).
              options(od_parse).positional(pod_args).run(), vm);
    po::notify(vm);

    // parse config file if required

    if (!configFilename.empty())
    {
        ifstream is(configFilename.c_str());

        if (is)
        {
            cout << "Reading configuration file " << configFilename << "\n\n";
            po::store(parse_config_file(is, od_config), vm);
            po::notify(vm);
        }
        else
        {
            cout << "Unable to read configuration file " << configFilename << "\n\n";
        }
    }

    // remember filenames from command line

    if (vm.count(label_args))
    {
        config.filenames = vm[label_args].as< vector<string> >();

        // expand the filenames by globbing to handle wildcards
        vector<bfs::path> globbedFilenames;
        BOOST_FOREACH(const string& filename, config.filenames)
            if (expand_pathmask(bfs::path(filename), globbedFilenames) == 0)
                cout <<  "[idconvert] no files found matching \"" << filename << "\"" << endl;

        config.filenames.clear();
        BOOST_FOREACH(const bfs::path& filename, globbedFilenames)
            config.filenames.push_back(filename.string());
    }

    // parse filelist if required

    if (!filelistFilename.empty())
    {
        ifstream is(filelistFilename.c_str());
        while (is)
        {
            string filename;
            getline(is, filename);
            if (is) config.filenames.push_back(filename);
        }
    }

    // check stuff

    if (config.filenames.empty())
        throw user_error("[idconvert] No files specified.");

    int count = format_text + format_mzIdentML + format_pepXML;
    if (count > 1) throw user_error("[idconvert] Multiple format flags specified.");
    if (format_text) config.writeConfig.format = IdentDataFile::Format_Text;
    if (format_mzIdentML) config.writeConfig.format = IdentDataFile::Format_MzIdentML;
    if (format_pepXML) config.writeConfig.format = IdentDataFile::Format_pepXML;

    //config.writeConfig.gzipped = gzip; // if true, file is written as .gz

    if (config.extension.empty())
    {
        switch (config.writeConfig.format)
        {
            case IdentDataFile::Format_Text:
                config.extension = ".txt";
                break;
            case IdentDataFile::Format_MzIdentML:
                config.extension = ".mzid";
                break;
            case IdentDataFile::Format_pepXML:
                config.extension = ".pepXML";
                break;
            default:
                throw user_error("[idconvert] Unsupported format."); 
        }
        /*if (config.writeConfig.gzipped) 
        {
            config.extension += ".gz";
        }*/
    }

    return config;
}


/*void addContactInfo(MSData& msd, const string& contactFilename)
{
    ifstream is(contactFilename.c_str());
    if (!is)
    {
        cerr << "unable to read contact info: " << contactFilename << endl; 
        return;
    }

    Contact contact;
    IO::read(is, contact);
    msd.fileDescription.contacts.push_back(contact);
}*/


struct UserFeedbackIterationListener : public IterationListener
{
    virtual Status update(const UpdateMessage& updateMessage)
    {
        int index = updateMessage.iterationIndex;
        int count = updateMessage.iterationCount;

        
        // when the index is 0, create a new line
        if (index == 0)
            cout << endl;

        cout << updateMessage.message;
        if (index > 0)
        {
            cout << ": " << index+1;
            if (count > 0)
                cout << "/" << count;
        }

        // add tabs to erase all of the previous line
        cout << "\t\t\t\r" << flush;

        return Status_Ok;
    }
};


/*void calculateSourceFilePtrSHA1(const SourceFilePtr& sourceFilePtr)
{
    calculateSourceFileSHA1(*sourceFilePtr);
}*/


/*int mergeFiles(const vector<string>& filenames, const Config& config, const ReaderList& readers)
{
    vector<MSDataPtr> msdList;
    int failedFileCount = 0;

    BOOST_FOREACH(const string& filename, filenames)
    {
        try
        {
            cout << "processing file: " << filename << endl;
            readers.read(filename, msdList);
        }
        catch (exception& e)
        {
            ++failedFileCount;
            cerr << "Error reading file " << filename << ":\n" << e.what() << endl;
        }
    }

    // handle progress updates if requested

    IterationListenerRegistry iterationListenerRegistry;
    UserFeedbackIterationListener feedback;
    // update on the first spectrum, the last spectrum, the 100th spectrum, the 200th spectrum, etc.
    const size_t iterationPeriod = 100;
    iterationListenerRegistry.addListener(feedback, iterationPeriod);
    IterationListenerRegistry* pILR = config.verbose ? &iterationListenerRegistry : 0;

    try
    {
        MSDataMerger msd(msdList);

        cout << "calculating source file checksums" << endl;
        // calculate SHA1 checksums
        for_each(msd.fileDescription.sourceFilePtrs.begin(),
                 msd.fileDescription.sourceFilePtrs.end(),
                 &calculateSourceFilePtrSHA1);

        if (!config.contactFilename.empty())
            addContactInfo(msd, config.contactFilename);

        SpectrumListFactory::wrap(msd, config.filters);

        string outputFilename = config.outputFilename("merged-spectra", msd);
        cout << "writing output file: " << outputFilename << endl;

        if (config.outputPath == "-")
            MSDataFile::write(msd, cout, config.writeConfig, pILR);
        else
            MSDataFile::write(msd, outputFilename, config.writeConfig, pILR);
    }
    catch (exception& e)
    {
        failedFileCount = (int)filenames.size();
        cerr << "Error merging files: " << e.what() << endl;
    }

    return failedFileCount;
}*/


void processFile(const string& filename, const Config& config, const ReaderList& readers)
{
    // handle progress updates if requested

    IterationListenerRegistry iterationListenerRegistry;
    // update on the first spectrum, the last spectrum, the 100th spectrum, the 200th spectrum, etc.
    const double iterationPeriod = 0.5;
    iterationListenerRegistry.addListenerWithTimer(IterationListenerPtr(new UserFeedbackIterationListener), iterationPeriod);
    IterationListenerRegistry* pILR = config.verbose ? &iterationListenerRegistry : 0; 

    // read in data file

    cout << "processing file: " << filename << endl;

    vector<IdentDataPtr> iddList;
    Reader::Config readerConfig;
    readerConfig.iterationListenerRegistry = pILR;
    readerConfig.ignoreProteinDetectionList = config.writeConfig.format == IdentDataFile::Format_pepXML;
    readers.read(filename, iddList, readerConfig);

    for (size_t i=0; i < iddList.size(); ++i)
    {
        IdentData& idd = *iddList[i];
        try
        {
            // process the data 

            /*if (!config.contactFilename.empty())
                addContactInfo(msd, config.contactFilename);*/

            // write out the new data file
            string outputFilename = config.outputFilename(filename, idd, iddList.size() > 1);
            cout << "writing output file: " << outputFilename << endl;

            if (config.outputPath == "-")
                IdentDataFile::write(idd, outputFilename, cout, config.writeConfig);
            else
                IdentDataFile::write(idd, outputFilename, config.writeConfig, pILR);
        }
        catch (exception& e)
        {
            cerr << "Error writing analysis " << (i+1) << " in " << bfs::path(filename).leaf() << ":\n" << e.what() << endl;
        }
    }
    cout << endl;
}


int go(const Config& config)
{
    cout << config;

    boost::filesystem::create_directories(config.outputPath);

    DefaultReaderList readers;

    int failedFileCount = 0;

    /*if (config.merge)
        failedFileCount = mergeFiles(config.filenames, config, readers);
    else*/
    {

        BOOST_FOREACH(const string& filename, config.filenames)
        {
            try
            {
                processFile(filename, config, readers);
            }
            catch (exception& e)
            {
                failedFileCount++;
                cout << e.what() << endl;
                cout << "Error processing file " << filename << "\n\n"; 
            }
        }
    }

    return failedFileCount;
}


int main(int argc, const char* argv[])
{
    try
    {
        Config config = parseCommandLine(argc, argv);        
        return go(config);
    }
    catch (usage_exception& e)
    {
        cerr << e.what() << endl;
        return 0;
    }
    catch (user_error& e)
    {
        cerr << e.what() << endl;
        return 1;
    }
    catch (boost::program_options::error& e)
    {
        cerr << "Invalid command-line: " << e.what() << endl;
        return 1;
    }
    catch (exception& e)
    {
        cerr << e.what() << endl;
    }
    catch (...)
    {
        cerr << "[" << argv[0] << "] Caught unknown exception.\n";
    }

    cerr << "Please report this error to support@proteowizard.org.\n"
         << "Attach the command output and this version information in your report:\n"
         << "\n"
         << "ProteoWizard release: " << pwiz::Version::str() << " (" << pwiz::Version::LastModified() << ")" << endl
         << "Build date: " << __DATE__ << " " << __TIME__ << endl;

    return 1;
}