File: InspectInfile.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 (481 lines) | stat: -rw-r--r-- 16,423 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
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
// --------------------------------------------------------------------------
//                   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: Andreas Bertsch $
// $Authors: $
// --------------------------------------------------------------------------

#include <OpenMS/FORMAT/InspectInfile.h>
#include <OpenMS/CHEMISTRY/EmpiricalFormula.h>
#include <OpenMS/SYSTEM/File.h>
#include <OpenMS/FORMAT/PTMXMLFile.h>

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

namespace OpenMS
{

  // default constructor
  InspectInfile::InspectInfile() :
    modifications_per_peptide_(-1),
    blind_(2),
    maxptmsize_(-1.0),
    precursor_mass_tolerance_(-1.0),
    peak_mass_tolerance_(-1.0),
    multicharge_(2),
    tag_count_(-1)
  {
  }

  // copy constructor
  InspectInfile::InspectInfile(const InspectInfile & inspect_infile) :
    spectra_(inspect_infile.getSpectra()),
    enzyme_(inspect_infile.getEnzyme()),
    modifications_per_peptide_(inspect_infile.getModificationsPerPeptide()),
    blind_(inspect_infile.getBlind()),
    maxptmsize_(inspect_infile.getMaxPTMsize()),
    precursor_mass_tolerance_(inspect_infile.getPrecursorMassTolerance()),
    peak_mass_tolerance_(inspect_infile.getPeakMassTolerance()),
    multicharge_(inspect_infile.getMulticharge()),
    instrument_(inspect_infile.getInstrument()),
    tag_count_(inspect_infile.getTagCount()),
    PTMname_residues_mass_type_(inspect_infile.getModifications())
  {
  }

  // destructor
  InspectInfile::~InspectInfile()
  {
    PTMname_residues_mass_type_.clear();
  }

  // assignment operator
  InspectInfile & InspectInfile::operator=(const InspectInfile & inspect_infile)
  {
    if (this != &inspect_infile)
    {
      spectra_ = inspect_infile.getSpectra();
      enzyme_ = inspect_infile.getEnzyme();
      modifications_per_peptide_ = inspect_infile.getModificationsPerPeptide();
      blind_ = inspect_infile.getBlind();
      maxptmsize_ = inspect_infile.getMaxPTMsize();
      precursor_mass_tolerance_ = inspect_infile.getPrecursorMassTolerance();
      peak_mass_tolerance_ = inspect_infile.getPeakMassTolerance();
      multicharge_ = inspect_infile.getMulticharge();
      instrument_ = inspect_infile.getInstrument();
      tag_count_ = inspect_infile.getTagCount();
      PTMname_residues_mass_type_ = inspect_infile.getModifications();
    }
    return *this;
  }

  // equality operator
  bool InspectInfile::operator==(const InspectInfile & inspect_infile) const
  {
    if (this != &inspect_infile)
    {
      bool equal = true;
      equal &= (spectra_ == inspect_infile.getSpectra());
      equal &= (enzyme_ == inspect_infile.getEnzyme());
      equal &= (modifications_per_peptide_ == inspect_infile.getModificationsPerPeptide());
      equal &= (blind_ == inspect_infile.getBlind());
      equal &= (maxptmsize_ == inspect_infile.getMaxPTMsize());
      equal &= (precursor_mass_tolerance_ == inspect_infile.getPrecursorMassTolerance());
      equal &= (peak_mass_tolerance_ == inspect_infile.getPeakMassTolerance());
      equal &= (multicharge_ == inspect_infile.getMulticharge());
      equal &= (instrument_ == inspect_infile.getInstrument());
      equal &= (tag_count_ == inspect_infile.getTagCount());
      equal &= (PTMname_residues_mass_type_ == inspect_infile.getModifications());
      return equal;
    }
    return true;
  }

