// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, %r14d
movl %r8d, %ebp
movq %rcx, 0x68(%rsp)
movl %edx, %ebx
movq %rsi, %r12
movsd %xmm0, 0x80(%rsp)
movq %rdi, 0x28(%rsp)
pushq $0x18
popq %rdi
callq 0x2170
movl %ebp, 0x54(%rsp)
movslq %ebp, %r15
movslq %ebx, %r13
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movq %rax, 0x40(%rsp)
movupd %xmm0, (%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1bf68
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbp
jg 0x1bf3f
cmpq %r13, %rbp
movsd 0x8(%rsp), %xmm0
jge 0x1bf25
addsd (%r12,%rbp,8), %xmm0
cmpq %r15, %rbp
jge 0x1bf34
movq 0x68(%rsp), %rax
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %rbp
jmp 0x1befb
movsd 0x8(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x1bf60
callq 0x22b0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
incq %rbx
jmp 0x1bec7
cmpb $0x0, 0x19259(%rip)     # 0x351c8
jne 0x1c3c5
movapd 0x30(%rsp), %xmm0
addsd 0xaa45(%rip), %xmm0     # 0x269c8
cvttsd2si %xmm0, %edi
callq 0x1e201
movq %rax, 0x58(%rsp)
cmpb $0x1, 0x19230(%rip)     # 0x351c8
movq 0x28(%rsp), %r13
je 0x1c3e8
pushq $0x18
popq %r15
movq %r15, %rdi
callq 0x2170
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r15, %rdi
callq 0x2170
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r15, %rdi
callq 0x2170
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x78(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x20(%rsp)
andq $0x0, 0x10(%rax)
xorl %r15d, %r15d
movq %rbp, 0x48(%rsp)
movq %rbx, 0x60(%rsp)
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x1c39f
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x54(%rsp), %ecx
cmpl %ecx, 0x20(%rax)
movq %r15, 0x18(%rsp)
jg 0x1c355
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0xf4240, %ebx          # imm = 0xF4240
xorl %ebp, %ebp
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %r14
jg 0x1c0b0
movsd (%r12,%r14,8), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0x22b0
cvttsd2si %xmm0, %eax
cmpl %eax, %ebp
cmovlel %eax, %ebp
cmpl %eax, %ebx
cmovgel %eax, %ebx
movsd 0x8(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %r14
jmp 0x1c063
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ebp
cmovgel %eax, %ebp
movl %ebp, 0x30(%rsp)
cmpl 0x30(%rsp), %ebx
jg 0x1c210
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebp
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r15d
cvtsi2sd %ebx, %xmm0
movslq %ebp, %r13
movslq %r15d, %r14
movq %r13, %rax
shlq $0x20, %rax
movq %r13, %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
movabsq $0x100000000, %rsi      # imm = 0x100000000
cmpq %r14, %rcx
jg 0x1c135
movsd (%r12,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1c12c
incq %rcx
addq %rsi, %rax
jmp 0x1c113
sarq $0x20, %rax
movq %rax, %r13
movl %ecx, %ebp
movq %r14, %rax
shlq $0x20, %rax
movq %r14, %rcx
cmpq %r13, %rcx
jl 0x1c162
movsd (%r12,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1c158
decq %rcx
addq %rdx, %rax
jmp 0x1c13f
sarq $0x20, %rax
movq %rax, %r14
movl %ecx, %r15d
andl $0x0, (%rsp)
movq %r12, %rdi
movq 0x68(%rsp), %rsi
movsd 0x80(%rsp), %xmm0
movl %ebp, %edx
movl %r15d, %ecx
movq 0x58(%rsp), %r8
movsd 0x78(%rsp), %xmm1
xorl %r9d, %r9d
callq 0x1a1bb
movsd %xmm0, 0x10(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7426
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
callq 0x1c87e
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x1c87e
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %r15
cmpq %r14, %r13
jg 0x1c1f1
addsd (%r12,%r13,8), %xmm0
incq %r13
jmp 0x1c1e1
divsd 0x8(%rsp), %xmm0
incl %ebx
movsd 0x7e57(%rip), %xmm1     # 0x24058
ucomisd %xmm0, %xmm1
movq 0x28(%rsp), %r13
jb 0x1c0be
movq 0x60(%rsp), %r14
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x1c2ea
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movq 0x48(%rsp), %rbp
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x1c28c
movq %r14, %rdi
movq %r15, %rsi
callq 0x7466
movsd 0x8(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x1c287
movq %r14, %rdi
movq %r15, %rsi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movl %r15d, %ebx
incq %r15
jmp 0x1c246
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x10(%rsp)
movslq %ebx, %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x10(%rsp), %rcx
movl %eax, 0xc(%rcx)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x10(%rsp), %rcx
movl %eax, 0x20(%rcx)
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7448
jmp 0x1c316
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7448
movq 0x48(%rsp), %rbp
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x1c323
movq %rax, 0x8(%r14)
movq (%rbp), %rax
cmpq %rax, 0x8(%rbp)
movq 0x18(%rsp), %r15
je 0x1c336
movq %rax, 0x8(%rbp)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1c34d
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %r15
jmp 0x1c009
movq 0x18c84(%rip), %rax     # 0x34fe0
movq (%rax), %r15
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebp
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ecx
movq %r15, %rdi
movq 0x18(%rsp), %r15
leaq 0xbef7(%rip), %rsi      # 0x2828b
movl %ebp, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x1c34d
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x1c3ae
callq 0x2210
movq 0x40(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18c14(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xbe38(%rip), %rsi      # 0x2820e
movapd 0x30(%rsp), %xmm0
movb $0x1, %al
callq 0x2240
jmp 0x1bf75
movq 0x18bf1(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movq 0x8(%r13), %rdx
subq (%r13), %rdx
shrq $0x3, %rdx
leaq 0xbe48(%rip), %rsi      # 0x2824d
xorl %eax, %eax
callq 0x2240
jmp 0x1bfa3