1 // $Id: forceparam.h,v 1.28 2011/03/07 06:08:49 bobgian Exp $
2 
3 /*
4   Copyright 2002  Peter Beerli, Mary Kuhner, Jon Yamato and Joseph Felsenstein
5 
6   This software is distributed free of charge for non-commercial use
7   and is copyrighted.  Of course, we do not guarantee that the software
8   works, and are not responsible for any damage you may cause or have.
9 */
10 
11 /********************************************************************
12  ForceParameters is a collection class used for internal communication
13  throughout Lamarc.
14 
15  ForceParameters is a package of information about the values of
16  all force parameters (for all forces--it is not polymorphic).
17  Any routine which wishes to pass or return a bundle of force
18  parameters should use this class.  It is normally passed by
19  value.
20 
21  Internally, the parameters are kept in separate vectors, but can
22  present them as one big vector using the registry's ForceSummary
23  object.
24 
25  Written by Jim Sloan, revised by Mary Kuhner, further revised by Lucian Smith
26 
27  Jon Yamato:
28     added SetToMeanOf(vector<forceparameters>)
29 ********************************************************************/
30 /* There are three 'modes' a ForceParameters object can come in.  In the
31    'fully aware' version, it holds values for all the parameters, and it
32    knows what those parameters should be for a particular genomic region.
33    In this case, m_space is known_region.  All the parameter vectors
34    will be filled with values in this case, and it can safely return either
35    regional or global values.
36 
37    There are two 'partially aware' versions of the object, one where all it
38    knows is the global parameter values, and one where all it knows are one
39    region's parameter values, but doesn't know what region that is.  In the
40    former case, m_space is global_region, and in the latter, m_space is
41    unknown_region.  The starting values, for example, are global_region,
42    since they must be used for every region, and the likelihood maximizer
43    works with unknown_region objects, since it's just working, and nobody
44    tells it what region it's working on.
45 
46    If you have either partially aware versions and want a fully aware version,
47    you construct a new ForceParameters object and tell it what region you
48    want:
49 
50    ForceParameters aware_fp(global_fp, reg#);
51    ForceParameters aware_fp(unknown_fp, reg#);
52 
53    This constructor copies the appropriate vectors, then looks up the scaling
54    factor it needs to fill in the other vectors.
55    -Lucian
56 */
57 
58 #ifndef FORCEPARAM_H
59 #define FORCEPARAM_H
60 
61 #include <vector>
62 #include "vectorx.h"
63 #include "constants.h"
64 #include "defaults.h"
65 
66 class ForceSummary;
67 class Epoch;
68 
69 enum param_space {global_region, known_region, unknown_region};
70 
71 class ForceParameters
72 {
73   private:
74     ForceParameters(); //undefined
75 
76     // the parameter vectors.  recrates is a vector of one element, for
77     // consistency of interface.
78     DoubleVec1d m_globalThetas;
79     DoubleVec1d m_regionalThetas;
80     DoubleVec1d m_migrates;
81     DoubleVec1d m_diseases;
82     DoubleVec1d m_recrates;
83     DoubleVec1d m_growths;
84     // one-element vector, for consistency
85     DoubleVec1d m_logisticSelectionCoefficient;
86     DoubleVec1d m_epochtimes;
87 
88     long m_region; //Either the appropriate region or FLAGLONG for global.
89     param_space m_space;
90 
91     vector<force_type> m_forceTags;
92     vector<long>       m_forceSizes;
93 
94     // non-owning pointer to Epoch information, null if none exists
95     const std::vector<Epoch>* m_epochptr;
96 
97     void CopyGlobalMembers(const ForceParameters& src);
98     void CopyRegionalMembers(const ForceParameters& src);
99 
100     DoubleVec1d GetLogParameters(bool isGlobal) const;
101     DoubleVec1d GetParameters(bool isGlobal)    const;
102 
103     void SetParameters(const DoubleVec1d& v, bool isGlobal);
104 
105     void FillGlobalParamsFromRegionalParams();
106     void FillRegionalParamsFromGlobalParams();
107 
108   public:
109     // ForceParameters(const ForceParameters& src); //we accept the default
110     // ForceParameters& operator=(const ForceParameters& src);
111     ForceParameters(long region);
112     ForceParameters(param_space space);
113     ForceParameters(param_space space,
114                     ForceTypeVec1d types, LongVec1d sizes);
115     ForceParameters(const ForceParameters& src, long region);
116     // If we ever end up creating one of these things in global space but
117     // don't know it, we would need a converter from unknown_region to global:
118     // ForceParameters(const ForceParameters& src, param_space space);
119 
120     // Setters
121     void  SetGlobalThetas  (const DoubleVec1d& v);
122     void  SetRegionalThetas(const DoubleVec1d& v);
123     void  SetMigRates(const DoubleVec1d& v);
124     void  SetDiseaseRates(const DoubleVec1d& v);
125     void  SetRecRates(const DoubleVec1d& v);
126     void  SetGrowthRates(const DoubleVec1d& v);
127     void  SetLogisticSelectionCoefficient(const DoubleVec1d& v);
128     void  SetEpochTimes(const DoubleVec1d& v);
129     void  SetGlobalParameters(const DoubleVec1d& v);
130     void  SetRegionalParameters(const DoubleVec1d& v);
131     void  SetGlobalParametersByTag(force_type tag, const DoubleVec1d& v);
132     void  SetRegionalParametersByTag(force_type tag, const DoubleVec1d& v);
133 
134     // Getters
135     // erynes 2004/01/22: made these return by constant reference
136     // to improve speed.  This might become inappropriate as the
137     // code base evolves.
GetParamSpace()138     param_space        GetParamSpace()    const { return m_space; };
139     const DoubleVec1d& GetGlobalThetas()  const;
140     const DoubleVec1d& GetRegionalThetas()const;
GetMigRates()141     const DoubleVec1d& GetMigRates()      const { return m_migrates; };
GetDiseaseRates()142     const DoubleVec1d& GetDiseaseRates()  const { return m_diseases; };
GetRecRates()143     const DoubleVec1d& GetRecRates()      const { return m_recrates; };
GetGrowthRates()144     const DoubleVec1d& GetGrowthRates()   const { return m_growths;  };
GetLogisticSelectionCoefficient()145     const DoubleVec1d& GetLogisticSelectionCoefficient() const
146     { return m_logisticSelectionCoefficient;  };
GetEpochTimes()147     const DoubleVec1d& GetEpochTimes()    const { return m_epochtimes; };
148     const DoubleVec1d& GetGlobalParametersByTag  (force_type tag)    const;
149     const DoubleVec1d& GetRegionalParametersByTag(force_type tag)    const;
150     double GetRegionalLogParameter(long pnum) const;
GetEpochs()151     const std::vector<Epoch>* GetEpochs() const { return m_epochptr; };
152 
153     // only pull the valid (in the paramvec sense) disease rates
154     DoubleVec1d GetOnlyDiseaseRates() const;
155 
156     // GetLogParameters() is used by the BayesArranger
157     DoubleVec1d GetGlobalLogParameters()           const;
158     DoubleVec1d GetRegionalLogParameters()         const;
159     DoubleVec1d GetGlobalParameters()              const;
160     DoubleVec1d GetRegionalParameters()            const;
161     DoubleVec2d GetGlobal2dRates(force_type tag)   const;
162     DoubleVec2d GetRegional2dRates(force_type tag) const;
163 
164     DoubleVec1d GetRegionalScalars() const;
165 
166     void WriteForceParameters(std::ofstream& out, long numtabs) const;
167 };
168 
169 #endif // FORCEPARAM_H
170 
171 //____________________________________________________________________________________
172