StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
userFunctions.cpp
Go to the documentation of this file.
1 //
2 // UserFunctions.cpp
3 //Copyright (c) 2007-2012 Paul C Lott
4 //University of California, Davis
5 //Genome and Biomedical Sciences Facility
6 //UC Davis Genome Center
7 //Ian Korf Lab
8 //Website: www.korflab.ucdavis.edu
9 //Email: lottpaul@gmail.com
10 //
11 //Permission is hereby granted, free of charge, to any person obtaining a copy of
12 //this software and associated documentation files (the "Software"), to deal in
13 //the Software without restriction, including without limitation the rights to
14 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
15 //the Software, and to permit persons to whom the Software is furnished to do so,
16 //subject to the following conditions:
17 //
18 //The above copyright notice and this permission notice shall be included in all
19 //copies or substantial portions of the Software.
20 //
21 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23 //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
24 //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 
28 #include "userFunctions.h"
29 namespace StochHMM{
30 
31 
35  }
36 
37  //!Assign a transition function to the StateFuncs class
38  //!\param str Name of function
39  //!\param ptrFunc pt2StateFunc to use for StateFunc
40  void StateFuncs::assignTransitionFunction(std::string& str, transitionFunc ptrFunc){
41 
42  if (transitionFunctions.count(str)==0){
43  transitionFunctions[str]=ptrFunc;
44  }
45  else{
46  std::cerr << "Function Name: " << str << " already exists. You need to choose a new function name that doesn't already exist. For reference here are a list of names already assigned as external functions\nAssigned Names:\n";
47 
48  std::map<std::string,transitionFunc>::iterator it;
49 
50  for(it=transitionFunctions.begin();it!=transitionFunctions.end();it++){
51  std::cerr << "\t" << it->first <<std::endl;
52  }
53  }
54  };
55 
56 
57  void StateFuncs::assignTransitionFunction(const char* str, transitionFunc ptrFunc){
58 
59  std::string st(str);
60  assignTransitionFunction(st, ptrFunc);
61  };
62 
63 
64  //!Assign a emission function to the StateFuncs class
65  //!\param str Name of function
66  //!\param ptrFunc pt2StateFunc to use for StateFunc
67  void StateFuncs::assignEmissionFunction(std::string& str, emissionFunc ptrFunc){
68 
69  if (emissionFunctions.count(str)==0){
70  emissionFunctions[str]=ptrFunc;
71  }
72  else{
73  std::cerr << "Function Name: " << str << " already exists. You need to choose a new function name that doesn't already exist. For reference here are a list of names already assigned as external functions\nAssigned Names:\n";
74 
75  std::map<std::string,emissionFunc>::iterator it;
76 
77  for(it=emissionFunctions.begin();it!=emissionFunctions.end();it++){
78  std::cerr << "\t" << it->first <<std::endl;
79  }
80  }
81  }
82 
83  void StateFuncs::assignEmissionFunction(const char* str, emissionFunc ptrFunc){
84  std::string st(str);
85  assignEmissionFunction(st, ptrFunc);
86  }
87 
88 
89 
90  //!Assign a Univariate Probability Distribution Function to the StateFuncs class
91  //!\param str Name of function
92  //!\param ptrFunc Pointer to pdfFunc to use for continuous emission
93  void StateFuncs::assignPDFFunction(std::string& str, pdfFunc ptrFunc){
94  if (pdfFunctions.count(str)==0){
95  pdfFunctions[str]=ptrFunc;
96  }
97  else{
98  std::cerr << "Function Name: " << str << " already exists. You need to choose a new function name that doesn't already exist. For reference here are a list of names already assigned as external functions\nAssigned Names:\n";
99 
100  std::map<std::string,pdfFunc>::iterator it;
101 
102  for(it=pdfFunctions.begin();it!=pdfFunctions.end();it++){
103  std::cerr << "\t" << it->first <<std::endl;
104  }
105  }
106  }
107 
108  void StateFuncs::assignPDFFunction(const char* str, pdfFunc ptrFunc){
109  std::string st(str);
110  assignPDFFunction(st, ptrFunc);
111  }
112 
113  //!Assign a Multivariate Probability Distribution Function to the StateFuncs class
114  //!\param str Name of function
115  //!\param ptrFunc Pointer to pdfFunc to use for continuous emission
117  if (multiPdfFunctions.count(str)==0){
118  multiPdfFunctions[str]=ptrFunc;
119  }
120  else{
121  std::cerr << "Function Name: " << str << " already exists. You need to choose a new function name that doesn't already exist. For reference here are a list of names already assigned as external functions\nAssigned Names:\n";
122 
123  std::map<std::string,multiPdfFunc>::iterator it;
124 
125  for(it=multiPdfFunctions.begin();it!=multiPdfFunctions.end();it++){
126  std::cerr << "\t" << it->first <<std::endl;
127  }
128  }
129  }
130 
132  std::string st(str);
133  assignMultivariatePdfFunction(st, ptrFunc);
134  }
135 
136 
137 
138  //!Get pointer to function with given name
139  //!\param name Name of the function
140  //!\return pt2StateFunc*
142  if (transitionFunctions.count(name)){
143  return &transitionFunctions[name];
144  }
145  else{
146  std::cerr << "Function named: " << name << " was not initialized. " <<std::endl;
147 
148  return NULL;
149  }
150  }
151 
152 
153  //!Get pointer to function with given name
154  //!\param name Name of the function
155  //!\return pt2StateFunc*
157  if (emissionFunctions.count(name)){
158  return &emissionFunctions[name];
159  }
160  else{
161  std::cerr << "Function named: " << name << " was not initialized. " <<std::endl;
162 
163  return NULL;
164  }
165  }
166 
167 
168  //!Get pointer to Univariate probability distribution function with given name
169  //!\param name Name of the function
170  //!\return pdfFunc*
171  pdfFunc* StateFuncs::getPDFFunction(std::string& name){
172  if (pdfFunctions.count(name)){
173  return &pdfFunctions[name];
174  }
175  else{
176  std::cerr << "Function named: " << name << " was not initialized. " <<std::endl;
177 
178  return NULL;
179  }
180  }
181 
182 
183  //!Get pointer to multivariate probability distribution function with given name
184  //!\param name Name of the function
185  //!\return pdfFunc*
187  if (multiPdfFunctions.count(name)){
188  return &multiPdfFunctions[name];
189  }
190  else{
191  std::cerr << "Function named: " << name << " was not initialized. " <<std::endl;
192 
193  return NULL;
194  }
195  }
196 
197 
198 
200  //Discrete with finite support
201  assignPDFFunction("BINOMIAL", static_cast<double (*)(const double , const std::vector<double>*)> (binomial_pdf));
202  assignPDFFunction("BETA_BINOMIAL",static_cast<double (*)(const double, const std::vector<double>*)> (beta_binomial_pdf));
203  assignPDFFunction("DEGENERATE",static_cast<double (*)(const double, const std::vector<double>*)> (degenerate_pdf));
204  assignPDFFunction("DISCRETE_UNIFORM",static_cast<double (*)(const double, const std::vector<double>*)> (discrete_uniform_pdf));
205  assignPDFFunction("HYPERGEOMETRIC", static_cast<double (*)(const double, const std::vector<double>*)> (hypergeometric_pdf));
206 
207  //Discrete with Infinite Support
208  assignPDFFunction("BETA_NEGATIVE_BINOMIAL", static_cast<double (*)(const double, const std::vector<double>*)> (beta_negative_binomial_pdf));
209  assignPDFFunction("MAXWELL_BOLTZMAN", static_cast<double (*)(const double, const std::vector<double>*)> (maxwell_boltzman_pdf));
210  assignPDFFunction("GEOMETRIC", static_cast<double (*)(const double, const std::vector<double>*)> (geometric_pdf));
211  assignPDFFunction("LOGARITHMIC", static_cast<double (*)(const double, const std::vector<double>*)> (logarithmic_pdf));
212  assignPDFFunction("NEGATIVE_BINOMIAL", static_cast<double (*)(const double, const std::vector<double>*)> (negative_binomial_pdf));
213  assignPDFFunction("POISSON", static_cast<double (*)(const double, const std::vector<double>*)> (poisson_pdf));
214  assignPDFFunction("YULE_SIMON", static_cast<double (*)(const double, const std::vector<double>*)> (yule_simon_pdf));
215  assignPDFFunction("ZIPF", static_cast<double (*)(const double, const std::vector<double>*)> (zipf_pdf));
216  assignPDFFunction("ZIPF-MANDELBROT", static_cast<double (*)(const double, const std::vector<double>*)> (zipf_mandelbrot_pdf));
217 
218  //Continuous on bounded interval
219  assignPDFFunction("ARCSINE", static_cast<double (*)(const double, const std::vector<double>*)> (arcsine_pdf));
220  assignPDFFunction("BETA", static_cast<double (*)(const double, const std::vector<double>*)> (beta_pdf));
221  assignPDFFunction("LOGIT_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (logit_normal_pdf));
222  assignPDFFunction("CONTINUOUS_UNIFORM", static_cast<double (*)(const double, const std::vector<double>*)> (continuous_uniform_pdf));
223  assignPDFFunction("KUMARASWAMY", static_cast<double (*)(const double, const std::vector<double>*)> (kumaraswamy_pdf));
224  assignPDFFunction("RAISED_COSINE", static_cast<double (*)(const double, const std::vector<double>*)> (raised_cosine_pdf));
225  assignPDFFunction("TRIANGULAR", static_cast<double (*)(const double, const std::vector<double>*)> (triangular_pdf));
226  assignPDFFunction("TRUNCATED_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (truncated_normal_pdf));
227  assignPDFFunction("U_QUADRATIC", static_cast<double (*)(const double, const std::vector<double>*)> (u_quadratic_pdf));
228  assignPDFFunction("WIGNER_SEMICIRCLE", static_cast<double (*)(const double, const std::vector<double>*)> (wigner_semicircle_pdf));
229 
230  //Continous on semi-bounded interval
231  assignPDFFunction("BETA_PRIME", static_cast<double (*)(const double, const std::vector<double>*)> (beta_prime_pdf));
232  assignPDFFunction("CHI", static_cast<double (*)(const double, const std::vector<double>*)> (chi_pdf));
233  assignPDFFunction("CHI_SQUARED", static_cast<double (*)(const double, const std::vector<double>*)> (chi_squared_pdf));
234  assignPDFFunction("INVERSE_CHI_SQUARED", static_cast<double (*)(const double, const std::vector<double>*)> (inverse_chi_squared_pdf));
235  assignPDFFunction("SCALED_INVERSE_CHI_SQUARED", static_cast<double (*)(const double, const std::vector<double>*)> (scaled_inverse_chi_squared_pdf));
236  assignPDFFunction("DAGUM", static_cast<double (*)(const double, const std::vector<double>*)> (dagum_pdf));
237  assignPDFFunction("EXPONENTIAL", static_cast<double (*)(const double, const std::vector<double>*)> (exponential_pdf));
238  assignPDFFunction("F_DIST", static_cast<double (*)(const double, const std::vector<double>*)> (f_pdf));
239  assignPDFFunction("FISHERS_Z", static_cast<double (*)(const double, const std::vector<double>*)> (fishers_z_pdf));
240  assignPDFFunction("FOLDED_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (folded_normal_pdf));
241  assignPDFFunction("FRECHET", static_cast<double (*)(const double, const std::vector<double>*)> (frechet_pdf));
242  assignPDFFunction("GAMMA", static_cast<double (*)(const double, const std::vector<double>*)> (gamma_pdf));
243  assignPDFFunction("INVERSE_GAMMA", static_cast<double (*)(const double, const std::vector<double>*)> (inv_gamma_pdf));
244  assignPDFFunction("HALF_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (half_normal_pdf));
245  assignPDFFunction("INVERSE_GAUSSIAN", static_cast<double (*)(const double, const std::vector<double>*)> (inv_gaussian_pdf));
246  assignPDFFunction("LEVY", static_cast<double (*)(const double, const std::vector<double>*)> (levy_pdf));
247  assignPDFFunction("LOG_CAUCHY", static_cast<double (*)(const double, const std::vector<double>*)> (log_cauchy_pdf));
248  assignPDFFunction("LOG_LAPLACE", static_cast<double (*)(const double, const std::vector<double>*)> (log_laplace_pdf));
249  assignPDFFunction("LOG_LOGISTIC", static_cast<double (*)(const double, const std::vector<double>*)> (log_logistic_pdf));
250  assignPDFFunction("LOG_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (log_normal_pdf));
251  assignPDFFunction("PARETO", static_cast<double (*)(const double, const std::vector<double>*)> (pareto_pdf));
252  assignPDFFunction("NAKAGAMI", static_cast<double (*)(const double, const std::vector<double>*)> (nakagami_pdf));
253  assignPDFFunction("RAYLEIGH", static_cast<double (*)(const double, const std::vector<double>*)> (rayleigh_pdf));
254  assignPDFFunction("GUMBEL_TYPE_TWO", static_cast<double (*)(const double, const std::vector<double>*)> (gumbel_type_two_pdf));
255  assignPDFFunction("WEIBULL", static_cast<double (*)(const double, const std::vector<double>*)> (weibull_distribution));
256 
257 
258  //Continuous on unbounded interval
259  assignPDFFunction("CAUCHY", static_cast<double (*)(const double, const std::vector<double>*)> (cauchy_pdf));
260  assignPDFFunction("GUMBEL", static_cast<double (*)(const double, const std::vector<double>*)> (gumbel_pdf));
261  assignPDFFunction("GENERALIZED_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (generalized_normal_pdf));
262  assignPDFFunction("HYPERBOLIC_SECANT", static_cast<double (*)(const double, const std::vector<double>*)> (hyperbolic_secant_pdf));
263  assignPDFFunction("LAPLACE", static_cast<double (*)(const double, const std::vector<double>*)> (laplace_pdf));
264  assignPDFFunction("LOGISTIC", static_cast<double (*)(const double, const std::vector<double>*)> (logistic_pdf));
265  assignPDFFunction("STANDARD_NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (standard_normal_pdf));
266  assignPDFFunction("NORMAL", static_cast<double (*)(const double, const std::vector<double>*)> (normal_pdf));
267  assignPDFFunction("STUDENT_T", static_cast<double (*)(const double, const std::vector<double>*)> (students_t_pdf));
268  assignPDFFunction("GUMBEL_TYPE_ONE", static_cast<double (*)(const double, const std::vector<double>*)> (gumbel_type_one_pdf));
269  assignPDFFunction("GENERALIZED_EXTREME_VALUE", static_cast<double (*)(const double, const std::vector<double>*)> (generalized_extreme_value_pdf));
270  assignPDFFunction("GENERALIZED_PARETO", static_cast<double (*)(const double, const std::vector<double>*)> (generalized_pareto_pdf));
271 
272  }
273 
274 
276  assignMultivariatePdfFunction("DIRICHLET",static_cast<double(*)(const std::vector<double>*, const std::vector<double>*)>(dirichlet_multi_pdf));
277  assignMultivariatePdfFunction("EWENS",static_cast<double(*)(const std::vector<double>*, const std::vector<double>*)>(ewens_multi_pdf));
278  }
279 
280 
281  //!Assign a function to the TrackFuncs class
282  //!\param str Name of function
283  //!\param ptrFunc pt2TrackFunc to use for TrackFunc
284  void TrackFuncs::assignFunction(std::string& str, pt2TrackFunc ptrFunc){
285 
286  if (functions.count(str)==0){
287  functions[str]=ptrFunc;
288  }
289  else{
290  std::cerr << "Function Name: " << str << " already exists. You need to choose a new function name that doesn't already exist. For reference here are a list of names already assigned as external functions\nAssigned Names:\n";
291 
292  std::map<std::string,pt2TrackFunc>::iterator it;
293 
294  for(it=functions.begin();it!=functions.end();it++){
295  std::cerr << "\t" << it->first <<std::endl;
296  }
297  }
298  };
299 
300 
301 
302  //!Get pointer to function with given name
303  //!\param name Name of the function
304  //!\return pt2TrackFunc*
306  if (functions.count(name)){
307  return &functions[name];
308  }
309  else{
310  std::cerr << "Function named: " << name << " was not initialized. " <<std::endl;
311 
312  return NULL;
313  }
314  }
315 
316 }