// File: /workspace/github_cpp_makefile_star2_O2/WeiqiangZhou[P]BIRD/source/predict_function.cpp
// Function: WriteWIG(double**, char**, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, char*, int, int, int, int, double, int)
int WriteWIG(double **data_out, char **select_idx, std::vector<std::string> outname, char *outfile, int bin_size, int loci_length, int sample_size, int flag, double up_bound, int flag_log)
{
	char *part;
	char temp_name[255];
	char chrname[10];
	int startsite, count;
	double value;
	FILE * pFile;

	//standard output
	if (flag != 1)
	{
		pFile = fopen(outfile, "w");
		if (pFile != NULL)
		{
			fprintf(pFile, "Chromosome\tStart\tEnd\t");
			for (int i = 0; i < sample_size-1; i++)
			{
				fprintf(pFile, "%s\t", outname[i].c_str());
			}
			fprintf(pFile, "%s\n", outname[sample_size-1].c_str());
			for (int i = 0; i < loci_length; i++)
			{
				fprintf(pFile, "%s\t", select_idx[i]);
				for (int j = 0; j < sample_size-1; j++)
				{
					value = data_out[j][i];
					if (value < 0)
					{
						value = 0;
					}
					else if (value > up_bound)
					{
						value = up_bound;
					}
					fprintf(pFile, "%lf\t", value);   //report original output value, log2(x+1) transformed.
				}

				value = data_out[sample_size-1][i];
				if (value < 0)
				{
					value = 0;
				}
				else if (value > up_bound)
				{
					value = up_bound;
				}
				fprintf(pFile, "%lf\n", value);

			}
			fclose(pFile);
		}
		else
		{
			std::cout << "Error! Cannot write file " << outfile << std::endl;
			return 1;
		}


	}
	else    //WIG output
	{
		std::vector<std::vector<int> > location;
		std::vector<int>::iterator it;
		for (int i = 0; i < 23; i++)
		{
			location.push_back(std::vector<int>());
		}

		for (int i = 0; i < loci_length; i++)
		{
			part = strtok(select_idx[i], "\t\0");
			if (strcmp(part, "chrX") == 0)
			{
				part = strtok(NULL, "\t\0");
				startsite = atoi(part);
				location[22].push_back(startsite);
			}
			else
			{
				for (int j = 0; j < 22; j++)
				{
					sprintf(chrname, "chr%d", j + 1);
					if (strcmp(part, chrname) == 0)
					{
						part = strtok(NULL, "\t\0");
						startsite = atoi(part);
						location[j].push_back(startsite);
						break;
					}
				}
			}
		}
        
        if (flag_log != 1)
        {
            for (int i = 0; i < sample_size; i++)
            {
                count = 0;
                strcpy(temp_name, outfile);
                strcat(temp_name, ".");
                strcat(temp_name, outname[i].c_str());
                strcat(temp_name, ".wig");
                std::cout << "Writing file " << temp_name << std::endl;
                pFile = fopen(temp_name, "w");
                if (pFile != NULL)
                {
                    fprintf(pFile, "track\ttype=wiggle_0\tname=%s\tvisibility=full\tautoScale=off\tmaxHeightPixels=100:50:10\tviewLimits=0.0:100.0\tyLineOnOff=off\n", outname[i].c_str());
                    //print chr1-chr22
                    for (int j = 0; j < 22; j++)
                    {
                        fprintf(pFile, "variableStep\tchrom=chr%d\tspan=%d\n", j + 1, bin_size);
                        for (it = location[j].begin(); it != location[j].end(); it++)
                        {
                            value = pow(2, data_out[i][count]) - 1;
                            if (value < 0)
                            {
                                value = 0;
                            }
                            else if (value > pow(2,up_bound))
                            {
                                value = pow(2,up_bound);
                            }
                            count++;
                            fprintf(pFile, "%d\t%lf\n", (*it), value);        //report value is limited from 0 to 10000.
                        }
                    }
                    //print chrX
                    fprintf(pFile, "variableStep\tchrom=chrX\tspan=%d\n", bin_size);
                    for (it = location[22].begin(); it != location[22].end(); it++)
                    {
                        value = pow(2, data_out[i][count]) - 1;
                        if (value < 0)
                        {
                            value = 0;
                        }
                        else if (value > pow(2,up_bound))
                        {
                            value = pow(2,up_bound);
                        }
                        count++;
                        fprintf(pFile, "%d\t%lf\n", (*it), value);
                    }

                    fclose(pFile);
                }
                else
                {
                    std::cout << "Error! Cannot write file " << temp_name << std::endl;
                    return 1;
                }

            }
        }
        else
        {
            for (int i = 0; i < sample_size; i++)
            {
                count = 0;
                strcpy(temp_name, outfile);
                strcat(temp_name, ".");
                strcat(temp_name, outname[i].c_str());
                strcat(temp_name, ".wig");
                std::cout << "Writing file " << temp_name << std::endl;
                pFile = fopen(temp_name, "w");
                if (pFile != NULL)
                {
                    fprintf(pFile, "track\ttype=wiggle_0\tname=%s\tvisibility=full\tautoScale=off\tmaxHeightPixels=100:50:10\tviewLimits=0.0:100.0\tyLineOnOff=off\n", outname[i].c_str());
                    //print chr1-chr22
                    for (int j = 0; j < 22; j++)
                    {
                        fprintf(pFile, "variableStep\tchrom=chr%d\tspan=%d\n", j + 1, bin_size);
                        for (it = location[j].begin(); it != location[j].end(); it++)
                        {
                            value = data_out[i][count];
                            if (value < 0)
                            {
                                value = 0;
                            }
                            else if (value > up_bound)
                            {
                                value = up_bound;
                            }
                            count++;
                            fprintf(pFile, "%d\t%lf\n", (*it), value);        //report value is limited from 0 to 10000.
                        }
                    }
                    //print chrX
                    fprintf(pFile, "variableStep\tchrom=chrX\tspan=%d\n", bin_size);
                    for (it = location[22].begin(); it != location[22].end(); it++)
                    {
                        value = data_out[i][count];
                        if (value < 0)
                        {
                            value = 0;
                        }
                        else if (value > up_bound)
                        {
                            value = up_bound;
                        }
                        count++;
                        fprintf(pFile, "%d\t%lf\n", (*it), value);
                    }

                    fclose(pFile);
                }
                else
                {
                    std::cout << "Error! Cannot write file " << temp_name << std::endl;
                    return 1;
                }

            }
        }
        
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movsd %xmm0, -0x30(%rbp)
movq %r9, -0x48(%rbp)
movl %r8d, -0x3c(%rbp)
movq %rcx, %r15
movq %rdx, -0x58(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdi, -0x60(%rbp)
movl 0x10(%rbp), %ebx
cmpl $0x1, 0x18(%rbp)
jne 0x4f72
movq %r15, -0x70(%rbp)
leaq -0x90(%rbp), %r14
andq $0x0, 0x10(%r14)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
pushq $0x17
popq %rbx
leaq -0xb0(%rbp), %r15
subl $0x1, %ebx
jb 0x4feb
xorpd %xmm0, %xmm0
movapd %xmm0, -0xb0(%rbp)
andq $0x0, -0xa0(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x6510
movq %r15, %rdi
callq 0x64fc
jmp 0x4f40
leaq 0x22db(%rip), %rsi      # 0x7254
movq %r15, %rdi
callq 0x2210
testq %rax, %rax
je 0x5642
movq %rax, %r14
leaq 0x256c(%rip), %rdi      # 0x7500
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x23a0
decl %ebx
testl %ebx, %ebx
movl $0x0, %r12d
movl %ebx, -0x38(%rbp)
cmovgl %ebx, %r12d
movq %r12, %rbx
shlq $0x5, %rbx
leaq 0x24af(%rip), %r15      # 0x7470
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x52fb
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rdx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2290
addq $0x20, %r13
jmp 0x4fc4
xorl %eax, %eax
movq -0x48(%rbp), %rcx
testl %ecx, %ecx
cmovlel %eax, %ecx
movq %rcx, -0x48(%rbp)
leaq 0x287e(%rip), %rbx      # 0x787f
leaq 0x2513(%rip), %r15      # 0x751b
leaq -0xba(%rbp), %r13
xorl %ecx, %ecx
cmpq -0x48(%rbp), %rcx
je 0x50ec
movq -0x68(%rbp), %rax
movq %rcx, -0x38(%rbp)
movq (%rax,%rcx,8), %rdi
movq %rbx, %rsi
callq 0x22e0
movq %rax, %r14
movq %rax, %rdi
leaq 0x24da(%rip), %rsi      # 0x7516
callq 0x2280
testl %eax, %eax
je 0x50ac
pushq $0x1
popq %r12
pushq $0x18
popq %rbx
cmpl $0xfffffe08, %ebx       # imm = 0xFFFFFE08
je 0x50d9
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
movq %r13, %rsi
callq 0x2280
addq $-0x18, %rbx
incl %r12d
testl %eax, %eax
jne 0x504c
xorl %edi, %edi
leaq 0x27f8(%rip), %rsi      # 0x787f
callq 0x22e0
movq %rax, %rdi
callq 0x22d0
movl %eax, -0x4c(%rbp)
movq -0x90(%rbp), %rdi
subq %rbx, %rdi
leaq -0x4c(%rbp), %rsi
callq 0x58e6
jmp 0x50d9
xorl %edi, %edi
movq %rbx, %rsi
callq 0x22e0
movq %rax, %rdi
callq 0x22d0
movl %eax, -0x4c(%rbp)
movq -0x90(%rbp), %rdi
movl $0x210, %eax            # imm = 0x210
addq %rax, %rdi
leaq -0x4c(%rbp), %rsi
callq 0x58e6
movq -0x38(%rbp), %rcx
incq %rcx
leaq 0x2798(%rip), %rbx      # 0x787f
jmp 0x5011
xorl %eax, %eax
movl 0x10(%rbp), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
movq %rax, -0x68(%rbp)
leaq -0x1c0(%rbp), %r14
xorl %r12d, %r12d
cmpl $0x1, 0x20(%rbp)
jne 0x53f2
movq -0x70(%rbp), %rsi
cmpq -0x68(%rbp), %r12
je 0x562f
movq %r14, %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x20f0
movw $0x2e, -0x1c0(%rbp,%rax)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %r12, %rbx
shlq $0x5, %rbx
movq (%rax,%rbx), %rsi
movq %r14, %rdi
callq 0x2350
movq %r14, %rdi
callq 0x20f0
movl $0x6769772e, -0x1c0(%rbp,%rax) # imm = 0x6769772E
movb $0x0, -0x1bc(%rbp,%rax)
movq 0x5e66(%rip), %rdi      # 0xafd8
leaq 0x23ad(%rip), %rsi      # 0x7526
callq 0x21d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
movq %r14, %rdi
leaq 0x20b9(%rip), %rsi      # 0x7254
callq 0x2210
testq %rax, %rax
je 0x566d
movq %rax, %r14
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq (%rax,%rbx), %rdx
xorl %ebx, %ebx
movq %r14, %rdi
leaq 0x2371(%rip), %rsi      # 0x7534
xorl %eax, %eax
callq 0x2290
xorl %r15d, %r15d
cmpq $0x16, %rbx
je 0x5262
movslq %r15d, %r15
leaq 0x1(%rbx), %rdx
movq %r14, %rdi
leaq 0x23c5(%rip), %rsi      # 0x75ad
movq %rdx, -0x38(%rbp)
movl -0x3c(%rbp), %ecx
xorl %eax, %eax
callq 0x2290
movq -0x90(%rbp), %rax
imulq $0x18, %rbx, %rbx
movq (%rax,%rbx), %r13
cmpq 0x8(%rax,%rbx), %r13
je 0x5259
movq -0x60(%rbp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r15,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x5234
ucomisd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
jbe 0x5234
movsd -0x30(%rbp), %xmm0
incq %r15
movl (%r13), %edx
movq %r14, %rdi
leaq 0x238a(%rip), %rsi      # 0x75cf
movb $0x1, %al
callq 0x2290
addq $0x4, %r13
movq -0x90(%rbp), %rax
jmp 0x5205
movq -0x38(%rbp), %rbx
jmp 0x51cd
movq %r14, %rdi
leaq 0x236b(%rip), %rsi      # 0x75d7
movl -0x3c(%rbp), %edx
xorl %eax, %eax
callq 0x2290
movq -0x90(%rbp), %rax
movq 0x210(%rax), %rbx
movslq %r15d, %r15
shlq $0x3, %r15
cmpq 0x218(%rax), %rbx
je 0x52e0
movq -0x60(%rbp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r15), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x52bc
ucomisd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
jbe 0x52bc
movsd -0x30(%rbp), %xmm0
movl (%rbx), %edx
movq %r14, %rdi
leaq 0x2307(%rip), %rsi      # 0x75cf
movb $0x1, %al
callq 0x2290
addq $0x4, %rbx
movq -0x90(%rbp), %rax
addq $0x8, %r15
jmp 0x528b
movq %r14, %rdi
callq 0x21e0
incq %r12
movq -0x70(%rbp), %rsi
leaq -0x1c0(%rbp), %r14
jmp 0x5112
movslq -0x38(%rbp), %rcx
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rcx, -0x38(%rbp)
shlq $0x5, %rcx
movq (%rax,%rcx), %rdx
leaq 0x2163(%rip), %rsi      # 0x747c
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
movq -0x48(%rbp), %rax
testl %eax, %eax
cmovlel %ebx, %eax
movq %rax, -0x48(%rbp)
leaq 0x2147(%rip), %r13      # 0x7480
xorl %r15d, %r15d
cmpq -0x48(%rbp), %r15
je 0x53e5
movq -0x68(%rbp), %rax
movq (%rax,%r15,8), %rdx
movq %r14, %rdi
leaq 0x2118(%rip), %rsi      # 0x7470
xorl %eax, %eax
callq 0x2290
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x53a0
movq -0x60(%rbp), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r15,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x538e
ucomisd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
jbe 0x538e
movsd -0x30(%rbp), %xmm0
movq %r14, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x2290
incq %rbx
jmp 0x5361
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r15,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x53cc
ucomisd -0x30(%rbp), %xmm1
movapd %xmm1, %xmm0
jbe 0x53cc
movsd -0x30(%rbp), %xmm0
movq %r14, %rdi
leaq 0x21fc(%rip), %rsi      # 0x75d2
movb $0x1, %al
callq 0x2290
incq %r15
jmp 0x533c
movq %r14, %rdi
callq 0x21e0
jmp 0x563b
movq -0x70(%rbp), %rsi
cmpq -0x68(%rbp), %r12
je 0x562f
movq %r14, %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x20f0
movw $0x2e, -0x1c0(%rbp,%rax)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %r12, %rbx
shlq $0x5, %rbx
movq (%rax,%rbx), %rsi
movq %r14, %rdi
callq 0x2350
movq %r14, %rdi
callq 0x20f0
movl $0x6769772e, -0x1c0(%rbp,%rax) # imm = 0x6769772E
movb $0x0, -0x1bc(%rbp,%rax)
movq 0x5b82(%rip), %rdi      # 0xafd8
leaq 0x20c9(%rip), %rsi      # 0x7526
callq 0x21d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
movq %r14, %rdi
leaq 0x1dd5(%rip), %rsi      # 0x7254
callq 0x2210
testq %rax, %rax
je 0x5699
movq %rax, %r15
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq (%rax,%rbx), %rdx
xorl %ebx, %ebx
movq %r15, %rdi
leaq 0x208d(%rip), %rsi      # 0x7534
xorl %eax, %eax
callq 0x2290
xorl %r14d, %r14d
cmpq $0x16, %rbx
je 0x556d
movslq %r14d, %r14
leaq 0x1(%rbx), %rdx
movq %r15, %rdi
leaq 0x20e1(%rip), %rsi      # 0x75ad
movq %rdx, -0x48(%rbp)
movl -0x3c(%rbp), %ecx
xorl %eax, %eax
callq 0x2290
movq -0x90(%rbp), %rax
imulq $0x18, %rbx, %r13
movq (%rax,%r13), %rbx
cmpq 0x8(%rax,%r13), %rbx
je 0x5564
movq -0x60(%rbp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x21a0
movapd %xmm0, %xmm1
addsd 0x1f41(%rip), %xmm1     # 0x7450
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x5541
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
callq 0x21a0
movsd -0x38(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jbe 0x5541
movsd -0x30(%rbp), %xmm0
callq 0x21a0
incq %r14
movl (%rbx), %edx
movq %r15, %rdi
leaq 0x207f(%rip), %rsi      # 0x75cf
movb $0x1, %al
callq 0x2290
addq $0x4, %rbx
movq -0x90(%rbp), %rax
jmp 0x54e9
movq -0x48(%rbp), %rbx
jmp 0x54b1
movq %r15, %rdi
leaq 0x2060(%rip), %rsi      # 0x75d7
movl -0x3c(%rbp), %edx
xorl %eax, %eax
callq 0x2290
movq -0x90(%rbp), %rax
movq 0x210(%rax), %rbx
movslq %r14d, %r14
shlq $0x3, %r14
cmpq 0x218(%rax), %rbx
je 0x5614
movq -0x60(%rbp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r14), %xmm0
callq 0x21a0
movapd %xmm0, %xmm1
addsd 0x1e92(%rip), %xmm1     # 0x7450
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
ja 0x55f0
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
callq 0x21a0
movsd -0x38(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jbe 0x55f0
movsd -0x30(%rbp), %xmm0
callq 0x21a0
movl (%rbx), %edx
movq %r15, %rdi
leaq 0x1fd3(%rip), %rsi      # 0x75cf
movb $0x1, %al
callq 0x2290
addq $0x4, %rbx
movq -0x90(%rbp), %rax
addq $0x8, %r14
jmp 0x5596
movq %r15, %rdi
callq 0x21e0
incq %r12
movq -0x70(%rbp), %rsi
leaq -0x1c0(%rbp), %r14
jmp 0x53f6
leaq -0x90(%rbp), %rdi
callq 0x590c
xorl %ebx, %ebx
jmp 0x56d2
pushq $0x1
popq %rbx
movq 0x598c(%rip), %rdi      # 0xafd8
leaq 0x1e32(%rip), %rsi      # 0x7485
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
jmp 0x56d2
movq 0x5964(%rip), %rdi      # 0xafd8
leaq 0x1e0a(%rip), %rsi      # 0x7485
callq 0x21d0
leaq -0x1c0(%rbp), %rsi
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
jmp 0x56c3
movq 0x5938(%rip), %rdi      # 0xafd8
leaq 0x1dde(%rip), %rsi      # 0x7485
callq 0x21d0
leaq -0x1c0(%rbp), %rsi
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
leaq -0x90(%rbp), %rdi
callq 0x590c
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x56f0
jmp 0x56f0
jmp 0x56f0
jmp 0x56f0
jmp 0x56f0
movq %rax, %rbx
jmp 0x5704
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x64fc
leaq -0x90(%rbp), %rdi
callq 0x590c
movq %rbx, %rdi
callq 0x2340