// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_profile_normalization.cpp
// Function: get_per_window_scaling_factors_for_profile1_per_profile2(double*, int, double*, int, int, double&, double&, double&)
void get_per_window_scaling_factors_for_profile1_per_profile2(double* signal_profile1, int l_prof1, double* signal_profile2, int l_prof2, int l_win,
	double& per_win_2DOF_lls_scaling_factor,
	double& per_win_1DOF_lls_scaling_factor,
	double& total_sig_scaling_factor)
{
	/*vector<int>* chip_seq_frag_cnt = new vector<int>();
	vector<int>* control_frag_cnt = new vector<int>();*/

	int l_processable_prof = (l_prof1 < l_prof2)?(l_prof1):(l_prof2);
	int n_wins_2_process = l_processable_prof / l_win;

	double* prof1_win_total_sigs = new double[n_wins_2_process + 2];
	double* prof2_win_total_sigs = new double[n_wins_2_process + 2];

	double total_prof1 = 0;
	double total_prof2 = 0;
	for(int i = 1; i <= l_prof1; i++)
	{
		total_prof1 += signal_profile1[i];
	} // i loop

	for(int i = 1; i <= l_prof2; i++)
	{
		total_prof2 += signal_profile2[i];
	} // i loop

	total_sig_scaling_factor = total_prof1 / total_prof2;

	// Count the numbers per window. 
	//int n_scaling_win = n_meg_wins * MEG_BASE / win_size;

	//int chip_seq_background_cnt = 0;
	//int control_cnt = 0;

	// Exploit the fact that the fragments are sorted with respect to base indices in the lists:
	// Keep track of the current fragment index for both fragment lists.
	//int cur_chr_fragment_list_base_i = 0;
	//int cur_chr_control_fragment_list_base_i = 0;

	int n_processed_wins = 0;

	FILE* f_per_win_vals = NULL;
if(__DUMP_INPUT_NORMALIZATION_MSGS__)
	f_per_win_vals = fopen("per_win_vals.txt", "w");
	for(int i_win = 0; i_win < n_wins_2_process; i_win++)
	{
		int cur_win_start = l_win * i_win + 1;
		int cur_win_end = (i_win + 1) * l_win;		

		double cur_total_prof1 = 0;
		double cur_total_prof2 = 0;
		for(int i_nuc = cur_win_start; i_nuc < cur_win_end; i_nuc++)
		{
			if(i_nuc < l_prof1 && i_nuc < l_prof2)
			{
				cur_total_prof1 += signal_profile1[i_nuc];
				cur_total_prof2 += signal_profile2[i_nuc];
			}
		} // i_nuc loop.

		if(cur_total_prof1 > 0 && cur_total_prof2 > 0)
		{
			prof1_win_total_sigs[n_processed_wins] = cur_total_prof1;
			prof2_win_total_sigs[n_processed_wins] = cur_total_prof2;
			n_processed_wins++;
		}

if(__DUMP_INPUT_NORMALIZATION_MSGS__)
		fprintf(f_per_win_vals, "%lf\t%lf\n", cur_total_prof1, cur_total_prof2);
	} // i_win loop.

if(__DUMP_INPUT_NORMALIZATION_MSGS__)
	fclose(f_per_win_vals);

     // Compute the slopes.
	per_win_2DOF_lls_scaling_factor = PeakSeq_slope(prof1_win_total_sigs, prof2_win_total_sigs, n_processed_wins);
	per_win_1DOF_lls_scaling_factor = slope(prof1_win_total_sigs, prof2_win_total_sigs, n_processed_wins);
	
	double r = pearson_correlation(prof1_win_total_sigs, prof2_win_total_sigs, n_processed_wins);

if(__DUMP_INPUT_NORMALIZATION_MSGS__)
	fprintf(stderr, "Pearson Coeff: %lf\nSignal Division: %lf\n1-DOF: %lf\n2-DOF: %lf\n", r, total_sig_scaling_factor, per_win_1DOF_lls_scaling_factor, 
		per_win_2DOF_lls_scaling_factor);

	//// If the total profile1 versus profile2 is much larger, use that.
	//if(total_prof1 / total_prof2 > per_win_1DOF_lls_scaling_factor)
	//{
	//	per_win_1DOF_lls_scaling_factor = total_prof1 / total_prof2;
	//}

	delete [] prof1_win_total_sigs;
	delete [] prof2_win_total_sigs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x40(%rsp)
movl %ecx, %r15d
movq %rdx, %r12
movl %esi, %r14d
movq %rdi, %r13
movq 0x88(%rsp), %rbp
movq 0x80(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl %ecx, %esi
movl %ecx, %eax
cmovll %esi, %eax
movl %eax, 0x18(%rsp)
cltd
movl %r8d, 0x4(%rsp)
idivl %r8d
leal 0x2(%rax), %ecx
shlq $0x3, %rcx
movq %rax, 0x8(%rsp)
cmpl $-0x2, %eax
pushq $-0x1
popq %rbx
cmovgeq %rcx, %rbx
movq %rbx, %rdi
callq 0x2040
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
callq 0x2040
movq %rax, 0x20(%rsp)
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
xorpd %xmm0, %xmm0
cmpq %rax, %r14
je 0x1d93e
addsd 0x8(%r13,%rax,8), %xmm0
incq %rax
jmp 0x1d92d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
xorpd %xmm1, %xmm1
cmpq %rax, %r15
je 0x1d95c
addsd 0x8(%r12,%rax,8), %xmm1
incq %rax
jmp 0x1d94b
divsd %xmm1, %xmm0
movsd %xmm0, (%rbp)
xorl %r15d, %r15d
cmpb $0x0, 0x1785b(%rip)     # 0x351ca
movq %rbp, 0x30(%rsp)
je 0x1d990
leaq 0xaa55(%rip), %rdi      # 0x283d2
leaq 0x7124(%rip), %rsi      # 0x24aa8
callq 0x21b0
movq %rax, 0x10(%rsp)
jmp 0x1d999
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movslq 0x18(%rsp), %rbp
movslq 0x4(%rsp), %rsi
testl %edx, %edx
cmovlel %r15d, %edx
xorpd %xmm2, %xmm2
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rsi, 0x8(%rsp)
cmpq %rdx, %rbx
je 0x1da6f
movslq %r15d, %rax
incq %rbx
movq %rbx, %rcx
imulq %rsi, %rcx
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
incq %rax
cmpq %rcx, %rax
jge 0x1d9f6
cmpq %rbp, %rax
jge 0x1d9da
addsd (%r13,%rax,8), %xmm0
addsd (%r12,%rax,8), %xmm1
jmp 0x1d9da
ucomisd %xmm2, %xmm0
jbe 0x1da1e
ucomisd %xmm2, %xmm1
jbe 0x1da1e
movslq %r14d, %r14
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x20(%rsp), %rax
movsd %xmm1, (%rax,%r14,8)
incl %r14d
cmpb $0x1, 0x177a5(%rip)     # 0x351ca
jne 0x1da65
movq 0x10(%rsp), %rdi
leaq 0x978d(%rip), %rsi      # 0x271c0
movb $0x2, %al
movq %r14, 0x18(%rsp)
movq %r15, %r14
movq %r13, %r15
movq %r12, %r13
movq %rdx, %r12
callq 0x2240
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %r13, %r12
movq %r15, %r13
movq %r14, %r15
movq 0x18(%rsp), %r14
addl 0x4(%rsp), %r15d
jmp 0x1d9bc
cmpb $0x0, 0x17754(%rip)     # 0x351ca
je 0x1da82
movq 0x10(%rsp), %rdi
callq 0x2160
movq 0x28(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
movl %r14d, %edx
callq 0x1db1c
movq 0x40(%rsp), %rbx
movsd %xmm0, (%rbx)
movq %r12, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x1dbbc
movq 0x38(%rsp), %r15
movsd %xmm0, (%r15)
movq %r12, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0x1dc36
cmpb $0x1, 0x176fa(%rip)     # 0x351ca
movq 0x30(%rsp), %rcx
je 0x1daf5
movq %r12, %rdi
callq 0x2210
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2210
movq 0x174e4(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movsd (%rcx), %xmm1
movsd (%r15), %xmm2
movsd (%rbx), %xmm3
leaq 0xa8d0(%rip), %rsi      # 0x283e3
movb $0x4, %al
callq 0x2240
jmp 0x1dad7