IsoSpec  1.95
marginalTrek++.h
1 /*
2  * Copyright (C) 2015-2018 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #include <tuple>
20 #include <unordered_map>
21 #include <queue>
22 #include <atomic>
23 #include "conf.h"
24 #include "allocator.h"
25 #include "operators.h"
26 #include "summator.h"
27 
28 
29 namespace IsoSpec
30 {
31 
32 Conf initialConfigure(int atomCnt, int isotopeNo, const double* probs);
33 
34 
35 void printMarginal(const std::tuple<double*,double*,int*,int>& results, int dim);
36 
38 
45 class Marginal
46 {
47 private:
48  bool disowned;
49 protected:
50  const unsigned int isotopeNo;
51  const unsigned int atomCnt;
52  const double* const atom_masses;
53  const double* const atom_lProbs;
54  const double loggamma_nominator;
55  const Conf mode_conf;
56  const double mode_lprob;
57  const double mode_mass;
58  const double mode_prob;
59  const double smallest_lprob;
62 public:
64 
71  Marginal(
72  const double* _masses, // masses size = logProbs size = isotopeNo
73  const double* _probs,
74  int _isotopeNo,
75  int _atomCnt
76  );
77 
78  // Get rid of the C++ generated copy and assignment constructors.
79  Marginal(Marginal& other) = delete;
80  Marginal& operator= (const Marginal& other) = delete;
81 
83  Marginal(Marginal&& other);
84 
86  virtual ~Marginal();
87 
89 
92  inline int get_isotopeNo() const { return isotopeNo; };
93 
95 
98  double getLightestConfMass() const;
99 
101 
104  double getHeaviestConfMass() const;
105 
107 
110  inline double getModeLProb() const { return mode_lprob; };
111 
113 
116  inline double getModeMass() const { return mode_mass; };
117 
119 
122  inline double getModeProb() const { return mode_prob; };
123 
125 
129  inline double getSmallestLProb() const { return smallest_lprob; };
130 
132 
136  inline double logProb(Conf conf) const { return loggamma_nominator + unnormalized_logProb(conf, atom_lProbs, isotopeNo); };
137 };
138 
139 
141 class MarginalTrek : public Marginal
142 {
143 private:
144  int current_count;
145  const KeyHasher keyHasher;
146  const ConfEqual equalizer;
147  const ConfOrderMarginal orderMarginal;
148  std::unordered_map<Conf,int,KeyHasher,ConfEqual> visited;
149  std::priority_queue<Conf,std::vector<Conf>,ConfOrderMarginal> pq;
150  Summator totalProb;
151  Conf candidate;
152  Allocator<int> allocator;
153  std::vector<double> _conf_lprobs;
154  std::vector<double> _conf_masses;
155  std::vector<int*> _confs;
156 
158  bool add_next_conf();
159 
160 public:
162 
166  MarginalTrek(
167  Marginal&& m,
168  int tabSize = 1000,
169  int hashSize = 1000
170  );
171 
173 
179  inline bool probeConfigurationIdx(int idx)
180  {
181  while(current_count <= idx)
182  if(!add_next_conf())
183  return false;
184  return true;
185  }
186 
187 
189 
193  int processUntilCutoff(double cutoff);
194 
195  inline const std::vector<double>& conf_lprobs() const { return _conf_lprobs; };
196  inline const std::vector<double>& conf_masses() const { return _conf_masses; };
197  inline const std::vector<int*>& confs() const { return _confs; };
198 
199 
200  virtual ~MarginalTrek();
201 };
202 
203 
205 
214 {
215 protected:
216  std::vector<Conf> configurations;
217  Conf* confs;
218  unsigned int no_confs;
219  double* masses;
220  double* lProbs;
221  double* probs;
222  Allocator<int> allocator;
223 public:
225 
233  Marginal&& m,
234  double lCutOff,
235  bool sort = true,
236  int tabSize = 1000,
237  int hashSize = 1000
238  );
239 
241  virtual ~PrecalculatedMarginal();
242 
244 
247  inline bool inRange(unsigned int idx) const { return idx < no_confs; };
248 
250 
254  inline const double& get_lProb(int idx) const { return lProbs[idx]; };
255 
257 
261  inline const double& get_prob(int idx) const { return probs[idx]; };
262 
264 
268  inline const double& get_mass(int idx) const { return masses[idx]; };
269 
271 
274  inline const double* get_lProbs_ptr() const { return lProbs; };
275 
277 
280  inline const double* get_masses_ptr() const { return masses; };
281 
282 
284 
288  inline const Conf& get_conf(int idx) const { return confs[idx]; };
289 
291 
294  inline unsigned int get_no_confs() const { return no_confs; };
295 };
296 
297 } // namespace IsoSpec
298 
IsoSpec::PrecalculatedMarginal::PrecalculatedMarginal
PrecalculatedMarginal(Marginal &&m, double lCutOff, bool sort=true, int tabSize=1000, int hashSize=1000)
The move constructor (disowns the Marginal).
Definition: marginalTrek++.cpp:375
IsoSpec::Marginal::Marginal
Marginal(const double *_masses, const double *_probs, int _isotopeNo, int _atomCnt)
Class constructor.
Definition: marginalTrek++.cpp:187
IsoSpec::PrecalculatedMarginal::get_masses_ptr
const double * get_masses_ptr() const
Get the table of the masses of subisotopologues.
Definition: marginalTrek++.h:280
IsoSpec::PrecalculatedMarginal
Precalculated Marginal class.
Definition: marginalTrek++.h:214
IsoSpec::Marginal
The marginal distribution class (a subisotopologue).
Definition: marginalTrek++.h:46
IsoSpec::Marginal::mode_lprob
const double mode_lprob
Definition: marginalTrek++.h:56
IsoSpec::PrecalculatedMarginal::get_lProbs_ptr
const double * get_lProbs_ptr() const
Get the table of the log-probabilities of subisotopologues.
Definition: marginalTrek++.h:274
IsoSpec::PrecalculatedMarginal::get_mass
const double & get_mass(int idx) const
Get the mass of the idx-th subisotopologue.
Definition: marginalTrek++.h:268
IsoSpec::Marginal::logProb
double logProb(Conf conf) const
Calculate the log-probability of a given subisotopologue.
Definition: marginalTrek++.h:136
IsoSpec::Allocator< int >
IsoSpec::MarginalTrek::processUntilCutoff
int processUntilCutoff(double cutoff)
Calculate subisotopologues with probability above or equal to the cut-off.
Definition: marginalTrek++.cpp:346
IsoSpec::Marginal::getHeaviestConfMass
double getHeaviestConfMass() const
Get the mass of the heaviest subisotopologue.
Definition: marginalTrek++.cpp:259
IsoSpec::Marginal::smallest_lprob
const double smallest_lprob
Definition: marginalTrek++.h:59
IsoSpec::Marginal::getSmallestLProb
double getSmallestLProb() const
The the log-probability of the lightest subisotopologue.
Definition: marginalTrek++.h:129
IsoSpec::initialConfigure
Conf initialConfigure(const int atomCnt, const int isotopeNo, const double *probs, const double *lprobs)
Find one of the most probable subisotopologues.
Definition: marginalTrek++.cpp:55
IsoSpec
Definition: allocator.cpp:22
IsoSpec::Marginal::getModeLProb
double getModeLProb() const
Get the log-probability of the mode subisotopologue.
Definition: marginalTrek++.h:110
IsoSpec::PrecalculatedMarginal::get_conf
const Conf & get_conf(int idx) const
Get the counts of isotopes that define the subisotopologue.
Definition: marginalTrek++.h:288
IsoSpec::PrecalculatedMarginal::get_lProb
const double & get_lProb(int idx) const
Get the log-probability of the idx-th subisotopologue.
Definition: marginalTrek++.h:254
IsoSpec::PrecalculatedMarginal::~PrecalculatedMarginal
virtual ~PrecalculatedMarginal()
Destructor.
Definition: marginalTrek++.cpp:451
IsoSpec::Marginal::mode_prob
const double mode_prob
Definition: marginalTrek++.h:58
IsoSpec::Marginal::getLightestConfMass
double getLightestConfMass() const
Get the mass of the lightest subisotopologue.
Definition: marginalTrek++.cpp:250
IsoSpec::ConfOrderMarginal
Definition: operators.h:79
IsoSpec::KeyHasher
Definition: operators.h:28
IsoSpec::Marginal::atom_masses
const double *const atom_masses
Definition: marginalTrek++.h:52
IsoSpec::Marginal::mode_mass
const double mode_mass
Definition: marginalTrek++.h:57
IsoSpec::Summator
Definition: summator.h:76
IsoSpec::Marginal::atomCnt
const unsigned int atomCnt
Definition: marginalTrek++.h:51
IsoSpec::ConfEqual
Definition: operators.h:46
IsoSpec::PrecalculatedMarginal::get_no_confs
unsigned int get_no_confs() const
Get the number of precomputed subisotopologues.
Definition: marginalTrek++.h:294
IsoSpec::Marginal::getModeMass
double getModeMass() const
The the mass of the mode subisotopologue.
Definition: marginalTrek++.h:116
IsoSpec::MarginalTrek::probeConfigurationIdx
bool probeConfigurationIdx(int idx)
Check if the table of computed subisotopologues does not have to be extended.
Definition: marginalTrek++.h:179
IsoSpec::Marginal::get_isotopeNo
int get_isotopeNo() const
Get the number of isotopes of the investigated element.
Definition: marginalTrek++.h:92
IsoSpec::Marginal::atom_lProbs
const double *const atom_lProbs
Definition: marginalTrek++.h:53
IsoSpec::Marginal::~Marginal
virtual ~Marginal()
Destructor.
Definition: marginalTrek++.cpp:239
IsoSpec::Marginal::getModeProb
double getModeProb() const
The the probability of the mode subisotopologue.
Definition: marginalTrek++.h:122
IsoSpec::MarginalTrek::MarginalTrek
MarginalTrek(Marginal &&m, int tabSize=1000, int hashSize=1000)
Move constructor: specializes the Marginal class.
Definition: marginalTrek++.cpp:269
IsoSpec::PrecalculatedMarginal::inRange
bool inRange(unsigned int idx) const
Is there a subisotopologue with a given number?
Definition: marginalTrek++.h:247
IsoSpec::MarginalTrek
The marginal distribution class (a subisotopologue).
Definition: marginalTrek++.h:142
IsoSpec::PrecalculatedMarginal::get_prob
const double & get_prob(int idx) const
Get the probability of the idx-th subisotopologue.
Definition: marginalTrek++.h:261
IsoSpec::Marginal::isotopeNo
const unsigned int isotopeNo
Definition: marginalTrek++.h:50
IsoSpec::Marginal::loggamma_nominator
const double loggamma_nominator
Definition: marginalTrek++.h:54
IsoSpec::Marginal::mode_conf
const Conf mode_conf
Definition: marginalTrek++.h:55