31 #ifndef FFPOPSIM_HIGHD_H_
32 #define FFPOPSIM_HIGHD_H_
35 #define HCF_MEMERR -131545
36 #define HCF_BADARG -131546
38 #define WORDLENGTH 28 //length used to chop bitsets into words
55 coeff_t(
double value_in, vector <int> loci_in){
59 for (
int i=0; i<order; i++) loci[i]=loci_in[i];
102 vector<coeff_single_locus_t>::iterator coefficients_single_locus_iter;
103 vector<coeff_t>::iterator coefficients_epistasis_iter;
106 vector<double> coefficients_single_locus_static;
123 int set_up(
int dim_in,
int s=0);
128 double get_func(boost::dynamic_bitset<>& genotype);
129 double get_additive_coefficient(
int locus);
130 double get_func_diff(boost::dynamic_bitset<>& genotype1, boost::dynamic_bitset<>& genotype2, vector<int> &diffpos);
134 void reset_additive();
135 int set_additive_coefficient(
double value,
int locus,
int expected_locus=-1);
136 int add_coefficient(
double value, vector <int> loci);
137 int set_random_epistasis_strength(
double sigma);
143 #define NO_GENOTYPE -1
144 #define HP_MINAF 0.02
145 #define MAX_DELTAFITNESS 8
146 #define MAX_POPSIZE 500000
147 #define HP_NOTHING 1e-12
148 #define HP_RANDOM_SAMPLE_FRAC 0.01
149 #define HP_VERY_NEGATIVE -1e15
152 #define HP_BADARG -879564
153 #define HP_MEMERR -986465
154 #define HP_EXPLOSIONWARN 4
155 #define HP_EXTINCTERR 5
156 #define HP_NOBINSERR 6
157 #define HP_WRONGBINSERR 7
158 #define HP_RUNTIMEERR 8
170 clone_t(
int n_traits=0) : genotype(boost::dynamic_bitset<>(0)), trait(n_traits, 0), fitness(0), clone_size(0) {};
176 if(fitness < other.
fitness)
return true;
177 else if (fitness > other.
fitness)
return false;
179 for(
size_t i=0; i < genotype.size(); i++) {
180 if((!genotype[i]) && (other.
genotype[i]))
return true;
181 else if((genotype[i]) && (!other.
genotype[i]))
return false;
187 if(fitness > other.
fitness)
return true;
188 else if (fitness < other.
fitness)
return false;
190 for(
size_t i=0; i < genotype.size(); i++) {
191 if((genotype[i]) && (!other.
genotype[i]))
return true;
192 else if((!genotype[i]) && (other.
genotype[i]))
return false;
208 #ifndef rooted_tree_H_
209 #define rooted_tree_H_
211 #define RT_VERYLARGE 10000000
212 #define RT_CHILDNOTFOUND -35343
213 #define RT_NODENOTFOUND -35765
222 #include <gsl/gsl_histogram.h>
232 if(age < other.
age)
return true;
233 else if (age > other.
age)
return false;
234 else {
return (index<other.
index); }
237 if(age > other.
age)
return true;
238 else if (age < other.
age)
return false;
239 else {
return (index>other.
index); }
241 tree_key_t(
int index=0,
int age=0) : index(index), age(age) {};
248 if(pos < other.
pos)
return true;
252 if(pos > other.
pos)
return true;
256 if(pos == other.
pos)
return true;
259 step_t(
int pos=0,
int step=0) : pos(pos), step(step) {};
287 poly_t(
int b=0,
int age=0,
double e=0,
double f=0,
double fvar=0) :
288 birth(b), sweep_time(age), effect(e), fitness(f), fitness_variance(fvar) {};
303 void add_generation(vector <node_t> &new_generation,
double mean_fitness);
304 int add_terminal_node(
node_t &newNode);
307 int external_branch_length();
308 int total_branch_length();
309 int ancestors_at_age(
int age,
tree_key_t subtree_root, vector <tree_key_t> &ancestors);
312 int calc_weight_distribution(
tree_key_t subtree_root);
313 void SFS(gsl_histogram *sfs);
315 int erase_child(map <tree_key_t,node_t>::iterator Pnode,
tree_key_t to_be_erased);
316 int delete_extra_children(
tree_key_t subtree_root);
317 int delete_one_child_nodes(
tree_key_t subtree_root);
319 int check_tree_integrity();
323 string print_newick();
325 string print_weight_distribution(
tree_key_t node_key);
328 int construct_subtree(vector <tree_key_t> subtree_leafs,
rooted_tree &other);
346 #ifndef MULTILOCUSGENEALOGY_H_
347 #define MULTILOCUSGENEALOGY_H_
356 void track_locus(
int new_locus);
357 void reset(){loci.clear(); trees.clear();newGenerations.clear();}
358 void reset_but_loci(){
for(
unsigned int i=0; i<loci.size(); i++){trees[i].reset();newGenerations[i].clear();}}
359 void add_generation(
double baseline);
360 int extend_storage(
int n);
384 haploid_highd(
int L=0,
int rng_seed=0,
int number_of_traits=1,
bool all_polymorphic=
false);
402 if(
HP_VERBOSE) cerr<<
"Cannot set the mutation rate with all_polymorphic."<<endl;
404 }
else mutation_rate=m;}
413 int L(){
return number_of_loci;}
415 int N(){
return population_size;}
424 int set_allele_frequencies(
double* frequencies,
unsigned long N);
425 int set_genotypes(vector <genotype_value_pair_t> gt);
426 int set_wildtype(
unsigned long N);
427 int track_locus_genealogy(vector <int> loci);
430 void add_genotype(boost::dynamic_bitset<> genotype,
int n=1);
435 void clear_traits(){
for(
int t=0; t<number_of_traits; t++){trait[t].reset();}}
444 int evolve(
int gen=1);
445 int bottleneck(
int size_of_bottleneck);
446 unsigned int flip_single_locus(
int locus);
450 void unique_clones();
451 vector <int> get_nonempty_clones();
460 int random_clones(
unsigned int n_o_individuals, vector <int> *
sample);
463 string get_genotype_string(
unsigned int i){
string gts; boost::to_string(population[i].genotype, gts);
return gts;}
464 int distance_Hamming(
unsigned int clone1,
unsigned int clone2, vector <unsigned int *> *chunks=NULL,
unsigned int every=1){
return distance_Hamming(population[clone1].genotype, population[clone2].genotype, chunks, every);}
465 int distance_Hamming(boost::dynamic_bitset<> gt1, boost::dynamic_bitset<> gt2, vector<unsigned int *> *chunks=NULL,
unsigned int every=1);
466 stat_t get_diversity_statistics(
unsigned int n_sample=1000);
467 stat_t get_divergence_statistics(
unsigned int n_sample=1000);
470 double get_allele_frequency(
int l) {
if (!allele_frequencies_up_to_date){calc_allele_freqs();}
return allele_frequencies[l];}
473 double get_pair_frequency(
int locus1,
int locus2);
474 vector <double> get_pair_frequencies(vector < vector <int> > *loci);
475 double get_chi(
int l) {
return 2 * get_allele_frequency(l) - 1;}
477 double get_chi2(
int locus1,
int locus2){
return get_moment(locus1, locus2)-get_chi(locus1)*get_chi(locus2);}
478 double get_LD(
int locus1,
int locus2){
return 0.25 * get_chi2(locus1, locus2);}
479 double get_moment(
int locus1,
int locus2){
return 4 * get_pair_frequency(locus1, locus2) + 1 - 2 * (get_allele_frequency(locus1) + get_allele_frequency(locus2));}
482 void set_trait_weights(
double *weights){
for(
int t=0; t<number_of_traits; t++) trait_weights[t] = weights[t];}
484 double get_fitness(
int n) {calc_individual_fitness(population[n]);
return population[n].fitness;}
486 double get_trait(
int n,
int t=0) {calc_individual_traits(population[n]);
return population[n].trait[t];}
492 void update_traits();
493 void update_fitness();
496 int get_divergence_histogram(gsl_histogram **hist,
unsigned int bins=10, vector <unsigned int *> *chunks=NULL,
unsigned int every=1,
unsigned int n_sample=1000);
497 int get_diversity_histogram(gsl_histogram **hist,
unsigned int bins=10, vector <unsigned int *> *chunks=NULL,
unsigned int every=1,
unsigned int n_sample=1000);
498 int get_fitness_histogram(gsl_histogram **hist,
unsigned int bins=10,
unsigned int n_sample=1000);
501 int print_allele_frequencies(ostream &out);
502 int read_ms_sample(istream >s,
int skip_locus,
int multiplicity);
503 int read_ms_sample_sparse(istream >s,
int skip_locus,
int multiplicity,
int distance);
513 int get_random_seed();
515 void produce_random_sample(
int size=1000);
527 int select_gametes();
528 double relaxation_value();
529 double get_logmean_expfitness();
531 unsigned int flip_single_locus(
unsigned int clonenum,
int locus);
532 void shuffle_genotypes();
533 int new_generation();
537 int partition_cumulative(vector <unsigned int> &partition_cum);
538 int provide_at_least(
int n);
552 void calc_allele_freqs();
558 void reassortment_pattern();
559 void crossover_pattern();
561 int add_recombinants();
562 int recombine(
int parent1,
int parent2);
563 int recombine_crossover(
int parent1,
int parent2,
int ng);
570 void calc_fitness_stat();
571 void calc_trait_stat();
572 void calc_individual_traits(
clone_t &tempgt);
573 void calc_individual_fitness(
clone_t &tempgt);
577 double get_trait_difference(
clone_t &tempgt1,
clone_t &tempgt2, vector<int>& diffpos,
int traitnum);
581 virtual void calc_individual_fitness_from_traits(
clone_t &tempgt);
583 void add_clone_to_genealogy(
int locus,
int dest,
int parent,
int left,
int right,
int cs,
int n);
594 vector <int> available_clones;
595 vector <int> clones_needed_for_recombination;
597 boost::dynamic_bitset<> rec_pattern;
600 static size_t number_of_instances;