libpappsomspp
Library for mass spectrometry
convert2dense.hpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 #ifndef CONVERT2DENSE_H
23 #define CONVERT2DENSE_H
24 
25 #include <vector>
26 #include <iostream>
27 #include <numeric>
28 //#include <boost/assert.hpp>
29 //#include <boost/cstdint.hpp>
30 #include "pwiz/utility/findmf/base/resample/masscomparefunctors.hpp"
31 #include "pwiz/utility/findmf/base/resample/breakspec.hpp"
32 #include "pwiz/utility/findmf/base/resample/bin1d.hpp"
33 #include <QFile>
34 #include "../../../../exception/exceptionoutofrange.h"
35 
36 
37 namespace ralab
38 {
39 namespace base
40 {
41 namespace resample
42 {
43 //typedef boost::int32_t int32_t;
45 {
46  ralab::base::resample::Bin1D bin_;
47  std::vector<int32_t> idx_; // small workder vecs
48  std::vector<double> weight_;
49  double am_; // parameter describing the sampling width
50  Convert2Dense(double am [[maybe_unused]] = 0.1) : bin_(), idx_(), weight_(), am_()
51  {
52  }
53 
54  /// computes split points of an map.
55  std::size_t
56  defBreak(std::pair<double, double> &mzrange, double ppm)
57  {
58  ralab::base::resample::PPMCompFunctor<double> ppmf(ppm);
59  qDebug() << ppm << ppmf.ppm_ << ppmf.window_;
60  ralab::base::resample::breaks(
61  mzrange.first - 1., mzrange.second + 1., ppmf, bin_.breaks_);
62  bin_.reset();
63  return bin_.breaks_.size();
64  }
65 
66  /// Converts a sparse spec to a dense spec
67  template <typename Tmass, typename Tintens, typename Tout>
68  void
69  convert2dense(Tmass beginMass, Tmass endMass, Tintens intens, Tout ass)
70  {
71  for(; beginMass != (endMass - 1); ++beginMass, ++intens)
72  {
73  double mass1 = *beginMass;
74  double mass2 = *(beginMass + 1);
75  double predmass2 = mass1 + (am_ * sqrt(mass1)) * 1.01;
76 
77  if(mass2 > predmass2)
78  {
79  mass2 = predmass2;
80  }
81 
82  double deltamass = mass2 - mass1;
83  double deltamasshalf;
84  if(true)
85  {
86  deltamasshalf = deltamass / 2.;
87  }
88  else
89  {
90  deltamasshalf = deltamass;
91  }
92 
93  bin_(mass1 - deltamasshalf, mass2 - deltamasshalf, idx_, weight_);
94 
95  double intensd = static_cast<double>(*intens);
96  double sum = std::accumulate(weight_.begin(), weight_.end(), 0.);
97 
98  if(fabs(deltamass - sum) > 1e-11)
99  {
100  // BOOST_ASSERT(fabs(deltamass- sum) < 1e-11);
102  QObject::tr("ERROR pwiz convert2dense :\n "
103  "BOOST_ASSERT(fabs(deltamass- sum) < 1e-11)"));
104  }
105 
106  double check = 0.;
107  for(std::size_t i = 0; i < idx_.size(); ++i)
108  {
109  if((idx_[i] >= 0) &
110  (idx_[i] < static_cast<int32_t>(bin_.breaks_.size() - 1)))
111  {
112  double bb = intensd * weight_[i] / deltamass;
113  *(ass + idx_[i]) += bb;
114  check += bb;
115  }
116  }
117  // BOOST_ASSERT( fabs(check - intensd) < 1e-3 );
118  }
119  } // convert2dense
120 
121  void
122  getMids(std::vector<double> &mids)
123  {
124  ralab::base::resample::getMids(bin_.breaks_, mids);
125  }
126 
127  /// Converts a sparse spec to a dense spec
128  template <typename Tmass, typename Tintens>
129  void
131  Tmass beginMass,
132  Tmass endMass,
133  Tintens intens,
134  std::vector<typename std::iterator_traits<Tintens>::value_type> &gg)
135  {
136  gg.resize(bin_.breaks_.size() - 1);
137  convert2dense(beginMass, endMass, intens, gg.begin());
138  }
139 };
140 
141 
142 } // namespace resample
143 } // namespace base
144 } // namespace ralab
145 #endif // CONVERT2DENSE_H
ralab::base::resample::Convert2Dense::defBreak
std::size_t defBreak(std::pair< double, double > &mzrange, double ppm)
computes split points of an map.
Definition: convert2dense.hpp:56
ralab::base::resample::Convert2Dense::convert2dense
void convert2dense(Tmass beginMass, Tmass endMass, Tintens intens, Tout ass)
Converts a sparse spec to a dense spec.
Definition: convert2dense.hpp:69
pappso::ExceptionOutOfRange
Definition: exceptionoutofrange.h:53
ralab::base::resample::Convert2Dense::weight_
std::vector< double > weight_
Definition: convert2dense.hpp:48
ralab::base::resample::Convert2Dense::convert2dense
void convert2dense(Tmass beginMass, Tmass endMass, Tintens intens, std::vector< typename std::iterator_traits< Tintens >::value_type > &gg)
Converts a sparse spec to a dense spec.
Definition: convert2dense.hpp:130
ralab::base::resample::Convert2Dense::getMids
void getMids(std::vector< double > &mids)
Definition: convert2dense.hpp:122
ralab::base::resample::Convert2Dense
Definition: convert2dense.hpp:45
ralab
Definition: peakpickerqtof.hpp:34
ralab::base::resample::Convert2Dense::bin_
ralab::base::resample::Bin1D bin_
Definition: convert2dense.hpp:46
ralab::base::resample::Convert2Dense::am_
double am_
Definition: convert2dense.hpp:49
ralab::base::resample::Convert2Dense::idx_
std::vector< int32_t > idx_
Definition: convert2dense.hpp:47
ralab::base::resample::Convert2Dense::Convert2Dense
Convert2Dense(double am[[maybe_unused]]=0.1)
Definition: convert2dense.hpp:50