1 //
2 //  rateheterotachy.hpp
3 //  iqtree
4 //
5 //  Created by Minh Bui on 11/8/16.
6 //
7 //
8 
9 #ifndef rateheterotachy_hpp
10 #define rateheterotachy_hpp
11 
12 
13 #include "rateheterogeneity.h"
14 
15 class PhyloTree;
16 
17 /**
18     rate-heterotachy model, allowing for mixed branch lengths
19 */
20 class RateHeterotachy: virtual public RateHeterogeneity {
21 
22     friend class ModelFactoryMixlen;
23 
24 public:
25 	/**
26 		constructor
27 		@param ncat number of rate categories
28         @param sorted_rates TRUE to sort the rate in increasing order
29 		@param tree associated phylogenetic tree
30 	*/
31     RateHeterotachy(int ncat, string params, PhyloTree *tree);
32 
33 	/**
34 		destructor
35 	*/
36     virtual ~RateHeterotachy();
37 
38     /**
39         @return TRUE if this is a heterotachy model, default: FALSE
40     */
isHeterotachy()41     virtual bool isHeterotachy() { return true; }
42 
43     /**
44         start structure for checkpointing
45     */
46     virtual void startCheckpoint();
47 
48     /**
49         save object into the checkpoint
50     */
51     virtual void saveCheckpoint();
52 
53     /**
54         restore object from the checkpoint
55     */
56     virtual void restoreCheckpoint();
57 
58 
59 	/**
60 	 * @return model name with parameters in form of e.g. GTR{a,b,c,d,e,f}
61 	 */
62 	virtual string getNameParams();
63 
64 
65     /**
66         fix parameters, so that no optimization done
67         @param mode some input mode
68     */
getFixParams()69     virtual int getFixParams() { return fix_params; }
70 
71     /**
72         fix parameters, so that no optimization done
73         @param mode some input mode
74     */
setFixParams(int mode)75     virtual void setFixParams(int mode) { fix_params = mode; }
76 
77 	/**
78 		return the number of dimensions
79 	*/
80 	virtual int getNDim();
81 
82 	/**
83 		@return the number of rate categories
84 	*/
getNRate()85 	virtual int getNRate() { return ncategory; }
86 
87 	/**
88 		get the number of rate categories for site-specific category model
89 		@return the number of rate categories
90 	*/
getNDiscreteRate()91 	virtual int getNDiscreteRate() { return ncategory; }
92 
93 	/**
94 		@param category category ID from 0 to #category-1
95 		@return the rate of the specified category
96 	*/
getRate(int category)97 	virtual double getRate(int category) {
98         return 1.0;
99     }
100 
101 	/**
102 		get the proportion of sites under a specified category.
103 		@param category category ID from 0 to #category-1
104 		@return the proportion of the specified category
105 	*/
getProp(int category)106 	virtual double getProp(int category) { return prop[category]; }
107 
108 	/**
109 		set the proportion of a specified category.
110 		@param category category ID from 0 to #category-1
111 		@return the proportion of the specified category
112 	*/
setProp(int category,double value)113 	virtual void setProp(int category, double value) { prop[category] = value; }
114 
115 
116     /**
117         set number of optimization steps
118         @param opt_steps number of optimization steps
119     */
setOptimizeSteps(int optimize_steps)120     virtual void setOptimizeSteps(int optimize_steps) { this->optimize_steps = optimize_steps; }
121 
122 	/**
123 		optimize parameters. Default is to optimize gamma shape
124 		@return the best likelihood
125 	*/
126 	virtual double optimizeParameters(double gradient_epsilon);
127 
128     /**
129         optimize rate parameters using EM algorithm
130         @return log-likelihood of optimized parameters
131     */
132     double optimizeWithEM();
133 
134 	/**
135 		write information
136 		@param out output stream
137 	*/
138 	virtual void writeInfo(ostream &out);
139 
140 	/**
141 		write parameters, used with modeltest
142 		@param out output stream
143 	*/
144 	virtual void writeParameters(ostream &out);
145 
146     /**
147         set number of rate categories
148         @param ncat #categories
149     */
150 	virtual void setNCategory(int ncat);
151 
152 protected:
153 
154 	/**
155 		number of rate categories
156 	*/
157 	int ncategory;
158 
159 
160 	/**
161 	 * proportion of sites for each rate categories
162 	 */
163     double *prop;
164 
165 	/** TRUE to fix parameters */
166 	int fix_params;
167 
168     /** number of optimization steps, default: ncategory*2 */
169     int optimize_steps;
170 
171 };
172 
173 
174 
175 
176 #endif /* rateheterotachy_hpp */
177