Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
GeneticMap Class Reference

GeneticMap. More...

#include <ttrait_with_map.h>

+ Collaboration diagram for GeneticMap:

Public Member Functions

 GeneticMap ()
 
 ~GeneticMap ()
 
bool getGeneticMap (trait_t trait, double **table, unsigned int table_length)
 
double getResolution ()
 
double setResolution (double val)
 
void rescaleMap (double val)
 
void reset_tables ()
 
void clear ()
 
void setLookupTable (unsigned int idx)
 Bbuilds the lookup table for each trait. More...
 
void recombine (sex_t SEX)
 Called by TTProtoWithMap::recombine twice to create the two gametes necessary for the creation of a new individual. More...
 
bool registerIndForRecombine (unsigned long ID)
 Called by TTProtoWithMap::recombine with individual ID passed down from Individual::recombine. More...
 
unsigned int addTrait (trait_t trait, unsigned int nChrm, unsigned int nLoc, unsigned int *nLocChrm, double resolution, unsigned int *locPositions)
 Returns the table index for the registered trait. More...
 
void unregisterTrait (trait_t trait)
 
vector< unsigned int > & getRecLoci (sex_t SEX, unsigned int trait)
 Returns a vector of the loci where crossing-overs take place. More...
 
vector< bool > & getFirstRecPosition (sex_t SEX)
 Returns the vector of the first chromosome position for recombination, used for all traits. More...
 
unsigned intgetLocusPositionTable (trait_t trait)
 

Private Attributes

unsigned long _currentIndividual
 
map< trait_t, unsigned int_traits
 Table mapping trait type to its position index in the following tables. More...
 
unsigned int _nTrait
 Number of traits registered in the map. More...
 
vector< unsigned int * > _lociLookupTable
 A list of tables that map the map position (cM) to a locus, for each trait. More...
 
vector< unsigned int_numChrsmPerTrait
 Vector of number of chromosomes for each trait. More...
 
vector< unsigned int_numLociPerTrait
 Vector of number of loci for each trait. More...
 
vector< unsigned int * > _numLociPerChrsmPerTrait
 Vector containing a table of number of loci per chromosome for each trait. More...
 
vector< unsigned int * > _locPositionsPerTrait
 Vector containing the table of map position for the loci of each trait. More...
 
vector< vector< unsigned int > > _recPositions [2]
 Vector of tables containing, for each trait, the locus number at which x-overs happen. More...
 
vector< bool > _chrsmFirstRecombPosition [2]
 Two vectors holding the starting copy of each chromosome to use when creating the two gametes that are used to create a new individual. More...
 
vector< unsigned int_junctions
 A vector to store the position of the recombination events. More...
 
unsigned int _numChromosome
 
unsigned int_perChrsmLength
 
unsigned int_chrsmFirstLocusPosition
 
unsigned int _totalLength
 
unsigned int _recombLength
 
unsigned int _totalNumLoci
 
double _resolution
 
double _totRecombEventsMean
 
double _recombinationRate
 

Detailed Description

Constructor & Destructor Documentation

◆ GeneticMap()

GeneticMap::GeneticMap ( )
inline
unsigned int _nTrait
Number of traits registered in the map.
Definition: ttrait_with_map.h:56
unsigned int _recombLength
Definition: ttrait_with_map.h:88
double _recombinationRate
Definition: ttrait_with_map.h:92
double _totRecombEventsMean
Definition: ttrait_with_map.h:91
unsigned int * _chrsmFirstLocusPosition
Definition: ttrait_with_map.h:86
unsigned int _totalLength
Definition: ttrait_with_map.h:87
unsigned int * _perChrsmLength
Definition: ttrait_with_map.h:85
unsigned int _numChromosome
Definition: ttrait_with_map.h:84
unsigned int _totalNumLoci
Definition: ttrait_with_map.h:89
double _resolution
Definition: ttrait_with_map.h:90
unsigned long _currentIndividual
Definition: ttrait_with_map.h:50

◆ ~GeneticMap()

GeneticMap::~GeneticMap ( )
inline
102{reset_tables();}
void reset_tables()
Definition: ttrait_with_map.cc:1172

References reset_tables().

Member Function Documentation

◆ addTrait()

unsigned int GeneticMap::addTrait ( trait_t  trait,
unsigned int  nChrm,
unsigned int  nLoc,
unsigned int nLocChrm,
double  resolution,
unsigned int locPositions 
)

Returns the table index for the registered trait.

