1 #ifndef DEF_POPULATION_H
2 #define DEF_POPULATION_H
6 #include "demographic_function.h"
7 #include "boost/utility/enable_if.hpp"
8 #include "boost/type_traits/is_base_of.hpp"
17 std::ostream& print(std::ostream&)
const;
18 void random_shuffle_pop();
20 void initiate_pop(
int N);
22 void initiate_pop(
int N,
int mt,
int xL,
int xS,
int y,
int aL,
int aS);
24 void reinitiate_ID_table();
25 std::vector<int> femaleIDtable;
26 std::vector<int> maleIDtable;
62 std::vector<double> growthFactor;
79 explicit Population(
int N,
int mt,
int xL,
int xS,
int y,
int aL,
int aS)
81 initiate_pop(N,mt,xL,xS,y,aL,aS);
85 initiate_pop(a.nbHuman,a.sizeMt,a.nbChromosomesX,a.sizePerX,a.sizeY,a.nbChromosomesA/2,a.sizePerA);
86 generation = a.generation;
87 nbFemale = a.nbFemale;
88 popsizeDemo = a.popsizeDemo;
89 matingSystem = a.matingSystem;
90 inbreeding = a.inbreeding;
103 femaleIDtable = a.femaleIDtable;
104 maleIDtable = a.maleIDtable;
105 growthFactor = a.growthFactor;
107 explicit Population(std::string
const& filename);
112 std::vector<T>
get_pop(
int current)
const;
114 friend std::ostream& operator << (std::ostream& os,
Population const&a)
120 void set_mating_system(
int);
124 void set_mu(
double,
double,
double,
double);
126 void set_mu(
double,
double,
double,
double,
double,
double,
double,
double);
144 int get_sizeY()
const;
145 int get_sizePerX()
const;
146 int get_sizePerA()
const;
147 int get_crumbA()
const;
148 int get_crumbY()
const;
149 int get_crumbX()
const;
150 int get_crumbMt()
const;
151 int get_nbCellPerA()
const;
152 int get_nbCellPerX()
const;
153 int get_nbChromosomesA()
const;
154 int get_nbChromosomesX()
const;
155 std::vector<double> get_growthFactor()
const;
169 void write_nexus(std::string
const& file_name)
const;
170 void write_fasta(std::string
const& file_name)
const;
175 void output_arlequin(std::string
const& filename);
180 int burnin(
int gen,
double facMu);
181 int burnin(
double diversityTreshold,
int gen,
double facMu);
194 void birth_boy(
int indexfemale,
int indexmale,
int nbBabyMale);
195 void birth_girl(
int indexfemale,
int indexmale,
int nbBabyFemale);
204 for (std::vector<int>::iterator it = femaleIDtable.begin(); it != femaleIDtable.end(); ++it)
210 for (std::vector<int>::iterator it = maleIDtable.begin(); it != maleIDtable.end(); ++it)
351 return nbHuman-nbFemale;
399 return nbChromosomesA;
405 return nbChromosomesX;
435 int parentG = !(generation&1);
436 std::vector<int> mates(nbHuman);
437 for(
int i=0; i<nbFemale; ++i)
438 mates[i] = female[parentG][i].getNbMate();
439 for(
int i=0; i<(nbHuman-nbFemale); ++i)
440 mates[i+nbFemale] = male[parentG][i].getNbMate();
447 int parentG = (generation&1)^1;
448 std::vector<int> children(nbHuman);
449 for(
int i=0; i<nbFemale; ++i)
450 children[i] = female[parentG][i].getNbChild();
451 for(
int i=0; i<(nbHuman-nbFemale); ++i)
452 children[i+nbFemale] = male[parentG][i].getNbChild();
467 int currentG = generation&1;
468 if(sampleSize>nbHuman)
470 std::cerr <<
"ERROR : attempt to subsample more individuals than the Population size\n";
471 sampleSize = nbHuman;
475 std::cerr <<
"ERROR : attempt to subsample more female than the Population size "<< nbWomen<<
" versus" << nbFemale<<
"\n";
478 Population sample(sampleSize,sizeMt,nbChromosomesX,sizePerX,sizeY,nbChromosomesA,sizePerA);
480 random_shuffle_pop();
481 sample.nbFemale=nbWomen;
482 sample.matingSystem=matingSystem;
483 for(
int i(0);i<nbWomen;++i)
485 sample.
female[0][i].reborn(female[currentG][i]);
487 for(
int i(0);i<sampleSize-nbWomen;++i)
489 sample.
male[0][i].reborn(male[currentG][i]);
499 int currentG = generation&1;
500 Population sample(2*nbFemale,sizeMt,nbChromosomesX,sizePerX,sizeY,nbChromosomesA,sizePerA);
501 sample.nbHuman = nbFemale;
502 for(
int i(0);i<nbFemale;++i)
504 sample.
female[0][i].reborn(female[currentG][i]);
513 int currentG = generation&1;
514 int nbMale = nbHuman-nbFemale;
515 Population sample(2*nbMale,sizeMt,nbChromosomesX,sizePerX,sizeY,nbChromosomesA,sizePerA);
516 sample.nbHuman = nbMale;
518 for(
int i(0);i<(nbHuman-nbFemale);++i)
520 sample.
male[0][i].reborn(male[currentG][i]);
530 for(
int i(0);i<gen;++i){
535 std::cout <<
"Population is extinct" << std::endl;
542 fs.open(filename.c_str(),std::ios::trunc);
543 for(
int i(0);i<gen;++i){
545 output_genealogy(fs);
553 random_shuffle_pop();
564 int currentG = (generation&1);
565 int nbMating(0),nbBabyToMake(floor(popsizeDemo));
566 int randomNbChildren,nbBabyFemale(0),nbBabyMale(0);
567 int indexmale(-1),indexfemale(-1);
568 int randomIndivSwap(0),testloop(0);
570 if((nbFemale>0)&&(nbHuman-nbFemale>0))
576 if(nbFemale>nbHuman/2){
577 nbMating = nbHuman-nbFemale; }
579 nbMating = nbFemale;}
580 for(
int i(0);i<nbMating;++i)
587 while((female[currentG][indexfemale].shared_parents2(male[currentG][indexmale])) && (testloop<100))
590 randomIndivSwap = myrand(nbHuman-nbFemale-i);
591 tmpmale.reborn(male[currentG][randomIndivSwap + i]);
592 male[currentG][randomIndivSwap + i].reborn(male[currentG][indexmale]);
593 male[currentG][indexmale].reborn(tmpmale);
596 randomNbChildren = rBinom(nbBabyToMake,1/(
double)(nbMating-i));
597 if(TRACK_MATE_AND_CHILDREN)
599 female[currentG][indexfemale].newMate();
600 male[currentG][indexmale].newMate();
601 female[currentG][indexfemale].hadChild(randomNbChildren);
602 male[currentG][indexmale].hadChild(randomNbChildren);
604 for(
int j(0);j<randomNbChildren;++j)
609 birth_girl(indexfemale,indexmale,nbBabyFemale);
614 birth_boy(indexfemale,indexmale,nbBabyMale);
625 for(
int i(0);i<nbMating;++i)
629 randomNbChildren = rBinom(nbBabyToMake,1/(
double)(nbMating-i));
630 if(TRACK_MATE_AND_CHILDREN)
632 female[currentG][indexfemale].hadChild(randomNbChildren);
634 for(
int j(0);j<randomNbChildren;++j)
637 indexmale = myrand(nbHuman-nbFemale);
641 while((female[currentG][indexfemale].shared_parents2(male[currentG][indexmale])) && (testloop<100))
643 randomIndivSwap = myrand(nbHuman-nbFemale);
644 tmpmale.reborn(male[currentG][randomIndivSwap + i]);
645 male[currentG][randomIndivSwap + i].reborn(male[currentG][indexmale]);
646 male[currentG][indexmale].reborn(tmpmale);
649 if(TRACK_MATE_AND_CHILDREN)
651 female[currentG][indexfemale].newMate();
652 male[currentG][indexmale].newMate();
653 male[currentG][indexmale].hadChild(1);
655 if(indexmale == nbHuman-nbFemale)
657 std::cerr <<
"RANDOM FAILURE" << std::endl;
663 birth_girl(indexfemale,indexmale,nbBabyFemale);
668 birth_boy(indexfemale,indexmale,nbBabyMale);
680 for(
int i(0);i<nbMating;++i)
684 randomNbChildren = rBinom(nbBabyToMake,1/(
double)(nbMating-i));
685 if(TRACK_MATE_AND_CHILDREN)
687 female[currentG][indexfemale].newMate();
688 female[currentG][indexfemale].hadChild(randomNbChildren);
690 for(
int j(0);j<randomNbChildren;++j)
692 indexmale = myrand(nbHuman-nbFemale);
696 while((female[currentG][indexfemale].shared_parents2(male[currentG][indexmale])) && (testloop<100))
698 randomIndivSwap = myrand(nbHuman-nbFemale-i);
699 tmpmale.reborn(male[currentG][randomIndivSwap + i]);
700 male[currentG][randomIndivSwap + i].reborn(male[currentG][indexmale]);
701 male[currentG][indexmale].reborn(tmpmale);
704 if(TRACK_MATE_AND_CHILDREN)
706 male[currentG][indexmale].newMate();
707 male[currentG][indexmale].hadChild(1);
712 birth_girl(indexfemale,indexmale,nbBabyFemale);
717 birth_boy(indexfemale,indexmale,nbBabyMale);
727 nbMating = nbHuman - nbFemale;
728 for(
int i(0);i<nbMating;++i)
731 randomNbChildren = rBinom(nbBabyToMake,1/(
double)(nbHuman-nbFemale));
732 if(TRACK_MATE_AND_CHILDREN)
734 male[currentG][indexmale].newMate();
735 male[currentG][indexmale].hadChild(randomNbChildren);
737 for(
int j(0);j<randomNbChildren;++j)
739 indexfemale = myrand(nbFemale);
743 while((female[currentG][indexfemale].shared_parents2(male[currentG][indexmale])) && (testloop<100))
745 randomIndivSwap = myrand(nbFemale);
746 tmpfemale.reborn(female[currentG][randomIndivSwap + i]);
747 female[currentG][randomIndivSwap + i].reborn(female[currentG][indexfemale]);
748 female[currentG][indexfemale].reborn(tmpfemale);
751 if(TRACK_MATE_AND_CHILDREN)
754 female[currentG][indexfemale].newMate();
755 female[currentG][indexfemale].hadChild(1);
760 birth_girl(indexfemale,indexmale,nbBabyFemale);
765 birth_boy(indexfemale,indexmale,nbBabyMale);
775 nbMating = std::floor(popsizeDemo);
776 for(
int i(0);i<nbMating;++i)
778 indexfemale = myrand(nbFemale);
779 indexmale = myrand(nbHuman-nbFemale);
780 if(TRACK_MATE_AND_CHILDREN)
782 female[currentG][indexfemale].hadChild(1);
783 male[currentG][indexmale].hadChild(1);
784 female[currentG][indexfemale].newMate();
785 male[currentG][indexmale].newMate();
787 if(indexmale == nbHuman-nbFemale)
789 std::cerr <<
"ERROR in the creation of the new generation" << std::endl;
795 birth_girl(indexfemale,indexmale,nbBabyFemale);
800 birth_boy(indexfemale,indexmale,nbBabyMale);
809 std::cout <<
"Non existing mating system !" << std::endl;
813 nbFemale = nbBabyFemale;
814 nbHuman = nbBabyMale+nbBabyFemale;
821 if(nbHuman > popsizeDemo)
823 std::cerr <<
"ERROR : reproduction scheme is not working, too many baby were created" << std::endl;
826 reinitiate_ID_table();
832 if(nbHuman>popsizeDemo)
834 std::cerr <<
"TROUBLE Nb of children not working !" << std::endl;
835 int nbDead,nbFemaleDead;
836 nbDead = nbHuman - std::floor(popsizeDemo);
837 nbFemaleDead = rBinom(nbDead,(
double)nbFemale/(
double)nbHuman);
838 nbFemale -= nbFemaleDead;
842 popsizeDemo = demographic_function(popsizeDemo,growthFactor[0],growthFactor[1],growthFactor[2]);
852 femaleIDtable = a.femaleIDtable;
853 maleIDtable = a.maleIDtable;
854 popsizeDemo = a.popsizeDemo;
856 nbFemale = a.nbFemale;
857 generation = a.generation;
858 growthFactor = a.growthFactor;
859 matingSystem = a.matingSystem;
860 nbCrumbMt = a.nbCrumbMt;
861 nbCrumbA = a.nbCrumbA;
862 nbCrumbY = a.nbCrumbY;
863 nbCrumbNuc = a.nbCrumbNuc;
864 nbCellPerA = a.nbCellPerA;
865 nbCellPerX = a.nbCellPerX;
866 nbCrumbX = a.nbCrumbX;
867 nbChromosomesA= a.nbChromosomesA;
868 nbChromosomesX= a.nbChromosomesX;
871 sizePerA = a.sizePerA;
872 sizePerX = a.sizePerX;
888 int currentG = (generation&1);
889 os <<
"Pop with " << nbHuman <<
"individuals with a sex ratio of " << nbFemale/nbHuman << std::endl;
890 for(
int i(0);i<nbFemale;++i)
892 os << female[currentG][i];
894 for(
int i(0);i<(nbHuman-nbFemale);++i)
896 os << male[currentG][i];
905 int currentG = (generation&1);
906 std::ofstream fs(file_name.std::string::c_str());
907 fs <<
"generation " << generation <<
" -- nb humans " << nbHuman <<
" -- Sex ratio " << nbFemale/nbHuman << std::endl;
910 for(
int i(0);i<nbFemale;++i)
912 fs << female[currentG][i];
914 for(
int i(0);i<(nbHuman-nbFemale);++i)
916 fs << male[currentG][i];
918 fs <<
"---" << std::endl;
925 std::cout <<
"generation " << generation <<
" -- nb humans " << nbHuman <<
" -- Sex ratio " << (double)nbFemale/(
double)nbHuman << std::endl;
931 int currentG = (generation&1);
932 for(
int i(0);i<nbFemale;++i)
934 fs << generation <<
"\t" << femaleIDtable[i] <<
"\t" << female[currentG][i].getMother() <<
"\t" << female[currentG][i].getFather() <<
"\n";
936 for(
int i(0);i<(nbHuman-nbFemale);++i)
938 fs << generation <<
"\t" << maleIDtable[i] <<
"\t" << male[currentG][i].getMother() <<
"\t" << male[currentG][i].getFather() <<
"\n";
976 femaleIDtable.resize(std::ceil(popsizeDemo));
977 maleIDtable.resize(std::ceil(popsizeDemo));
979 for (std::vector<int>::iterator it = femaleIDtable.begin(); it != femaleIDtable.end(); ++it)
985 for (std::vector<int>::iterator it = maleIDtable.begin(); it != maleIDtable.end(); ++it)
990 T tmp(nbCrumbMt,nbCrumbNuc);
994 for(
int i(0);i<popsizeDemo;++i)
997 female[0].push_back(people);
998 T people2(tmpfemale);
999 female[1].push_back(people2);
1001 for(
int i(0);i<popsizeDemo;++i)
1004 male[0].push_back(people);
1006 male[1].push_back(people2);
1018 for (std::vector<int>::iterator it = femaleIDtable.begin(); it != femaleIDtable.end(); ++it)
1024 for (std::vector<int>::iterator it = maleIDtable.begin(); it != maleIDtable.end(); ++it)
1029 for(
int i(0);i<std::ceil(N/2.0);++i)
1033 for(
int i(0);i<N/2;++i)
1035 female[0][i].reset();
1056 nbCrumbMt = std::ceil(mt*1.0/CRUMB);
1057 sizeMt = std::ceil(mt*1.0/CRUMB)*CRUMB;
1058 sizePerA = std::ceil(aS*1.0/CRUMB)*CRUMB;
1059 nbChromosomesA = aL * 2;
1060 nbCellPerA = sizePerA/CRUMB;
1061 nbCrumbA = nbChromosomesA * sizePerA /CRUMB;
1062 sizeY = std::ceil(y*1.0/CRUMB)*CRUMB;
1063 nbCrumbY = sizeY/CRUMB;
1064 sizePerX = std::ceil(xS*1.0/CRUMB)*CRUMB;
1065 nbCellPerX = sizePerX /CRUMB;
1066 nbChromosomesX = xL;
1067 nbCrumbX = nbChromosomesX * sizePerX /CRUMB;
1068 int nbCrumbSex = std::max(nbCrumbX,nbCrumbY);
1069 nbCrumbNuc = nbCrumbA + nbCrumbX + nbCrumbSex;
1070 growthFactor.resize(3);
1074 femaleIDtable.resize(std::ceil(popsizeDemo));
1075 maleIDtable.resize(std::ceil(popsizeDemo));
1077 for (std::vector<int>::iterator it = femaleIDtable.begin(); it != femaleIDtable.end(); ++it)
1083 for (std::vector<int>::iterator it = maleIDtable.begin(); it != maleIDtable.end(); ++it)
1088 T tmp(nbCrumbMt,nbCrumbNuc);
1092 for(
int i(0);i<popsizeDemo;++i)
1094 T people(tmpfemale);
1095 female[0].push_back(people);
1096 T people2(tmpfemale);
1097 female[1].push_back(people2);
1099 for(
int i(0);i<popsizeDemo;++i)
1102 male[0].push_back(people);
1104 male[1].push_back(people2);
1111 int oldsize = male[0].size();
1112 for(
int i(0); i<(a-oldsize); i++)
1115 male[0].push_back(newH0);
1117 male[1].push_back(newH1);
1119 female[0].push_back(newF0);
1121 female[1].push_back(newF1);
1123 femaleIDtable.resize(a);
1124 maleIDtable.resize(a);
1130 int currentG = (generation%2);
1133 for (
int i = nbFemale - 1; i > 0; --i)
1136 tmpfemale.reborn(female[currentG][i]);
1137 female[currentG][i].reborn(female[currentG][n]);
1138 female[currentG][n].reborn(tmpfemale);
1139 tmpIndex = femaleIDtable[i];
1140 femaleIDtable[i] = femaleIDtable[n];
1141 femaleIDtable[n] = tmpIndex;
1143 for (
int i = nbHuman - nbFemale - 1; i > 0; --i)
1146 tmpmale.reborn(male[currentG][i]);
1147 male[currentG][i].reborn(male[currentG][n]);
1148 male[currentG][n].reborn(tmpmale);
1149 tmpIndex = maleIDtable[i];
1150 maleIDtable[i] = maleIDtable[n];
1151 maleIDtable[n] = tmpIndex;
1158 std::cout << generation <<
"\t" << generation <<
"\t" << diversity_mtdna() <<
"\t"<< pairwise_distance_mtdna() <<
" \t " << diversity_A() <<
"\t" << diversity_X() <<
"\t" << diversity_Y() << std::endl;
1164 std::vector<int> nbChildren = get_nb_children();
1165 for(
int i(nbFemale);i<nbHuman-nbFemale;++i)
1166 std::cout <<
"\n" << nbChildren[i];
1167 std::cout<<std::endl;
1173 std::vector<int> nbMate = get_nb_mates();
1174 for(
int i(0);i<nbFemale;++i)
1175 std::cout<<
"\n"<<nbMate[i];
1176 std::cout<<std::endl;
void write_nexus(std::string const &file_name) const
Population(Population const &a)
Definition: population.h:83
void evolve(int gen)
Definition: population.h:528
void write_smartFormat(std::string const &filename) const
std::vector< T > male[2]
Definition: population.h:75
Definition: population.h:14
Population subsample_female()
Definition: population.h:495
std::vector< int > get_nb_mates()
Definition: population.h:433
double get_muX2() const
Definition: population.h:300
double diversity_X() const
typeDNA polySites(int nuclear) const
Population(int N, int mt, int xL, int xS, int y, int aL, int aS)
Definition: population.h:79
void output_nb_mates()
Definition: population.h:1171
double get_muA2() const
Definition: population.h:288
double get_popsizeDemo() const
Definition: population.h:355
typeDNA polySites_mtdna() const
double pairwise_distance_mtdna() const
int get_matingSystem() const
Definition: population.h:224
void output_nb_child()
Definition: population.h:1162
int get_nbFemale() const
Definition: population.h:343
Population & operator=(Population const &)
Definition: population.h:846
double diversity_mtdna() const
Population subsample(int sampleSize, int nbWomen)
Definition: population.h:463
void set_mu(double, double, double, double)
Definition: population.h:242
double diversity_Y() const
void set_inbreeding(int)
Definition: population.h:230
double get_muY2() const
Definition: population.h:312
void output_diversity()
Definition: population.h:1156
int get_nbHuman() const
Definition: population.h:337
Population subsample_male()
Definition: population.h:511
void evolve_gene(int gen, std::string filename)
Definition: population.h:539
void resize(int)
Definition: population.h:1109
int get_inbreeding() const
Definition: population.h:236
~Population()
Definition: population.h:108
double get_muX1() const
Definition: population.h:294
int get_nbMale() const
Definition: population.h:349
int get_generation() const
Definition: population.h:331
void output_pop_size() const
Definition: population.h:923
void set_growthFactor(double, double, double)
Definition: population.h:268
Population()
Definition: population.h:77
std::vector< int > get_nb_children()
Definition: population.h:445
double diversity_A() const
std::vector< T > get_pop(int current) const
double get_muMt2() const
Definition: population.h:324
double get_muMt1() const
Definition: population.h:318
double get_muY1() const
Definition: population.h:306
int get_sizeMt() const
Definition: population.h:409
void output_genealogy(std::ofstream &fs)
Definition: population.h:929
std::vector< T > female[2]
Definition: population.h:74
void write_fasta(std::string const &file_name) const
void write_txt_pop(std::string const &) const
Definition: population.h:903
double get_muA1() const
Definition: population.h:282