// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_p_value_minimization(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double*, int, double*, int, int)
vector<t_annot_region*>* prune_region_ends_per_p_value_minimization(vector<t_annot_region*>* regions, 
	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 500.
		max_sig = (max_sig > 500)?(500):(max_sig);

		// Start thresholding over all possible thresholds.
		int prev_pr_s = -1;
		int prev_pr_e = -1;
		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.

            if(prev_pr_s == pruned_start && prev_pr_e == pruned_end)
            {
                    continue;
            }

            prev_pr_s = pruned_start;
            prev_pr_e = pruned_end;

			// 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);

			// 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.

			// TODO::Following should actually be removed since it creates issues with overextended ER ends.
			// 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(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)
movq %rsi, 0x158(%rsp)
movl %edx, 0x154(%rsp)
movq %rcx, 0x148(%rsp)
movl %r8d, 0x144(%rsp)
movl %r9d, 0x140(%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, 0x138(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x130(%rsp)
movl $0x0, 0x12c(%rsp)
movl 0x12c(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x3900f
xorps %xmm0, %xmm0
movsd %xmm0, 0x120(%rsp)
movq 0x160(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x160(%rsp), %rdi
movslq 0x12c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x60(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x38fc0
movl 0x11c(%rsp), %eax
cmpl 0x154(%rsp), %eax
jge 0x38f71
movq 0x158(%rsp), %rax
movslq 0x11c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
movl 0x11c(%rsp), %eax
cmpl 0x144(%rsp), %eax
jge 0x38fa8
movq 0x148(%rsp), %rax
movslq 0x11c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
jmp 0x38faa
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x38f07
movsd 0x130(%rsp), %xmm1
movsd 0x120(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x38ff7
movsd 0x120(%rsp), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x130(%rsp)
jmp 0x38ff9
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0x38eb2
testb $0x1, 0x2c1ca(%rip)     # 0x651e0
je 0x39039
movq 0x2bfc1(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0x130(%rsp), %xmm0
leaq 0x1d588(%rip), %rsi     # 0x565ba
movb $0x1, %al
callq 0x2250
movsd 0x1bd17(%rip), %xmm0    # 0x54d58
addsd 0x130(%rsp), %xmm0
cvttsd2si %xmm0, %edi
callq 0x3f420
movq %rax, 0x110(%rsp)
testb $0x1, 0x2c17e(%rip)     # 0x651e0
je 0x39095
movq 0x2bf75(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq 0x58(%rsp), %rdi
movl %eax, %edx
leaq 0x1d56b(%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, 0x108(%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, 0x100(%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, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x160(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x397d4
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
cmpl 0x144(%rsp), %eax
jle 0x391ef
movq 0x2be4f(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq 0x30(%rsp), %rdi
movl 0x38(%rsp), %edx
movq (%rax), %rax
movl 0x20(%rax), %ecx
leaq 0x1d454(%rip), %rsi     # 0x56637
movb $0x0, %al
callq 0x2250
jmp 0x397be
movl $0x0, 0xf0(%rsp)
movl $0xf4240, 0xec(%rsp) # imm = 0xF4240
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x2c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x39304
movq 0x158(%rsp), %rax
movslq 0xdc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jge 0x392a9
movl 0xd8(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl 0xec(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jle 0x392c7
movl 0xd8(%rsp), %eax
movl %eax, 0xec(%rsp)
movq 0x158(%rsp), %rax
movslq 0xdc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x39233
cmpl $0x1f4, 0xf0(%rsp) # imm = 0x1F4
jle 0x3931c
movl $0x1f4, %eax            # imm = 0x1F4
movl %eax, 0x28(%rsp)
jmp 0x39327
movl 0xf0(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl $0xffffffff, 0xd4(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xd0(%rsp) # imm = 0xFFFFFFFF
movl 0xec(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jg 0x39605
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc8(%rsp)
movq 0x160(%rsp), %rdi
movslq 0xf4(%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 0x39415
movq 0x158(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0xcc(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x39400
movl 0xc0(%rsp), %eax
movl %eax, 0xc8(%rsp)
jmp 0x39415
jmp 0x39402
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x393bc
movl 0xc4(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jl 0x3947c
movq 0x158(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0xcc(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x39467
movl 0xbc(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0x3947c
jmp 0x39469
movl 0xbc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x39423
movl 0xd4(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jne 0x394a1
movl 0xd0(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jne 0x394a1
jmp 0x395ef
movl 0xc8(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0xd0(%rsp)
movq 0x158(%rsp), %rdi
movq 0x148(%rsp), %rsi
movl 0xc8(%rsp), %edx
movl 0xc4(%rsp), %ecx
movq 0x110(%rsp), %r8
cvtsi2sdl 0x140(%rsp), %xmm0
xorl %r9d, %r9d
movl $0x0, (%rsp)
callq 0x370e0
movsd %xmm0, 0xb0(%rsp)
movq 0x108(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xc850
movq 0x100(%rsp), %rdi
cvtsi2sdl 0xc8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
leaq 0xa8(%rsp), %rsi
callq 0x3ae20
movq 0xf8(%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 0x395cb
movq 0x158(%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 0x39581
movsd 0x98(%rsp), %xmm1
divsd 0xe0(%rsp), %xmm1
movsd 0x18a53(%rip), %xmm0    # 0x52038
ucomisd %xmm1, %xmm0
jb 0x395ed
jmp 0x39605
jmp 0x395ef
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x39356
movq 0x108(%rsp), %rdi
callq 0x11c40
cmpq $0x0, %rax
jbe 0x39763
movq 0x108(%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 0x108(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x396eb
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x108(%rsp), %rdi
movslq 0x80(%rsp), %rsi
callq 0xc930
movsd 0x18(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x396d3
movq 0x108(%rsp), %rdi
movslq 0x80(%rsp), %rsi
callq 0xc930
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x396d5
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x39650
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x78(%rsp)
movq 0x100(%rsp), %rdi
movslq 0x84(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0xf8(%rsp), %rdi
movslq 0x84(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x138(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xc8c0
jmp 0x39797
movq 0x160(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x70(%rsp)
movq 0x138(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xc8c0
movq 0x108(%rsp), %rdi
callq 0x3ae00
movq 0x100(%rsp), %rdi
callq 0x3ae00
movq 0xf8(%rsp), %rdi
callq 0x3ae00
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x3913f
movq 0x110(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x397f1
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x138(%rsp), %rax
addq $0x168, %rsp            # imm = 0x168
retq
nopw %cs:(%rax,%rax)