File: OMSSAXMLFile.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 (430 lines) | stat: -rw-r--r-- 13,083 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
// --------------------------------------------------------------------------
//                   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: Andreas Bertsch $
// --------------------------------------------------------------------------

#include <OpenMS/FORMAT/OMSSAXMLFile.h>
#include <OpenMS/FORMAT/TextFile.h>
#include <OpenMS/SYSTEM/File.h>
#include <OpenMS/CHEMISTRY/ModificationsDB.h>

using namespace std;

namespace OpenMS
{

  OMSSAXMLFile::OMSSAXMLFile() :
    XMLHandler("", 1.1),
    XMLFile(),
    peptide_identifications_(0)
  {
    readMappingFile_();
  }

  OMSSAXMLFile::~OMSSAXMLFile()
  {
  }

  void OMSSAXMLFile::load(const String & filename, ProteinIdentification & protein_identification, vector<PeptideIdentification> & peptide_identifications, bool load_proteins)
  {
    //Filename for error messages in XMLHandler
    file_ = filename;

    load_proteins_ = load_proteins;
    peptide_identifications_ = &peptide_identifications;

    parse_(filename, this);


    DateTime now = DateTime::now();
    String identifier("OMSSA_" + now.get());

    // post-processing
    vector<String> accessions;
    for (vector<PeptideIdentification>::iterator it = peptide_identifications.begin(); it != peptide_identifications.end(); ++it)
    {
      it->setScoreType("OMSSA");
      it->setHigherScoreBetter(false);
      it->setIdentifier(identifier);
      it->assignRanks();

      if (load_proteins)
      {
        for (vector<PeptideHit>::const_iterator pit = it->getHits().begin(); pit != it->getHits().end(); ++pit)
        {
          accessions.insert(accessions.end(), pit->getProteinAccessions().begin(), pit->getProteinAccessions().end());
        }
      }
    }

    if (load_proteins)
    {
      sort(accessions.begin(), accessions.end());
      vector<String>::const_iterator end_unique = unique(accessions.begin(), accessions.end());

      for (vector<String>::const_iterator it = accessions.begin(); it != end_unique; ++it)
      {
        ProteinHit hit;
        hit.setAccession(*it);
        protein_identification.insertHit(hit);
      }


      // E-values
      protein_identification.setHigherScoreBetter(false);
      protein_identification.setScoreType("OMSSA");
      protein_identification.setIdentifier(identifier);
    }

    // version of OMSSA is not available
    // Date of the search is not available -> set it to now
    protein_identification.setDateTime(now);
    protein_identification.setIdentifier(identifier);

    // search parameters are also not available
  }

  void OMSSAXMLFile::startElement(const XMLCh * const /*uri*/, const XMLCh * const /*local_name*/, const XMLCh * const qname, const xercesc::Attributes & /*attributes*/)
  {
    tag_ = String(sm_.convert(qname)).trim();

    if (tag_ == "MSHitSet_number")
    {
      return;
    }

    if (tag_ == "MSPepHit")
    {
      return;
    }
    if (tag_ == "MSHits")
    {
      return;
    }

    if (tag_ == "MSHitSet")
    {
      return;
    }
  }