660{
661 map<trait_t, unsigned int>::iterator tIter;
662
663 unsigned int traitIdx = 0;
664
665 bool do_add = true;
666
667 tIter = _traits.find(trait);
668
669 if ( tIter != _traits.end() ) {
670 //this shouldn't happen... unless we are loading a pop from source!!
671
672 traitIdx = tIter->second;
673
674 if(_numChrsmPerTrait[traitIdx] != nChrm)
675 fatal("mismatch while loading source population and resetting genetic map for trait \"%s\" \n\
676>>>>> number of chromosomes differ (%i != %i)\n\
677>>>>> please check match between init file and source population\n",trait.c_str(),_numChrsmPerTrait[traitIdx],nChrm);
678
679 if(_numLociPerTrait[traitIdx] != nLoc)
680 fatal("mismatch while loading source population and resetting genetic map for trait \"%s\"\n\
681>>>>> number of loci differ (%i != %i)\n\
682>>>>> please check match between init file and source population\n",trait.c_str(),_numLociPerTrait[traitIdx], nLoc);
683
684 unsigned int *locTable = _numLociPerChrsmPerTrait[traitIdx];
685
686 for (unsigned int i=0; i<nChrm; i++)
687 if(locTable[i] != nLocChrm[i])
688 fatal("mismatch while loading source population and resetting genetic map for trait \"%s\"\n\
689>>>>> number of loci differ (%i != %i) on chromosome %i\n\
690>>>>> please check match between init file and source population\n",trait.c_str(),locTable[i], nLocChrm[i], i+1);
691
692 do_add = false;
693
694 }
695
696#ifdef _DEBUG_
697 cout << "GeneticMap::addTrait: \n";
698 cout << " _numChromosome = "<<nChrm<<endl;
699 cout << " _totalNumLoci = "<<_totalNumLoci<<endl;
700 cout << " _resolution = "<<_resolution<<endl;
701 cout << " _totalLength = "<<_totalLength<<endl;
702 cout << ":::"<<trait<<"::: params:\n";
703 cout << " num chromosome = "<<nChrm<<endl;
704 cout << " num loci = "<<nLoc<<endl;
705 cout << " map resolution = "<<resolution<<endl;
706#endif
707
708 if (_nTrait == 0) { //this is the first trait to register its map
709
710 _numChromosome = nChrm;
711 _totalNumLoci = nLoc;
712 _resolution = resolution;
713 _totalLength = locPositions[nLoc-1];
714
715 //destroy all previously allocated tables
716 reset_tables();
717
718 _perChrsmLength = new unsigned int[_numChromosome];
719 _chrsmFirstLocusPosition = new unsigned int[_numChromosome];
720
721 } else {
722 //we have already registered some trait's genetic map, we append this trait's info and update the map
723
724 if (nChrm != _numChromosome) //check chromosome numbers
725 fatal("Traits in init file don't have same number of chromosomes, cannot set the genetic map!\n");
726
727 //number of loci
728 _totalNumLoci = max(_totalNumLoci, nLoc);
729
730 //setting the resolution:
731 if (resolution < _resolution) {
732
733// cout << "GeneticMap::addTrait: rescaling map resolution (old "<< _resolution << "; new "<< resolution<< ")\n";
734
735 rescaleMap(resolution);
736
737 } else if (resolution > _resolution) {
738
739// cout << "GeneticMap::addTrait: rescaling locus position (old "<< _resolution << "; new "<< resolution<< "; ratio "<< resolution/_resolution <<")\n";
740
741 double ratio = resolution/_resolution;
742
743 for(unsigned int i = 0; i < nLoc; ++i)
744 locPositions[i] = (unsigned int)((double)locPositions[i]*ratio);
745 }
746
747 }
748
749 if(do_add) { //only at the start of a simulation, not when loading pop from binary source
750
751 traitIdx = _nTrait;
752
753 _traits[trait] = _nTrait++;
754
755
756 //---- record num chrmsm
757 if ( _numChrsmPerTrait.size() != _nTrait -1 ) {
758 fatal("Genetic map::wrong size of table of num chrms per trait (%i != %i)",
759 _numChrsmPerTrait.size(), _nTrait -1);
760 } else
761 _numChrsmPerTrait.push_back(nChrm);
762
763 //---- record num loci
764 if ( _numLociPerTrait.size() != _nTrait -1 ) {
765 fatal("Genetic map::wrong size of table of num loci per trait (%i != %i)",
766 _numLociPerTrait.size(), _nTrait -1);
767 } else
768 _numLociPerTrait.push_back(nLoc);
769
770 //---- record table of num loci per chrmsm
771 if ( _numLociPerChrsmPerTrait.size() != _nTrait -1 )
772 fatal("Genetic map::wrong size of table of num loci per chrms per trait (%i != %i)",
774
775 unsigned int* locTable = new unsigned int[nChrm];
776
777 for (unsigned int i = 0; i < nChrm; i++) locTable[i] = nLocChrm[i];
778
779 _numLociPerChrsmPerTrait.push_back(locTable);
780
781 //---- record the locus positions
782 if ( _locPositionsPerTrait.size() != _nTrait -1 ) {
783 fatal("Genetic map::wrong size of table of loc position per trait (%i != %i)",
785 }
786
787 //copy the locus positions, they are at the correct resolution already
788 unsigned int* posTable = new unsigned int[nLoc];
789
790 for (unsigned int i = 0; i < nLoc; ++i) {
791 posTable[i] = locPositions[i];
792 }
793
794 _locPositionsPerTrait.push_back(posTable);
795
796
797 //---- create tables to record positions of x-over, for each gamete (male and female)
798
799 for(unsigned int s = 0; s < 2; ++s){
800 if ( _recPositions[s].size() != _nTrait -1 )
801 fatal("Genetic map::wrong size of rec positions table (%i != %i)",
802 _recPositions[s].size(), _nTrait-1);
803
804 _recPositions[s].push_back(vector<unsigned int>()); //add an empty array for each trait
805 }
806
807
808 //---- set the map positions according to other trait's
809 //---- need to correctly set starting position and length of each chromosome
810
811 if (_nTrait == 1) { //first trait to register
812
813 for (unsigned int c = 0, stride = 0; c < _numChromosome; ++c) {
814 _chrsmFirstLocusPosition[c] = locPositions[stride];
815 _perChrsmLength[c] = locPositions[stride + nLocChrm[c] - 1] - locPositions[stride]; //position of last locus - position of first locus on that chromosome.
816 stride += nLocChrm[c];
817 }
818
819 } else {
820
821 for (unsigned int c = 0, lastPos, stride = 0; c < _numChromosome; ++c) {
822
823 lastPos = max(locPositions[stride + nLocChrm[c] - 1],
825
826 _chrsmFirstLocusPosition[c] = min(_chrsmFirstLocusPosition[c], locPositions[stride]);
827
829
830 stride += nLocChrm[c];
831 }
832 }
833
834 //---- compute total chromosome length
835 _totalLength = 0;
836 for (unsigned int c = 0; c < _numChromosome; ++c) _totalLength += _perChrsmLength[c];
837
838 //---- mean num recombination events, map length of 1M = 1 x-over on average
840
841 //---- set the map size used to draw recombination spots; one less operation in recombine()...
843
844 //---- set the recombination rate between adjacent positions on the map;
846
847
848#ifdef _DEBUG_
849 cout << "GeneticMap::addTrait: "<<endl;
850 cout << " map stats for trait \""<<trait<<"\"\n";
851 cout << " _totalLength = "<<_totalLength<<endl;
852 cout << " _totRecombEventsMean = "<<_totRecombEventsMean<<endl;
853#endif
854
855
856 //---- create new slot for the lookup table of that trait
857 // will be allocated and filled in setLookupTable
858 if ( _lociLookupTable.size() != _nTrait -1 )
859 fatal("Genetic map::wrong size of loci lookup tables (%i != %i)",
860 _lociLookupTable.size(), _nTrait-1);
861
862 _lociLookupTable.push_back(NULL);
863
864 //---- set all lookup tables (all genetic maps):
865 // we reset all tables each time a trait is added because _totalLength changes
866 for (unsigned int t = 0; t < _nTrait; ++t) setLookupTable(t);
867 }
868
869 return traitIdx;
870}
void setLookupTable(unsigned int idx)
Bbuilds the lookup table for each trait.
Definition: ttrait_with_map.cc:933
vector< unsigned int > _numLociPerTrait
Vector of number of loci for each trait.
Definition: ttrait_with_map.h:65
vector< unsigned int * > _lociLookupTable
A list of tables that map the map position (cM) to a locus, for each trait.
Definition: ttrait_with_map.h:61
vector< unsigned int > _numChrsmPerTrait
Vector of number of chromosomes for each trait.
Definition: ttrait_with_map.h:63
vector< unsigned int * > _locPositionsPerTrait
Vector containing the table of map position for the loci of each trait.
Definition: ttrait_with_map.h:70
vector< vector< unsigned int > > _recPositions[2]
Vector of tables containing, for each trait, the locus number at which x-overs happen.
Definition: ttrait_with_map.h:75
vector< unsigned int * > _numLociPerChrsmPerTrait
Vector containing a table of number of loci per chromosome for each trait.
Definition: ttrait_with_map.h:67
map< trait_t, unsigned int > _traits
Table mapping trait type to its position index in the following tables.
Definition: ttrait_with_map.h:53
void rescaleMap(double val)
Definition: ttrait_with_map.cc:904
void fatal(const char *str,...)
Definition: output.cc:96

References _chrsmFirstLocusPosition, _lociLookupTable, _locPositionsPerTrait, _nTrait, _numChromosome, _numChrsmPerTrait, _numLociPerChrsmPerTrait, _numLociPerTrait, _perChrsmLength, _recombinationRate, _recombLength, _recPositions, _resolution, _totalLength, _totalNumLoci, _totRecombEventsMean, _traits, fatal(), rescaleMap(), reset_tables(), and setLookupTable().

Referenced by TTProtoWithMap::registerGeneticMap().

+ Here is the caller graph for this function:

◆ clear()

void GeneticMap::clear ( )
647{
648 reset_tables();
649 _traits.clear();
650 _nTrait = 0;
651}

References _nTrait, _traits, and reset_tables().

Referenced by IndFactory::clearPrototype().

+ Here is the caller graph for this function:

◆ getFirstRecPosition()

vector< bool > & GeneticMap::getFirstRecPosition ( sex_t  SEX)
inline

Returns the vector of the first chromosome position for recombination, used for all traits.

164 { return _chrsmFirstRecombPosition[SEX]; }
vector< bool > _chrsmFirstRecombPosition[2]
Two vectors holding the starting copy of each chromosome to use when creating the two gametes that ar...
Definition: ttrait_with_map.h:79

References _chrsmFirstRecombPosition.

Referenced by TProtoBDMI::inherit(), TProtoDeletMutations_bitstring::inherit_low(), TProtoQuanti::inherit_low(), and TProtoNeutralGenes::inherit_low().

+ Here is the caller graph for this function:

◆ getGeneticMap()

bool GeneticMap::getGeneticMap ( trait_t  trait,
double **  table,
unsigned int  table_length 
)
875{
876 map< trait_t, unsigned int>::const_iterator ITER = _traits.find(trait);
877
878 if(ITER == _traits.end())
879 return(error("failed to find trait \"%s\" in genetic map"), trait.c_str());
880
881 unsigned int idx = ITER->second;
882
883 assert(table_length == _numLociPerTrait[idx]);
884
885 unsigned int chr = 0, loc_cnt = _numLociPerChrsmPerTrait[idx][0];
886
887 for(unsigned int i = 0; i < table_length; ++i) {
888
889 if(i >= loc_cnt) {
890 chr++;
891 loc_cnt += _numLociPerChrsmPerTrait[idx][chr];
892 }
893
894 table[i][0] = chr;
895 table[i][1] = _locPositionsPerTrait[idx][i]*_resolution;
896
897 }
898
899 return true;
900}
int error(const char *str,...)
Definition: output.cc:77

References _locPositionsPerTrait, _numLociPerChrsmPerTrait, _numLociPerTrait, _resolution, _traits, and error().

Referenced by TTNeutralGenesFH::write_PLINK(), and TTQuantiFH::write_PLINK().

+ Here is the caller graph for this function:

◆ getLocusPositionTable()

unsigned int * GeneticMap::getLocusPositionTable ( trait_t  trait)
inline
167 {return _locPositionsPerTrait[ _traits[ trait ] ];}

References _locPositionsPerTrait, and _traits.

◆ getRecLoci()

vector< unsigned int > & GeneticMap::getRecLoci ( sex_t  SEX,
unsigned int  trait 
)
inline

Returns a vector of the loci where crossing-overs take place.

158 {
159 return _recPositions[SEX][trait];
160 }

References _recPositions.

Referenced by TProtoBDMI::inherit(), TProtoDeletMutations_bitstring::inherit_low(), TProtoQuanti::inherit_low(), and TProtoNeutralGenes::inherit_low().

+ Here is the caller graph for this function:

◆ getResolution()

double GeneticMap::getResolution ( )
inline
106{return _resolution;}

References _resolution.

Referenced by TTProtoWithMap::recordRandomMap().

+ Here is the caller graph for this function:

◆ recombine()

void GeneticMap::recombine ( sex_t  SEX)

Called by TTProtoWithMap::recombine twice to create the two gametes necessary for the creation of a new individual.

This function sets the location of the x-over on the chromosome map and records the loci positions at which x-over happen for each trait separately. The positions are stored in GeneticMap::_recPositions, which are then passed to the trait-specific inheritance functions when setting up the genetics of the traits in new individuals.

Parameters
SEXthe origin of the gamete, whether from the father or the mother
1049{
1050 unsigned int nbRec = 0;
1051
1052
1053 //empty the array of x-over positions
1054 for (unsigned int t = 0; t < _nTrait; t++) {
1055 _recPositions[SEX][t].clear();
1056 }
1057
1058 //create a gamete:
1059
1060 //draw the number of x-over depending on the total map size
1061 nbRec = (unsigned int)RAND::Poisson( _totRecombEventsMean );
1062
1063// nbRec = (unsigned int)RAND::Binomial( _recombinationRate , _totalLength );
1064// call to Poisson() is faster
1065
1066// cout << "\n@@@ GeneticMap::recombine( SEX = "<<SEX<<" nbRec = "<<nbRec<<")"<<endl;
1067
1068 //adjust
1069 if(nbRec > _totalNumLoci - 1) nbRec = _totalNumLoci - 1;
1070
1071 //reset the list of recombination events.
1072 _junctions.resize(nbRec);
1073
1074 //now determine where the x-over will take place
1075 if(nbRec > 0) {
1076
1077 auto drawRecPosition = [this](unsigned int& n){n = (unsigned int)(RAND::Uniform()*_recombLength);};
1078// auto print = [this](const unsigned int& n){cout<<" "<<n;};
1079
1080 //draw the positions
1081 for_each(_junctions.begin(), _junctions.end(), drawRecPosition);
1082
1083 //sort them
1084 sort(_junctions.begin(), _junctions.end());
1085
1086// cout<< " --- sorted junctions ("<< _junctions.size()<<"): ";
1087// for_each(_junctions.cbegin(), _junctions.cend(), print);
1088
1089 //remove the duplicates (they cancel each other):
1090// unique(_junctions.begin(), _junctions.end());
1091
1092// cout<<"\n --- after removing duplicates ("<< _junctions.size()<<"): ";
1093// for_each(_junctions.cbegin(), _junctions.cend(), print);
1094
1095
1096 //the (automatically sorted) table of < locus number, num of x-over at locus position >
1097 map< unsigned int, unsigned int > cumul_rec_positions;
1098
1099 //the iterator to read within the map
1100 map< unsigned int, unsigned int>::const_iterator recIter;
1101
1102// cout<<"\n --- computing rec map position: \n";
1103 //set up trait recombination spots, more than one x-over may happen b/n two loci of a given trait
1104 //two consecutive x-overs b/n two loci cancel each other
1105 //we need to count the number of consecutive x-overs for each trait separately
1106 //the use of a map to store the x-over positions insures that they will be sorted
1107
1108 nbRec = _junctions.size();
1109
1110 for (unsigned int t = 0; t < _nTrait; t++) {
1111
1112 cumul_rec_positions.clear();
1113
1114// cout<<" trait "<< t << endl;
1115
1116 for(unsigned int i = 0, hit, num_interleaved_xover; i < nbRec; ++i) {
1117
1118
1119 // RAND::Uniform() : draw position of x-over
1120 // _totalLength+1 : the +1 is necessary because the lookupTable (the map) starts with position 0
1121 // position 0 should never be drawn as it would suppress recombination b/n two first loci
1122 // it is safe so because the lookupTable has _totalLength +1 elements
1123 // _lociLookupTable : gives the locus to the right of that 'hit' position
1124
1125 size_t pos = _junctions[i];
1126
1127 hit = _lociLookupTable[t][pos];
1128
1129// cout<< " @"<<pos<<" -> loc "<< hit <<"\n";
1130
1131 num_interleaved_xover = cumul_rec_positions[hit];
1132 //returns 0 if position hit was not yet recorded
1133
1134 cumul_rec_positions[hit] = ++num_interleaved_xover;
1135 //indicates that one more x-over happened b/n two loci
1136
1137 }
1138// cout<<" recording rec locus position: ";
1139 // record only the positions at which an odd number of x-over happened:
1140 for (recIter = cumul_rec_positions.begin(); recIter != cumul_rec_positions.end(); recIter++) {
1141
1142 if(recIter->second % 2) {
1143
1144 _recPositions[SEX][t].push_back(recIter->first); //add locus position
1145
1146// cout<<recIter->first<<", ";
1147 }
1148 }
1149// cout<<endl;
1150 }// end_for_traits
1151
1152 } // end_if_nbRec
1153
1154 // we always add the last position possible, to avoid returning an empty vector
1155 for (unsigned int t = 0; t < _nTrait; t++) {
1156 _recPositions[SEX][t].push_back(_numLociPerTrait[t]);
1157 }
1158
1159 //set the starting point for each chromosome randomly (which chromosome copy to use first)
1160
1161 _chrsmFirstRecombPosition[SEX].assign(_numChromosome, 0); //re-initialise the array
1162
1163 for(unsigned int c = 0; c < _numChromosome; ++c) {
1165 }
1166
1167}
vector< unsigned int > _junctions
A vector to store the position of the recombination events.
Definition: ttrait_with_map.h:82
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:220
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:125
static bool RandBool()
Returns a random boolean.
Definition: Uniform.h:163

References _chrsmFirstRecombPosition, _junctions, _lociLookupTable, _nTrait, _numChromosome, _numLociPerTrait, _recombLength, _recPositions, _totalNumLoci, _totRecombEventsMean, RAND::Poisson(), RAND::RandBool(), and RAND::Uniform().

Referenced by TTProtoWithMap::recombine().

+ Here is the caller graph for this function:

◆ registerIndForRecombine()

bool GeneticMap::registerIndForRecombine ( unsigned long  ID)
inline

Called by TTProtoWithMap::recombine with individual ID passed down from Individual::recombine.

Returns false when already called by the same individual. The function is called for each trait separately but recombination must be computed once per individual only. It is so because some functions create new individual's trait separately, for instance in breed_selection when traits under viability selection are created before neutral traits.

Parameters
IDthe ID number of the individual calling the recombination function
142 {
143 if (ID == _currentIndividual) return false;
144 else _currentIndividual = ID;
145 return true;
146 }

References _currentIndividual.

Referenced by TTProtoWithMap::recombine().

+ Here is the caller graph for this function:

◆ rescaleMap()

void GeneticMap::rescaleMap ( double  val)
905{
906 assert(val < _resolution);
907
908 double ratio = _resolution/val; //it is assumed that val < _resolution
909
910 _resolution = val;
911
912 //we have to update all positions
913 for (unsigned int i = 0; i < _nTrait; ++i) {
914 for (unsigned int j = 0; j < _numLociPerTrait[i]; ++j) {
915 _locPositionsPerTrait[i][j] *= ratio;
916 }
917 }
918
919 for (unsigned int i = 0; i < _numChromosome; ++i) {
920 _perChrsmLength[i] *= ratio;
921 _chrsmFirstLocusPosition[i] *= ratio;
922 }
923
924 _totalLength *= ratio;
926
927 //reset all lookup tables, the total length has changed!
928 for (unsigned int i = 0; i < _nTrait; ++i) setLookupTable(i);
929}

References _chrsmFirstLocusPosition, _locPositionsPerTrait, _nTrait, _numChromosome, _numLociPerTrait, _perChrsmLength, _resolution, _totalLength, _totRecombEventsMean, and setLookupTable().

Referenced by addTrait().

+ Here is the caller graph for this function:

◆ reset_tables()

void GeneticMap::reset_tables ( )
1173{
1174 if(_perChrsmLength != NULL) delete [] _perChrsmLength;
1175 _perChrsmLength = NULL;
1176
1179
1180 _numChrsmPerTrait.clear();
1181 _numLociPerTrait.clear();
1182
1183 for (unsigned int i = 0; i < _lociLookupTable.size(); ++i) {
1184 if(_lociLookupTable[i] != NULL) delete [] _lociLookupTable[i];
1185 else error("GeneticMap::reset_tables::found null pointer in _lociLookupTable\n");
1186 }
1187 _lociLookupTable.clear();
1188
1189 for (unsigned int i = 0; i < _numLociPerChrsmPerTrait.size(); ++i) {
1190 if(_numLociPerChrsmPerTrait[i] != NULL) delete [] _numLociPerChrsmPerTrait[i];
1191 else error("GeneticMap::reset_tables::found null pointer in _numLociPerChrsmPerTrait\n");
1192 }
1194
1195 for (unsigned int i = 0; i < _locPositionsPerTrait.size(); ++i) {
1196 if(_locPositionsPerTrait[i] != NULL) delete [] _locPositionsPerTrait[i];
1197 else error("GeneticMap::reset_tables::found null pointer in _locPositionsPerTrait\n");
1198 }
1199 _locPositionsPerTrait.clear();
1200
1201 _recPositions[0].clear();
1202 _recPositions[1].clear();
1203
1204 _chrsmFirstRecombPosition[0].clear();
1205 _chrsmFirstRecombPosition[1].clear();
1206
1207}

References _chrsmFirstLocusPosition, _chrsmFirstRecombPosition, _lociLookupTable, _locPositionsPerTrait, _numChrsmPerTrait, _numLociPerChrsmPerTrait, _numLociPerTrait, _perChrsmLength, _recPositions, and error().

Referenced by addTrait(), clear(), and ~GeneticMap().

+ Here is the caller graph for this function:

◆ setLookupTable()

void GeneticMap::setLookupTable ( unsigned int  idx)

Bbuilds the lookup table for each trait.

A lookup table maps a chromosomal position to a locus so that when a x-over is placed at a given map position, the corresponding locus can be directly found. The size of the lookup table depends on the _totalLength and _resolution of the map. The lookup table for a 1M map at the 0.01 cM scale will have 10,000 elements.

Parameters
idxthe index of the trait in the table (stored in the trait prototype)

!!! +1 needed here because map must start with 0 !!!!

!!! this means mumLocus may be returned, adjust _recPositions accordingly !!!!

934{
935 if(_lociLookupTable[idx] != NULL) delete [] _lociLookupTable[idx];
936
937 _lociLookupTable[idx] = new unsigned int [_totalLength +1];
939
940 if(_lociLookupTable[idx] == NULL)
941 fatal("GeneticMap::set lookup table: memory exhausted?\n");
942
943 //------------------------------------------------------------------------
944 int pre_offset = 0, post_offset = 0, offset;
945// cout<<"\n-- Genetic map #"<<idx<<":"<<endl;
946
947 for (unsigned int loc = 0, pos = 0, stride = 0,
948 c = 0; c < _numChromosome; ++c) {
949
950// cout<<"Chrm "<<c+1<<" ("<<_perChrsmLength[c]<<" ["<<_resolution<<"cM]):\n"<<"{ ";
951 if(c > 0) loc = stride;
952
953 stride += _numLociPerChrsmPerTrait[idx][c];
954
955 //the map must start with position zero for the first locus with lowest position
956 pre_offset = -_chrsmFirstLocusPosition[c];
957
958 //chromosomes are contiguous
959 offset = pre_offset + post_offset;
960
961 for(; loc < stride && loc < _numLociPerTrait[idx]; ++loc)
962 {
963
964 while (pos <= ( _locPositionsPerTrait[idx][loc] + offset) &&
965 ((int)pos - post_offset) <= (int)_perChrsmLength[c])
966 //table has 1 more elmnt, hence "<="
967
968 _lociLookupTable[idx][pos++] = loc;
969
970// cout << loc<<":"<<pos-1<<"("<<(int)pos - post_offset<<"; tab["<<pos-1<<"]="<<_lociLookupTable[idx][pos-1]<<") ";
971
972 }
973
974 post_offset += _perChrsmLength[c];
975
976 while (pos < _totalLength && loc >= _numLociPerTrait[idx])
977 {
978
979 _lociLookupTable[idx][pos++] = _numLociPerTrait[idx];
981 //(i.e. must contain num loci + 1 elements)!!!!
982
983
984// cout << loc<<":"<<pos-1<<"(tab["<<pos-1<<"]="<<_lociLookupTable[idx][pos-1]<<") ";
985
986 }
987// cout << "}\n";
988 }
989
990
991#ifdef _DEBUG_
992 cout << "\nGeneticMap::setLookupTable ("<<idx<<")\n";
993
994 cout << "lookup table: [";
995 for(unsigned int i = 0; i < _totalLength+1 && i < 101; ++i) {
996 cout << _lociLookupTable[idx][i] << " ";
997 if( i == 100) cout << "|>100) ";
998 }
999 if(_totalLength >= 100) cout << "... last pos ("<<_totalLength<<") "<<_lociLookupTable[idx][_totalLength];
1000 cout << "] (truncated if > 100 positions)\n";
1001
1002 pre_offset = 0; post_offset = 0;
1003 for(unsigned int c = 0, stride = 0; c < _numChromosome; c++) {
1004
1005 cout<<"+++Chrm "<<c+1<<" ("<<_perChrsmLength[c]<<" ["<<_resolution<<"cM]):\n";
1006 cout << "Loc positions: [ ";
1007
1008 for(unsigned int i = 0; i < _numLociPerChrsmPerTrait[idx][c] && i < 101; ++i){
1009 if( i < 100 || !(i % 100) )
1010 cout << _locPositionsPerTrait[idx][i+stride] << " " ;
1011 if( i == 100) cout << "|>100) ";
1012 }
1013 cout << "]\n";
1014
1015 pre_offset = -_chrsmFirstLocusPosition[c];
1016 offset = pre_offset + post_offset;
1017
1018 cout << "lookup table: [ ";
1019
1020 for(unsigned int cnt=0, i = _chrsmFirstLocusPosition[c]+offset; i < _chrsmFirstLocusPosition[c]+_perChrsmLength[c]+offset && cnt < 100; ++i, ++cnt){
1021 cout << _lociLookupTable[idx][i] << " " ;
1022 }
1023 cout << "] (first 100 positions only)\n{position:locus} {";
1024
1025 for(unsigned int i = 0; i < _numLociPerChrsmPerTrait[idx][c]-1; ++i) {
1026 if( i < 100 || !(i % 100) ) cout<<_locPositionsPerTrait[idx][i+stride]+offset<<":"<<_lociLookupTable[idx][_locPositionsPerTrait[idx][i+stride]+offset]<<", ";
1027 if( i == 100) cout << "|>100) ";
1028 }
1029 cout<<_locPositionsPerTrait[idx][stride + _numLociPerChrsmPerTrait[idx][c]-1]+offset
1030 <<":"<<_lociLookupTable[idx][_locPositionsPerTrait[idx][stride + _numLociPerChrsmPerTrait[idx][c]-1]+offset]<<"}\n";
1031
1032 post_offset += _perChrsmLength[c];
1033 stride += _numLociPerChrsmPerTrait[idx][c];
1034 }
1035 cout<<endl;
1036// cout<<"Loc pos lookup table:"<<endl<<"{";
1037// for(unsigned int i = 0; i < _superChrsmLength; ++i)
1038// cout<<_recLociPositionTable[i]<<", ";
1039// cout<<"}"<<endl;
1040
1041 cout<<" Tot map length: "<<_totalLength<<endl;
1042 cout<<" Mean num recombination events: "<<_totRecombEventsMean<<endl;
1043#endif
1044}

References _chrsmFirstLocusPosition, _lociLookupTable, _locPositionsPerTrait, _numChromosome, _numLociPerChrsmPerTrait, _numLociPerTrait, _perChrsmLength, _resolution, _totalLength, _totRecombEventsMean, and fatal().

Referenced by addTrait(), and rescaleMap().

+ Here is the caller graph for this function:

◆ setResolution()

double GeneticMap::setResolution ( double  val)
inline
109 {
110 _resolution = (val < _resolution ? val : _resolution);
111 return _resolution;
112 }

References _resolution.

◆ unregisterTrait()

void GeneticMap::unregisterTrait ( trait_t  trait)
624{
625 map<trait_t, unsigned int>::iterator tIter;
626
627 tIter = _traits.find(trait);
628
629 if ( tIter != _traits.end() ) {
630
631// cout<<"GeneticMap::unregisterTrait::"<<tIter->first<<" (idx: "<<tIter->second<<")"<<endl;
632
633 _traits.erase(tIter);
634
635 //@TODO trait lookup tables must be erased as well
636
637// cout<<"GeneticMap::unregisterTrait::done\n";
638 }
639 else fatal("Genetic map::unregisterTrait: trait \"%s\" is not registered\n", trait.c_str());
640
641 _nTrait--;
642}

References _nTrait, _traits, and fatal().

Referenced by TTProtoWithMap::reset(), and TTProtoWithMap::unregisterFromGeneticMap().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _chrsmFirstLocusPosition

unsigned int* GeneticMap::_chrsmFirstLocusPosition
private

◆ _chrsmFirstRecombPosition

vector< bool > GeneticMap::_chrsmFirstRecombPosition[2]
private

Two vectors holding the starting copy of each chromosome to use when creating the two gametes that are used to create a new individual.

Referenced by getFirstRecPosition(), recombine(), and reset_tables().

◆ _currentIndividual

unsigned long GeneticMap::_currentIndividual
private

Referenced by registerIndForRecombine().

◆ _junctions

vector< unsigned int > GeneticMap::_junctions
private

A vector to store the position of the recombination events.

Referenced by recombine().

◆ _lociLookupTable

vector< unsigned int* > GeneticMap::_lociLookupTable
private

A list of tables that map the map position (cM) to a locus, for each trait.

The length of the table is the length of the genetic map, which depends on the map resolution (cM by default).

Referenced by addTrait(), recombine(), reset_tables(), and setLookupTable().

◆ _locPositionsPerTrait

vector< unsigned int* > GeneticMap::_locPositionsPerTrait
private

Vector containing the table of map position for the loci of each trait.

Positions are recorded according to the minimum map resolution as used in the lookup table.

Referenced by addTrait(), getGeneticMap(), getLocusPositionTable(), rescaleMap(), reset_tables(), and setLookupTable().

◆ _nTrait

unsigned int GeneticMap::_nTrait
private

Number of traits registered in the map.

Length of following arrays.

Referenced by addTrait(), clear(), recombine(), rescaleMap(), and unregisterTrait().

◆ _numChromosome

unsigned int GeneticMap::_numChromosome
private

◆ _numChrsmPerTrait

vector< unsigned int > GeneticMap::_numChrsmPerTrait
private

Vector of number of chromosomes for each trait.

Referenced by addTrait(), and reset_tables().

◆ _numLociPerChrsmPerTrait

vector< unsigned int* > GeneticMap::_numLociPerChrsmPerTrait
private

Vector containing a table of number of loci per chromosome for each trait.

Referenced by addTrait(), getGeneticMap(), reset_tables(), and setLookupTable().

◆ _numLociPerTrait

vector< unsigned int > GeneticMap::_numLociPerTrait
private

Vector of number of loci for each trait.

Referenced by addTrait(), getGeneticMap(), recombine(), rescaleMap(), reset_tables(), and setLookupTable().

◆ _perChrsmLength

unsigned int* GeneticMap::_perChrsmLength
private

◆ _recombinationRate

double GeneticMap::_recombinationRate
private

Referenced by addTrait().

◆ _recombLength

unsigned int GeneticMap::_recombLength
private

Referenced by addTrait(), and recombine().

◆ _recPositions

vector< vector < unsigned int > > GeneticMap::_recPositions[2]
private

Vector of tables containing, for each trait, the locus number at which x-overs happen.

Updated at each generation. There is matrix per sex/gamete with one trait per row and a variable number of elements in each row.

Referenced by addTrait(), getRecLoci(), recombine(), and reset_tables().

◆ _resolution

double GeneticMap::_resolution
private

◆ _totalLength

unsigned int GeneticMap::_totalLength
private

◆ _totalNumLoci

unsigned int GeneticMap::_totalNumLoci
private

Referenced by addTrait(), and recombine().

◆ _totRecombEventsMean

double GeneticMap::_totRecombEventsMean
private

◆ _traits

map< trait_t, unsigned int > GeneticMap::_traits
private

Table mapping trait type to its position index in the following tables.

Referenced by addTrait(), clear(), getGeneticMap(), getLocusPositionTable(), and unregisterTrait().


The documentation for this class was generated from the following files:

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR