// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %ebp
movq %rsi, %r14
leaq 0x50(%rdi), %rax
movq %rax, 0x48(%rsp)
leaq 0x58(%rdi), %rax
xorl %ecx, %ecx
movl %ecx, 0x58(%rdi)
movq %rcx, 0x60(%rdi)
movq %rax, 0x68(%rdi)
movq %rax, 0x70(%rdi)
movq %rcx, 0x78(%rdi)
leaq 0x80(%rdi), %r15
leaq 0x88(%rdi), %rax
movl %ecx, 0x88(%rdi)
movq %rcx, 0x90(%rdi)
movq %rax, 0x98(%rdi)
movq %rax, 0xa0(%rdi)
movq %rcx, 0xa8(%rdi)
leaq 0xb8(%rdi), %rax
movl %ecx, 0xb8(%rdi)
movq %rcx, 0xc0(%rdi)
movq %rax, 0xc8(%rdi)
movq %rax, 0xd0(%rdi)
movq %rcx, 0xd8(%rdi)
leaq 0xe8(%rdi), %rax
movl %ecx, 0xe8(%rdi)
movq %rcx, 0xf0(%rdi)
movq %rax, 0xf8(%rdi)
movq %rax, 0x100(%rdi)
leaq 0x110(%rdi), %rax
movq %rax, 0x58(%rsp)
leaq 0x128(%rdi), %rax
movq %rax, 0x50(%rsp)
movb $0x0, 0x2d(%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x148(%rdi)
movupd %xmm0, 0x138(%rdi)
movupd %xmm0, 0x128(%rdi)
movupd %xmm0, 0x118(%rdi)
movq %rdi, (%rsp)
movupd %xmm0, 0x108(%rdi)
movq %rsi, %rdi
movq %r15, 0x70(%rsp)
callq 0x2e34a
movl %eax, %ebx
movq (%rsp), %rcx
movq $0x0, 0x20(%rcx)
movq %r14, 0x18(%rcx)
cmpl $0x3, %eax
je 0x222bc
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
movq 0x2740e(%rip), %rax     # 0x493a8
movq %rax, 0x10(%rcx)
incq %rax
movq %rax, 0x27400(%rip)     # 0x493a8
leaq 0x27959(%rip), %r14     # 0x49908
movq (%r14), %rdi
callq 0x1083a
movq (%rsp), %rcx
movl %eax, 0x44(%rcx)
addl %eax, %eax
movl %eax, 0x48(%rcx)
movq 0x10(%r14), %rdi
movq 0x48(%rsp), %rsi
callq 0x1402e
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x1402e
movq (%r14), %rdi
movl %ebx, %esi
callq 0x13442
movq %rax, 0x80(%rsp)
movq 0x18(%r14), %rdi
movq (%rsp), %r14
callq 0x1887a
cmpl $0x0, 0x48(%r14)
jle 0x2226c
movq %rax, %r15
xorl %eax, %eax
cmpl $0x1, %ebx
setne %al
leaq 0x60(,%rax,8), %r14
xorl %ebx, %ebx
movq %r14, 0x78(%rsp)
movl %ebp, %r14d
movl %ebx, 0xc(%rsp)
movl %ebx, %r12d
andl $-0x2, %r12d
shrl %r12d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq (%r12,%r12,2), %r13
shlq $0x4, %r13
movq 0x80(%rsp), %rax
movq 0x18(%rax,%r13), %rbx
leaq (%rax,%r13), %rbp
addq $0x8, %rbp
cmpq %rbp, %rbx
je 0x220d4
movsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rsp)
movb %r14b, 0x10(%rsp)
leaq 0x279c2(%rip), %rdi     # 0x49a40
callq 0xbaa8
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setb %al
shlb $0x5, %al
orb $0x41, %al
movb %al, 0x11(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x220b5
movzwl 0x10(%rsp), %eax
movw %ax, (%rsi)
addq $0x2, 0x38(%rsp)
jmp 0x220c4
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x26ca8
movq %rbx, %rdi
callq 0x7270
movq %rax, %rbx
cmpq %rbp, %rax
jne 0x22067
movq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2682e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%r15), %rcx
movq 0x8(%r15), %rdx
subq %rcx, %rdx
sarq $0x4, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %r12, %rdx
jbe 0x222ab
movl %r14d, %ebp
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
imulq %rsi, %rdx
cmpq %r12, %rdx
movq 0x78(%rsp), %r14
jbe 0x2229a
movq 0x18(%rcx,%r13), %rbx
addq %r13, %rax
addq $0x8, %rax
cmpq %rax, %rbx
je 0x2221c
movsd (%rbx,%r14), %xmm0
movsd %xmm0, 0x28(%rsp)
movb %bpl, 0x60(%rsp)
testb $0x1, 0xc(%rsp)
je 0x2217f
cmpl $0x0, 0x70(%rbx)
jne 0x2217f
movb 0x49(%rbx), %al
movb %al, 0x61(%rsp)
movl $0x58, %eax
jmp 0x221b2
leaq 0x278ba(%rip), %rdi     # 0x49a40
callq 0xbaa8
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %rbx, %rax
adcq $0x48, %rax
movb (%rax), %al
movb %al, 0x61(%rsp)
xorl %ecx, %ecx
cmpb 0x48(%rbx), %al
setne %cl
leaq 0x50(,%rcx,8), %rax
movsd (%rbx,%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x221db
movupd 0x60(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, 0x18(%rsp)
jmp 0x221ea
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x27064
movq %rbx, %rdi
callq 0x7270
movq %rax, %rbx
movq (%r15), %rax
movq 0x8(%r15), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r12, %rdx
ja 0x22143
jmp 0x2229a
movq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2685a
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movl 0xc(%rsp), %ebx
je 0x22246
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2225d
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
incl %ebx
movq (%rsp), %rax
cmpl 0x48(%rax), %ebx
jl 0x22021
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x22396
movq %rbx, %rdi
callq 0x23024
movq %rbx, %rdi
callq 0x230b4
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14980(%rip), %rdi     # 0x36c21
movq %r12, %rsi
xorl %eax, %eax
callq 0x7530
leaq 0x1496f(%rip), %rdi     # 0x36c21
movq %r12, %rsi
xorl %eax, %eax
callq 0x7530
movl $0x8, %edi
callq 0x7180
leaq 0x14b13(%rip), %rcx     # 0x36de0
movq %rcx, (%rax)
movq 0x26c61(%rip), %rsi     # 0x48f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
jmp 0x222e5
jmp 0x222e5
movq %rax, %rbx
jmp 0x22328
jmp 0x222f7
jmp 0x222f2
jmp 0x222f7
jmp 0x222f7
movq %rax, %rbx
jmp 0x22311
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x22311
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x22328
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq (%rsp), %r15
movq 0x140(%r15), %rdi
testq %rdi, %rdi
je 0x22347
movq 0x150(%r15), %rsi
subq %rdi, %rsi
callq 0x7390
leaq 0xe0(%r15), %r14
addq $0xb0, %r15
movq 0x50(%rsp), %rdi
callq 0x26792
movq 0x58(%rsp), %rdi
callq 0x267e0
movq %r14, %rdi
callq 0x1bb2c
movq %r15, %rdi
callq 0x1bb2c
movq 0x70(%rsp), %rdi
callq 0x1bb2c
movq 0x48(%rsp), %rdi
callq 0x1bb2c
movq %rbx, %rdi
callq 0x76a0
nop