// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_modified_binomial_p_value_minimization(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double, double*, int, double*, int, int)
vector<t_annot_region*>* prune_region_ends_per_modified_binomial_p_value_minimization(vector<t_annot_region*>* regions, 
	double flip_prob,
	double* signal_profile, int l_profile, 
	double* control_profile, int l_control,
	int l_fragment)
{
	// List of pruned regions.
	vector<t_annot_region*>* pruned_regions = new vector<t_annot_region*>();


	double max_grand_total_int = 0.0;
	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		// Get the extrema signal values.
		double current_grand_total = 0.0;
		for(int i = regions->at(i_reg)->start; i <= regions->at(i_reg)->end; i++)
		{
			if(i < l_profile)
			{
				current_grand_total += signal_profile[i];
			}

			if(i < l_control)
			{
				current_grand_total += control_profile[i];
			}
		} // i loop.

		if(max_grand_total_int < current_grand_total)
		{
			max_grand_total_int = (int)(floor(current_grand_total));
		} 
	} // i_reg loop.

if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
	fprintf(stderr, "Setting up the log factorials with maximum grand total of %lf\n", max_grand_total_int);
}

	double* log_factorials = buffer_log_factorials((int)(max_grand_total_int+20));

if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
	fprintf(stderr, "Pruning region ends for %d regions via p-value minimization.\n", (int)regions->size());
}

	// Go over all the regions.
	vector<double>* p_values_per_threshold = new vector<double>();
	vector<double>* pruned_starts_per_thresholds = new vector<double>();
	vector<double>* pruned_ends_per_thresholds = new vector<double>();
	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		//if(i_reg % 1000 == 0)
		//{
		//	fprintf(stderr, "Processing %d.(%d) region: %d-%d\n", i_reg, (int)regions->size(), regions->at(i_reg)->start, regions->at(i_reg)->end);
		//}

		// Skip the region if the signal is out of the control signal range.
		if(regions->at(i_reg)->end > l_control)
		{
			fprintf(stderr, "Skipping pruning %d-%d\n", regions->at(i_reg)->start, regions->at(i_reg)->end);
			continue;
		}

		// Get the extrema signal values.
		int max_sig = 0;
		int min_sig = 1000*1000;
		double total_signal = 0;
		for(int i = regions->at(i_reg)->start; i <= regions->at(i_reg)->end; i++)
		{
			int cur_floorized_val = (int)(floor(signal_profile[i]));
			if(max_sig < cur_floorized_val)
			{
				max_sig = cur_floorized_val;
			}

			if(min_sig > cur_floorized_val)
			{
				min_sig = cur_floorized_val;
			}

			total_signal += signal_profile[i];
		} // i loop.

		// Limit the maximum at 400.
		max_sig = (max_sig > 500)?(500):(max_sig);

		// Start thresholding over all possible thresholds.			
		for(int sig_thresh = min_sig; sig_thresh <= max_sig; sig_thresh++)
		{
			// Threshold the ends.
			int pruned_start = regions->at(i_reg)->start;
			int pruned_end = regions->at(i_reg)->end;

			//bool passed_threshold = false;
			for(int i_pr = pruned_start; i_pr <= pruned_end; i_pr++)
			{
				if(signal_profile[i_pr] > sig_thresh)
				{
					//passed_threshold = true;
					pruned_start = i_pr;
					break;
				}
			} // i_pr loop.

			for(int i_pr = pruned_end; i_pr >= pruned_start; i_pr--)
			{
				if(signal_profile[i_pr] > sig_thresh)
				{
					pruned_end = i_pr;
					break;
				}
			} // i_pr loop.

			// Get the binomial p-value for current region.
			//double cur_pruned_pvalue = get_binomial_pvalue_per_region(signal_profile, control_profile, pruned_start, pruned_end, log_factorials, l_fragment, false, 0);
			double cur_pruned_pvalue = get_modified_binomial_pvalue_per_region(signal_profile, control_profile, flip_prob, pruned_start, pruned_end, log_factorials, l_fragment, false, 0);

			// Add the current p-value.
			p_values_per_threshold->push_back(cur_pruned_pvalue);
			pruned_starts_per_thresholds->push_back(pruned_start);
			pruned_ends_per_thresholds->push_back(pruned_end);

			double total_pruned_sig = 0;
			for(int i_sig = pruned_start; i_sig <= pruned_end; i_sig++)
			{
				total_pruned_sig += signal_profile[i_sig];
			} // i_sig.

			// If we are below 50% of the total probability, stop since most of the signal is lost already.
			if(total_pruned_sig/total_signal <= 0.5)
			{
				break;
			}
		} // sig thresh loop.

		// If the condition is satisfied, we are done.
		if((int)p_values_per_threshold->size() > 0)
		{
			double min_p_val = p_values_per_threshold->at(0);
			int min_p_val_i = 0;
			for(int i_th = 0; i_th < (int)p_values_per_threshold->size(); i_th++)
			{
				if(min_p_val > p_values_per_threshold->at(i_th))
				{
					min_p_val = p_values_per_threshold->at(i_th);
					min_p_val_i = i_th;
				}
			} // total mass fraction check.

			t_annot_region* pruned_peak = duplicate_region(regions->at(i_reg));
			pruned_peak->start = (int)(pruned_starts_per_thresholds->at(min_p_val_i));
			pruned_peak->end = (int)(pruned_ends_per_thresholds->at(min_p_val_i));

			pruned_regions->push_back(pruned_peak);
		}
		else
		{
			t_annot_region* pruned_peak = duplicate_region(regions->at(i_reg));
			pruned_regions->push_back(pruned_peak);
		}

		// Clear the values.
		p_values_per_threshold->clear();
		pruned_starts_per_thresholds->clear();
		pruned_ends_per_thresholds->clear();
	} // i_reg loop.

	delete [] log_factorials;

	// Dump the bed file.
	//dump_BED(op_bed_fp, pruned_regions);

	return(pruned_regions);
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x160(%rsp)
movsd %xmm0, 0x158(%rsp)
movq %rsi, 0x150(%rsp)
movl %edx, 0x14c(%rsp)
movq %rcx, 0x140(%rsp)
movl %r8d, 0x13c(%rsp)
movl %r9d, 0x138(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x68(%rsp), %rdi
callq 0xc670
movq 0x68(%rsp), %rax
movq %rax, 0x130(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x3a618
xorps %xmm0, %xmm0
movsd %xmm0, 0x118(%rsp)
movq 0x160(%rsp), %rdi
movslq 0x124(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x114(%rsp)
movl 0x114(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x160(%rsp), %rdi
movslq 0x124(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x60(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x3a5c9
movl 0x114(%rsp), %eax
cmpl 0x14c(%rsp), %eax
jge 0x3a57a
movq 0x150(%rsp), %rax
movslq 0x114(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x118(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
movl 0x114(%rsp), %eax
cmpl 0x13c(%rsp), %eax
jge 0x3a5b1
movq 0x140(%rsp), %rax
movslq 0x114(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x118(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
jmp 0x3a5b3
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x3a510
movsd 0x128(%rsp), %xmm1
movsd 0x118(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a600
movsd 0x118(%rsp), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x128(%rsp)
jmp 0x3a602
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x3a4bb
testb $0x1, 0x2abc1(%rip)     # 0x651e0
je 0x3a642
movq 0x2a9b8(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0x128(%rsp), %xmm0
leaq 0x1bf7f(%rip), %rsi     # 0x565ba
movb $0x1, %al
callq 0x2250
movsd 0x1a70e(%rip), %xmm0    # 0x54d58
addsd 0x128(%rsp), %xmm0
cvttsd2si %xmm0, %edi
callq 0x3f420
movq %rax, 0x108(%rsp)
testb $0x1, 0x2ab75(%rip)     # 0x651e0
je 0x3a69e
movq 0x2a96c(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq 0x58(%rsp), %rdi
movl %eax, %edx
leaq 0x1bf62(%rip), %rsi     # 0x565f9
movb $0x0, %al
callq 0x2250
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x40(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x40(%rsp), %rdi
callq 0xc840
movq 0x40(%rsp), %rax
movq %rax, 0x100(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x48(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x48(%rsp), %rdi
callq 0xc840
movq 0x48(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x50(%rsp), %rdi
callq 0xc840
movq 0x50(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x3ad8e
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
cmpl 0x13c(%rsp), %eax
jle 0x3a7f8
movq 0x2a846(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq 0x30(%rsp), %rdi
movl 0x38(%rsp), %edx
movq (%rax), %rax
movl 0x20(%rax), %ecx
leaq 0x1be4b(%rip), %rsi     # 0x56637
movb $0x0, %al
callq 0x2250
jmp 0x3ad78
movl $0x0, 0xe8(%rsp)
movl $0xf4240, 0xe4(%rsp) # imm = 0xF4240
xorps %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x3a90d
movq 0x150(%rsp), %rax
movslq 0xd4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
movl %eax, 0xd0(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jge 0x3a8b2
movl 0xd0(%rsp), %eax
movl %eax, 0xe8(%rsp)
movl 0xe4(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jle 0x3a8d0
movl 0xd0(%rsp), %eax
movl %eax, 0xe4(%rsp)
movq 0x150(%rsp), %rax
movslq 0xd4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x3a83c
cmpl $0x1f4, 0xe8(%rsp) # imm = 0x1F4
jle 0x3a925
movl $0x1f4, %eax            # imm = 0x1F4
movl %eax, 0x28(%rsp)
jmp 0x3a930
movl 0xe8(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0xe8(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xe8(%rsp), %eax
jg 0x3abc0
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc8(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0xc4(%rsp)
movl 0xc8(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jg 0x3aa08
movq 0x150(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0xcc(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3a9f3
movl 0xc0(%rsp), %eax
movl %eax, 0xc8(%rsp)
jmp 0x3aa08
jmp 0x3a9f5
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x3a9af
movl 0xc4(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jl 0x3aa6f
movq 0x150(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0xcc(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3aa5a
movl 0xbc(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0x3aa6f
jmp 0x3aa5c
movl 0xbc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x3aa16
movq 0x150(%rsp), %rdi
movq 0x140(%rsp), %rsi
movsd 0x158(%rsp), %xmm0
movl 0xc8(%rsp), %edx
movl 0xc4(%rsp), %ecx
movq 0x108(%rsp), %r8
cvtsi2sdl 0x138(%rsp), %xmm1
xorl %r9d, %r9d
movl $0x0, (%rsp)
callq 0x37400
movsd %xmm0, 0xb0(%rsp)
movq 0x100(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xc850
movq 0xf8(%rsp), %rdi
cvtsi2sdl 0xc8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
leaq 0xa8(%rsp), %rsi
callq 0x3ae20
movq 0xf0(%rsp), %rdi
cvtsi2sdl 0xc4(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0xa0(%rsp), %rsi
callq 0x3ae20
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0xc8(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jg 0x3ab86
movq 0x150(%rsp), %rax
movslq 0x94(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x3ab3c
movsd 0x98(%rsp), %xmm1
divsd 0xd8(%rsp), %xmm1
movsd 0x17498(%rip), %xmm0    # 0x52038
ucomisd %xmm1, %xmm0
jb 0x3aba8
jmp 0x3abc0
jmp 0x3abaa
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x3a949
movq 0x100(%rsp), %rdi
callq 0x11c40
cmpl $0x0, %eax
jle 0x3ad1d
movq 0x100(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc930
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl $0x0, 0x80(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x100(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x3aca5
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x100(%rsp), %rdi
movslq 0x80(%rsp), %rsi
callq 0xc930
movsd 0x18(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x3ac8d
movq 0x100(%rsp), %rdi
movslq 0x80(%rsp), %rsi
callq 0xc930
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x3ac8f
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x3ac0a
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x78(%rsp)
movq 0xf8(%rsp), %rdi
movslq 0x84(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0xf0(%rsp), %rdi
movslq 0x84(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x130(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xc8c0
jmp 0x3ad51
movq 0x160(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x70(%rsp)
movq 0x130(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xc8c0
movq 0x100(%rsp), %rdi
callq 0x3ae00
movq 0xf8(%rsp), %rdi
callq 0x3ae00
movq 0xf0(%rsp), %rdi
callq 0x3ae00
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3a748
movq 0x108(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3adab
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x130(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
nopl (%rax,%rax)