// File: 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:
subq $0xb8, %rsp
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq %rdi, 0xb0(%rsp)
movl %esi, 0xac(%rsp)
movq %rdx, 0xa0(%rsp)
movl %ecx, 0x9c(%rsp)
movl %r8d, 0x98(%rsp)
movq %r9, 0x90(%rsp)
movl 0xac(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x3e362
movl 0xac(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x3e36d
movl 0x9c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cltd
idivl 0x98(%rsp)
movl %eax, 0x88(%rsp)
movl 0x88(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x80(%rsp)
movl 0x88(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xac(%rsp), %eax
jg 0x3e447
movq 0xb0(%rsp), %rax
movslq 0x64(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x3e40f
movl $0x1, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jg 0x3e487
movq 0xa0(%rsp), %rax
movslq 0x60(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x3e44f
movsd 0x70(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movsd %xmm0, (%rax)
movl $0x0, 0x5c(%rsp)
movq $0x0, 0x50(%rsp)
testb $0x1, 0x26d2b(%rip)     # 0x651e2
je 0x3e4d1
leaq 0x18313(%rip), %rdi     # 0x567d3
leaq 0x145dd(%rip), %rsi     # 0x52aa4
callq 0x21d0
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x3e629
movl 0x98(%rsp), %eax
imull 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
imull 0x98(%rsp), %eax
movl %eax, 0x44(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0x3e599
movl 0x2c(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0x3e58a
movl 0x2c(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x3e58a
movq 0xb0(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0xa0(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x3e58c
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3e52a
movsd 0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3e5ef
movsd 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3e5ef
movsd 0x38(%rsp), %xmm0
movq 0x80(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x30(%rsp), %xmm0
movq 0x78(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
testb $0x1, 0x26bec(%rip)     # 0x651e2
je 0x3e617
movq 0x50(%rsp), %rdi
movsd 0x38(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
leaq 0x16f76(%rip), %rsi     # 0x55586
movb $0x2, %al
callq 0x2250
jmp 0x3e619
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3e4d9
testb $0x1, 0x26bb2(%rip)     # 0x651e2
je 0x3e63c
movq 0x50(%rsp), %rdi
callq 0x2180
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x3e730
movq 0x90(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x3e920
movq 0xc0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x3ea30
movsd %xmm0, 0x20(%rsp)
testb $0x1, 0x26b3f(%rip)     # 0x651e2
je 0x3e6e7
movq 0x26934(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0x20(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movsd (%rax), %xmm1
movq 0xc0(%rsp), %rax
movsd (%rax), %xmm2
movq 0x90(%rsp), %rax
movsd (%rax), %xmm3
leaq 0x18104(%rip), %rsi     # 0x567e4
movb $0x4, %al
callq 0x2250
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3e704
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3e71e
movq 0x8(%rsp), %rdi
callq 0x2220
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)