// File: /workspace/github_cpp_makefile_star2_O2/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 0x1edff(%rip), %rdi     # 0x2d9f0
xorl %eax, %eax
callq 0x7050
leaq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x7220
movq (%r14), %rdi
leaq 0x2108c(%rip), %rsi     # 0x2fc9a
callq 0x74a0
testq %rax, %rax
je 0xf084
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdx
callq 0x75e0
testq %rax, %rax
je 0xf0a1
movq %r14, 0x60(%rsp)
leaq 0x1ee19(%rip), %rsi     # 0x2da62
xorl %ebp, %ebp
leaq 0xd0(%rsp), %rdi
leaq 0xc(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x73c0
movslq (%r14), %rax
testq %rax, %rax
je 0xf0b2
movl %eax, 0x8(%rbx)
movq %rax, %r14
shlq $0x3, %r14
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %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)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
leaq 0x68(%rbx), %r15
movq %r15, %rdi
callq 0x16e9c
leaq 0x38(%rsp), %r14
leaq 0x30(%rsp), %r13
cmpl 0x8(%rbx), %ebp
jge 0xed18
andl $0x0, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x18e10
andl $0x0, 0x38(%rsp)
andq $0x0, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq %r14, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x16ea4
movq %r13, %rdi
callq 0x17c40
incl %ebp
jmp 0xecd1
movq %r15, 0x68(%rsp)
leaq 0x50(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
pushq $0x30
popq %r12
leaq 0x30(%rsp), %r13
cmpl $0x2, %r15d
je 0xedac
movslq 0xc(%rsp), %r14
movq %r14, %rax
mulq %r12
seto %cl
orq $0x8, %rax
movq 0x58(%rbx), %rbp
testb %cl, %cl
pushq $-0x1
popq %rcx
cmovneq %rcx, %rax
movq %rax, %rdi
callq 0x7030
movq %r14, (%rax)
testl %r14d, %r14d
je 0xed8e
leaq 0x10(%rax), %rcx
imulq $0x30, %r14, %rdx
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
addq $0x30, %rcx
addq $-0x30, %rdx
jne 0xed6f
addq $0x8, %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x193cc
incl %r15d
jmp 0xed32
andl $0x0, 0x8(%rsp)
leaq 0x1ecf9(%rip), %rsi     # 0x2dab1
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x9512
leaq 0x36b67(%rip), %rsi     # 0x45938
leaq 0x30(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0xf118
movq 0x60(%rsp), %r12
movq 0x10(%rsp), %r15
leaq 0x20842(%rip), %rsi     # 0x2f636
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x954d
movb %al, 0x7(%rsp)
movq %r13, %r14
movq %r13, %rdi
callq 0x71a0
leaq 0xb0(%rsp), %rdi
callq 0x71a0
leaq 0xd0(%rsp), %rbp
leaq 0x80(%rsp), %r13
movq %rbp, %rdi
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdx
callq 0x75e0
testq %rax, %rax
je 0xf03a
cmpb $0x3a, 0xd0(%rsp)
jne 0xef00
movq %rbp, %rdi
leaq 0x1ec64(%rip), %rsi     # 0x2dac2
leaq 0x8(%rsp), %rdx
movq %r14, %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0x73c0
cmpl $0x3, %eax
jg 0xee8b
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, 0x20(%rsp)
movslq 0x8(%rsp), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0x8(%rsp)
cmpl 0xc(%rsp), %eax
jg 0xf061
movsd 0x30(%rsp), %xmm0
movq 0x30(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x20(%rsp), %xmm0
movq 0x38(%rbx), %rax
movsd %xmm0, -0x8(%rax,%rsi,8)
movsd 0x30(%rsp), %xmm0
addsd 0x40(%rbx), %xmm0
movsd %xmm0, 0x40(%rbx)
movsd 0x28(%rsp), %xmm1
movq 0x48(%rbx), %rax
movsd %xmm1, -0x8(%rax,%rsi,8)
movsd 0x30(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
leaq 0x1ec47(%rip), %rdi     # 0x2db3b
movb $0x3, %al
callq 0x7050
jmp 0xee2d
movq %rbp, %rdi
leaq 0x1ec87(%rip), %rsi     # 0x2db91
leaq 0x8c(%rsp), %rdx
movq %r13, %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
xorl %eax, %eax
callq 0x73c0
cmpl $0x4, %eax
jne 0xee2d
movq (%r15), %rax
movslq 0x8(%rsp), %rcx
imulq $0x30, %rcx, %rdi
addq (%rax), %rdi
movq %r13, %rsi
callq 0x195c2
movq (%r15), %rcx
movq (%rcx), %rdi
movslq 0x8(%rsp), %rsi
imulq $0x30, %rsi, %rcx
leaq (%rdi,%rcx), %rdx
addq $0x8, %rdx
cmpq %rdx, %rax
je 0xef7e
movsd 0x80(%rsp), %xmm0
leaq 0x1ec33(%rip), %rdi     # 0x2dba5
movb $0x1, %al
callq 0x7050
jmp 0xee2d
addq %rcx, %rdi
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r13, %rsi
callq 0x16ed0
movsd 0x1e073(%rip), %xmm0    # 0x2d010
cmpb $0x0, 0x7(%rsp)
jne 0xefaa
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movslq 0x8(%rsp), %rcx
imulq $0x30, %rcx, %rdi
addq 0x8(%rax), %rdi
movq %r13, %rsi
callq 0x16ed0
xorps %xmm0, %xmm0
cmpb $0x0, 0x7(%rsp)
jne 0xefe7
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movslq 0x8(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x16f22
movl (%rax), %r15d
movslq 0x8(%rsp), %rax
imulq $0x30, %rax, %rdi
movq 0x68(%rsp), %rax
addq (%rax), %rdi
movq %r13, %rsi
callq 0x16f44
movl %r15d, (%rax)
movslq 0x8(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x16f22
incl (%rax)
movq 0x10(%rsp), %r15
jmp 0xee2d
movq %r12, %rdi
callq 0x73e0
leaq 0x90(%rsp), %rdi
callq 0x18d86
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x71d0
leaq 0x1ea8d(%rip), %rcx     # 0x2dafd
movq %rcx, (%rax)
movq 0x35ed6(%rip), %rsi     # 0x44f50
movq %rax, %rdi
xorl %edx, %edx
callq 0x76a0
leaq 0x1e987(%rip), %rdi     # 0x2da12
callq 0x75c0
pushq $0x8
popq %rdi
callq 0x71d0
leaq 0x1e98f(%rip), %rcx     # 0x2da2e
jmp 0xf0c1
pushq $0x8
popq %rdi
callq 0x71d0
leaq 0x1e997(%rip), %rcx     # 0x2da47
jmp 0xf0c1
pushq $0x8
popq %rdi
callq 0x71d0
leaq 0x1e9bc(%rip), %rcx     # 0x2da7d
movq %rcx, (%rax)
movq 0x35e85(%rip), %rsi     # 0x44f50
movq %rax, %rdi
xorl %edx, %edx
callq 0x76a0
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x71a0
jmp 0xf103
jmp 0xf100
jmp 0xf100
jmp 0xf100
jmp 0xf100
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x17c40
jmp 0xf103
jmp 0xf100
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x18d86
movq %rbx, %rdi
callq 0x76d0