// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, 0x14(%rsp)
movl %r8d, %r14d
movq %rcx, 0x78(%rsp)
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
movl %r14d, 0x64(%rsp)
movslq %r14d, %r12
movslq %ebp, %r13
movq %rbx, %rbp
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movq %rax, 0x48(%rsp)
movupd %xmm0, (%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
xorl %r14d, %r14d
movq %rbx, 0x28(%rsp)
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x1b209
movq %rbp, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbp
jg 0x1b1db
cmpq %r13, %rbp
movsd 0x8(%rsp), %xmm0
jge 0x1b1c1
addsd (%r15,%rbp,8), %xmm0
cmpq %r12, %rbp
jge 0x1b1d0
movq 0x78(%rsp), %rax
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %rbp
jmp 0x1b197
movsd 0x8(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x1b1fc
callq 0x22b0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
incq %r14
movq 0x28(%rsp), %rbp
jmp 0x1b164
cmpb $0x0, 0x19fb8(%rip)     # 0x351c8
jne 0x1b67c
movapd 0x30(%rsp), %xmm0
addsd 0xb7a4(%rip), %xmm0     # 0x269c8
cvttsd2si %xmm0, %edi
callq 0x1e201
movq %rax, 0x68(%rsp)
cmpb $0x1, 0x19f8f(%rip)     # 0x351c8
je 0x1b69f
pushq $0x18
popq %r14
movq %r14, %rdi
callq 0x2170
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x2170
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x2170
cvtsi2sdl 0x14(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x40(%rsp)
andq $0x0, 0x10(%rax)
xorl %r13d, %r13d
movq %r12, 0x70(%rsp)
movq %rbx, 0x50(%rsp)
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x1b656
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x64(%rsp), %ecx
cmpl %ecx, 0x20(%rax)
movq %r13, 0x20(%rsp)
jg 0x1b60c
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0xf4240, %ebx          # imm = 0xF4240
xorl %r12d, %r12d
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %r14
jg 0x1b353
movsd (%r15,%r14,8), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0x22b0
cvttsd2si %xmm0, %eax
cmpl %eax, %r12d
cmovlel %eax, %r12d
cmpl %eax, %ebx
cmovgel %eax, %ebx
movsd 0x8(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %r14
jmp 0x1b304
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %r12d
cmovgel %eax, %r12d
pushq $-0x1
popq %rax
movl %eax, 0x30(%rsp)
movl %eax, 0x14(%rsp)
cmpl %r12d, %ebx
jg 0x1b4ba
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r13d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r14d
cvtsi2sd %ebx, %xmm0
movslq %r13d, %rbp
movslq %r14d, %rax
movq %rbp, %rcx
shlq $0x20, %rcx
movq %rbp, %rdx
movabsq $0x100000000, %rsi      # imm = 0x100000000
cmpq %rax, %rdx
jg 0x1b3e2
movsd (%r15,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1b3d8
incq %rdx
addq %rsi, %rcx
jmp 0x1b3bf
sarq $0x20, %rcx
movq %rcx, %rbp
movl %edx, %r13d
cmpq %rbp, %rax
jl 0x1b3fb
movsd (%r15,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1b3f8
decq %rax
jmp 0x1b3e2
movl %eax, %r14d
cmpl %r13d, 0x30(%rsp)
jne 0x1b40d
cmpl %r14d, 0x14(%rsp)
je 0x1b4b3
andl $0x0, (%rsp)
movq %r15, %rdi
movq 0x78(%rsp), %rsi
movl %r13d, %edx
movl %r14d, %ecx
movq 0x68(%rsp), %r8
movsd 0x80(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x19fac
movsd %xmm0, 0x18(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7426
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1c87e
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1c87e
movslq %r14d, %rax
xorpd %xmm0, %xmm0
cmpq %rax, %rbp
jg 0x1b495
addsd (%r15,%rbp,8), %xmm0
incq %rbp
jmp 0x1b485
divsd 0x8(%rsp), %xmm0
movsd 0x8bb5(%rip), %xmm1     # 0x24058
ucomisd %xmm0, %xmm1
movl %r13d, 0x30(%rsp)
movl %r14d, 0x14(%rsp)
jae 0x1b4ba
incl %ebx
jmp 0x1b36a
movq 0x70(%rsp), %r12
movq 0x8(%r12), %rax
cmpq (%r12), %rax
je 0x1b59a
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %r13
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x1b539
movq %r12, %rdi
movq %r14, %rsi
callq 0x7466
movsd 0x8(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x1b534
movq %r12, %rdi
movq %r14, %rsi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movl %r14d, %ebx
incq %r14
jmp 0x1b4f1
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x18(%rsp)
movslq %ebx, %r14
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0xc(%rcx)
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x20(%rcx)
movq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7448
jmp 0x1b5d0
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7448
movq 0x50(%rsp), %rbx
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x1b5e0
movq %rax, 0x8(%r12)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x1b5ed
movq %rax, 0x8(%rbx)
movq 0x40(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1b604
movq 0x40(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %r13
jmp 0x1b2a9
movq 0x199cd(%rip), %rax     # 0x34fe0
movq (%rax), %r14
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r13d
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ecx
movq %r14, %rdi
leaq 0xcc46(%rip), %rsi      # 0x2828b
movl %r13d, %edx
movq 0x20(%rsp), %r13
xorl %eax, %eax
callq 0x2240
jmp 0x1b604
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1b665
callq 0x2210
movq 0x48(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1995d(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xcb81(%rip), %rsi      # 0x2820e
movapd 0x30(%rsp), %xmm0
movb $0x1, %al
callq 0x2240
jmp 0x1b216
movq 0x1993a(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movq 0x8(%rbp), %rdx
subq (%rbp), %rdx
shrq $0x3, %rdx
leaq 0xcb91(%rip), %rsi      # 0x2824d
xorl %eax, %eax
callq 0x2240
jmp 0x1b23f