// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: merge_SSERs_2_features_per_chromosome(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::vector<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::allocator<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*>>*, double*, int, double*, int, double, int, int)
vector<t_annot_region*>* merge_SSERs_2_features_per_chromosome(vector<t_annot_region*>* top_scale_merged_regions,
																vector<vector<t_annot_region*>*>* per_scale_SSERs, 
																double* signal_profile, int l_chip_signal,
																double* control_profile, int l_control_signal,
																double max_p_val,
																int l_fragment, 
																int l_p_val_norm_win)
{
	// Initialize the list of regions.
	for(int i_reg = 0; i_reg < (int)top_scale_merged_regions->size(); i_reg++)
	{
		vector<t_annot_region*>** per_scale_merged_regions = new vector<t_annot_region*>*[per_scale_SSERs->size() + 2];
		for(int scale_i = 0; scale_i < (int)per_scale_SSERs->size(); scale_i++)
		{
			per_scale_merged_regions[scale_i] = new vector<t_annot_region*>();
		} // scale_i

		top_scale_merged_regions->at(i_reg)->data = per_scale_merged_regions;
	} // i_reg loop.

	// Merge SSERs cumulatively.
	double max_grand_total_int = 1000*1000*10;
	double* log_factorials = buffer_log_factorials((int)(max_grand_total_int+20));
	for(int scale_i = 0; scale_i < (int)per_scale_SSERs->size(); scale_i++)
	{
		fprintf(stderr, "Generating cumulative merged regions @ scale %d                  \r", scale_i);

		// If there are no SSERs in this scale, dont process.
		if(per_scale_SSERs->at(scale_i)->size() == 0)
		{
			continue;
		}

		// Pool all the regions upto this scale.
		vector<t_annot_region*>* SSERs_upto_cur_scale = new vector<t_annot_region*>();
		for(int scale_j = 0; scale_j <= scale_i; scale_j++)
		{
			SSERs_upto_cur_scale->insert(SSERs_upto_cur_scale->end(), per_scale_SSERs->at(scale_j)->begin(), per_scale_SSERs->at(scale_j)->end());
		} // scale_j 

		vector<t_annot_region*>* merged_SSERs_upto_cur_scale = merge_annot_regions(SSERs_upto_cur_scale, 1);

		fprintf(stderr, "Computing p-values @ scale %d                  \r", scale_i);
		for(int i_reg = 0; i_reg < (int)merged_SSERs_upto_cur_scale->size(); i_reg++)
		{
			double cur_region_log_p_val = 0;
			if(merged_SSERs_upto_cur_scale->at(i_reg)->end < l_control_signal && 
				merged_SSERs_upto_cur_scale->at(i_reg)->end < l_chip_signal)
			{
				cur_region_log_p_val = get_binomial_pvalue_per_region_neighborhood_window_control(signal_profile, control_profile, 
																	merged_SSERs_upto_cur_scale->at(i_reg)->start, 
																	merged_SSERs_upto_cur_scale->at(i_reg)->end,
																	log_factorials,
																	l_fragment, 
																	true, 
																	l_p_val_norm_win, 
																	l_chip_signal, 
																	l_control_signal,
																	1);
			}

			t_significance_info* sig_info = new t_significance_info();
			sig_info->log_p_val = cur_region_log_p_val;
			merged_SSERs_upto_cur_scale->at(i_reg)->data = sig_info;
		} // i_reg loop.

		vector<t_annot_region*>* cur_scale_intersects = intersect_annot_regions(top_scale_merged_regions, merged_SSERs_upto_cur_scale, true);
		for(int i_int = 0; i_int < (int)cur_scale_intersects->size(); i_int++)
		{
			t_intersect_info* int_info = (t_intersect_info*)(cur_scale_intersects->at(i_int)->data);
			t_annot_region* cur_top_scale_merged_reg = int_info->src_reg;
			t_annot_region* merged_SSER_reg_upto_cur_scale = int_info->dest_reg;

			vector<t_annot_region*>* cur_scale_regs = ((vector<t_annot_region*>**)(cur_top_scale_merged_reg->data))[scale_i];
			t_annot_region* dup_reg = duplicate_region(merged_SSER_reg_upto_cur_scale);
			dup_reg->data = merged_SSER_reg_upto_cur_scale->data;
			cur_scale_regs->push_back(dup_reg);

			delete int_info;
		} // i_int loop.

		// Free memory.
		delete_annot_regions(cur_scale_intersects);
		delete_annot_regions(merged_SSERs_upto_cur_scale);
		delete SSERs_upto_cur_scale;
	} // scale_i loop.
	delete [] log_factorials;

	// For each region at the highest scale, compute p-value, if not significant, fall down to smaller scale merged ERs. If there are significant ERs, use those.
	//FILE* f_merging_info = fopen("merging_info.bed", "w");
	vector<t_annot_region*>* merged_SSERs = new vector<t_annot_region*>();
	for(int i_reg = 0; i_reg < (int)top_scale_merged_regions->size(); i_reg++)
	{
		vector<t_annot_region*>** cur_regs_per_scale_SSERs = (vector<t_annot_region*>**)(top_scale_merged_regions->at(i_reg)->data); 

		//Start from top scale and check whether there is a significant merged SSER.
		bool found_cur_reg_scale = false;
		//int found_scale_i = 0;
		for(int scale_i = per_scale_SSERs->size()-1; scale_i >= 0; scale_i--)
		{
			for(int cur_scale_reg_i = 0; cur_scale_reg_i < (int)(cur_regs_per_scale_SSERs[scale_i]->size()); cur_scale_reg_i++)
			{
				t_annot_region* cur_reg_per_scale_SSERs = cur_regs_per_scale_SSERs[scale_i]->at(cur_scale_reg_i);
				t_significance_info* sig_info = (t_significance_info*)(cur_reg_per_scale_SSERs->data);
				if(sig_info->log_p_val < max_p_val)
				{
					// Significant region. Break.
					found_cur_reg_scale = true;
					break;
				}
			} // cur_scale_reg_i optino.

			if(found_cur_reg_scale)
			{
				for(int cur_scale_reg_i = 0; cur_scale_reg_i < (int)(cur_regs_per_scale_SSERs[scale_i]->size()); cur_scale_reg_i++)
				{
					t_annot_region* cur_reg_per_scale_SSERs = cur_regs_per_scale_SSERs[scale_i]->at(cur_scale_reg_i);
					//t_significance_info* sig_info = (t_significance_info*)(cur_reg_per_scale_SSERs->data);
					merged_SSERs->push_back(duplicate_region(cur_reg_per_scale_SSERs));

					//fprintf(f_merging_info, "%s\t%d\t%d\t%s\t%d\t%d\t%lf\n", top_scale_merged_regions->at(i_reg)->chrom, 
					//	top_scale_merged_regions->at(i_reg)->start,
					//	top_scale_merged_regions->at(i_reg)->end, 
					//	cur_reg_per_scale_SSERs->chrom, 
					//	cur_reg_per_scale_SSERs->start, 
					//	cur_reg_per_scale_SSERs->end,
					//	sig_info->log_p_val);
				} // cur_scale_reg_i loop.

				//found_scale_i = scale_i;

				break;
			}
		} // scale_i loop.

		if(!found_cur_reg_scale)
		{
			fprintf(stderr, "Could not find a significant scale for %s:%d-%d\n", top_scale_merged_regions->at(i_reg)->chrom, 
				top_scale_merged_regions->at(i_reg)->start, top_scale_merged_regions->at(i_reg)->end);
			exit(0);
		}

		//else if(found_scale_i != per_scale_SSERs->size()-1)
		//{
		//	fprintf(stderr, "Significant scale not top %s:%d-%d\n", top_scale_merged_regions->at(i_reg)->chrom,
		//			top_scale_merged_regions->at(i_reg)->start, top_scale_merged_regions->at(i_reg)->end);
		//			getc(stdin);
		//}
	} // i_reg loop.

	//fclose(f_merging_info);

	// Free the merged region list.
	for(int i_reg = 0; i_reg < (int)top_scale_merged_regions->size(); i_reg++)
	{
		vector<t_annot_region*>** per_scale_merged_regions = (vector<t_annot_region*>**)(top_scale_merged_regions->at(i_reg)->data);
		for(int scale_i = 0; scale_i < (int)per_scale_SSERs->size(); scale_i++)
		{
			for(int cur_scale_reg_i = 0; cur_scale_reg_i < (int)(per_scale_merged_regions[scale_i]->size()); cur_scale_reg_i++)
			{
				t_significance_info* sig_info = (t_significance_info*)(per_scale_merged_regions[scale_i]->at(cur_scale_reg_i)->data);
				delete sig_info;
			} // i_reg loop.

			if(per_scale_merged_regions[scale_i]->size() > 0)
			{
				delete_annot_regions(per_scale_merged_regions[scale_i]);
			}
			else
			{
				delete per_scale_merged_regions[scale_i];
			}
		} // scale_i
		delete [] per_scale_merged_regions;
	} // i_reg loop.

	return(merged_SSERs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm0, 0x58(%rsp)
movq %r9, 0x40(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x70(%rsp)
movq %rsi, %r14
movq (%rdi), %rcx
movq %rdi, 0x18(%rsp)
movq 0x8(%rdi), %rax
movq %rcx, 0x10(%rsp)
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, 0x8(%rsp)
testl %eax, %eax
jle 0xbb53
movq 0x8(%r14), %rbp
subq (%r14), %rbp
sarq $0x3, %rbp
leaq 0x2(%rbp), %rax
shrq $0x3d, %rax
xorl %r12d, %r12d
negq %rax
movl $0x0, %ecx
sbbq %rcx, %rcx
leaq 0x10(,%rbp,8), %rax
orq %rax, %rcx
movq %rcx, 0x20(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r15
andq %rax, %r15
movq %rbp, 0x28(%rsp)
andq %rax, %rbp
movq 0x20(%rsp), %rdi
callq 0x2040
movq %rax, %rbx
cmpl $0x0, 0x28(%rsp)
jle 0xbb33
xorl %r13d, %r13d
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, (%rbx,%r13,8)
incq %r13
cmpq %r13, %rbp
jne 0xbb0d
cmpq 0x8(%rsp), %r12
je 0xc185
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rax
movq %rbx, 0x48(%rax)
incq %r12
cmpq %r15, %r12
jne 0xbaf6
movl $0x989694, %edi         # imm = 0x989694
callq 0x23bc5
movq %rax, %r12
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
movq 0x40(%rsp), %r13
jle 0xbe93
movq 0x50(%rsp), %rax
cmpl %eax, %r13d
cmovll %r13d, %eax
movl %eax, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xb0(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl $0x1, %ebp
movq 0x2e43a(%rip), %rbx     # 0x39fe0
leaq 0x21f0a(%rip), %r15     # 0x2dab7
xorl %r8d, %r8d
movq %r8, 0x8(%rsp)
movq (%rbx), %rdi
movq %r15, %rsi
movl %r8d, %edx
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %r8
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0xc1b5
movq (%rax,%r8,8), %rax
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
jne 0xbc0b
incq %r8
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
incq %rbp
cmpq %rax, %r8
jl 0xbbb0
jmp 0xbe93
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
xorl %ebx, %ebx
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0xc15c
movq 0x8(%r15), %rsi
movq (%rax,%rbx,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %r15, %rdi
callq 0xb5e2
incq %rbx
cmpq %rbx, %rbp
jne 0xbc2a
movq %r12, 0x38(%rsp)
movq %r15, %rdi
movl $0x1, %esi
callq 0x1ba25
movq %rax, %rbx
movq 0x2e364(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x21e74(%rip), %rsi     # 0x2dafa
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x2250
movq %rbx, %rsi
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x60(%rsp), %rbx
movq %rsi, 0x20(%rsp)
jle 0xbd71
xorl %r12d, %r12d
cmpq %r12, %rdx
jbe 0xc179
movq (%rax,%r12,8), %rax
movl 0x20(%rax), %ecx
xorpd %xmm0, %xmm0
cmpl 0x28(%rsp), %ecx
jge 0xbd07
movl 0xc(%rax), %edx
movq 0x70(%rsp), %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %r8
movsd 0x68(%rsp), %xmm0
movl $0x1, %r9d
pushq $0x1
pushq %r13
pushq 0x60(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
callq 0x1e859
addq $0x20, %rsp
movsd %xmm0, 0x10(%rsp)
movl $0x10, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x20(%rsp), %rsi
movq (%rsi), %rcx
movq 0x8(%rsi), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0xc179
movq (%rcx,%r12,8), %rcx
movq %rax, 0x48(%rcx)
incq %r12
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
jl 0xbcb8
movq 0x18(%rsp), %rdi
movl $0x1, %edx
callq 0x193f3
movq (%rax), %rsi
movq %rax, 0x10(%rsp)
movq 0x8(%rax), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbe3c
xorl %r13d, %r13d
movq %rbp, 0x48(%rsp)
cmpq %r13, %rdx
jbe 0xc1a9
movq (%rsi,%r13,8), %rax
movq 0x48(%rax), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rbp
movq 0x48(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %r12
movq %rbp, %rdi
callq 0x172ac
movq %rax, 0x30(%rsp)
movq 0x48(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0xbdf4
movq %rax, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0xbe01
movq %r12, %rdi
leaq 0x30(%rsp), %rdx
callq 0xb976
movq 0x48(%rsp), %rbp
movl $0x18, %esi
movq %rbx, %rdi
callq 0x2190
incq %r13
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rcx
subq %rsi, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0xbda3
movq 0x10(%rsp), %rdi
callq 0x1acd9
movq 0x20(%rsp), %rdi
callq 0x1acd9
movq (%r15), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %r12
movq 0x2e17e(%rip), %rbx     # 0x39fe0
je 0xbe70
movq 0x10(%r15), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %r15, %rdi
callq 0x2190
movq 0x40(%rsp), %r13
leaq 0x21c2e(%rip), %r15     # 0x2dab7
movq 0x8(%rsp), %r8
jmp 0xbbee
testq %r12, %r12
je 0xbea0
movq %r12, %rdi
callq 0x2220
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rax)
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movsd 0x58(%rsp), %xmm0
jle 0xc021
xorl %esi, %esi
movl $0x1, %ebp
cmpq %rsi, %rdx
jbe 0xc196
movq (%rax,%rsi,8), %rax
movq 0x8(%r14), %rcx
subq (%r14), %rcx
movq 0x48(%rax), %r15
shrq $0x3, %rcx
movl %ecx, %eax
xorl %r13d, %r13d
movl %eax, %ecx
decl %ecx
js 0xbfee
movl %ecx, %r12d
movq (%r15,%r12,8), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbf73
movl %edx, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %rdx, %rdi
negq %rdi
movl $0x1, %r8d
leaq (%rdi,%r8), %r9
cmpq $0x1, %r9
je 0xc168
movq -0x8(%rcx,%r8,8), %r9
movq 0x48(%r9), %r9
ucomisd (%r9), %xmm0
movzbl %r13b, %r13d
cmoval %ebp, %r13d
ja 0xbf73
leaq 0x1(%r8), %r9
cmpq %r10, %r8
movq %r9, %r8
jb 0xbf41
decq %rax
testb $0x1, %r13b
je 0xbf08
movq (%r15,%r12,8), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbfee
movq %rsi, 0x10(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0xc15c
movq (%rax,%rbx,8), %rdi
callq 0x172ac
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x10b7e
incq %rbx
movq (%r15,%r12,8), %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, %rbx
jl 0xbf99
movsd 0x58(%rsp), %xmm0
movq 0x10(%rsp), %rsi
testb $0x1, %r13b
je 0xc19f
incq %rsi
movq 0x18(%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, %rsi
jl 0xbee7
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xc148
xorl %r12d, %r12d
cmpq %r12, %rdx
jbe 0xc179
movq (%rax,%r12,8), %rax
movq 0x48(%rax), %r13
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0xc112
xorl %r15d, %r15d
movq (%r13,%r15,8), %rbp
movq (%rbp), %rdi
movq 0x8(%rbp), %rax
movq %rax, %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xc0cc
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0xc15c
movq (%rdi,%rbx,8), %rax
movq 0x48(%rax), %rdi
testq %rdi, %rdi
je 0xc0a3
movl $0x10, %esi
callq 0x2190
incq %rbx
movq (%r13,%r15,8), %rbp
movq (%rbp), %rdi
movq 0x8(%rbp), %rax
movq %rax, %rcx
subq %rdi, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0xc083
cmpq %rdi, %rax
je 0xc0db
movq %rbp, %rdi
callq 0x1acd9
jmp 0xc0f9
testq %rdi, %rdi
je 0xc0ec
movq 0x10(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %rbp, %rdi
callq 0x2190
movq 0x8(%r14), %rax
subq (%r14), %rax
incq %r15
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jl 0xc066
testq %r13, %r13
je 0xc11f
movq %r13, %rdi
callq 0x2220
incq %r12
movq 0x18(%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, %r12
jl 0xc03f
movq 0x8(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21773(%rip), %rdi     # 0x2d8d6
movq %rbx, %rsi
jmp 0xc172
leaq 0x21767(%rip), %rdi     # 0x2d8d6
movq %rdx, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x21756(%rip), %rdi     # 0x2d8d6
movq %r12, %rsi
jmp 0xc172
leaq 0x2174a(%rip), %rdi     # 0x2d8d6
movq %r12, %rsi
movq 0x8(%rsp), %rdx
jmp 0xc172
leaq 0x21739(%rip), %rdi     # 0x2d8d6
jmp 0xc172
movq 0x18(%rsp), %rdi
callq 0x33ab
leaq 0x21726(%rip), %rdi     # 0x2d8d6
movq %r13, %rsi
jmp 0xc172
leaq 0x2171a(%rip), %rdi     # 0x2d8d6
movq %r8, %rsi
jmp 0xc172