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

« back to all changes in this revision

Viewing changes to source/CHEMISTRY/SvmTheoreticalSpectrumGeneratorSet.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: Sandro Andreotti $
 
25
// $Authors: Sandro Andreotti $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#include<OpenMS/CHEMISTRY/SvmTheoreticalSpectrumGeneratorSet.h>
 
29
#include <OpenMS/FORMAT/TextFile.h>
 
30
#include <OpenMS/SYSTEM/File.h>
 
31
 
 
32
 
 
33
 
 
34
namespace OpenMS
 
35
{
 
36
 
 
37
  // Default constructor
 
38
  SvmTheoreticalSpectrumGeneratorSet::SvmTheoreticalSpectrumGeneratorSet(){}
 
39
 
 
40
  // Copy constructor
 
41
  SvmTheoreticalSpectrumGeneratorSet::SvmTheoreticalSpectrumGeneratorSet(const SvmTheoreticalSpectrumGeneratorSet& source):
 
42
      simulators_(source.simulators_)
 
43
  {}
 
44
 
 
45
  //Destructor
 
46
  SvmTheoreticalSpectrumGeneratorSet::~SvmTheoreticalSpectrumGeneratorSet()
 
47
  {}
 
48
 
 
49
  // Assignment operator
 
50
  SvmTheoreticalSpectrumGeneratorSet& SvmTheoreticalSpectrumGeneratorSet::operator =(const SvmTheoreticalSpectrumGeneratorSet& rhs)
 
51
  {
 
52
    if (this != &rhs)
 
53
    {
 
54
      simulators_=rhs.simulators_;
 
55
    }
 
56
    return *this;
 
57
  }
 
58
 
 
59
  // Generate the MS/MS according to the given probabilistic model
 
60
  void SvmTheoreticalSpectrumGeneratorSet::simulate(RichPeakSpectrum &spectrum, const AASequence &peptide, const gsl_rng *rng, Size precursor_charge)
 
61
  {
 
62
    std::map<Size, SvmTheoreticalSpectrumGenerator>::iterator it=simulators_.find(precursor_charge);
 
63
    if(it!=simulators_.end())
 
64
    {
 
65
      it->second.simulate(spectrum, peptide, rng, precursor_charge);
 
66
    }
 
67
    else
 
68
    {
 
69
      throw OpenMS::Exception::InvalidValue(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Invalid Precursor charge, no Model available", String(precursor_charge));
 
70
    }
 
71
  }
 
72
 
 
73
  //Load a trained Svm and Prob. models
 
74
  void SvmTheoreticalSpectrumGeneratorSet::load(String filename)
 
75
  {
 
76
    if (! File::readable( filename ) )
 
77
    { // look in OPENMS_DATA_PATH
 
78
      filename = File::find( filename );
 
79
    }
 
80
    TextFile file(filename);
 
81
 
 
82
    Param sim_param = SvmTheoreticalSpectrumGenerator().getDefaults();
 
83
    for(Size line_num=1; line_num<file.size(); ++line_num)
 
84
    {
 
85
      String line(file[line_num]);
 
86
      std::vector<String>spl;
 
87
      line.split(":",spl);
 
88
      Int precursor_charge=spl[0].toInt();
 
89
 
 
90
      if(spl.size()!=2 || precursor_charge<1)
 
91
      {
 
92
        OpenMS::Exception::ParseError(__FILE__, __LINE__, __PRETTY_FUNCTION__, file[line_num]," Invalid entry in SVM model File");
 
93
      }
 
94
 
 
95
      //load the model into the map
 
96
      sim_param.setValue("model_file_name", File::path(filename)+"/"+spl[1]);
 
97
      simulators_[precursor_charge].setParameters(sim_param);
 
98
      simulators_[precursor_charge].load();
 
99
    }
 
100
  }
 
101
 
 
102
  //Return precursor charges for which a model is contained in the set
 
103
  void SvmTheoreticalSpectrumGeneratorSet::getSupportedCharges(std::set<Size>&charges)
 
104
  {
 
105
    charges.clear();
 
106
    std::map<Size, SvmTheoreticalSpectrumGenerator>::const_iterator it;
 
107
    for(it=simulators_.begin(); it!=simulators_.end(); ++it)
 
108
    {
 
109
      charges.insert(it->first);
 
110
    }
 
111
  }
 
112
 
 
113
  //return a modifiable reference to the SVM model with given charge. If charge is not supported throw exception
 
114
  SvmTheoreticalSpectrumGenerator & SvmTheoreticalSpectrumGeneratorSet::getSvmModel(Size prec_charge)
 
115
  {
 
116
    std::map<Size, SvmTheoreticalSpectrumGenerator>::iterator it = simulators_.find(prec_charge);
 
117
    if(it==simulators_.end())
 
118
    {
 
119
      throw OpenMS::Exception::InvalidValue(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Invalid Precursor charge, no Model available", String(prec_charge));
 
120
    }
 
121
    return it->second;
 
122
  }
 
123
}
 
124
 
 
125