// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: get_binomial_pvalue_per_region_neighborhood_window_control_per_max_profile_signal(double*, double*, int, int, double*, double, bool, int, int, int, int)
double get_binomial_pvalue_per_region_neighborhood_window_control_per_max_profile_signal(double* signal_track_data, double* control_track_data, int region_start, int region_end, double* _log_factorials, double scaling_factor,
	bool length_normalization, int l_norm_win, int l_signal, int l_control, int n_vicinity_wins)
{
	// Get the total profile signal: This is computed by sliding a window over the region.
	double total_profile_signal = 0;
	if(region_end - region_start + 1 > l_norm_win)
	{
		double cur_win_total = 0.0;
		for(int i = region_start; i < region_start + l_norm_win; i++)
		{
			cur_win_total += floor(signal_track_data[i]);
		} // i loop.

		total_profile_signal = cur_win_total;

		// Following loop computes the per window total signals.
		int cur_win_start = region_start;
		while(1)
		{
			// Remove the value that just left the window.
			cur_win_total -= floor(signal_track_data[cur_win_start]);

			// Add the newly entering value to the window.
			cur_win_total += floor(signal_track_data[cur_win_start+l_norm_win]);

			if(cur_win_total > total_profile_signal)
			{
				total_profile_signal = cur_win_total;
			}
			cur_win_start++;

			// Are we at the end of the region?
			if(cur_win_start+l_norm_win >= region_end)
			{
				break;
			}
		} // window loop.
	}
	else
	{
		for(int i = region_start; i <= region_end; i++)
		{
			total_profile_signal += floor(signal_track_data[i]);
		} // i loop.

		// Update the normalization window length, this way there is no underpowering for the shorter regions.
		l_norm_win = region_end - region_start;
	}

	// Get the control signal: Get the total profile signal: This is computed by sliding a window over the vicinity extended region (in terms of length of the whole ER).
	int l_reg = (region_end - region_start + 1);
	int ext_region_start = region_start - n_vicinity_wins * l_reg;
	int ext_region_end = region_end + n_vicinity_wins * l_reg;
	if(ext_region_start < 1)
	{
		ext_region_start = 1;
	}

	if(ext_region_end > l_control)
	{
		ext_region_end = l_control;
	}

	double total_control_signal = 0;

	// If we are past the end of the chromosome, flag this regions as insignificant.
	if(ext_region_start + l_norm_win > l_control)
	{
		return(log(1.0));
	}

	// Must make sure we do not go over the limits.
	double cur_win_total = 0.0;
	for(int i = ext_region_start; i < MIN(l_control, ext_region_start + l_norm_win); i++)
	{
		//cur_win_total += floor(control_track_data[i]);
		cur_win_total += control_track_data[i];
	} // i loop.

	total_control_signal = cur_win_total;

	// Following loop computes the per window total control signals.
	int cur_win_start = ext_region_start;
	while(1)
	{
		// Remove the value that just left the window.
		//cur_win_total -= floor(control_track_data[cur_win_start]);
		cur_win_total -= control_track_data[cur_win_start];

		// Add the newly entering value to the window.
		//cur_win_total += floor(control_track_data[cur_win_start+l_norm_win]);
		cur_win_total += control_track_data[cur_win_start+l_norm_win];

		if(cur_win_total > total_control_signal)
		{
			total_control_signal = cur_win_total;
		}
		cur_win_start++;

		// Are we at the end of the region?
		if(cur_win_start+l_norm_win >= ext_region_end)
		{
			break;
		}
	} // window loop.

	// Scale the control and profile signals.
	total_control_signal /= scaling_factor;
	total_profile_signal /= scaling_factor;

	if(floor(total_control_signal) == 0)
	{
		total_control_signal = 1.0;
	}

	// The total signal hit 0, return 1.
	if(floor(total_profile_signal) == 0)
	{
		//fprintf(stderr, "Hit 0.\n");
		// Return the most insignificant p-value.
		return(xlog(1.0));
	}

	int grand_total_int = (int)(total_profile_signal + total_control_signal);

	// Setup the log factorials.
	double* log_factorials = NULL;
	if(_log_factorials == NULL)
	{
		log_factorials = buffer_log_factorials(grand_total_int+3);
	}
	else
	{
		log_factorials = _log_factorials;
	}

	// Compute the p-values.
	double log_flip = log(.5);
	double cur_region_log_p_val = xlog(0.0);
	for(int i = 0; i <= total_control_signal; i++)
	{
		double log_cur_half_pow = log_flip * grand_total_int;
		double log_cur_perm = 0.0; // = xlog(1.0).

		// Compute the current permutation, using shortcuts, bypassing the function calls from xlog_math library.
		log_cur_perm = xlog_div(log_factorials[grand_total_int], xlog_mul(log_factorials[i], log_factorials[grand_total_int-i]));
		cur_region_log_p_val = xlog_sum(cur_region_log_p_val, xlog_mul(log_cur_perm, log_cur_half_pow));
	} // i loop.

	// Free memory if it is allocated in the function.
	if(_log_factorials == NULL)
	{
		delete [] log_factorials;
	}

	return(cur_region_log_p_val);
}

