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  ralab::base::resample::breaks(
60  mzrange.first - 1., mzrange.second + 1., ppmf, bin_.breaks_);
61  bin_.reset();
62  return bin_.breaks_.size();
63  }
64 
65  /// Converts a sparse spec to a dense spec
66  template <typename Tmass, typename Tintens, typename Tout>
67  void
68  convert2dense(Tmass beginMass, Tmass endMass, Tintens intens, Tout ass)
69  {
70  for(; beginMass != (endMass - 1); ++beginMass, ++intens)
71  {
72  double mass1 = *beginMass;
73  double mass2 = *(beginMass + 1);
74  double predmass2 = mass1 + (am_ * sqrt(mass1)) * 1.01;
75  if(mass2 > predmass2)
76  {
77  mass2 = predmass2;
78  }
79 
80  double deltamass = mass2 - mass1;
81  double deltamasshalf;
82  if(true)
83  {
84  deltamasshalf = deltamass / 2.;
85  }
86  else
87  {
88  deltamasshalf = deltamass;
89  }
90 
91  bin_(mass1 - deltamasshalf, mass2 - deltamasshalf, idx_, weight_);
92 
93  double intensd = static_cast<double>(*intens);
94  double sum = std::accumulate(weight_.begin(), weight_.end(), 0.);
95 
96  if(fabs(deltamass - sum) > 1e-11)
97  {
98  // BOOST_ASSERT(fabs(deltamass- sum) < 1e-11);
100  QObject::tr("ERROR pwiz convert2dense :\n "
101  "BOOST_ASSERT(fabs(deltamass- sum) < 1e-11)"));
102  }
103 
104  double check = 0.;
105  for(std::size_t i = 0; i < idx_.size(); ++i)
106  {
107  if((idx_[i] >= 0) &
108  (idx_[i] < static_cast<int32_t>(bin_.breaks_.size() - 1)))
109  {
110  double bb = intensd * weight_[i] / deltamass;
111  *(ass + idx_[i]) += bb;
112  check += bb;
113  }
114  }
115  // BOOST_ASSERT( fabs(check - intensd) < 1e-3 );
116  }
117  } // convert2dense
118 
119  void
120  getMids(std::vector<double> &mids)
121  {
122  ralab::base::resample::getMids(bin_.breaks_, mids);
123  }
124 
125  /// Converts a sparse spec to a dense spec
126  template <typename Tmass, typename Tintens>
127  void
129  Tmass beginMass,
130  Tmass endMass,
131  Tintens intens,
132  std::vector<typename std::iterator_traits<Tintens>::value_type> &gg)
133  {
134  gg.resize(bin_.breaks_.size() - 1);
135  convert2dense(beginMass, endMass, intens, gg.begin());
136  }
137 };
138 
139 
140 } // namespace resample
141 } // namespace base
142 } // namespace ralab
143 #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:68
pappso::ExceptionOutOfRange
Definition: exceptionoutofrange.h:32
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:128
ralab::base::resample::Convert2Dense::getMids
void getMids(std::vector< double > &mids)
Definition: convert2dense.hpp:120
ralab::base::resample::Convert2Dense
Definition: convert2dense.hpp:45
ralab
Definition: peakpickerqtof.hpp:33
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