  void InspectInfile::store(const String & filename)
  {
    ofstream ofs(filename.c_str());
    if (!ofs)
    {
      throw Exception::UnableToCreateFile(__FILE__, __LINE__, __PRETTY_FUNCTION__, filename);
    }
    stringstream file_content;

    file_content << "spectra," << spectra_ << "\n";

    if (!db_.empty())
      file_content << "db," << db_ << "\n";

    if (!enzyme_.empty())
      file_content << "protease," << enzyme_ << "\n";

    if (blind_ != 2)
      file_content << "blind," << blind_ << "\n";

    //mod,+57,C,fix,carbamidomethylation
    for (Map<String, vector<String> >::iterator mods_i = PTMname_residues_mass_type_.begin(); mods_i != PTMname_residues_mass_type_.end(); ++mods_i)
    {
      // fix", "cterminal", "nterminal", and "opt
      mods_i->second[2].toLower();
      if (mods_i->second[2].hasSuffix("term"))
        mods_i->second[2].append("inal");
      file_content << "mod," << mods_i->second[1] << "," << mods_i->second[0] << "," << mods_i->second[2] << "," << mods_i->first << "\n";
    }

    if (modifications_per_peptide_ > -1)
      file_content << "mods," << modifications_per_peptide_ << "\n";

    if (maxptmsize_ >= 0)
      file_content << "maxptmsize," << maxptmsize_ << "\n";

    if (precursor_mass_tolerance_ >= 0)
      file_content << "PM_tolerance," << precursor_mass_tolerance_ << "\n";

    if (peak_mass_tolerance_ >= 0)
      file_content << "IonTolerance," << peak_mass_tolerance_ << "\n";

    if (multicharge_ != 2)
      file_content << "multicharge," << multicharge_ << "\n";

    if (!instrument_.empty())
      file_content << "instrument," << instrument_ << "\n";

    if (tag_count_ >= 0)
      file_content << "TagCount," << tag_count_ << "\n";

    ofs << file_content.str();

    ofs.close();
    ofs.clear();
  }

  void InspectInfile::handlePTMs(const String & modification_line, const String & modifications_filename, const bool monoisotopic)
  {
    PTMname_residues_mass_type_.clear();
    // to store the information about modifications from the ptm xml file
    Map<String, pair<String, String> > ptm_informations;
    if (!modification_line.empty())       // if modifications are used look whether whether composition and residues (and type and name) is given, the name (type) is used (then the modifications file is needed) or only the mass and residues (and type and name) is given
    {
      vector<String> modifications, mod_parts;
      modification_line.split(':', modifications);       // get the single modifications
      if (modifications.empty())
        modifications.push_back(modification_line);

      // to get masses from a formula
      EmpiricalFormula add_formula, substract_formula;

      String types = "OPT#FIX#";
      String name, residues, mass, type;

      // 0 - mass; 1 - composition; 2 - ptm name
      Int mass_or_composition_or_name(-1);

      for (vector<String>::const_iterator mod_i = modifications.begin(); mod_i != modifications.end(); ++mod_i)
      {
        if (mod_i->empty())
          continue;
        // clear the formulae
        add_formula = substract_formula = name = residues = mass = type = "";

        // get the single parts of the modification string
        mod_i->split(',', mod_parts);
        if (mod_parts.empty())
          mod_parts.push_back(*mod_i);
        mass_or_composition_or_name = -1;

        // check whether the first part is a mass, composition or name

        // check whether it is a mass
        try
        {
          mass = mod_parts.front();
          // to check whether the first part is a mass, it is converted into a float and then back into a string and compared to the given string
          // remove + signs because they don't appear in a float
          if (mass.hasPrefix("+"))
            mass.erase(0, 1);
          if (mass.hasSuffix("+"))
            mass.erase(mass.length() - 1, 1);
          if (mass.hasSuffix("-"))             // a - sign at the end will not be converted
          {
            mass.erase(mass.length() - 1, 1);
            mass.insert(0, "-");
          }
          // if it is a mass
          if (String(mass.toFloat()) == mass)
            mass_or_composition_or_name = 0;
        }
        catch (Exception::ConversionError & /*c_e*/)
        {
          mass_or_composition_or_name = -1;
        }

        // check whether it is a name (look it up in the corresponding file)
        if (mass_or_composition_or_name == -1)
        {
          if (ptm_informations.empty())             // if the ptm xml file has not been read yet, read it
          {
            if (!File::exists(modifications_filename))
            {
              throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, modifications_filename);
            }
            if (!File::readable(modifications_filename))
            {
              throw Exception::FileNotReadable(__FILE__, __LINE__, __PRETTY_FUNCTION__, modifications_filename);
            }

            // getting all available modifications from a file
            PTMXMLFile().load(modifications_filename, ptm_informations);
          }
          // if the modification cannot be found
          if (ptm_informations.find(mod_parts.front()) != ptm_informations.end())
          {
            mass = ptm_informations[mod_parts.front()].first;             // composition
            residues = ptm_informations[mod_parts.front()].second;             // residues
            name = mod_parts.front();             // name

            mass_or_composition_or_name = 2;
          }
        }

        // check whether it's an empirical formula / if a composition was given, get the mass
        if (mass_or_composition_or_name == -1)
          mass = mod_parts.front();
        if (mass_or_composition_or_name == -1 || mass_or_composition_or_name == 2)
        {
          // check whether there is a positive and a negative formula
          String::size_type pos = mass.find("-");
          try
          {
            if (pos != String::npos)
            {
              add_formula = mass.substr(0, pos);
              substract_formula = mass.substr(++pos);
            }
            else
            {
              add_formula = mass;
            }
            // sum up the masses
            if (monoisotopic)
              mass = String(add_formula.getMonoWeight() - substract_formula.getMonoWeight());
            else
              mass = String(add_formula.getAverageWeight() - substract_formula.getAverageWeight());
            if (mass_or_composition_or_name == -1)
              mass_or_composition_or_name = 1;
          }
          catch (Exception::ParseError & /*pe*/)
          {
            PTMname_residues_mass_type_.clear();
            throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, *mod_i, "There's something wrong with this modification. Aborting!");
          }
        }

        // now get the residues
        mod_parts.erase(mod_parts.begin());
        if (mass_or_composition_or_name < 2)
        {
          if (mod_parts.empty())
          {
            PTMname_residues_mass_type_.clear();
            throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, *mod_i, "No residues for modification given. Aborting!");
          }

          // get the residues
          residues = mod_parts.front();
          residues.substitute('*', 'X');
          residues.toUpper();
          mod_parts.erase(mod_parts.begin());
        }

