// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: get_binomial_pvalue_per_region(double*, double*, int, int, double*, double, bool, int)
double get_binomial_pvalue_per_region(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)
{
	// Get the p-value for the current region.
	double total_profile_signal = 0;
	double total_control_signal = 0;
	for(int i = region_start; i <= region_end; i++)
	{
		total_profile_signal += floor(signal_track_data[i]);
		total_control_signal += (control_track_data[i]);
	} // i loop.

	//// If the region size is smaller than 2000 base pairs, get the control signal from a larger region.
	//if((region_end - region_start+1) <= 2000)
	//{
	//	int eff_reg_start = (region_start+region_end)/2 - 2500;
	//	int eff_reg_end = (region_start+region_end)/2 + 2500;

	//	for(int i = eff_reg_start; i <= eff_reg_end; i++)
	//	{
	//		total_control_signal += floor(control_track_data[i]);
	//	} // i loop.

	//	// Re-normalize the control signal.
	//	total_control_signal = total_control_signal * (region_end - region_start+1) / 5000;
	//}

	total_profile_signal /= scaling_factor;

	//// Get the control signal: Make sure there is enough reads.
	//double total_control_signal = 0.0;
	//int ext_region_start = region_start;
	//int ext_region_end = region_end;
	//while(total_control_signal < MIN_N_CONTROL_SIGNAL/scaling_factor &&
	//	ext_region_start > 0)
	//{
	//	for(int i = ext_region_start; i <= ext_region_end; i++)
	//	{
	//		total_control_signal += floor(control_track_data[i]);	
	//	} // i loop.
	//} // Extension loop.

	//// Region not significant: There is not enough control reads.
	//if(total_control_signal < MIN_N_CONTROL_SIGNAL/scaling_factor)
	//{
	//	//fprintf(stderr, "Could not find enough control signal, returning p_val=1.\n");
	//	return(1.0);
	//}

	// Scale control signal.
	total_control_signal /= scaling_factor;

	// Normalize both signals with respect to normalization region, if they are larger than the region size.
	if(length_normalization && 
		(region_end - region_start+1) > l_norm_win)
	{
		total_profile_signal = total_profile_signal / ((double)(region_end - region_start+1) / l_norm_win);
		total_control_signal = total_control_signal / ((double)(region_end - region_start+1) / l_norm_win);

		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.
		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 $0x98, %rsp
movb %r9b, %al
movl 0xa0(%rsp), %r9d
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movl %edx, 0x7c(%rsp)
movl %ecx, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movsd %xmm0, 0x68(%rsp)
andb $0x1, %al
movb %al, 0x67(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x78(%rsp), %eax
jg 0x3718d
movq 0x88(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x80(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x37135
movsd 0x68(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x68(%rsp), %xmm1
movsd 0x50(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
testb $0x1, 0x67(%rsp)
je 0x3728b
movl 0x78(%rsp), %eax
subl 0x7c(%rsp), %eax
addl $0x1, %eax
cmpl 0xa0(%rsp), %eax
jle 0x3728b
movsd 0x58(%rsp), %xmm0
movl 0x78(%rsp), %eax
subl 0x7c(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xa0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x50(%rsp), %xmm0
movl 0x78(%rsp), %eax
subl 0x7c(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xa0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x37258
jp 0x37258
movsd 0x1adc6(%rip), %xmm0    # 0x52018
movsd %xmm0, 0x50(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x37289
jp 0x37289
movsd 0x1ada2(%rip), %xmm0    # 0x52018
callq 0x441e0
movsd %xmm0, 0x90(%rsp)
jmp 0x373ef
jmp 0x3728b
movsd 0x58(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x372c3
movl 0x48(%rsp), %edi
addl $0x3, %edi
callq 0x3f420
movq %rax, 0x40(%rsp)
jmp 0x372cd
movq 0x70(%rsp), %rax
movq %rax, 0x40(%rsp)
movsd 0x1ad63(%rip), %xmm0    # 0x52038
callq 0x2140
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
callq 0x441e0
movsd %xmm0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
cvtsi2sdl 0x2c(%rsp), %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x373be
movsd 0x38(%rsp), %xmm0
cvtsi2sdl 0x48(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x40(%rsp), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x40(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movl 0x48(%rsp), %ecx
subl 0x2c(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
callq 0x445a0
movsd %xmm0, 0x18(%rsp)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x44310
movsd %xmm0, 0x30(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x372f6
cmpq $0x0, 0x70(%rsp)
jne 0x373e0
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x373de
movq (%rsp), %rdi
callq 0x2220
jmp 0x373e0
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
addq $0x98, %rsp
retq