// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
movq %rdx, %r13
movl %esi, %r15d
movq %rdi, %rbp
movq 0x98(%rsp), %r12
movq 0x90(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl %ecx, %esi
movq %rcx, %rbx
movl %ecx, %eax
cmovll %esi, %eax
cltd
movq %r8, 0x28(%rsp)
idivl %r8d
movl %eax, 0x4(%rsp)
cltq
leaq 0x10(,%rax,8), %rcx
cmpl $-0x2, %eax
movq $-0x1, %r14
cmovgeq %rcx, %r14
movq %r14, %rdi
callq 0x2040
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x2040
movq %rax, 0x18(%rsp)
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
testl %r15d, %r15d
jle 0x2327a
leal 0x1(%r15), %eax
movl $0x1, %ecx
addsd (%rbp,%rcx,8), %xmm1
incq %rcx
cmpq %rcx, %rax
jne 0x2326c
testl %ebx, %ebx
jle 0x23299
leal 0x1(%rbx), %eax
xorpd %xmm0, %xmm0
movl $0x1, %ecx
addsd (%r13,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x2328a
divsd %xmm0, %xmm1
movsd %xmm1, (%r12)
movq $0x0, 0x8(%rsp)
cmpb $0x0, 0x16f1f(%rip)     # 0x3a1d2
movq %r12, 0x30(%rsp)
je 0x232d4
leaq 0xc111(%rip), %rdi      # 0x2f3d2
leaq 0x87e6(%rip), %rsi      # 0x2baae
callq 0x21c0
movq %rax, 0x10(%rsp)
jmp 0x232dd
movq $0x0, 0x10(%rsp)
movl 0x4(%rsp), %esi
testl %esi, %esi
jle 0x233e0
cmpl %ebx, %r15d
cmovll %r15d, %ebx
movq 0x28(%rsp), %rax
movslq %eax, %rdi
movslq %ebx, %rbx
addq $0x8, %rbp
leaq (,%rdi,8), %r8
addq $0x8, %r13
leal -0x1(%rax), %r12d
xorl %r15d, %r15d
movl $0x1, %r14d
xorpd %xmm2, %xmm2
movq $0x0, 0x8(%rsp)
movq %rdi, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movl %r15d, %eax
movq 0x28(%rsp), %rdx
imull %edx, %eax
incl %r15d
movl %r15d, %ecx
imull %edx, %ecx
incl %eax
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
cmpl %ecx, %eax
jge 0x23371
xorl %eax, %eax
leaq (%r14,%rax), %rcx
cmpq %rbx, %rcx
jge 0x23369
addsd (%rbp,%rax,8), %xmm0
addsd (%r13,%rax,8), %xmm1
incq %rax
cmpl %eax, %r12d
jne 0x23353
ucomisd %xmm2, %xmm0
jbe 0x233a0
ucomisd %xmm2, %xmm1
jbe 0x233a0
movq 0x8(%rsp), %rcx
movslq %ecx, %rcx
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movsd %xmm1, (%rax,%rcx,8)
incl %ecx
movq %rcx, 0x8(%rsp)
cmpb $0x1, 0x16e2b(%rip)     # 0x3a1d2
jne 0x233ce
movq 0x10(%rsp), %rdi
leaq 0xae0b(%rip), %rsi      # 0x2e1c0
movb $0x2, %al
callq 0x2250
xorpd %xmm2, %xmm2
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %rdi
movl 0x4(%rsp), %esi
addq %rdi, %r14
addq %r8, %rbp
addq %r8, %r13
cmpl %esi, %r15d
jne 0x2332f
cmpb $0x0, 0x16deb(%rip)     # 0x3a1d2
je 0x233f3
movq 0x10(%rsp), %rdi
callq 0x2170
movq 0x20(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
movq 0x8(%rsp), %r13
movl %r13d, %edx
callq 0x23492
movq 0x40(%rsp), %rbx
movsd %xmm0, (%rbx)
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x23518
movq 0x38(%rsp), %r14
movsd %xmm0, (%r14)
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x23580
cmpb $0x1, 0x16d8c(%rip)     # 0x3a1d2
movq 0x30(%rsp), %rcx
je 0x2346b
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 0x16b6e(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
movsd (%rcx), %xmm1
movsd (%r14), %xmm2
movsd (%rbx), %xmm3
leaq 0xbf5a(%rip), %rsi      # 0x2f3e3
movb $0x4, %al
callq 0x2250
jmp 0x2344d