~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/FORMAT/HANDLERS/UnimodXMLHandler.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
Import upstream version 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
// 
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Andreas Bertsch $
 
25
// $Authors: Andreas Bertsch $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#include <OpenMS/CHEMISTRY/ResidueModification.h>
 
29
#include <OpenMS/FORMAT/HANDLERS/UnimodXMLHandler.h>
 
30
#include <xercesc/sax2/Attributes.hpp>
 
31
 
 
32
using namespace std;
 
33
using namespace xercesc;
 
34
 
 
35
namespace OpenMS
 
36
{
 
37
        namespace Internal
 
38
        {
 
39
  
 
40
  UnimodXMLHandler::UnimodXMLHandler(vector<ResidueModification*>& mods, const String& filename)
 
41
                : XMLHandler(filename, "2.0"),
 
42
                        avge_mass_(0.0),
 
43
                        mono_mass_(0.0),
 
44
                        modification_(0),
 
45
                        modifications_(mods)
 
46
                        
 
47
  {
 
48
  }
 
49
   
 
50
  UnimodXMLHandler::~UnimodXMLHandler()
 
51
  {
 
52
    
 
53
  }
 
54
 
 
55
  void UnimodXMLHandler::startElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname, const Attributes& attributes)
 
56
        {
 
57
 
 
58
                tag_ = String(sm_.convert(qname));
 
59
                
 
60
                // new modification?
 
61
                if (tag_ == "umod:mod" || tag_ == "mod")
 
62
                {
 
63
                        sites_.clear();
 
64
                        modification_ = new ResidueModification();
 
65
                        String title(attributeAsString_(attributes, "title"));
 
66
                        modification_->setId(title);
 
67
 
 
68
                        String full_name(attributeAsString_(attributes, "full_name"));
 
69
                        // full_name.substitute("®", ""); // remove damn character (will be interpreted differently across platforms)
 
70
                        // deleted this in the unimod.xml file
 
71
                        modification_->setFullName(full_name);
 
72
 
 
73
                        String record_id(attributeAsString_(attributes, "record_id"));
 
74
                        modification_->setUniModAccession("UniMod:" + record_id);
 
75
                        return;
 
76
                }
 
77
 
 
78
                // which residues are allowed?
 
79
                if (tag_ == "umod:specificity" || tag_ == "specificity")
 
80
                {
 
81
                        // classification of mod
 
82
                        // TODO do this for all mods, do not overwrite for each specificity
 
83
                        String classification(attributeAsString_(attributes, "classification"));
 
84
                        modification_->setSourceClassification(classification);
 
85
 
 
86
                        // allowed site
 
87
                        String site(attributeAsString_(attributes, "site"));
 
88
                        //sites_.push_back(site);
 
89
 
 
90
                        // allowed positions
 
91
                        ResidueModification::Term_Specificity position = ResidueModification::ANYWHERE;
 
92
                        String pos(attributeAsString_(attributes, "position"));
 
93
                        if (pos == "Anywhere")
 
94
                        {
 
95
                                position = ResidueModification::ANYWHERE;
 
96
                        }
 
97
                        else
 
98
                        {
 
99
                                if (pos == "Protein N-term")
 
100
                                {
 
101
                                        position = ResidueModification::N_TERM;
 
102
                                }
 
103
                                else
 
104
                                {
 
105
                                        if (pos == "Protein C-term")
 
106
                                        {
 
107
                                                position = ResidueModification::C_TERM;
 
108
                                        }
 
109
                                        else
 
110
                                        {
 
111
                                                if (pos == "Any C-term")
 
112
                                                {
 
113
                                                        position = ResidueModification::C_TERM;
 
114
                                                }
 
115
                                                else
 
116
                                                {
 
117
                                                        if (pos == "Any N-term")
 
118
                                                        {
 
119
                                                                position = ResidueModification::N_TERM;
 
120
                                                        }
 
121
                                                        else
 
122
                                                        {
 
123
                                                                warning(LOAD, String("Don't know allowed position called: '") + pos  + "' - setting to anywhere");
 
124
                                                        }
 
125
                                                }
 
126
                                        }
 
127
                                }
 
128
                        }
 
129
 
 
130
                        if (!pos.hasSubstring("Protein"))
 
131
                        {
 
132
                                term_specs_.push_back(position);
 
133
                                sites_.push_back(site);
 
134
                        }
 
135
                        return;
 
136
                }
 
137
        
 
138
 
 
139
                if (tag_ == "umod:NeutralLoss" || tag_ == "NeutralLoss")
 
140
                {
 
141
                        // mono_mass="97.976896" avge_mass="97.9952" flag="false"
 
142
                        //                               composition="H(3) O(4) P">
 
143
 
 
144
                }
 
145
                
 
146
                // delta mass defintions?
 
147
                if (tag_ == "umod:delta" || tag_ == "delta")
 
148
                {
 
149
                        // avge_mass="-0.9848" mono_mass="-0.984016" composition="H N O(-1)" >
 
150
                        avge_mass_ = String(sm_.convert(attributes.getValue(attributes.getIndex(sm_.convert("avge_mass"))))).toDouble();
 
151
                        mono_mass_ = String(sm_.convert(attributes.getValue(attributes.getIndex(sm_.convert("mono_mass"))))).toDouble();
 
152
                        return;
 
153
                }
 
154
 
 
155
                // <umod:element symbol="H" number="1"/>
 
156
                if (tag_ == "umod:element")
 
157
                {
 
158
                        String symbol = sm_.convert(attributes.getValue(attributes.getIndex(sm_.convert("symbol"))));
 
159
                        String num = sm_.convert(attributes.getValue(attributes.getIndex(sm_.convert("number"))));
 
160
                        String isotope, tmp_symbol;
 
161
      for (Size i = 0; i != symbol.size(); ++i)
 
162
      {
 
163
        if (isdigit(symbol[i]))
 
164
        {
 
165
                isotope += symbol[i];
 
166
        }
 
167
        else
 
168
        {
 
169
          tmp_symbol += symbol[i];
 
170
        }
 
171
                        }
 
172
      
 
173
      String formula;
 
174
      if (isotope != "")
 
175
      {
 
176
        formula = '(' + isotope + ')' + tmp_symbol + String(num);
 
177
      }
 
178
      else
 
179
      {
 
180
        formula = tmp_symbol + num;
 
181
      }
 
182
      diff_formula_ += formula;                 
 
183
                        
 
184
                }
 
185
                
 
186
        }
 
187
          
 
188
  void UnimodXMLHandler::endElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname)
 