  void OMSSAXMLFile::endElement(const XMLCh * const /*uri*/, const XMLCh * const /*local_name*/, const XMLCh * const qname)
  {
    tag_ = String(sm_.convert(qname)).trim();

    // protein hits (MSPepHits) are handled in ::characters(...)

    // end of peptide hit
    if (tag_ == "MSHits")
    {
      actual_peptide_id_.insertHit(actual_peptide_hit_);
      actual_peptide_hit_ = PeptideHit();
      tag_ = "";
      return;
    }

    // end of peptide id
    if (tag_ == "MSHitSet")
    {
      peptide_identifications_->push_back(actual_peptide_id_);
      actual_peptide_id_.assignRanks();
      actual_peptide_id_ = PeptideIdentification();
    }

    /*
    Modifications:

    <MSModHit>
        <MSModHit_site>1</MSModHit_site>
  <MSModHit_modtype>
    <MSMod>3</MSMod>
  </MSModHit_modtype>
</MSModHit>
    */
    if (tag_ == "MSModHit")
    {
      if (mods_map_.has(actual_mod_type_.toInt()) && mods_map_[actual_mod_type_.toInt()].size() > 0)
      {
        if (mods_map_[actual_mod_type_.toInt()].size() > 1)
        {
          warning(LOAD, String("Cannot determine exact type of modification of position ") + actual_mod_site_ + " in sequence " + actual_peptide_hit_.getSequence().toString() + " using modification " + actual_mod_type_ + " - using first possibility!");
        }
        AASequence pep = actual_peptide_hit_.getSequence();
        if (mods_map_[actual_mod_type_.toInt()].begin()->getTermSpecificity() == ResidueModification::N_TERM)
        {
          pep.setNTerminalModification(mods_map_[actual_mod_type_.toInt()].begin()->getFullId());
        }
        else if (mods_map_[actual_mod_type_.toInt()].begin()->getTermSpecificity() == ResidueModification::C_TERM)
        {
          pep.setCTerminalModification(mods_map_[actual_mod_type_.toInt()].begin()->getFullId());
        }
        else
        {
          pep.setModification(actual_mod_site_, mods_map_[actual_mod_type_.toInt()].begin()->getFullId());
        }
        actual_peptide_hit_.setSequence(pep);
      }
      else
      {
        warning(LOAD, String("Cannot find PSI-MOD mapping for mod -  ignoring '") + actual_mod_type_ + "'");
      }
    }

    tag_ = "";
  }

