FFPopSim  2.0
Library for Population Genetics
 All Classes Files Functions Variables Macros Pages
ffpopsim_lowd.h
Go to the documentation of this file.
1 
24 #ifndef FFPOPSIM_LOWD_H_
25 #define FFPOPSIM_LOWD_H_
26 #include "ffpopsim_generic.h"
27 
28 #define HC_MEMERR -131545 //memory error code
29 #define HC_BADARG -131546 //bad argument error code
30 #define HC_VERBOSE 0 //debugging: if set to one, each function prints out a message into the error stream
31 #define HC_FUNC 1 //hypercube_lowd.func is up-to-date
32 #define HC_COEFF -1 //hypercube_lowd.coeff is up-to-date
33 #define HC_FUNC_EQ_COEFF 0 //hypercube_lowd.func equal hypercube_lowd.coeff
34 
35 using namespace std;
36 
52 {
53 public:
54  //dimension of the hypercube_lowd
55  int dim;
56 
57  int state; //takes values HC_FUNC, HC_COEFF, HC_HC_FUNC_EQ_COEFF, depending on the current state of hypercube_lowd
58  double *coeff; //array holding 2^N coefficients: a entry 0101001101 corresponds to a term with spins at each 1
59  double *func; //array holding the values of the function on the hypercube_lowd
60 
61  int *order; //Auxiliary array holding the number of spins, i.e. the number of ones of coeff[k]
62 
63  // construction / destruction
65  hypercube_lowd(int dim_in, int s=0);
66  ~hypercube_lowd();
67  int set_up(int dim_in, int s=0);
68 
69  // set coefficients
70  int gaussian_coefficients(double* vark, bool add=false);
71  int additive(double* additive_effects, bool add=false);
72  int init_rand_gauss(double sigma, bool add=false);
73  int init_list(vector<index_value_pair_t> iv, bool add=false);
74  int init_coeff_list(vector <index_value_pair_t> iv, bool add=false);
75  void calc_order();
76  void set_state(int s){state=s;}
77 
78  //in and out
79  int read_coeff(istream &in);
80  int write_func(ostream &out);
81  int write_coeff(ostream &in, bool label=false);
82  int read_func(istream &out);
83  int read_func_labeled(istream &in);
84 
85  //analysis
86  int signature(int point);
87 
88  //transform from coefficients to function and vice versa
89  int fft_func_to_coeff();
90  int fft_coeff_to_func();
91 
92  //read out
93  int get_state() {return state;}
94  unsigned int get_dim(){return dim;}
95  unsigned int get_seed() {return seed;}
96  double get_func(int point) {if (state==HC_COEFF) {fft_coeff_to_func();} return func[point]; }
97  double get_coeff(int point) {if (state==HC_FUNC) {fft_func_to_coeff();} return coeff[point]; }
98 
99  //operations on the function
100  int argmax();
101  double valuemax();
102  void func_set(int point, double f) {func[point]=f; }
103  void func_increment(int point, double f) {func[point]+=f; }
104  int normalize(double targetnorm=1.0);
105  int reset();
106  int scale(double scale);
107  int shift(double shift);
108  int test();
109 
110 protected:
111  //random number generator
112  gsl_rng *rng;
113  unsigned int seed;
114 
115 private:
116  // memory management
117  bool mem;
118  int allocate_mem();
119  int free_mem();
120 };
121 
122 
123 #define HG_VERBOSE 0
124 #define HG_LONGTIMEGEN 1000000
125 #define HG_CONTINUOUS 10000
126 #define HG_NOTHING 1e-15
127 #define HG_EXTINCT -9287465
128 #define HG_BADARG -879564
129 #define HG_MEMERR -32656845
130 
142 public:
143  // public hypercube_lowds
146 
147  // construction / destruction
148  haploid_lowd(int L=1, int rng_seed=0);
149  virtual ~haploid_lowd();
150 
151  // population parameters (read/write)
154  bool circular; //topology of the chromosome
155 
156  // population parameters (read only)
157  int L(){return number_of_loci;}
158  int get_number_of_loci(){return number_of_loci;}
159  double N(){return population_size;}
160  double get_population_size(){return population_size;}
161  double get_generation(){return long_time_generation+generation;}
162  void set_generation(double g){if(g > HG_LONGTIMEGEN) {generation = fmod(g, HG_LONGTIMEGEN); long_time_generation = g - generation;} else generation = g;}
163  double get_mutation_rate(int locus, int direction) {return mutation_rates[direction][locus];}
164  int get_recombination_model(){return recombination_model;}
165  double get_recombination_rate(int locus);
166 
167  //initialization
168  int set_allele_frequencies(double* frequencies, unsigned long N);
169  int set_genotypes(vector <index_value_pair_t> gt);
170  int set_wildtype(unsigned long N);
171 
172  // modify population
173  int set_recombination_model(int rec_model);
174  int set_recombination_rates(double *rec_rates, int rec_model=-1);
175  int set_mutation_rates(double m);
176  int set_mutation_rates(double m1, double m2);
177  int set_mutation_rates(double* m);
178  int set_mutation_rates(double** m);
179 
180  //evolution
181  int evolve(int gen=1);
182  int evolve_norec(int gen=1);
183  int evolve_deterministic(int gen=1);
184 
185  // readout
186  // Note: these functions are for the general public and are not expected to be
187  // extremely fast. If speed is a major concern, consider subclassing and working
188  // with protected methods.
189 
190  // genotype readout
191  double get_genotype_frequency(int genotype){return population.get_func(genotype);}
192 
193  // allele frequencies
194  double get_allele_frequency(int locus){return 0.5 * (1 + get_chi(locus));}
195  double get_pair_frequency(int locus1, int locus2){return 0.25 * (get_moment(locus1, locus2) - 1) + 0.5 * (get_allele_frequency(locus1) + get_allele_frequency(locus2));}
196 
197  double get_chi(int locus){return (1<<number_of_loci)*population.get_coeff(1<<locus);}
198  double get_chi2(int locus1, int locus2){return get_moment(locus1, locus2)-get_chi(locus1)*get_chi(locus2);}
199  double get_LD(int locus1, int locus2){return 0.25 * get_chi2(locus1, locus2);}
200  double get_moment(int locus1, int locus2){return (1<<number_of_loci)*population.get_coeff((1<<locus1)+(1<<locus2));}
201 
202  // entropy
203  double genotype_entropy();
204  double allele_entropy();
205 
206  // fitness/phenotype readout
207  double get_fitness(int genotype) {return fitness.get_func(genotype);}
208  stat_t get_fitness_statistics();
209 
210 protected:
211  //random number generator used for resampling and seeding the hypercube_lowds
212  gsl_rng* rng; //uses the same RNG as defined in hypercube_lowd.h from the GSL library.
213  int seed; //seed of the rng
214  int get_random_seed(); //get random seed from the OS
215 
216  //hypercube_lowds that store the distribution of recombinations and the change in the
217  //population distribution due to mutations
220  double** recombination_patterns; // array that holds the probabilities of all possible recombination outcomes for every subset of loci
221  int recombination_model; //model of recombination to be used
222 
223  // population parameters
228  double** mutation_rates; // the mutation rate can be made locus specific and genotype dependent.
229 
230  //evolution
231  int select();
232  int mutate();
233  int recombine();
234  int resample();
235 
236  // recombination
237  int set_recombination_rates_general(double *rec_rates);
238  int set_recombination_patterns(vector<index_value_pair_t> iv);
239  int marginalize_recombination_patterns();
240  int set_recombination_rates_single_crossover(double *rec_rates);
241  int calculate_recombinants_free();
242  int calculate_recombinants_single();
243  int calculate_recombinants_general();
244 
245 private:
246  // Memory management is private, subclasses must take care only of their own memory
247  bool mem;
248  int allocate_mem();
249  int free_mem();
250  int allocate_recombination_mem(int rec_model);
251  int free_recombination_mem();
252 
253  // counting reference
254  static size_t number_of_instances;
255 };
256 
257 #endif /* FFPOPSIM_LOWD_H_ */