// File: /workspace/github_cpp_makefile_star2_O1/melop[P]admixem/config.cpp
// Function: MarkerConfigurations::LoadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void MarkerConfigurations::LoadFromFile(string szConfigFile) {

	FILE *pConfigFile;

	printf("Start loading marker file %s ...\n", szConfigFile.c_str());

	_szConfigFilename = szConfigFile; // save name of config file

	pConfigFile = fopen(szConfigFile.c_str(), "r");

	if (!pConfigFile) {
		printf("%s\n", "Cannot open marker file ...");
		throw "Cannot open marker file!";
	}


	char szBuffer[2048];

	if (fgets(szBuffer, 2048, pConfigFile) == NULL) {
		throw "Error reading marker file!";
	}

	int nHaploidChrNum;

	sscanf(szBuffer, "HaploidChromosomeNum =  %d", &nHaploidChrNum); // Read in chromosome number

	if (nHaploidChrNum == NULL) {
		throw "Haploid Chromosome Number in marker file incorrect!";
	}

	this->_nHaploidChrNum = nHaploidChrNum; //save the haploid chromosome number

	//initialize arrays:
	this->pChrLen = new double[nHaploidChrNum];
	this->pPaternalTransBias = new double[nHaploidChrNum];
	this->pCentromerePos = new double[nHaploidChrNum];

	vector<int> vIndexCounters; //init index counter
	vMarkerIndex.clear();

	for (int i=0;i<_nHaploidChrNum;i++) {
		vIndexCounters.push_back(0);
		map<double, int> oTempMap;
		vMarkerIndex.push_back(oTempMap);
	}
	
	for (int nPop=0;nPop<2;nPop++) 
	{
		vMarkerFreq.insert( vMarkerFreq.end(), new map<double, double>[nHaploidChrNum] );
	}

	int nCurrChr = 0;

	int nNum;
	double nAbsPos, nFreqPop1, nFreqPop2;
	//char sAbsPos[100], sFreqPop1[100], sFreqPop2[100];
	bool bIgnoreMarkerFreq = (SimulConfig.GetConfig("IgnoreMarkerFreq") == "yes");

	while(fgets(szBuffer, 2048, pConfigFile) != NULL) 
	{

		//sscanf(szBuffer, "%d %*f %f %f %f", &nNum, &nAbsPos, &nFreqPop1, &nFreqPop2);

		if (szBuffer[0] == ':') { // it's a chr definition line

			double nChrLen;
			double nCentromerePos;
			double nPaternalTransBias; // when transmitting gametes, the bias towards paternal chr. 0.5 is neutral, 0 is maternal

			int nItemsRead = sscanf(szBuffer, ":chr %d len = %lf centromere = %lf paternaltransbias = %lf", &nCurrChr, &nChrLen , &nCentromerePos, &nPaternalTransBias);

			if (nItemsRead < 4) {
				nPaternalTransBias = 0.5;
			}
			//nCurrChr = (int)strtol( sCurrChr , NULL, 10);
			//nChrLen = strtod( sChrLen, NULL );
			//nCentromerePos = strtod( sCentromerePos , NULL);

			
			nCurrChr--; 

			//printf("%d %f %f", nCurrChr, nChrLen , nCentromerePos );

			if (nCurrChr > nHaploidChrNum) 
			{
				throw "The chromosome number is larger than total chromosome number!";
			}

			// save chromosome information:
			pChrLen[nCurrChr] = nChrLen;
			pPaternalTransBias[nCurrChr] = nPaternalTransBias;
			nTotalGenomeLen += nChrLen;
			pCentromerePos[nCurrChr] = nCentromerePos;
			printf("Reading markers on chromosome %d , length=%f, centromere=%f paternalTransBias=%f ...\n", (nCurrChr + 1), nChrLen, nCentromerePos,nPaternalTransBias);

		}
		else if (sscanf(szBuffer, "%d %*lf %lf %lf %lf", &nNum, &nAbsPos, &nFreqPop1, &nFreqPop2)==4) {

			if (vMarkerFreq[0][nCurrChr].find(nAbsPos) == vMarkerFreq[0][nCurrChr].end()) { // make sure the position is not occupied
				
				vMarkerFreq[0][nCurrChr][nAbsPos] = bIgnoreMarkerFreq? 1: nFreqPop1;
				vMarkerFreq[1][nCurrChr][nAbsPos] = bIgnoreMarkerFreq? 0: nFreqPop2;

				vMarkerIndex[nCurrChr][nAbsPos] = vIndexCounters.at(nCurrChr);
				vIndexCounters.at(nCurrChr) += 1;
			}
			else
			{

				printf("Warning: repetitive definition of position  %f on chromosome %d found. second one will be ignored\n", nAbsPos, nCurrChr );
			}
		}

		else {

		}

		
		/*
		if (sscanf_s(szBuffer, "%d %*f %f %f %f", &nNum, &nAbsPos, &nFreqPop1, &nFreqPop2)!=4) { // if this is not a data line try parsing: :chr 1	len = 3.3e+007	centromere = 1.1e+007
			
			double nChrLen;
			double nCentromerePos;
			
			//sscanf(szBuffer, ":chr %d	len = %f	centromere = %f", &nCurrChr, &nChrLen , &nCentromerePos);

			if (sscanf_s(szBuffer, ":chr %d len = %f centromere = %f", &nCurrChr, &nChrLen , &nCentromerePos) != 3) {
				continue;
			}

			nCurrChr--; 

			//printf("%d %f %f", nCurrChr, nChrLen , nCentromerePos );

			if (nCurrChr > nHaploidChrNum) 
			{
				throw "The chromosome number is larger than total chromosome number!";
			}

			// save chromosome information:
			pChrLen[nCurrChr] = nChrLen;
			pCentromerePos[nCurrChr] = nCentromerePos;

		}
		else { //this is a dataline!
			vMarkerFreq[0][nCurrChr][nAbsPos] = nFreqPop1;
			vMarkerFreq[1][nCurrChr][nAbsPos] = nFreqPop2;
			//printf("%d %f %f %f\n", nNum, nAbsPos, nFreqPop1, nFreqPop2 );
		}

		*/

	}

	fclose(pConfigFile);

	//printf("%f",vMarkerFreq[0][0].begin()->first );

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8c8, %rsp            # imm = 0x8C8
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rsi
leaq 0x26007(%rip), %rdi     # 0x35a20
xorl %eax, %eax
callq 0x7060
leaq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x71d0
movq (%r14), %rdi
leaq 0x2828c(%rip), %rsi     # 0x37cc2
callq 0x7440
testq %rax, %rax
je 0xffca
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdx
callq 0x7590
testq %rax, %rax
je 0xffe9
movq %r14, 0x70(%rsp)
leaq 0x26021(%rip), %rsi     # 0x35a92
leaq 0xc0(%rsp), %rdi
leaq 0xc(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x7360
movslq (%r14), %rax
testq %rax, %rax
je 0xfffc
movl %eax, 0x8(%rbx)
movq %rax, %rcx
shlq $0x3, %rcx
testl %eax, %eax
movq $-0x1, %r14
cmovnsq %rcx, %r14
movq %r14, %rdi
callq 0x7030
movq %rax, 0x30(%rbx)
movq %r14, %rdi
callq 0x7030
movq %rax, 0x38(%rbx)
movq %r14, %rdi
callq 0x7030
movq %rax, 0x48(%rbx)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x68(%rbx), %r15
movq 0x70(%rbx), %r12
cmpq %r15, %r12
je 0xfb07
movq %r15, %r14
movq %r14, %rdi
callq 0x1ad32
addq $0x30, %r14
cmpq %r12, %r14
jne 0xfaf2
movq %r15, 0x70(%rbx)
leaq 0x68(%rbx), %rax
movq %rax, 0x78(%rsp)
cmpl $0x0, 0x8(%rbx)
jle 0xfb92
xorl %r14d, %r14d
leaq 0x48(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x40(%rsp), %r13
xorl %ebp, %ebp
movl $0x0, 0x40(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xfb4f
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xfb5a
movq %r12, %rdi
movq %r13, %rdx
callq 0x1bc0c
movl $0x0, 0x48(%rsp)
movq %r14, 0x50(%rsp)
movq %r15, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq %r14, 0x68(%rsp)
movq 0x78(%rsp), %rdi
movq %r13, %rsi
callq 0x19db0
movq %r13, %rdi
callq 0x1ad32
incl %ebp
cmpl 0x8(%rbx), %ebp
jl 0xfb2a
leaq 0x50(%rbx), %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
movslq 0xc(%rsp), %r14
movq %r14, %rax
movl $0x30, %ecx
mulq %rcx
seto %cl
orq $0x8, %rax
movq 0x58(%rbx), %rbp
testb %cl, %cl
movq $-0x1, %rcx
cmovneq %rcx, %rax
movq %rax, %rdi
callq 0x7030
movq %r14, (%rax)
testl %r14d, %r14d
je 0xfc00
leaq 0x10(%rax), %rcx
shlq $0x4, %r14
leaq (%r14,%r14,2), %rdx
movl $0x0, (%rcx)
movq %r15, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %r15, 0x20(%rcx)
addq $0x30, %rcx
addq $-0x30, %rdx
jne 0xfbe0
addq $0x8, %rax
movq %rax, 0x40(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
callq 0x1c12c
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
je 0xfb9c
movl $0x0, 0x4(%rsp)
leaq 0xb0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x25e9d(%rip), %rsi     # 0x35ae1
leaq 0x25ea6(%rip), %rdx     # 0x35af1
leaq 0xa0(%rsp), %rdi
callq 0xa886
leaq 0x39ca9(%rip), %rsi     # 0x49908
leaq 0x40(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x1007e
leaq 0x279fe(%rip), %rsi     # 0x37676
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x7100
movl %eax, 0x8(%rsp)
movq (%r14), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xfca3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
je 0xfcc0
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x7390
leaq 0xc0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq 0x70(%rsp), %r15
movq %r15, %rdx
callq 0x7590
testq %rax, %rax
je 0xff66
leaq 0xc0(%rsp), %r13
movabsq $0x3fe0000000000000, %r14 # imm = 0x3FE0000000000000
leaq 0x38(%rsp), %r12
leaq 0x88(%rsp), %rbp
cmpb $0x3a, 0xc0(%rsp)
jne 0xfdc3
movq %r13, %rdi
leaq 0x25dd8(%rip), %rsi     # 0x35af2
leaq 0x4(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0x7360
cmpl $0x3, %eax
jg 0xfd42
movq %r14, 0x30(%rsp)
movslq 0x4(%rsp), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0x4(%rsp)
cmpl 0xc(%rsp), %eax
jg 0xff97
movsd 0x40(%rsp), %xmm0
movq 0x30(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x30(%rsp), %xmm0
movq 0x38(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x40(%rsp), %xmm0
addsd 0x40(%rbx), %xmm0
movsd %xmm0, 0x40(%rbx)
movsd 0x80(%rsp), %xmm0
movq 0x48(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x40(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x25db4(%rip), %rdi     # 0x35b6b
movb $0x3, %al
callq 0x7060
jmp 0xff4d
movq %r13, %rdi
leaq 0x25df4(%rip), %rsi     # 0x35bc1
leaq 0x9c(%rsp), %rdx
movq %r12, %rcx
leaq 0x90(%rsp), %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0x7360
cmpl $0x4, %eax
jne 0xff4d
movq 0x50(%rbx), %rax
movq (%rax), %rdi
movslq 0x4(%rsp), %rsi
leaq (%rsi,%rsi,2), %rax
shlq $0x4, %rax
leaq (%rdi,%rax), %rcx
addq $0x8, %rcx
movq 0x8(%rcx), %r8
movq %rcx, %rdx
testq %r8, %r8
je 0xfe3f
movsd 0x38(%rsp), %xmm0
movq %rcx, %rdx
xorl %r9d, %r9d
ucomisd 0x20(%r8), %xmm0
cmovbeq %r8, %rdx
seta %r9b
movq 0x10(%r8,%r9,8), %r8
testq %r8, %r8
jne 0xfe24
movq %rcx, %r8
cmpq %rcx, %rdx
je 0xfe5a
movsd 0x20(%rdx), %xmm0
ucomisd 0x38(%rsp), %xmm0
movq %rcx, %r8
ja 0xfe5a
movq %rdx, %r8
cmpq %rcx, %r8
je 0xfe78
movsd 0x38(%rsp), %xmm0
leaq 0x25d69(%rip), %rdi     # 0x35bd5
movb $0x1, %al
callq 0x7060
jmp 0xff4d
addq %rax, %rdi
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r12, %rsi
callq 0x19ddc
movsd 0x2517e(%rip), %xmm0    # 0x35018
cmpl $0x0, 0x8(%rsp)
je 0xfea7
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x50(%rbx), %rax
movslq 0x4(%rsp), %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq 0x8(%rax), %rdi
movq %r12, %rsi
callq 0x19ddc
xorpd %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
je 0xfee8
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0x4(%rsp), %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0xffbc
movl (%rax,%rsi,4), %r15d
leaq (%rsi,%rsi,2), %rdi
shlq $0x4, %rdi
movq 0x78(%rsp), %rax
addq (%rax), %rdi
movq %r12, %rsi
callq 0x19e46
movl %r15d, (%rax)
movslq 0x4(%rsp), %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0xffbc
incl (%rax,%rsi,4)
movq 0x70(%rsp), %r15
movq %r13, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r15, %rdx
callq 0x7590
testq %rax, %rax
jne 0xfd02
movq %r15, %rdi
callq 0x7370
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xff85
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
addq $0x8c8, %rsp            # imm = 0x8C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x7180
leaq 0x25b85(%rip), %rcx     # 0x35b2d
movq %rcx, (%rax)
movq 0x38f86(%rip), %rsi     # 0x48f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
leaq 0x26c5e(%rip), %rdi     # 0x36c21
xorl %eax, %eax
callq 0x7530
leaq 0x25a71(%rip), %rdi     # 0x35a42
callq 0x7560
movl $0x8, %edi
callq 0x7180
leaq 0x25a77(%rip), %rcx     # 0x35a5e
jmp 0x1000d
movl $0x8, %edi
callq 0x7180
leaq 0x25a7d(%rip), %rcx     # 0x35a77
jmp 0x1000d
movl $0x8, %edi
callq 0x7180
leaq 0x25aa0(%rip), %rcx     # 0x35aad
movq %rcx, (%rax)
movq 0x38f21(%rip), %rsi     # 0x48f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r15, %rdi
je 0x1005f
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x1005f
jmp 0x1005c
jmp 0x1005c
jmp 0x1005c
jmp 0x1005c
jmp 0x1005c
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1ad32
jmp 0x1005f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x10076
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq %rbx, %rdi
callq 0x76a0