        // get the type
        if (mod_parts.empty())
          type = "OPT";
        else
        {
          type = mod_parts.front();
          type.toUpper();
          if (types.find(type) != String::npos)
            mod_parts.erase(mod_parts.begin());
          else
            type = "OPT";
        }

        if (mod_parts.size() > 1)
        {
          PTMname_residues_mass_type_.clear();
          throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, *mod_i, "There's something wrong with the type of this modification. Aborting!");
        }

        // get the name
        if (mass_or_composition_or_name < 2)
        {
          if (mod_parts.empty())
            name = "PTM_" + String(PTMname_residues_mass_type_.size());
          else
            name = mod_parts.front();
        }

        // insert the modification
        if (PTMname_residues_mass_type_.find(name) == PTMname_residues_mass_type_.end())
        {
          PTMname_residues_mass_type_[name] = vector<String>(3);
          PTMname_residues_mass_type_[name][0] = residues;
          // mass must not have more than 5 digits after the . (otherwise the test may fail)
          PTMname_residues_mass_type_[name][1] = mass.substr(0, mass.find(".") + 6);
          PTMname_residues_mass_type_[name][2] = type;
        }
        else
        {
          PTMname_residues_mass_type_.clear();
          throw Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, *mod_i, "There's already a modification with this name. Aborting!");
        }
      }
    }
  }

  const Map<String, vector<String> > & InspectInfile::getModifications() const
  {
    return PTMname_residues_mass_type_;
  }

  const String & InspectInfile::getSpectra() const
  {
    return spectra_;
  }

  void InspectInfile::setSpectra(const String & spectra)
  {
    spectra_ = spectra;
  }

  const String & InspectInfile::getDb() const
  {
    return db_;
  }

  void InspectInfile::setDb(const String & db)
  {
    db_ = db;
  }

  const String & InspectInfile::getEnzyme() const
  {
    return enzyme_;
  }

  void InspectInfile::setEnzyme(const String & enzyme)
  {
    enzyme_ = enzyme;
  }

  Int InspectInfile::getModificationsPerPeptide() const
  {
    return modifications_per_peptide_;
  }

  void InspectInfile::setModificationsPerPeptide(Int modifications_per_peptide)
  {
    modifications_per_peptide_ = modifications_per_peptide;
  }

  UInt InspectInfile::getBlind() const
  {
    return blind_;
  }

  void InspectInfile::setBlind(UInt blind)
  {
    blind_ = blind;
  }

  Real InspectInfile::getMaxPTMsize() const
  {
    return maxptmsize_;
  }

  void InspectInfile::setMaxPTMsize(Real maxptmsize)
  {
    maxptmsize_ = maxptmsize;
  }

  Real InspectInfile::getPrecursorMassTolerance() const
  {
    return precursor_mass_tolerance_;
  }

  void InspectInfile::setPrecursorMassTolerance(Real precursor_mass_tolerance)
  {
    precursor_mass_tolerance_ = precursor_mass_tolerance;
  }

  Real InspectInfile::getPeakMassTolerance() const
  {
    return peak_mass_tolerance_;
  }

  void InspectInfile::setPeakMassTolerance(Real ion_tolerance)
  {
    peak_mass_tolerance_ = ion_tolerance;
  }

  UInt InspectInfile::getMulticharge() const
  {
    return multicharge_;
  }

  void InspectInfile::setMulticharge(UInt multicharge)
  {
    multicharge_ = multicharge;
  }

  const String & InspectInfile::getInstrument() const
  {
    return instrument_;
  }

  void InspectInfile::setInstrument(const String & instrument)
  {
    instrument_ = instrument;
  }

  Int InspectInfile::getTagCount() const
  {
    return tag_count_;
  }

  void InspectInfile::setTagCount(Int tag_count)
  {
    tag_count_ = tag_count;
  }

} // namespace OpenMS