// File: /workspace/github_cpp_makefile_star2_O3/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 $0x8d8, %rsp            # imm = 0x8D8
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rsi
leaq 0x24e97(%rip), %rdi     # 0x34a40
xorl %eax, %eax
callq 0x7060
leaq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x71d0
movq (%r14), %rdi
leaq 0x27104(%rip), %rsi     # 0x36cca
callq 0x7440
testq %rax, %rax
je 0x10189
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdx
callq 0x7590
testq %rax, %rax
je 0x101a8
movq %r14, 0x88(%rsp)
leaq 0x24eae(%rip), %rsi     # 0x34ab2
leaq 0xd0(%rsp), %rdi
leaq 0x14(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x7360
movslq (%r14), %r15
testq %r15, %r15
je 0x101bb
movl %r15d, 0x8(%rbx)
movq %r15, %rax
shlq $0x3, %rax
testl %r15d, %r15d
movq $-0x1, %r14
cmovnsq %rax, %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, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x68(%rbx), %r12
movq 0x70(%rbx), %r13
cmpq %r12, %r13
je 0xfca0
movq %r12, %r14
movq %r14, %rdi
callq 0x1ae3c
addq $0x30, %r14
cmpq %r13, %r14
jne 0xfc87
movq %r12, 0x70(%rbx)
movl 0x8(%rbx), %r15d
leaq 0x68(%rbx), %rax
movq %rax, 0x80(%rsp)
testl %r15d, %r15d
jle 0xfd30
xorl %r14d, %r14d
leaq 0x58(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0x50(%rsp), %r13
xorl %ebp, %ebp
movl $0x0, 0x50(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0xfcea
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0xfcf5
movq %r12, %rdi
movq %r13, %rdx
callq 0x1bcea
movl $0x0, 0x58(%rsp)
movq %r14, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movq %r15, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x19e2e
movq %r13, %rdi
callq 0x1ae3c
incl %ebp
cmpl 0x8(%rbx), %ebp
jl 0xfcc5
leaq 0x50(%rbx), %rax
movq %rax, 0x40(%rsp)
movb $0x1, %r12b
leaq 0x50(%rsp), %r13
xorl %r15d, %r15d
movslq 0x14(%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 0xfda8
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 0xfd88
addq $0x8, %rax
movq %rax, 0x50(%rsp)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1c1fc
testb $0x1, %r12b
movl $0x0, %r12d
jne 0xfd44
movl $0x0, 0xc(%rsp)
leaq 0xc0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x24d15(%rip), %rsi     # 0x34b01
leaq 0x24d1e(%rip), %rdx     # 0x34b11
leaq 0xb0(%rsp), %rdi
callq 0xa91c
leaq 0x38b01(%rip), %rsi     # 0x48908
leaq 0x50(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x1023e
movq 0x88(%rsp), %r12
leaq 0x2682e(%rip), %rsi     # 0x36656
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x7100
movl %eax, 0x10(%rsp)
movq (%r14), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xfe53
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0xfe70
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x7390
leaq 0xd0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x7590
testq %rax, %rax
je 0x10125
leaq 0x24c7d(%rip), %r15     # 0x34b12
leaq 0xd0(%rsp), %r13
leaq 0xc(%rsp), %r14
leaq 0x50(%rsp), %rbp
cmpb $0x3a, 0xd0(%rsp)
jne 0xff61
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
leaq 0x90(%rsp), %r8
leaq 0x48(%rsp), %r9
xorl %eax, %eax
callq 0x7360
cmpl $0x3, %eax
jg 0xfee9
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, 0x48(%rsp)
movslq 0xc(%rsp), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0xc(%rsp)
cmpl 0x14(%rsp), %eax
jg 0x10156
movsd 0x50(%rsp), %xmm0
movq 0x30(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x48(%rsp), %xmm0
movq 0x38(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x50(%rsp), %xmm0
addsd 0x40(%rbx), %xmm0
movsd %xmm0, 0x40(%rbx)
movsd 0x90(%rsp), %xmm1
movq 0x48(%rbx), %rax
movsd %xmm1, -0x8(%rax,%rsi,8)
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm2
leaq 0x24c36(%rip), %rdi     # 0x34b8b
movb $0x3, %al
callq 0x7060
jmp 0x100fa
movq %r13, %rdi
leaq 0x24c76(%rip), %rsi     # 0x34be1
leaq 0xac(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0xa0(%rsp), %r8
leaq 0x98(%rsp), %r9
xorl %eax, %eax
callq 0x7360
cmpl $0x4, %eax
jne 0x100fa
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movslq 0xc(%rsp), %rsi
leaq (%rsi,%rsi,2), %rax
shlq $0x4, %rax
movq 0x10(%rdi,%rax), %rcx
testq %rcx, %rcx
je 0xfff9
leaq (%rdi,%rax), %r8
addq $0x8, %r8
movsd 0x18(%rsp), %xmm0
movq %r8, %rdx
xorl %r9d, %r9d
ucomisd 0x20(%rcx), %xmm0
cmovbeq %rcx, %rdx
seta %r9b
movq 0x10(%rcx,%r9,8), %rcx
testq %rcx, %rcx
jne 0xffcb
cmpq %r8, %rdx
je 0xfff9
movsd 0x20(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x10115
movq %r12, %r13
movq %rbp, %r12
movq %r14, %rbp
movq %r15, %r14
addq %rax, %rdi
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x18(%rsp), %rsi
callq 0x19e5a
movsd 0x23fef(%rip), %xmm0    # 0x34018
cmpl $0x0, 0x10(%rsp)
je 0x10036
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movslq 0xc(%rsp), %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq 0x8(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0x19e5a
xorpd %xmm0, %xmm0
cmpl $0x0, 0x10(%rsp)
je 0x1007d
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0xc(%rsp), %rsi
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x1017b
movl (%rax,%rsi,4), %r15d
leaq (%rsi,%rsi,2), %rdi
shlq $0x4, %rdi
movq 0x80(%rsp), %rax
addq (%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0x19ec6
movl %r15d, (%rax)
movslq 0xc(%rsp), %rsi
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x1017b
incl (%rax,%rsi,4)
movq %r14, %r15
movq %rbp, %r14
movq %r12, %rbp
movq %r13, %r12
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x7590
testq %rax, %rax
jne 0xfea7
jmp 0x10125
leaq 0x24ad9(%rip), %rdi     # 0x34bf5
movb $0x1, %al
callq 0x7060
jmp 0x100fa
movq %r12, %rdi
callq 0x7370
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x10144
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x7180
leaq 0x249e6(%rip), %rcx     # 0x34b4d
movq %rcx, (%rax)
movq 0x37dc7(%rip), %rsi     # 0x47f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
leaq 0x25a44(%rip), %rdi     # 0x35bc6
xorl %eax, %eax
callq 0x7530
leaq 0x248d2(%rip), %rdi     # 0x34a62
callq 0x7560
movl $0x8, %edi
callq 0x7180
leaq 0x248d8(%rip), %rcx     # 0x34a7e
jmp 0x101cc
movl $0x8, %edi
callq 0x7180
leaq 0x248de(%rip), %rcx     # 0x34a97
jmp 0x101cc
movl $0x8, %edi
callq 0x7180
leaq 0x24901(%rip), %rcx     # 0x34acd
movq %rcx, (%rax)
movq 0x37d62(%rip), %rsi     # 0x47f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0x1021e
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x1021e
jmp 0x1021b
jmp 0x1021b
jmp 0x1021b
jmp 0x1021b
jmp 0x1021b
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x1ae3c
jmp 0x1021e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x10235
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq %rbx, %rdi
callq 0x76a0
nop