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 
double getLightestConfMass() const
Get the mass of the lightest subisotopologue.
double getModeProb() const
The the probability of the mode subisotopologue.
bool inRange(unsigned int idx) const
Is there a subisotopologue with a given number?
const double mode_prob
The marginal distribution class (a subisotopologue).
const unsigned int isotopeNo
const Conf mode_conf
const unsigned int atomCnt
const double & get_prob(int idx) const
Get the probability of the idx-th subisotopologue.
int get_isotopeNo() const
Get the number of isotopes of the investigated element.
virtual ~Marginal()
Destructor.
const double & get_lProb(int idx) const
Get the log-probability of the idx-th subisotopologue.
const double *const atom_lProbs
double getHeaviestConfMass() const
Get the mass of the heaviest subisotopologue.
const double loggamma_nominator
const double * get_lProbs_ptr() const
Get the table of the log-probabilities of subisotopologues.
unsigned int get_no_confs() const
Get the number of precomputed subisotopologues.
double getModeLProb() const
Get the log-probability of the mode subisotopologue.
Conf initialConfigure(const int atomCnt, const int isotopeNo, const double *probs, const double *lprobs)
Find one of the most probable subisotopologues.
const double smallest_lprob
Marginal(const double *_masses, const double *_probs, int _isotopeNo, int _atomCnt)
Class constructor.
const double & get_mass(int idx) const
Get the mass of the idx-th subisotopologue.
bool probeConfigurationIdx(int idx)
Check if the table of computed subisotopologues does not have to be extended.
const double mode_lprob
const double *const atom_masses
const Conf & get_conf(int idx) const
Get the counts of isotopes that define the subisotopologue.
double getModeMass() const
The the mass of the mode subisotopologue.
const double mode_mass
The marginal distribution class (a subisotopologue).
Precalculated Marginal class.
double getSmallestLProb() const
The the log-probability of the lightest subisotopologue.
double logProb(Conf conf) const
Calculate the log-probability of a given subisotopologue.
const double * get_masses_ptr() const
Get the table of the masses of subisotopologues.