// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq 0x8(%rdi), %rax
movq %rcx, 0x10(%rsp)
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, 0x8(%rsp)
testl %eax, %eax
jle 0xb927
movq 0x8(%r14), %rbp
subq (%r14), %rbp
sarq $0x3, %rbp
leaq 0x2(%rbp), %rax
shrq $0x3d, %rax
xorl %r13d, %r13d
negq %rax
movl $0x0, %ecx
sbbq %rcx, %rcx
leaq 0x10(,%rbp,8), %rax
orq %rax, %rcx
movq %rcx, 0x18(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r15
andq %rax, %r15
movq %rbp, 0x28(%rsp)
andq %rax, %rbp
movq 0x18(%rsp), %rdi
callq 0x2040
movq %rax, %rbx
cmpl $0x0, 0x28(%rsp)
jle 0xb907
xorl %r12d, %r12d
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, (%rbx,%r12,8)
incq %r12
cmpq %r12, %rbp
jne 0xb8e1
cmpq 0x8(%rsp), %r13
je 0xbf03
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rax
movq %rbx, 0x48(%rax)
incq %r13
cmpq %r15, %r13
jne 0xb8ca
movl $0x989694, %edi         # imm = 0x989694
callq 0x236bc
movq %rax, %r12
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
movq 0x40(%rsp), %r13
jle 0xbc5e
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 0x2d666(%rip), %rbx     # 0x38fe0
leaq 0x21136(%rip), %r15     # 0x2cab7
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), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0xbf36
movq (%rcx,%r8,8), %rcx
movq 0x8(%rcx), %rdx
cmpq (%rcx), %rdx
jne 0xb9db
incq %r8
shrq $0x3, %rax
cltq
incq %rbp
cmpq %rax, %r8
jl 0xb984
jmp 0xbc5e
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 0xbeda
movq 0x8(%r15), %rsi
movq (%rax,%rbx,8), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %r15, %rdi
callq 0xb3f0
incq %rbx
cmpq %rbx, %rbp
jne 0xb9fa
movq %r12, 0x38(%rsp)
movq %r15, %rdi
movl $0x1, %esi
callq 0x1b5ff
movq %rax, %rbx
movq 0x2d594(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x210a4(%rip), %rsi     # 0x2cafa
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x2250
movq (%rbx), %rcx
movq %rbx, 0x18(%rsp)
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x60(%rsp), %rbx
jle 0xbb30
xorl %r12d, %r12d
cmpq %r12, %rdx
jbe 0xbef7
movq (%rcx,%r12,8), %rax
movl 0x20(%rax), %ecx
xorpd %xmm0, %xmm0
cmpl 0x28(%rsp), %ecx
jge 0xbad4
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 0x1e3e6
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 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rsi
subq %rcx, %rsi
movq %rsi, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0xbef7
movq (%rcx,%r12,8), %rdi
movq %rax, 0x48(%rdi)
incq %r12
shrq $0x3, %rsi
movslq %esi, %rax
cmpq %rax, %r12
jl 0xba85
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x1, %edx
callq 0x19046
movq (%rax), %rsi
movq %rax, 0x10(%rsp)
movq 0x8(%rax), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbc00
xorl %r13d, %r13d
movq %rbp, 0x48(%rsp)
cmpq %r13, %rdx
jbe 0xbf2a
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 0x16f1f
movq %rax, 0x30(%rsp)
movq 0x48(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0xbbb8
movq %rax, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0xbbc5
movq %r12, %rdi
leaq 0x30(%rsp), %rdx
callq 0xb74c
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 0xbb67
movq 0x10(%rsp), %rdi
callq 0x1a88a
movq 0x18(%rsp), %rdi
callq 0x1a88a
movq (%r15), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %r12
movq 0x2d3ba(%rip), %rbx     # 0x38fe0
je 0xbc34
movq 0x10(%r15), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %r15, %rdi
callq 0x2190
movq 0x8(%r14), %rax
subq (%r14), %rax
movq 0x40(%rsp), %r13
leaq 0x20e63(%rip), %r15     # 0x2cab7
movq 0x8(%rsp), %r8
jmp 0xb9c5
testq %r12, %r12
je 0xbc6b
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 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbec6
xorl %r12d, %r12d
movsd 0x58(%rsp), %xmm0
cmpq %r12, %rdx
jbe 0xbef7
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
movl %ecx, %edx
decl %edx
setns %bpl
js 0xbd82
movq (%rax,%r12,8), %rax
movq 0x48(%rax), %r15
movl %ecx, %ecx
movl %edx, %r13d
movq (%r15,%r13,8), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbd1d
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edi, %edi
cmpq %rdi, %rdx
je 0xbeeb
movq (%rax,%rdi,8), %r8
movq 0x48(%r8), %r8
ucomisd (%r8), %xmm0
ja 0xbd2c
incq %rdi
cmpq %rdi, %rsi
jne 0xbcfd
decq %rcx
movl %ecx, %edx
decl %edx
setns %bpl
jns 0xbcda
jmp 0xbd82
testl %edx, %edx
jle 0xbd82
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0xbeda
movq (%rax,%rbx,8), %rdi
callq 0x16f1f
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x108ac
incq %rbx
movq (%r15,%r13,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 0xbd32
movsd 0x58(%rsp), %xmm0
testb $0x1, %bpl
je 0xbf11
incq %r12
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, %r12
jl 0xbcae
testl %edx, %edx
jle 0xbec6
xorl %r15d, %r15d
cmpq %r15, %rdx
jbe 0xbf1e
movq (%rax,%r15,8), %rax
movq 0x48(%rax), %r13
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0xbe90
xorl %r12d, %r12d
movq (%r13,%r12,8), %rbp
movq (%rbp), %rdi
movq 0x8(%rbp), %rax
movq %rax, %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xbe48
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0xbeda
movq (%rdi,%rbx,8), %rax
movq 0x48(%rax), %rdi
movl $0x10, %esi
callq 0x2190
incq %rbx
movq (%r13,%r12,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 0xbe04
cmpq %rdi, %rax
je 0xbe57
movq %rbp, %rdi
callq 0x1a88a
jmp 0xbe75
testq %rax, %rax
je 0xbe68
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 %r12
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jl 0xbde7
jmp 0xbe95
testq %r13, %r13
je 0xbe9d
movq %r13, %rdi
callq 0x2220
incq %r15
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, %r15
jl 0xbdc0
movq 0x8(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x209ef(%rip), %rdi     # 0x2c8d0
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x209de(%rip), %rdi     # 0x2c8d0
movq %rdx, %rsi
jmp 0xbee4
leaq 0x209d2(%rip), %rdi     # 0x2c8d0
movq %r12, %rsi
jmp 0xbee4
leaq 0x209c6(%rip), %rdi     # 0x2c8d0
movq 0x8(%rsp), %rdx
jmp 0xbef2
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x33a2
leaq 0x209ab(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0xbee4
leaq 0x2099f(%rip), %rdi     # 0x2c8d0
movq %r13, %rsi
jmp 0xbee4
leaq 0x20993(%rip), %rdi     # 0x2c8d0
movq %r8, %rsi
jmp 0xbee4