// Assembly:
subq $0xd8, %rsp
movb %r9b, %al
movl 0xf8(%rsp), %r9d
movl 0xf0(%rsp), %r9d
movl 0xe8(%rsp), %r9d
movl 0xe0(%rsp), %r9d
movq %rdi, 0xc8(%rsp)
movq %rsi, 0xc0(%rsp)
movl %edx, 0xbc(%rsp)
movl %ecx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movsd %xmm0, 0xa8(%rsp)
andb $0x1, %al
movb %al, 0xa7(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0xb8(%rsp), %eax
subl 0xbc(%rsp), %eax
addl $0x1, %eax
cmpl 0xe0(%rsp), %eax
jle 0x365dd
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl 0xbc(%rsp), %ecx
addl 0xe0(%rsp), %ecx
cmpl %ecx, %eax
jge 0x364fe
movq 0xc8(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x364a6
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x88(%rsp)
movq 0xc8(%rsp), %rax
movslq 0x88(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
movaps %xmm0, %xmm1
movsd 0x90(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xc8(%rsp), %rax
movl 0x88(%rsp), %ecx
addl 0xe0(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
ucomisd 0x98(%rsp), %xmm0
jbe 0x365ac
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
movl 0x88(%rsp), %eax
addl 0xe0(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jl 0x365d6
jmp 0x365db
jmp 0x3651e
jmp 0x3664f
movl 0xbc(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jg 0x3663a
movq 0xc8(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x365eb
movl 0xb8(%rsp), %eax
subl 0xbc(%rsp), %eax
movl %eax, 0xe0(%rsp)
movl 0xb8(%rsp), %eax
subl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
movl 0xbc(%rsp), %eax
movl 0xf8(%rsp), %ecx
imull 0x80(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x7c(%rsp)
movl 0xb8(%rsp), %eax
movl 0xf8(%rsp), %ecx
imull 0x80(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x78(%rsp)
cmpl $0x1, 0x7c(%rsp)
jge 0x366ae
movl $0x1, 0x7c(%rsp)
movl 0x78(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jle 0x366c6
movl 0xf0(%rsp), %eax
movl %eax, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x7c(%rsp), %eax
addl 0xe0(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jle 0x366fe
movsd 0x1b92d(%rip), %xmm0    # 0x52018
callq 0x2140
movsd %xmm0, 0xd0(%rsp)
jmp 0x36a1c
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0xf0(%rsp), %eax
movl 0x7c(%rsp), %ecx
addl 0xe0(%rsp), %ecx
cmpl %ecx, %eax
jle 0x3673e
movl 0x7c(%rsp), %eax
addl 0xe0(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0x36749
movl 0xf0(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x24(%rsp), %eax
movl 0x20(%rsp), %ecx
cmpl %ecx, %eax
jge 0x36780
movq 0xc0(%rsp), %rax
movslq 0x64(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x3670f
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0xc0(%rsp), %rax
movslq 0x60(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x68(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xc0(%rsp), %rax
movl 0x60(%rsp), %ecx
addl 0xe0(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x367f7
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %eax
addl 0xe0(%rsp), %eax
cmpl 0x78(%rsp), %eax
jl 0x36815
jmp 0x3681a
jmp 0x36794
movsd 0xa8(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0xa8(%rsp), %xmm1
movsd 0x98(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x70(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x36876
jp 0x36876
movsd 0x1b7a8(%rip), %xmm0    # 0x52018
movsd %xmm0, 0x70(%rsp)
movsd 0x98(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x368aa
jp 0x368aa
movsd 0x1b781(%rip), %xmm0    # 0x52018
callq 0x441e0
movsd %xmm0, 0xd0(%rsp)
jmp 0x36a1c
movsd 0x98(%rsp), %xmm0
addsd 0x70(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
movq $0x0, 0x50(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x368e8
movl 0x5c(%rsp), %edi
addl $0x3, %edi
callq 0x3f420
movq %rax, 0x50(%rsp)
jmp 0x368f5
movq 0xb0(%rsp), %rax
movq %rax, 0x50(%rsp)
movsd 0x1b73b(%rip), %xmm0    # 0x52038
callq 0x2140
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
callq 0x441e0
movsd %xmm0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
cvtsi2sdl 0x3c(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x369e6
movsd 0x48(%rsp), %xmm0
cvtsi2sdl 0x5c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x50(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x50(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x50(%rsp), %rax
movl 0x5c(%rsp), %ecx
subl 0x3c(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x445a0
movsd %xmm0, 0x28(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
callq 0x44310
movsd %xmm0, 0x40(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x3691e
cmpq $0x0, 0xb0(%rsp)
jne 0x36a0d
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x36a0b
movq 0x8(%rsp), %rdi
callq 0x2220
jmp 0x36a0d
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
addq $0xd8, %rsp
retq
nopl (%rax)