// File: /workspace/github_cpp_makefile_star2_O2/melop[P]admixem/makemarkerfile.cpp
// Function: UIMakeNewMarkerFile()
void UIMakeNewMarkerFile() {

int nCurrChromosome = 1;
int nHaploidChromosomeNum, nTotalMarkers;
string sOutFile = "";
double nChromosomeSize, nCentromerePos, nPop1AvgAlleleFreq, nPop1AvgAlleleFreqStdDev,  nPop2AvgAlleleFreq, nPop2AvgAlleleFreqStdDev;

	//Random::Set(0.42);

		while(true) { //Get random seed from user
		
			std::cout << "\nEnter a random seed (0.0~1.0). Write down this number to repeat simulation: ";
			std::cin >> nRandSeed;
			
			if (nRandSeed > 0 && nRandSeed < 1.0 ) {
				Random::Set(nRandSeed);
				break; // 
			}
			
			
			
		}

			
			/*
		  ofstream myfile;
			myfile.open("testnormal.txt");
			
			for (int i=0;i<=999;i++)
			{
				myfile << NormalExt(0.8, 0.1 , 0.5, 1.0) << "\n"; // draw from normal distribution, mean=0.8, stdev=0.1, >=0.5, <= 1.0
			
			}
			myfile.close();
	*/

	//std::cout << NormalExt(0.8, 0.1) << "\n";

		while(true) { //Get haploid chromosome number from user
		
			std::cout << "\nEnter the number of chromosomes in the haploid genome (n): ";
			std::cin >> nHaploidChromosomeNum;
			
			if (nHaploidChromosomeNum > 0 ) {
				break; // 
			}
			
			
			
		}
		
		pChromosomeSizes = new double[nHaploidChromosomeNum];
		double * pCentromerePos = new double[nHaploidChromosomeNum];
		
		while(true) { // Get sizes of each chromosome from user
		
			std::cout << "\nEnter the size of chromosome " << nCurrChromosome << "(enter a negative number to finish, enter 0 set it to the same number as the previous one): ";
			std::cin >> nChromosomeSize;

			if (nChromosomeSize == 0 && nCurrChromosome == 1) {
				std::cout << "Can't use this option\n";
				continue;
			}

			if (nChromosomeSize == 0) { //set it to the same as the previous one
				pChromosomeSizes[nCurrChromosome-1]  = pChromosomeSizes[nCurrChromosome-2];
				nChromosomeSize = pChromosomeSizes[nCurrChromosome-1];
			}
			
			if (nChromosomeSize < 0 ) {
				break; // 
			}
			
			if (nChromosomeSize > 0) {
				pChromosomeSizes[nCurrChromosome-1] = nChromosomeSize;
			}
			
			/*
			Now do not allow users to move centromere any more since centromere can be simply a marker, and it's forced 
			to the end of the chromosome.

			std::cout << "\nEnter the centromere position of chromosome " << nCurrChromosome << " (in same unit as chromosome size. -1 default: at 50% of chromosome, enter 0 set it to the same as the previous): ";
			std::cin >> nCentromerePos;
			 
			if  (nCentromerePos == -1) {
				pCentromerePos[nCurrChromosome-1] = nChromosomeSize / 2 ;
				
			}
			
			else if (nCentromerePos < 0 || nCentromerePos > nChromosomeSize ) {
				cout << "Centromere position out of bound, enter chromosome info again" << endl ;
				continue;
			}
			
			else if (nCentromerePos == 0 && nCurrChromosome > 1) {
				pCentromerePos[nCurrChromosome-1] =  pCentromerePos[nCurrChromosome-2];
			}
			
			else {
				pCentromerePos[nCurrChromosome-1] =  nCentromerePos;
			}

			*/

			pCentromerePos[nCurrChromosome-1] = nChromosomeSize;
			
			nCurrChromosome++;
			if (nCurrChromosome > nHaploidChromosomeNum ) {
				break;
			}
		}
		
		while(true) { // Get total number of markers
		
			std::cout << "\nHow many markers in total? (this will be distributed to all chromosomes weighted by their lengths): ";
			std::cin >> nTotalMarkers;
			
			if (nTotalMarkers <= 0 ) {
				continue; // 
			}
			
			break;
			
		}
		
		while(true) { // Get average freq of the more abundant allele in pop 1
		
			std::cout << "\nFor parental population 1, what is the average frequency of the more abundant allele [ 0.5~1.0 ] : ";
			std::cin >> nPop1AvgAlleleFreq;
			
			if (nPop1AvgAlleleFreq < 0.5 || nPop1AvgAlleleFreq > 1.0) {
				continue; // 
			}
			
			break;
		}
		
		while(true) { //  Get std. dev of the more abundant allele in pop 1
		
			std::cout << "\nFor parental population 1, what is the standard deviation of the more abundant allele [ 0~0.5 ] : ";
			std::cin >> nPop1AvgAlleleFreqStdDev;
			
			if (nPop1AvgAlleleFreqStdDev < 0 || nPop1AvgAlleleFreqStdDev > 0.5) {
				continue; // 
			}
			
			break;
		}
		
		while(true) { // Get average freq of the more abundant allele in pop 2
		
			std::cout << "\nFor parental population 2, what is the average frequency of the more abundant allele [ 0.5~1.0 ] : ";
			std::cin >> nPop2AvgAlleleFreq;
			
			if (nPop2AvgAlleleFreq < 0.5 || nPop2AvgAlleleFreq > 1.0) {
				continue; // 
			}
			
			break;
		}
		
		while(true) { //  Get std. dev of the more abundant allele in pop 2
		
			std::cout << "\nFor parental population 2, what is the standard deviation of the frequency of the more abundant allele [ 0~0.5 ] : ";
			std::cin >> nPop2AvgAlleleFreqStdDev;
			
			if (nPop2AvgAlleleFreqStdDev < 0 || nPop2AvgAlleleFreqStdDev > 0.5) {
				continue; // 
			}
			
			break;
		}
		
		cout << "Output File name : ";
		
		while(true) { //  Get std. dev of the more abundant allele in pop 2
		
			
		
			
			getline(cin, sOutFile) ;
			
			
			if (sOutFile.size() > 3 ) {
				break;
			}
			
			
			continue;
		}
		
		
		// now prepare file:
		//Figure out how to distribute the markers
		
		ofstream fOutFile;
		fOutFile.open(sOutFile.c_str());
		fOutFile << "HaploidChromosomeNum = " << nHaploidChromosomeNum << endl; //Write haploid n.
		fOutFile << "RandomSeed = " << nRandSeed << endl; //Write random seed n.
		fOutFile << "Pop1AvgAlleleFreq = " << nPop1AvgAlleleFreq << endl; //
		fOutFile << "Pop1AvgAlleleFreqStdev = " << nPop1AvgAlleleFreqStdDev << endl; //
		fOutFile << "Pop2AvgAlleleFreq = " << nPop2AvgAlleleFreq << endl; //
		fOutFile << "Pop2AvgAlleleFreqStdev = " << nPop2AvgAlleleFreqStdDev << endl; //
			/*	
			for (int i=0;i<=999;i++)
			{
				myfile << NormalExt(0.8, 0.1 , 0.5, 1.0) << "\n"; // draw from normal distribution, mean=0.8, stdev=0.1, >=0.5, <= 1.0
			
			}
			*/
			
			
		
		double nSumLens = fnSum(pChromosomeSizes , nHaploidChromosomeNum); // get sum of chromosome lengths
		double nLargestChromSize = 0;
		
		for (int i=0;  i < nHaploidChromosomeNum; i++) { // generate markers chromosome by chromosome
			
			double nChrLen = pChromosomeSizes[i];
			int	   nNumMarkers = (int) ceil( ((double) nTotalMarkers) * (nChrLen / nSumLens));
			//fOutFile << nNumMarkers <<endl;
			
			nLargestChromSize = (nLargestChromSize < nChrLen)? nChrLen : nLargestChromSize;
			
			//cout<< "MarkerNum:" << nNumMarkers << endl;
			
			fOutFile << ":chr " << (i+1) << "\tlen = " << nChrLen << "\tcentromere = " << pCentromerePos[i] << endl; // chromosome number
			fOutFile << "\nNum\tPosition Percentage\tPosition Abs.\tFreq. Pop 1\tFreq. Pop 2\n" ;
			
			double * pPos = new double[nNumMarkers];
			for (int j=0;j<nNumMarkers;j++) {
				pPos[j] = UniformGen.Next(); // create random set of uniformly distributed markers
			}
			
			//cout<< "MarkerNum:" << nNumMarkers << endl;
			//Sort markers according to position on chromosome:
			
			qsort (pPos , nNumMarkers , sizeof(double), fnCompare);
			
			//cout<< "MarkerNum:" << pPos[0] << endl;
			
			for (int j=0;j<nNumMarkers;j++) {
				fOutFile << (j + 1);
				fOutFile << '\t';
				fOutFile << pPos[j] ;
				fOutFile << '\t';
				fOutFile << pPos[j] * nChrLen ;
				fOutFile << '\t';
				fOutFile << NormalExt(nPop1AvgAlleleFreq ,nPop1AvgAlleleFreqStdDev , 0.5, 1.0);
				fOutFile << '\t';
				fOutFile <<  NormalExt( 1.0 - nPop2AvgAlleleFreq ,nPop2AvgAlleleFreqStdDev , 0, 0.5);
				fOutFile << endl; 
			}
			
			//cout<< "MarkerNum:" << nNumMarkers << endl;
			
			delete[] pPos;
		}
		
		fOutFile.close();
		
		
		while(true) { //  Get std. dev of the more abundant allele in pop 2
		
			char sRs;
			std::cout << "\nDo you want to generate random recombination probabilities along these chromosomes? y - yes, n - no : ";
			std::cin >> sRs;
			
			if (sRs == 'y') {
				
				UIGenerateRecombinationFreqMap(nHaploidChromosomeNum , nLargestChromSize, pChromosomeSizes, pCentromerePos); 
			}
			
			break;
		}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
leaq 0x992d(%rip), %rsi      # 0x2dfe9
leaq 0x68(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x9512
movq 0x208bb(%rip), %rbx     # 0x44f90
leaq 0xa964(%rip), %r14      # 0x2f040
movq 0x208ed(%rip), %r15     # 0x44fd0
leaq 0x2138e(%rip), %r12     # 0x45a78
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7280
movsd (%r12), %xmm0
ucomisd 0x890a(%rip), %xmm0     # 0x2d018
jbe 0x246ea
movsd 0x88f8(%rip), %xmm1     # 0x2d010
ucomisd %xmm0, %xmm1
jbe 0x246ea
callq 0xb388
movq 0x20866(%rip), %rbx     # 0x44f90
leaq 0xa95d(%rip), %r14      # 0x2f08e
movq 0x20898(%rip), %r15     # 0x44fd0
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7250
movslq 0x8(%rsp), %r13
testq %r13, %r13
jle 0x2473d
shlq $0x3, %r13
movq %r13, %rdi
callq 0x7030
movq %rax, 0x212c8(%rip)     # 0x45a38
movq %r13, %rdi
callq 0x7030
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rax
movq 0x20809(%rip), %r15     # 0x44f90
leaq 0xa93d(%rip), %r12      # 0x2f0cb
leaq 0xa955(%rip), %r13      # 0x2f0ea
movq 0x20834(%rip), %rbp     # 0x44fd0
leaq 0x48(%rsp), %rbx
movl %eax, %r14d
movq %r15, %rdi
movq %r12, %rsi
callq 0x73d0
movq %rax, %rdi
movl %r14d, %esi
callq 0x76b0
movq %rax, %rdi
movq %r13, %rsi
callq 0x73d0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7280
movsd 0x48(%rsp), %xmm0
cmpl $0x1, %r14d
jne 0x247ff
ucomisd 0x8834(%rip), %xmm0     # 0x2d018
jne 0x247ff
jp 0x247ff
movq %r15, %rdi
leaq 0xa955(%rip), %rsi      # 0x2f147
callq 0x73d0
pushq $0x1
popq %rax
movl %eax, %r14d
jmp 0x247a4
ucomisd 0x8811(%rip), %xmm0     # 0x2d018
jne 0x24827
jp 0x24827
movq 0x21226(%rip), %rax     # 0x45a38
movslq %r14d, %rcx
movsd -0x10(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x8(%rax,%rcx,8)
movsd %xmm0, 0x48(%rsp)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x24873
ucomisd 0x87df(%rip), %xmm0     # 0x2d018
jbe 0x24853
movq 0x211f6(%rip), %rcx     # 0x45a38
movslq %r14d, %rax
movsd %xmm0, -0x8(%rcx,%rax,8)
movsd 0x48(%rsp), %xmm0
jmp 0x24856
movslq %r14d, %rax
movq 0x10(%rsp), %rcx
movsd %xmm0, -0x8(%rcx,%rax,8)
leal 0x1(%r14), %eax
cmpl 0x8(%rsp), %r14d
movl %eax, %r14d
jl 0x247a4
movq 0x20716(%rip), %rbx     # 0x44f90
leaq 0xa8dd(%rip), %r14      # 0x2f15e
movq 0x20748(%rip), %r15     # 0x44fd0
leaq 0xc(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7250
cmpl $0x0, 0xc(%rsp)
jle 0x2488d
movq 0x206df(%rip), %rbx     # 0x44f90
leaq 0xa90c(%rip), %r14      # 0x2f1c4
movq 0x20711(%rip), %r15     # 0x44fd0
leaq 0x40(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7280
movsd 0x40(%rsp), %xmm0
movsd 0x8b10(%rip), %xmm1     # 0x2d3f8
ucomisd %xmm0, %xmm1
ja 0x248c4
ucomisd 0x871a(%rip), %xmm0     # 0x2d010
ja 0x248c4
movq 0x20691(%rip), %rbx     # 0x44f90
leaq 0xa923(%rip), %r14      # 0x2f229
movq 0x206c3(%rip), %r15     # 0x44fd0
leaq 0x38(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7280
movsd 0x38(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x24912
ucomisd 0x8ab8(%rip), %xmm0     # 0x2d3f8
ja 0x24912
movq 0x20647(%rip), %rbx     # 0x44f90
leaq 0xa93d(%rip), %r14      # 0x2f28d
movq 0x20679(%rip), %r15     # 0x44fd0
leaq 0x30(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7280
movsd 0x30(%rsp), %xmm0
movsd 0x8a78(%rip), %xmm1     # 0x2d3f8
ucomisd %xmm0, %xmm1
ja 0x2495c
ucomisd 0x8682(%rip), %xmm0     # 0x2d010
ja 0x2495c
movq 0x205f9(%rip), %rbx     # 0x44f90
leaq 0xa954(%rip), %r14      # 0x2f2f2
movq 0x2062b(%rip), %r15     # 0x44fd0
leaq 0x28(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x7280
movsd 0x28(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x249aa
ucomisd 0x8a20(%rip), %xmm0     # 0x2d3f8
ja 0x249aa
movq 0x205af(%rip), %rdi     # 0x44f90
leaq 0xa97f(%rip), %rsi      # 0x2f367
callq 0x73d0
movq 0x205dc(%rip), %rbx     # 0x44fd0
leaq 0x68(%rsp), %r14
leaq 0x21078(%rip), %r15     # 0x45a78
movq %rbx, %rdi
movq %r14, %rsi
callq 0x70b0
cmpq $0x4, 0x70(%rsp)
jb 0x24a00
leaq 0x88(%rsp), %rdi
callq 0x7690
movq 0x68(%rsp), %rsi
leaq 0x88(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x7060
leaq 0xa93f(%rip), %rsi      # 0x2f37b
leaq 0x88(%rsp), %rdi
callq 0x73d0
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x76b0
movq %rax, %rdi
callq 0x71c0
leaq 0xa92f(%rip), %rsi      # 0x2f393
leaq 0x88(%rsp), %rdi
callq 0x73d0
movsd (%r15), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
leaq 0xa914(%rip), %rsi      # 0x2f3a1
leaq 0x88(%rsp), %rdi
callq 0x73d0
movsd 0x40(%rsp), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
leaq 0xa8ff(%rip), %rsi      # 0x2f3b6
leaq 0x88(%rsp), %rdi
callq 0x73d0
movsd 0x38(%rsp), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
leaq 0xa8ef(%rip), %rsi      # 0x2f3d0
leaq 0x88(%rsp), %rdi
callq 0x73d0
movsd 0x30(%rsp), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
leaq 0xa8da(%rip), %rsi      # 0x2f3e5
leaq 0x88(%rsp), %rdi
callq 0x73d0
movsd 0x28(%rsp), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
movq 0x20f03(%rip), %rdi     # 0x45a38
movl 0x8(%rsp), %esi
callq 0x25c06
movsd %xmm0, 0x50(%rsp)
xorpd %xmm0, %xmm0
leaq 0x88(%rsp), %r15
pushq $0x9
popq %r13
xorl %ebx, %ebx
movslq 0x8(%rsp), %rax
cmpq %rax, %rbx
jge 0x24d64
movsd %xmm0, 0x60(%rsp)
movq 0x20ec7(%rip), %rax     # 0x45a38
cvtsi2sdl 0xc(%rsp), %xmm1
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
divsd 0x50(%rsp), %xmm0
mulsd %xmm1, %xmm0
callq 0x7760
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
leaq 0xa85e(%rip), %rsi      # 0x2f3ff
callq 0x73d0
leaq 0x1(%rbx), %r14
movq %rax, %rdi
movl %r14d, %esi
callq 0x76b0
movq %rax, %rdi
leaq 0xa846(%rip), %rsi      # 0x2f405
callq 0x73d0
movq %rax, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x7620
movq %rax, %rdi
leaq 0xa831(%rip), %rsi      # 0x2f40d
callq 0x73d0
movq 0x10(%rsp), %rcx
movsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x7620
movq %rax, %rdi
callq 0x71c0
movq %r15, %rdi
leaq 0xa817(%rip), %rsi      # 0x2f41c
callq 0x73d0
movq %r14, 0x58(%rsp)
cvttsd2si 0x18(%rsp), %r14d
movslq %r14d, %rbx
leaq (,%rbx,8), %rdi
testl %ebx, %ebx
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x7030
movq %rax, %r12
movsd 0x20(%rsp), %xmm0
maxsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x24c6e
leaq 0x20e12(%rip), %rdi     # 0x45a70
callq 0xb2e2
movsd %xmm0, (%r12,%rbp,8)
incq %rbp
jmp 0x24c52
movq %r12, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
leaq 0xfa3(%rip), %rcx       # 0x25c21
callq 0x7710
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x24d4c
leal 0x1(%rbx), %esi
movq %r15, %rdi
callq 0x76b0
movq %r15, %rdi
movl %r13d, %esi
callq 0x7490
movsd (%r12,%rbx,8), %xmm0
movq %r15, %rdi
callq 0x7620
movq %r15, %rdi
movl %r13d, %esi
callq 0x7490
movsd (%r12,%rbx,8), %xmm0
mulsd 0x20(%rsp), %xmm0
movq %r15, %rdi
callq 0x7620
movq %r15, %rdi
movl %r13d, %esi
callq 0x7490
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x8708(%rip), %xmm2     # 0x2d3f8
movsd 0x8318(%rip), %xmm3     # 0x2d010
callq 0x25ba4
movq %r15, %rdi
callq 0x7620
movq %r15, %rdi
movl %r13d, %esi
callq 0x7490
movsd 0x82f8(%rip), %xmm0     # 0x2d010
subsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
xorps %xmm2, %xmm2
movsd 0x86c9(%rip), %xmm3     # 0x2d3f8
callq 0x25ba4
movq %r15, %rdi
callq 0x7620
incq %rbx
movq %r15, %rdi
callq 0x71c0
jmp 0x24c85
movq %r12, %rdi
callq 0x74f0
movq 0x58(%rsp), %rbx
movsd 0x18(%rsp), %xmm0
jmp 0x24b56
leaq 0x88(%rsp), %rdi
callq 0x7560
movq 0x20218(%rip), %rdi     # 0x44f90
leaq 0xa6dd(%rip), %rsi      # 0x2f45c
callq 0x73d0
movq 0x20245(%rip), %rdi     # 0x44fd0
leaq 0x7(%rsp), %rsi
callq 0x7510
cmpb $0x79, 0x7(%rsp)
jne 0x24db1
movl 0x8(%rsp), %edi
movq 0x20c91(%rip), %rsi     # 0x45a38
movq 0x10(%rsp), %rdx
callq 0x24e25
leaq 0x88(%rsp), %rdi
callq 0x7590
leaq 0x68(%rsp), %rdi
callq 0x71a0
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24e03
jmp 0x24e03
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24e03
jmp 0x24e03
jmp 0x24dfa
jmp 0x24e03
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
jmp 0x24dfa
movq %rax, %rbx
jmp 0x24e13
jmp 0x24e03
jmp 0x24e03
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x7590
leaq 0x68(%rsp), %rdi
callq 0x71a0
movq %rbx, %rdi
callq 0x76d0