// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movl %r8d, %ebx
movq %rcx, 0x48(%rsp)
movl %edx, %ebp
movq %rsi, 0x50(%rsp)
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r14), %r9
movq %r14, 0x20(%rsp)
movq 0x8(%r14), %rdi
movq %rdi, %rax
subq %r9, %rax
sarq $0x3, %rax
testl %eax, %eax
movl %ebx, 0x1c(%rsp)
movq %r15, 0x70(%rsp)
jle 0x2052e
movslq %ebp, %rbp
movslq %ebx, %r14
movq %rax, 0x60(%rsp)
movl %eax, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movq 0x48(%rsp), %rdx
cmpq %r9, %rdi
je 0x20b0b
movq (%r9,%r13,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x204fb
subl %eax, %ecx
incl %ecx
cmpq %rbp, %rax
jge 0x204ea
movq 0x50(%rsp), %rsi
addsd (%rsi,%rax,8), %xmm0
cmpq %r14, %rax
jge 0x204f4
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x204db
ucomisd %xmm1, %xmm0
jbe 0x20524
movq %rdi, %r15
movq %r9, %rbx
callq 0x22c0
movq %rbx, %r9
movq %r15, %rdi
movq 0x70(%rsp), %r15
movq 0x48(%rsp), %rdx
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %r13
cmpq %r12, %r13
jne 0x204bb
jmp 0x20532
xorpd %xmm1, %xmm1
cmpb $0x0, 0x19c97(%rip)     # 0x3a1d0
jne 0x20a9b
addsd 0xd471(%rip), %xmm1     # 0x2d9b8
cvttsd2si %xmm1, %edi
callq 0x23bc5
movq %rax, 0x78(%rsp)
cmpb $0x1, 0x19c74(%rip)     # 0x3a1d0
movl 0x1c(%rsp), %ebp
movq 0x50(%rsp), %r14
je 0x20ac8
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
movq %rax, 0x30(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x40(%rsp)
movq %rbx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x20a77
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
xorl %r8d, %r8d
cmpq %r8, %rdx
jbe 0x20b1c
movq (%rax,%r8,8), %rax
movl 0x20(%rax), %ebx
cmpl %ebp, %ebx
movq %r8, 0x8(%rsp)
jg 0x20a53
movslq 0xc(%rax), %rax
cmpl %ebx, %eax
jle 0x20621
xorpd %xmm2, %xmm2
movl $0xf4240, %ebp          # imm = 0xF4240
xorl %r13d, %r13d
jmp 0x20678
leaq (%r14,%rax,8), %r12
subl %eax, %ebx
incl %ebx
xorl %r14d, %r14d
movl $0xf4240, %r15d         # imm = 0xF4240
xorps %xmm2, %xmm2
xorl %r13d, %r13d
movsd %xmm2, 0x10(%rsp)
movsd (%r12,%r14,8), %xmm0
movsd %xmm0, 0x60(%rsp)
callq 0x22c0
movsd 0x10(%rsp), %xmm2
cvttsd2si %xmm0, %ebp
cmpl %ebp, %r13d
cmovlel %ebp, %r13d
cmpl %ebp, %r15d
cmovll %r15d, %ebp
addsd 0x60(%rsp), %xmm2
incq %r14
movl %ebp, %r15d
cmpl %r14d, %ebx
jne 0x20638
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %r13d
cmovgel %eax, %r13d
cmpl %r13d, %ebp
movq 0x70(%rsp), %r15
movq 0x50(%rsp), %r14
movq 0x8(%rsp), %r10
jg 0x20891
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movsd %xmm2, 0x10(%rsp)
movq 0x20(%rsp), %rdx
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq %r10, %rdx
jbe 0x20aee
movq (%rsi,%r10,8), %rsi
movslq 0xc(%rsi), %rdx
movl %edx, %ebx
movl 0x20(%rsi), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
cmpl %r12d, %edx
jg 0x2070f
leaq (%r14,%rdx,8), %rsi
movl %r12d, %edi
notl %edi
addq %rbx, %rdi
xorl %r8d, %r8d
movsd (%rsi,%r8,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2070a
incq %r8
movl %edi, %r9d
addl %r8d, %r9d
jne 0x206f1
jmp 0x2070f
addl %r8d, %edx
movl %edx, %ebx
cmpl %ebx, %r12d
jl 0x2073c
movslq %r12d, %rdx
movslq %ebx, %rsi
incq %rdx
movl %r12d, %edi
movsd -0x8(%r14,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x20739
decl %edi
decq %rdx
cmpq %rsi, %rdx
jg 0x20720
jmp 0x2073c
movl %edi, %r12d
cmpl %ebx, %ecx
jne 0x2074e
movl $0x10, %edx
cmpl %r12d, %eax
je 0x2087e
movl $0x0, (%rsp)
movq %r14, %rdi
movq 0x48(%rsp), %rsi
movl %ebx, %edx
movl %r12d, %ecx
movq 0x78(%rsp), %r8
movsd 0x80(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x1eb72
movsd %xmm0, 0x38(%rsp)
movq 0x30(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2079b
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x207a5
leaq 0x38(%rsp), %rdx
callq 0xb836
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
movq 0x40(%rsp), %rax
movq 0x8(%rsp), %r10
movsd 0x10(%rsp), %xmm2
je 0x207df
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x207f9
leaq 0x58(%rsp), %rdx
callq 0x21e80
movsd 0x10(%rsp), %xmm2
movq 0x8(%rsp), %r10
movq 0x40(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x2081f
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x20837
movq %rax, %rdi
leaq 0x58(%rsp), %rdx
callq 0x21e80
movsd 0x10(%rsp), %xmm2
movq 0x8(%rsp), %r10
xorpd %xmm0, %xmm0
cmpl %r12d, %ebx
jg 0x2085c
movslq %ebx, %rax
leaq (%r14,%rax,8), %rax
movl %r12d, %ecx
subl %ebx, %ecx
incl %ecx
xorl %edx, %edx
addsd (%rax,%rdx,8), %xmm0
incq %rdx
cmpl %edx, %ecx
jne 0x20850
divsd %xmm2, %xmm0
movsd 0xa7f0(%rip), %xmm1     # 0x2b058
ucomisd %xmm0, %xmm1
movl $0xe, %edx
movl $0x0, %eax
cmovbl %eax, %edx
movl %ebx, %ecx
movl %r12d, %eax
testb $0xf, %dl
jne 0x20891
leal 0x1(%rbp), %edx
cmpl %r13d, %ebp
movl %edx, %ebp
jne 0x206ac
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
je 0x208ec
sarq $0x3, %rdx
testl %edx, %edx
movl 0x1c(%rsp), %ebp
jle 0x20935
movsd (%rax), %xmm0
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0x20adb
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %esi, %edi
minsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rcx
jne 0x208c2
movslq %edi, %rbx
jmp 0x20937
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r10, %rdx
movl 0x1c(%rsp), %ebp
jbe 0x20aee
movq (%rax,%r10,8), %rdi
callq 0x172ac
movq %rax, 0x38(%rsp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x209e0
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x209ed
xorl %ebx, %ebx
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r10, %rdx
jbe 0x20aee
movq (%rax,%r10,8), %rdi
callq 0x172ac
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x20aff
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x40(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x20aff
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
movq 0x8(%rsp), %r8
je 0x209c2
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x209d4
movq %r15, %rdi
leaq 0x38(%rsp), %rdx
callq 0xb976
movq 0x8(%rsp), %r8
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
jmp 0x209fc
movq %r15, %rdi
leaq 0x38(%rsp), %rdx
callq 0xb976
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %r8
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x20a09
movq %rax, 0x8(%rcx)
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x20a16
movq %rax, 0x8(%rdx)
movq 0x40(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x20a28
movq %rax, 0x8(%rcx)
incq %r8
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r8
jl 0x205ee
jmp 0x20a77
movq 0x19586(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movl 0xc(%rax), %edx
leaq 0xe824(%rip), %rsi      # 0x2f28b
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %r8
jmp 0x20a28
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x20a86
callq 0x2220
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1953e(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0xe762(%rip), %rsi      # 0x2f20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x60(%rsp)
callq 0x2250
movapd 0x60(%rsp), %xmm1
jmp 0x2053f
movq 0x20(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x52a5
jmp 0x2056b
leal -0x1(%rdx), %esi
cmpq %rsi, %rdx
cmovbq %rdx, %rsi
leaq 0xcdea(%rip), %rdi      # 0x2d8d6
jmp 0x20af8
leaq 0xcde1(%rip), %rdi      # 0x2d8d6
movq %r10, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0xcdd0(%rip), %rdi      # 0x2d8d6
movq %rbx, %rsi
jmp 0x20af8
leaq 0xcdc4(%rip), %rdi      # 0x2d8d6
movq 0x60(%rsp), %rdx
movq %rdx, %rsi
jmp 0x20af8
leaq 0xcdb3(%rip), %rdi      # 0x2d8d6
movq %r8, %rsi
jmp 0x20af8