File: SpecLibCreator.C

package info (click to toggle)
openms 1.11.1-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436,688 kB
  • ctags: 150,907
  • sloc: cpp: 387,126; xml: 71,547; python: 7,764; ansic: 2,626; php: 2,499; sql: 737; ruby: 342; sh: 325; makefile: 128
file content (316 lines) | stat: -rw-r--r-- 11,374 bytes parent folder | download
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
// --------------------------------------------------------------------------
//                   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: David Wojnar $
// $Authors: $
// --------------------------------------------------------------------------

#include <OpenMS/KERNEL/MSExperiment.h>
#include <OpenMS/FORMAT/CsvFile.h>
#include <OpenMS/APPLICATIONS/TOPPBase.h>
#include <OpenMS/FORMAT/FileTypes.h>
#include <OpenMS/FORMAT/FileHandler.h>
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/FORMAT/MSPFile.h>
#include <OpenMS/KERNEL/RichPeak1D.h>
#include <iostream>

#include <vector>
#include <cmath>
using namespace OpenMS;
using namespace std;

//-------------------------------------------------------------
//Doxygen docu
//-------------------------------------------------------------

/**
  @page UTILS_SpecLibCreator SpecLibCreator

    @brief creates with given data a .MSP format spectral library.

    Information file should have the following information: peptide, retention time, measured weight, charge state.
    Extra information is allowed.

    @experimental This Utility is not well tested and some features might not work as expected.

    <B>The command line parameters of this tool are:</B>
    @verbinclude UTILS_SpecLibCreator.cli
    <B>INI file documentation of this tool:</B>
    @htmlinclude UTILS_SpecLibCreator.html
*/

// We do not want this class to show up in the docu:
/// @cond TOPPCLASSES

