StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
weight.h
Go to the documentation of this file.
1 //
2 // weight.h
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 #ifndef weight_cpp
29 #define weight_cpp
30 #include <iostream>
31 #include <vector>
32 #include <algorithm>
33 #include <map>
34 #include <stdlib.h>
35 #include "text.h"
36 #include "stochMath.h"
37 namespace StochHMM{
38 
39 
40 bool compXval(const std::pair<double,double>&, const std::pair<double,double>&);
41 
42 //!weight class contains scaling factors for external scores
43 
44 //!weight class contains either an
45 //!1. Absolute scaling value: meaning the returned value multiplied by constant
46 //!2. Score Mapping: x and y values describing the transformation from x=returned score
47 //! to y the scaled score. If the returned value isn't equal to a specific y the score
48 //! is interpolated or extrapolated.
49 //! If the return score x<MIN then y=scaled MIN
50 //! If the return score x>MAX then y=scaled MAX
51 //! Scores provided to weight should be log(Prob)
52 //! It will return a log(Prob) scaled score
53 
54 
55 //Scaling weight given some score we map it to some probability or other weighed score
56 class weight{
57 public:
58  weight();
59  ~weight();
60 
61  //ACCESSORS
62 
63  /*!
64  Checks to see if weight is defined as a constant
65  \returns bool TRUE if weight is defined as a constant, else FALSE
66  */
67  inline bool isAbsolute(){return absolute;};
68 
69  inline double getAbsolute(){ return absoluteValue;};
70 
71  inline std::string& getName(){return name;};
72 
73  /*!
74  Returns F(x) given some score x
75  \param score The value returned by some function
76  \return double F(x) which interpolated or extrapolated as necessary
77  */
78  double getWeightedScore(double); //!Given a score (x) it will map the score to F(x)
79 
80  /*!
81  Returns the number of mapped values = equal to size of xVals handed to setWeights(...)
82  \returns size_t value of vector size;
83  */
84  inline size_t size(){return vals->size();};
85 
86  /*!
87  Prints out the weight type to text. Includes the x and F(x) vectors and MIN and MAX
88  */
89  inline void print(){std::cout << stringify() << std::endl;};
90 
91 
92  /*!
93  Converts weights to string
94  */
95  std::string stringify();
96 
97  //MUTATORS
98 
99  /*!
100  Sets the scaling factor to a constant value
101  \param val double that is returned x->Constant
102  */
103  inline void setAbsolute(double val){absoluteValue=val; absolute=true; return;};
104 
105 
106  /*!
107  Setup the score mapping from x->F(x) 2 vectors of equal length
108  \param xVals vector of doubles that contains the scores for x
109  \param yVals vector of doubles that contains the corresponding scores F(x)
110  \param xMIN pair of doubles defines MIN x->F(x)
111  \param xMAX pair of doubles defines MAX x->F(x)
112  */
113  void setWeights(std::vector<double>& ,std::vector<double>& ,std::pair<double,double>* , std::pair<double,double>* );
114 
115 
116  /*!
117  Setup the score mapping from x->F(x) 2 vectors of equal length
118  \param xVals vector of doubles that contains the scores for x
119  \param yVals vector of doubles that contains the corresponding scores F(x)
120  */
121  void setWeights(std::vector<double>&,std::vector<double>&);
122 
123 
124  /*!
125  Sets the MAX value for x to MAX F(MAX(x));
126  \param x The MAX x value to allow
127  \param y The value to return for x>=MAX
128  */
129  void setMaxWeight(double&,double&);
130 
131 
132  /*!
133  Sets the MIN value for x to F(MIN(x))
134  \param x The MIN x value to allow
135  \param y The value to return for x<=MIN
136  */
137  void setMinWeight(double&,double&);
138 
139  inline void setName(std::string& nm){name=nm;};
140 
141 
142  //! Parse weight from string
143  bool parse(const std::string&);
144 
145 private:
146  bool absolute; //! Stores whether Weight is a constant value
147  double absoluteValue; //! Stores the constant value
148  std::string name;
149 
150  std::pair<double,double>* maxValue; //!Stores the MAX value of x and the corresponding F(x)
151  std::pair<double,double>* minValue; //!Stores the MIN value of x and the corresponding F(x)
152 
153  std::vector<std::pair<double,double> >* vals; //!Values for mapping x->F(x)
154 };
155 
156 
157 
158  //!Weights class keeps track of multiple weight types
159  //!Each weight can be indexed by a name
160  class weights{
161  public:
162  //MUTATORS
163 
164  void addWeight(weight*);
165 
166  //ACCESSORS
167  weight* operator[](std::string&);
168  inline size_t count(std::string& name){return wts.count(name);};
169 
170  void print();
171  std::string stringify();
172 
173  private:
174  std::map<std::string, weight*> wts;
175  };
176 
177 
178 
179 }
180 #endif