// File: melop[P]admixem/Individual.cpp
// Function: Individual::Individual(void*, char)
Individual::Individual(void * pPop, char nAncestryLabel) { //Initializing a founder

	this->_bDead = false;
	int nPopId = ((Population *)pPop)->GetPopId();
	this->_pPrevPop = NULL;
	this->_pPop = pPop;
	if (nPopId == 3) {
		throw "You cannot directly create hybrid founders.";
	}

	this->_nFatherId = 0; //founder, father unknown.
	this->_nMotherId = 0; //founder, mother unknown
	this -> _nId = nCurrIndividualId;
	nCurrIndividualId++;

	this->_nHaploidChrNum = SimulConfig.pMarkerConfig->GetHaploidChromosomeNum();
	this->_nTotalChrNum = 2* this->_nHaploidChrNum ;

	SimulConfig.pPhenotypeConfig->InitKeys(&this->_mpPhenotypes);
	SimulConfig.pPhenotypeConfig->InitKeys(&this->_mpEnvPhenotypes);

	map<double, double> * pMarkerInfo = SimulConfig.pMarkerConfig->GetMarkersInfoByPop(nPopId);
	vector< map<double , GeneProperties> > * pGenes = SimulConfig.pGeneConfig->GetMpGenes();

	//this->_pmMarkers = new map<double, Marker>[_nTotalChrNum];
	//this->_arrMarkers = new vector<Marker>* [_nTotalChrNum];
	//this->_pmGenes   = new map<double, Gene>[_nTotalChrNum];
	//this->_arrGenes = new vector<Gene>* [_nTotalChrNum];

	//fill individual genome with markers
	for (int i=0; i<this->_nTotalChrNum; i++) {

		int nCurrChrNum;
		if (i % 2 ==0) {
			nCurrChrNum = i / 2;
		}
		else {
			nCurrChrNum = (i-1) / 2;
		}

		//map<double, Marker> oMarkerOnChr;
		vector<Marker> oMarkerOnChr;
	
		for (map<double, double>::iterator it=pMarkerInfo[nCurrChrNum].begin(); it!=pMarkerInfo[nCurrChrNum].end(); ++it)
		{
			

			double nFrequency = it->second;

			Marker oMarker;
			oMarker.Ancestry = nAncestryLabel;

			/*
			if (UniformGen.Next() <= nFrequency) {
				oMarker.Allele = 'A';
			}
			else {
				oMarker.Allele = 'a';
			}
			*/
			oMarker.Allele = (UniformGen.Next() <= nFrequency)? 'A':'a';
			/*
			char nAbundantAllele = (nPopId==1)? 'A':'a';
			char nRareAllele = (nPopId==1)? 'a':'A'; // make sure these are ancestry informative.

			oMarker.Allele = (UniformGen.Next() <= nFrequency )? nAbundantAllele : nRareAllele;
			*/
			//oMarkerOnChr.insert(oMarkerOnChr.end(),pair<double, Marker>(it->first , oMarker));
			oMarkerOnChr.push_back(oMarker);
				
		}

		//this->_pmMarkers[i] = oMarkerOnChr;
		this->_arrMarkers.push_back(oMarkerOnChr);

		//map<double, Gene> oGeneOnChr;
		vector<Gene> oGeneOnChr;

		for (map<double, GeneProperties>::iterator it2=pGenes->at(nCurrChrNum).begin(); it2!=pGenes->at(nCurrChrNum).end(); ++it2) // genes
		{
			double nFrequency = (nPopId==1)? it2->second.DominantFreqPop1 : it2->second.DominantFreqPop2;
			Gene oGene;
			oGene.Ancestry = nAncestryLabel;
			/*
				if (i==19) {
						printf("%s\n", (it2->second.AlleleMode == GeneProperties::Hemizygous && (i % 2)==1)? "true":"false" );
					}
					*/
			if (it2->second.AlleleMode == GeneProperties::Hemizygous && (i % 2)==1 ) { // if this gene is hemizygous, then only need to randomize for the first chromosome.
				//if ( this->_pmGenes[i-1][it->first].Allele == it->second.DominantLabel ) { // if the homologous chrom. already has the dominant allele
					oGene.Allele = it2->second.RecessiveLabel; // make the current one recessive.
					oGene.Value = it2->second.RecessiveValue;

				//}
			}
			/*
			else if (UniformGen.Next() <= nFrequency) {
				oGene.Allele = it2->second.DominantLabel;
			}
			else 
			{
				oGene.Allele = it2->second.RecessiveLabel;
			}
			*/
			/*
			else {
				oGene.Allele = (rand() % 100 <= nFrequency * 100)?  it2->second.DominantLabel:it2->second.RecessiveLabel;
			}
			*/
			else {
				oGene.Allele = (UniformGen.Next()<= nFrequency)?  it2->second.DominantLabel:it2->second.RecessiveLabel;
				oGene.Value = (oGene.Allele == it2->second.DominantLabel)?  it2->second.DominantValue:it2->second.RecessiveValue;
				/*if ( nCurrChrNum == 9 ) {
					printf("something wrong.\n");
				}*/
			}

			//oGeneOnChr[it2->first] = oGene;
			//oGeneOnChr.insert(oGeneOnChr.end(),pair<double, Gene>(it2->first , oGene));
			oGeneOnChr.push_back(oGene);
		}

		//this->_pmGenes[i] = oGeneOnChr;
		this->_arrGenes.push_back(oGeneOnChr);

	}

	this->fnDeterminePhenotypes();
	this->fnDetermineSex();
	this->fnDetermineNumGametes();
	/*
	vector<vector<Marker> > vTempMarkers;
	vector<vector<Gene> > vTempGenes;
	this->GetGamete(vTempMarkers, vTempGenes);
	//printf("hello");
	*/

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
addq $0x50, %rdi
movq %rdi, -0x100(%rbp)
callq 0x2d7c0
movq -0xc8(%rbp), %rdi
subq $-0x80, %rdi
movq %rdi, -0xf8(%rbp)
callq 0x2d7c0
movq -0xc8(%rbp), %rdi
addq $0xb0, %rdi
movq %rdi, -0xf0(%rbp)
callq 0x2d7c0
movq -0xc8(%rbp), %rdi
addq $0xe0, %rdi
movq %rdi, -0xe8(%rbp)
callq 0x2d7c0
movq -0xc8(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq %rdi, -0xe0(%rbp)
callq 0x5b530
movq -0xc8(%rbp), %rdi
addq $0x128, %rdi            # imm = 0x128
movq %rdi, -0xd8(%rbp)
callq 0x5b550
movq -0xc8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq %rdi, -0xd0(%rbp)
callq 0x5b570
movq -0xc8(%rbp), %rax
movb $0x0, 0x2d(%rax)
movq -0x10(%rbp), %rdi
callq 0x6c530
movl %eax, -0xbc(%rbp)
jmp 0x55538
movq -0xc8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, -0x18(%rbp)
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x18(%rax)
cmpl $0x3, -0x18(%rbp)
jne 0x5559b
movl $0x8, %edi
callq 0x8230
movq %rax, %rdi
leaq 0x22a2e(%rip), %rax     # 0x77fa0
movq %rax, (%rdi)
movq 0x549cc(%rip), %rsi     # 0xa9f48
xorl %eax, %eax
movl %eax, %edx
callq 0x8820
jmp 0x55ab7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x55a5a
movq -0xc8(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, 0x8(%rax)
movq 0x54f30(%rip), %rcx     # 0xaa4e8
movq %rcx, 0x10(%rax)
movq 0x54f25(%rip), %rax     # 0xaa4e8
incq %rax
movq %rax, 0x54f1b(%rip)     # 0xaa4e8
leaq 0x55484(%rip), %rax     # 0xaaa58
movq (%rax), %rdi
callq 0x1ce90
movl %eax, -0x104(%rbp)
jmp 0x555e4
movq -0xc8(%rbp), %rsi
movl -0x104(%rbp), %eax
movl %eax, 0x44(%rsi)
movl 0x44(%rsi), %eax
addl %eax, %eax
movl %eax, 0x48(%rsi)
leaq 0x55455(%rip), %rax     # 0xaaa58
movq 0x10(%rax), %rdi
addq $0x50, %rsi
callq 0x21a70
jmp 0x55612
movq -0xc8(%rbp), %rsi
leaq 0x55438(%rip), %rax     # 0xaaa58
movq 0x10(%rax), %rdi
subq $-0x80, %rsi
callq 0x21a70
jmp 0x5562f
leaq 0x55422(%rip), %rax     # 0xaaa58
movq (%rax), %rdi
movl -0x18(%rbp), %esi
callq 0x20bf0
movq %rax, -0x110(%rbp)
jmp 0x5564a
movq -0x110(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq 0x553fc(%rip), %rax     # 0xaaa58
movq 0x18(%rax), %rdi
callq 0x26ce0
movq %rax, -0x118(%rbp)
jmp 0x5566e
movq -0x118(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0xc8(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x48(%rcx), %eax
jge 0x55a27
movl -0x3c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x556b3
movl -0x3c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
jmp 0x556c4
movl -0x3c(%rbp), %eax
subl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5b6b0
movq -0x30(%rbp), %rdi
movslq -0x40(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x5b6d0
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x40(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x2a2d0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x5b700
testb $0x1, %al
jne 0x55710
jmp 0x5578b
leaq -0x60(%rbp), %rdi
callq 0x5b730
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movb -0x11(%rbp), %al
movb %al, -0x72(%rbp)
leaq 0x55460(%rip), %rdi     # 0xaab90
callq 0x19e20
movsd %xmm0, -0x120(%rbp)
jmp 0x5573f
movsd -0x120(%rbp), %xmm1
movsd -0x70(%rbp), %xmm0
ucomisd %xmm1, %xmm0
setb %al
shlb $0x5, %al
orb $0x41, %al
movb %al, -0x71(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x72(%rbp), %rsi
callq 0x5b760
jmp 0x5576a
jmp 0x5576c
leaq -0x60(%rbp), %rdi
callq 0x38650
jmp 0x556e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x55a1c
movq -0xc8(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
leaq -0x58(%rbp), %rsi
callq 0x5b7d0
jmp 0x557a4
leaq -0x90(%rbp), %rdi
callq 0x5b840
movq -0x38(%rbp), %rdi
movslq -0x40(%rbp), %rsi
callq 0x5b860
movq %rax, -0x128(%rbp)
jmp 0x557c6
movq -0x128(%rbp), %rdi
callq 0x5b8a0
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x40(%rbp), %rsi
callq 0x5b860
movq %rax, -0x130(%rbp)
jmp 0x557ef
movq -0x130(%rbp), %rdi
callq 0x2cf40
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x5b8d0
testb $0x1, %al
jne 0x5581e
jmp 0x559dd
cmpl $0x1, -0x18(%rbp)
jne 0x5583f
leaq -0x98(%rbp), %rdi
callq 0x5b900
movsd 0x40(%rax), %xmm0
movsd %xmm0, -0x138(%rbp)
jmp 0x55858
leaq -0x98(%rbp), %rdi
callq 0x5b900
movsd 0x48(%rax), %xmm0
movsd %xmm0, -0x138(%rbp)
movsd -0x138(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movb -0x11(%rbp), %al
movb %al, -0xb8(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5b900
cmpl $0x0, 0x50(%rax)
jne 0x558e3
movl -0x3c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x558e3
leaq -0x98(%rbp), %rdi
callq 0x5b900
movb 0x29(%rax), %al
movb %al, -0xb7(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5b900
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x559b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5ba00
jmp 0x55a1c
leaq 0x552a6(%rip), %rdi     # 0xaab90
callq 0x19e20
movsd %xmm0, -0x140(%rbp)
jmp 0x558f9
movsd -0x140(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x55926
leaq -0x98(%rbp), %rdi
callq 0x5b900
movb 0x28(%rax), %al
movb %al, -0x141(%rbp)
jmp 0x5593b
leaq -0x98(%rbp), %rdi
callq 0x5b900
movb 0x29(%rax), %al
movb %al, -0x141(%rbp)
movb -0x141(%rbp), %al
movb %al, -0xb7(%rbp)
movsbl -0xb7(%rbp), %eax
movl %eax, -0x148(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5b900
movq %rax, %rcx
movl -0x148(%rbp), %eax
movsbl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jne 0x5598c
leaq -0x98(%rbp), %rdi
callq 0x5b900
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
jmp 0x559a5
leaq -0x98(%rbp), %rdi
callq 0x5b900
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
movsd -0x150(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5b920
jmp 0x559ca
jmp 0x559cc
leaq -0x98(%rbp), %rdi
callq 0x50220
jmp 0x557d9
movq -0xc8(%rbp), %rdi
addq $0x128, %rdi            # imm = 0x128
leaq -0x90(%rbp), %rsi
callq 0x5b990
jmp 0x559f9
leaq -0x90(%rbp), %rdi
callq 0x5ba00
leaq -0x58(%rbp), %rdi
callq 0x5ba60
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x55680
leaq -0x58(%rbp), %rdi
callq 0x5ba60
jmp 0x55a5a
movq -0xc8(%rbp), %rdi
callq 0x55ac0
jmp 0x55a35
movq -0xc8(%rbp), %rdi
callq 0x56800
jmp 0x55a43
movq -0xc8(%rbp), %rdi
callq 0x568d0
jmp 0x55a51
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x5b590
movq -0xd8(%rbp), %rdi
callq 0x5b5f0
movq -0xe0(%rbp), %rdi
callq 0x5b650
movq -0xe8(%rbp), %rdi
callq 0x2d860
movq -0xf0(%rbp), %rdi
callq 0x2d860
movq -0xf8(%rbp), %rdi
callq 0x2d860
movq -0x100(%rbp), %rdi
callq 0x2d860
movq -0x20(%rbp), %rdi
callq 0x8860
nopw (%rax,%rax)