// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, %r12
movl %ecx, %r14d
movq %rdx, %r13
movl %esi, %r15d
movq %rdi, %rbp
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl %ecx, %esi
movl %ecx, %eax
cmovll %esi, %eax
movl %eax, 0x8(%rsp)
cltd
movq %r8, 0x38(%rsp)
idivl %r8d
leal 0x2(%rax), %ecx
shlq $0x3, %rcx
movq %rax, 0x18(%rsp)
cmpl $-0x2, %eax
movq $-0x1, %rbx
cmovgeq %rcx, %rbx
movq %rbx, %rdi
callq 0x2040
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0x2040
movq %rax, 0x28(%rsp)
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
testl %r15d, %r15d
jle 0x22d46
incl %r15d
movl $0x1, %eax
addsd (%rbp,%rax,8), %xmm1
incq %rax
cmpq %rax, %r15
jne 0x22d38
testl %r14d, %r14d
jle 0x22d66
incl %r14d
xorpd %xmm0, %xmm0
movl $0x1, %eax
addsd (%r13,%rax,8), %xmm0
incq %rax
cmpq %rax, %r14
jne 0x22d57
divsd %xmm0, %xmm1
movq 0x20(%rsp), %rax
movsd %xmm1, (%rax)
cmpb $0x0, 0x16458(%rip)     # 0x391d2
movq %r12, 0x48(%rsp)
je 0x22da3
movq $0x0, (%rsp)
leaq 0xb642(%rip), %rdi      # 0x2e3d2
leaq 0x7d11(%rip), %rsi      # 0x2aaa8
callq 0x21c0
movq %rax, 0x10(%rsp)
jmp 0x22db4
movq $0x0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rsi
testl %esi, %esi
jle 0x22eb6
movq 0x38(%rsp), %rax
movslq %eax, %rdi
movslq 0x8(%rsp), %r15
addq $0x8, %rbp
leaq (,%rdi,8), %r8
addq $0x8, %r13
leal -0x1(%rax), %r12d
xorl %r14d, %r14d
movl $0x1, %ebx
xorpd %xmm2, %xmm2
movq $0x0, (%rsp)
movq %rdi, 0x50(%rsp)
movq %r8, 0x8(%rsp)
movl %r14d, %eax
movq 0x38(%rsp), %rdx
imull %edx, %eax
incl %r14d
movl %r14d, %ecx
imull %edx, %ecx
incl %eax
xorpd %xmm1, %xmm1
cmpl %ecx, %eax
jge 0x22e71
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq (%rbx,%rax), %rcx
cmpq %r15, %rcx
jge 0x22e3a
addsd (%rbp,%rax,8), %xmm0
addsd (%r13,%rax,8), %xmm1
incq %rax
cmpl %eax, %r12d
jne 0x22e24
ucomisd %xmm2, %xmm0
jbe 0x22e75
ucomisd %xmm2, %xmm1
jbe 0x22e75
movq (%rsp), %rcx
movslq %ecx, %rcx
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
movsd %xmm1, (%rax,%rcx,8)
incl %ecx
movq %rcx, (%rsp)
jmp 0x22e75
xorpd %xmm0, %xmm0
cmpb $0x1, 0x16356(%rip)     # 0x391d2
jne 0x22ea4
movq 0x10(%rsp), %rdi
leaq 0xa336(%rip), %rsi      # 0x2d1c0
movb $0x2, %al
callq 0x2250
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %r8
movq 0x50(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq %rdi, %rbx
addq %r8, %rbp
addq %r8, %r13
cmpl %esi, %r14d
jne 0x22e00
cmpb $0x0, 0x16315(%rip)     # 0x391d2
je 0x22ec9
movq 0x10(%rsp), %rdi
callq 0x2170
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq (%rsp), %r13
movl %r13d, %edx
callq 0x22f67
movq 0x48(%rsp), %rbx
movsd %xmm0, (%rbx)
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x22fed
movq 0x40(%rsp), %r14
movsd %xmm0, (%r14)
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x2304f
cmpb $0x1, 0x162b7(%rip)     # 0x391d2
je 0x22f3b
movq %r15, %rdi
callq 0x2220
movq %r12, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
movq 0x1609e(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movsd (%rax), %xmm1
movsd (%r14), %xmm2
movsd (%rbx), %xmm3
leaq 0xb485(%rip), %rsi      # 0x2e3e3
movb $0x4, %al
callq 0x2250
jmp 0x22f1d