189
        {
 
190
                tag_ = String(sm_.convert(qname));
 
191
 
 
192
                // write the modifications to vector
 
193
                if (tag_ == "umod:mod" || tag_ == "mod")
 
194
                {
 
195
                        modification_->setDiffAverageMass(avge_mass_);
 
196
                        modification_->setDiffMonoMass(mono_mass_);
 
197
                        modification_->setDiffFormula(diff_formula_);
 
198
                        for (Size i = 0; i != sites_.size(); ++i)
 
199
                        {
 
200
                                ResidueModification* new_mod = new ResidueModification(*modification_);
 
201
                                new_mod->setOrigin(sites_[i]);
 
202
                                new_mod->setTermSpecificity(term_specs_[i]);
 
203
                                modifications_.push_back(new_mod);
 
204
                        }
 
205
                        
 
206
                        avge_mass_ = 0.0;
 
207
                        mono_mass_ = 0.0;
 
208
                        diff_formula_ = EmpiricalFormula();
 
209
                        term_specs_.clear();
 
210
                        sites_.clear();
 
211
 
 
212
                        delete modification_;
 
213
                        return;
 
214
                }
 
215
 
 
216
                if (tag_ == "umod:NeutralLoss" || tag_ == "NeutralLoss")
 
217
                {
 
218
                        // now diff_formula_ contains the neutral loss diff formula
 
219
                        modification_->setNeutralLossDiffFormula(diff_formula_);
 
220
                        modification_->setNeutralLossMonoMass(mono_mass_);
 
221
                        modification_->setNeutralLossAverageMass(avge_mass_);
 
222
                        avge_mass_ = 0.0;
 
223
                        mono_mass_ = 0.0;
 
224
                        diff_formula_ = EmpiricalFormula();
 
225
                }
 
226
        } 
 
227
 
 
228
  void UnimodXMLHandler::characters(const XMLCh* const /*chars*/, const XMLSize_t /*length*/)
 
229
  {
 
230
                // nothing to do here
 
231
        }
 
232
 
 
233
        } // namespace Internal
 
234
} // namespace OpenMS