class TOPPSpecLibCreator :
  public TOPPBase
{
public:
  TOPPSpecLibCreator() :
    TOPPBase("SpecLibCreator", "Creates an MSP formatted spectral library.", false)
  {
  }

protected:
  void registerOptionsAndFlags_()
  {
    registerInputFile_("info", "<file>", "", "Holds id, peptide, retention time etc.");
    setValidFormats_("info", StringList::create("csv"));
    
    registerStringOption_("itemseperator", "<char>", ",", " Seperator between items. e.g. ,", false);
    registerStringOption_("itemenclosed", "<bool>", "false", "'true' or 'false' if true every item is enclosed e.g. '$peptide$,$run$...", false);
    setValidStrings_("itemenclosed", StringList::create("true,false"));
    
    registerInputFile_("spec", "<file>", "", "spectra");
    setValidFormats_("spec", StringList::create("mzData,mzXML"));

    registerOutputFile_("out", "<file>", "", "output MSP formated spectra library");
    setValidFormats_("out",StringList::create("msp"));
  }

  ExitCodes main_(int, const char **)
  {
    //-------------------------------------------------------------
    // parameter handling
    //-------------------------------------------------------------

    String info = getStringOption_("info");
    String itemseperator = getStringOption_("itemseperator");
    String out = getStringOption_("out");
    bool itemenclosed;
    if (getStringOption_("itemenclosed") == "true")
    {
      itemenclosed  = true;
    }
    else
    {
      itemenclosed = false;
    }

    String spec = getStringOption_("spec");
    if (info == String::EMPTY)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "info");
    }
    if (spec == String::EMPTY)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "spec");
    }


    //-------------------------------------------------------------
    // loading input
    //-------------------------------------------------------------
    Int retention_time = -1;
    Int peptide = -1;
    Int measured_weight = -1;
    //UInt first_scan;
    UInt charge_state(0), Experimental_id(0);        //,found_by, track, comment, vaccination_peptid,epitope, confident, hlaallele;
    const char * sepi = itemseperator.c_str();
    char sepo = *sepi;
    CsvFile csv_file(info, sepo, itemenclosed);
    vector<StringList>  list;

    list.resize(csv_file.size());

    for (UInt i = 0; i < csv_file.size(); ++i)
    {
      csv_file.getRow(i, list[i]);
    }
    for (UInt i = 0; i < list[0].size(); ++i)
    {

      if (list[0][i].toLower().removeWhitespaces().compare("retentiontime") == 0)
      {
        retention_time = i;
      }
      else if (list[0][i].toLower().hasSubstring("_id"))
      {
        Experimental_id = i;
      }
      else if (list[0][i].toLower() == "last scan")
      {
        // last_scan = i;
      }
      else if (list[0][i].toLower() == "modification")
      {
        // modification = i;
      }
      else if (list[0][i].toLower().removeWhitespaces().compare("chargestate") == 0 || list[0][i].toLower().removeWhitespaces().hasSubstring("charge"))
      {
        charge_state = i;
      }
      else if (list[0][i].toLower().trim().compare("peptide") == 0)
      {
        peptide = i;
      }
      else if (list[0][i].toLower().removeWhitespaces().hasSubstring("measuredweight")  || list[0][i].removeWhitespaces().compare("measuredweight[M+nH]n+") == 0)
      {
        measured_weight = i;
      }
    }
    if (retention_time  == -1)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "unclear which parameter is retention time");
    }
    if (peptide  == -1)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "unclear which parameter is peptide");
    }
    if (measured_weight  == -1)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "unclear which parameter is measured weight");
    }
    FileHandler fh;
    FileTypes::Type in_type = fh.getType(spec);
    /*MSExperiment<>*/ PeakMap msexperiment;

    if (in_type == FileTypes::UNKNOWN)
    {
      writeLog_("Warning: Could not determine input file type!");
    }
    else if (in_type == FileTypes::MZDATA)
    {
      MzDataFile mzData;
      mzData.load(spec, msexperiment);
    }
    else if (in_type == FileTypes::MZXML)
    {
      MzXMLFile mzXML;
      mzXML.load(spec, msexperiment);
    }
    if (msexperiment.getMinRT() == 0)
    {
      throw Exception::RequiredParameterNotGiven(__FILE__, __LINE__, __PRETTY_FUNCTION__, "EMPTY??");
    }
    RichPeakMap library;

    //-------------------------------------------------------------
    // creating library
    //-------------------------------------------------------------
    UInt found_counter = 0;

    for (UInt i = 1; i < list.size(); ++i)
    {
      bool no_peptide = true;
      DoubleReal rt =  (60 * (list[i][retention_time].toFloat()));              // from minutes to seconds
      DoubleReal mz = list[i][measured_weight].toFloat();
      for (MSExperiment<>::Iterator it = msexperiment.begin(); it < msexperiment.end(); ++it)
      {
        //cout<<"i =" <<i<<endl;
        //cout<<rt <<" (rt) - " << it->getRT()<<" (getRT) = "<<(rt - it->getRT())<<endl;
        if ((abs(rt - it->getRT()) < 5) && (abs(mz - it->getPrecursors()[0].getMZ()) < 0.1))
        {
          //if ( ceil(rt) == ceil(it->getRT()) || ceil(rt) == floor(it->getRT()) || floor(rt) == ceil(it->getRT()) || floor(rt) == floor(it->getRT()))
          ++found_counter;
          no_peptide = false;
          cout << "Found Peptide " << list[i][peptide] << " with id: " << list[i][Experimental_id] << "\n";
          cout << "rt: " << it->getRT() << " and mz: " << it->getPrecursors()[0].getMZ() << "\n";

          // MSSpectrum<RichPeak1D> spec;
          // for(UInt k = 0; k < it->size(); ++k)
          // {
          //  spec.push_back(it->operator[](k));
          //
          // }
          MSSpectrum<RichPeak1D> speci;
          speci.setRT(it->getRT());
          speci.setMSLevel(2);
          speci.setPrecursors(it->getPrecursors());
          for (UInt j = 0; j < it->size(); ++j)
          {

            RichPeak1D richy;
            richy.setIntensity(it->operator[](j).getIntensity());
            richy.setPosition(it->operator[](j).getPosition());
            richy.setMZ(it->operator[](j).getMZ());
            richy.setPos(it->operator[](j).getPos());                    //ALIAS for setMZ???

            speci.push_back(richy);
          }
          PeptideHit hit;                  // = *it->getPeptideIdentifications().begin()->getHits().begin();
          AASequence aa(list[i][peptide]);
          hit.setSequence(aa);
          hit.setCharge(list[i][charge_state].toInt());
          vector<PeptideHit> hits;
          hits.push_back(hit);
          vector<PeptideIdentification> pepi;
          PeptideIdentification pep;
          pep.setHits(hits);
          pepi.push_back(pep);
          speci.setPeptideIdentifications(pepi);
          //it->getPeptideIdentifications().begin()->setHits(hits);
          library.addSpectrum(speci);
        }
      }
      if (no_peptide)
      {
        cout << "Peptide: " << list[i][peptide] << " not found\n";
      }
    }
    cout << "Found " << found_counter << " peptides\n";
    //library = static_cast<MSExperiment<MSSpectrum<RichPeak1D> > >(msexperiment);

    //-------------------------------------------------------------
    // writing output
    //-------------------------------------------------------------
    in_type = fh.getType(out);
    if (in_type == FileTypes::MZDATA)
    {
      MzDataFile mzData;
      mzData.store(out, library);
    }
    else if (in_type == FileTypes::MZXML)
    {
      MzXMLFile mzXML;
      mzXML.store(out, library);
    }
    else
    {
      MSPFile msp;
      msp.store(out, library);
    }

    return EXECUTION_OK;
  }

};




int main(int argc, const char ** argv)
{
  TOPPSpecLibCreator tool;
  return tool.main(argc, argv);
}

/// @endcond