  void OMSSAXMLFile::characters(const XMLCh * const chars, const XMLSize_t /*length*/)
  {
    String value = ((String)sm_.convert(chars)).trim();
    // MSPepHit section
    // <MSPepHit_start>0</MSPepHit_start>
    // <MSPepHit_stop>8</MSPepHit_stop>
    // <MSPepHit_accession>6599</MSPepHit_accession>
    // <MSPepHit_defline>CRHU2 carbonate dehydratase (EC 4.2.1.1) II [validated] - human</MSPepHit_defline>
    // <MSPepHit_protlength>260</MSPepHit_protlength>
    // <MSPepHit_oid>6599</MSPepHit_oid>
    if (tag_ == "MSPepHit_start")
    {
      tag_ = "";
      return;
    }
    if (tag_ == "MSPepHit_stop")
    {
      tag_ = "";
      return;
    }
    if (tag_ == "MSPepHit_accession")
    {
      if (load_proteins_)
      {
        actual_peptide_hit_.addProteinAccession(value);
      }
      tag_ = "";
      return;
    }
    if (tag_ == "MSPepHit_defline")
    {
      // TODO add defline to ProteinHit?
      tag_ = "";
      return;
    }
    if (tag_ == "MSPepHit_protlength")
    {
      tag_ = "";
      return;
    }
    if (tag_ == "MSPepHit_oid")
    {
      tag_ = "";
      return;
    }

    // MSHits section
    // <MSHits_evalue>0.00336753988893542</MSHits_evalue>
    // <MSHits_pvalue>1.30819399070598e-08</MSHits_pvalue>
    // <MSHits_charge>1</MSHits_charge>
    // <MSHits_pepstring>MSHHWGYGK</MSHits_pepstring>
    // <MSHits_mass>1101492</MSHits_mass>
    // <MSHits_pepstart></MSHits_pepstart>
    // <MSHits_pepstop>H</MSHits_pepstop>
    // <MSHits_theomass>1101484</MSHits_theomass>
    if (tag_ == "MSHits_evalue")
    {
      actual_peptide_hit_.setScore(value.toDouble());
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_charge")
    {
      actual_peptide_hit_.setCharge(value.toInt());
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_pvalue")
    {
      // TODO extra field?
      //actual_peptide_hit_.setScore(value.toDouble());
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_pepstring")
    {
      AASequence seq = value.trim();
      if (mod_def_set_.getNumberOfFixedModifications() != 0 && seq.isValid())
      {
        set<String> fixed_mod_names = mod_def_set_.getFixedModificationNames();
        for (set<String>::const_iterator it = fixed_mod_names.begin(); it != fixed_mod_names.end(); ++it)
        {
          String origin = ModificationsDB::getInstance()->getModification(*it).getOrigin();
          UInt position(0);
          for (AASequence::Iterator ait = seq.begin(); ait != seq.end(); ++ait, ++position)
          {
            if (ait->getOneLetterCode() == origin)
            {
              seq.setModification(position, *it);
            }
          }
        }
      }
      actual_peptide_hit_.setSequence(seq);
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_mass")
    {
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_pepstart")
    {
      if (value != "")
      {
        actual_peptide_hit_.setAABefore(value[0]);
      }
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_pepstop")
    {
      if (value != "")
      {
        actual_peptide_hit_.setAAAfter(value[0]);
      }
      tag_ = "";
      return;
    }
    if (tag_ == "MSHits_theomass")
    {

      tag_ = "";
      return;
    }

    // modifications
    //<MSHits_mods>
    // <MSModHit>
    //  <MSModHit_site>4</MSModHit_site>
    //  <MSModHit_modtype>
    //   <MSMod>2</MSMod>
    //  </MSModHit_modtype>
    // </MSModHit>
    //</MSHits_mods>


    if (tag_ == "MSHits_mods")
    {
      actual_mod_site_ = 0;
      actual_mod_type_ = "";
    }
    if (tag_ == "MSModHit_site")
    {
      actual_mod_site_ = value.trim().toInt();
    }
    if (tag_ == "MSMod")
    {
      actual_mod_type_ = value.trim();
    }

    // m/z value and rt
    if (tag_ == "MSHitSet_ids_E")
    {
      // value might be  ( OMSSA 2.1.8): 359.213256835938_3000.13720000002_controllerType=0 controllerNumber=1 scan=4655
      //                 (<OMSSA 2.1.8): 359.213256835938_3000.13720000002
      if (value.trim() != "")
      {
        if (value.has('_'))
        {
          StringList sp = StringList::create(value, '_');
          try
          {
            actual_peptide_id_.setMetaValue("MZ", sp[0].toDouble());
            actual_peptide_id_.setMetaValue("RT", sp[1].toDouble());
          }
          catch (...)
          {
            // if exception happens to occur here, s.th. went wrong, e.g. the value does not contain numbers
          }
        }
      }
    }
  }

  void OMSSAXMLFile::readMappingFile_()
  {
    String file = File::find("CHEMISTRY/OMSSA_modification_mapping");
    TextFile infile(file);

    for (TextFile::ConstIterator it = infile.begin(); it != infile.end(); ++it)
    {
      vector<String> split;
      it->split(',', split);

      if (it->size() > 0 && (*it)[0] != '#')
      {
        Int omssa_mod_num = split[0].trim().toInt();
        if (split.size() < 2)
        {
          fatalError(LOAD, String("Invalid mapping file line: '") + *it + "'");
        }
        vector<ResidueModification> mods;
        for (Size i = 2; i != split.size(); ++i)
        {
          String tmp(split[i].trim());
          if (!tmp.empty())
          {
            ResidueModification mod = ModificationsDB::getInstance()->getModification(tmp);
            mods.push_back(mod);
            mods_to_num_[mod.getFullId()] = omssa_mod_num;
          }
        }
        mods_map_[omssa_mod_num] = mods;
      }
    }
  }

  void OMSSAXMLFile::setModificationDefinitionsSet(const ModificationDefinitionsSet & mod_set)
  {
    mod_def_set_ = mod_set;
    UInt omssa_mod_num(119);
    set<String> mod_names = mod_set.getVariableModificationNames();
    for (set<String>::const_iterator it = mod_names.begin(); it != mod_names.end(); ++it)
    {
      if (!mods_to_num_.has(*it))
      {
        mods_map_[omssa_mod_num].push_back(ModificationsDB::getInstance()->getModification(*it));
        mods_to_num_[*it] = omssa_mod_num;
        ++omssa_mod_num;
      }
    }
  }

} // namespace OpenMS