// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movsd %xmm0, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, %r14
movq %rdi, %r13
pushq $0x18
popq %rbx
xorl %r15d, %r15d
movq %rdi, 0x10(%rsp)
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x88e3
movq 0x8(%r14), %rbp
subq (%r14), %rbp
sarq $0x3, %rbp
leaq 0x2(%rbp), %rax
shrq $0x3d, %rax
leaq 0x10(,%rbp,8), %rcx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x2040
movq %rax, %r12
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x88c4
movq %rbx, %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, (%r12,%r13,8)
incq %r13
jmp 0x88a1
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movq %r12, 0x48(%rax)
incq %r15
jmp 0x884c
movl $0x989694, %edi         # imm = 0x989694
callq 0x1e201
movq %rax, %r15
cvtsi2sdl 0xa0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
pushq $0x1
popq %rbp
movq 0x2c6d7(%rip), %rbx     # 0x34fe0
leaq 0x1e1a7(%rip), %r12     # 0x26ab7
xorl %r13d, %r13d
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x8ba4
movq (%rbx), %rdi
movq %r12, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2240
movq %r14, %rdi
movq %r13, %rsi
callq 0xcf88
movq (%rax), %rax
movq 0x8(%rax), %rcx
cmpq (%rax), %rcx
jne 0x8958
incq %r13
incq %rbp
jmp 0x8913
movq %r13, 0x8(%rsp)
movq %r15, 0x30(%rsp)
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x89bb
movq 0x8(%r13), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0xcf88
movq (%rax), %rax
movq (%rax), %r12
movq %r14, %rdi
movq %rbx, %rsi
callq 0xcf88
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x73e4
incq %rbx
jmp 0x897c
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x1709e
movq %rax, %r12
movq 0x2c610(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x1e120(%rip), %rsi     # 0x26afa
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
xorl %r15d, %r15d
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x8acd
movq %r12, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq 0x18(%rsp), %rcx
cmpl %ecx, 0x20(%rax)
jge 0x8a97
movq %r12, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x38(%rsp), %rcx
cmpl %ecx, 0x20(%rax)
jge 0x8a97
movq %r12, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r12, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ecx
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl %ebx, %edx
movq 0x30(%rsp), %r8
movsd 0x48(%rsp), %xmm0
pushq $0x1
popq %r9
pushq $0x1
pushq 0x20(%rsp)
pushq 0x48(%rsp)
movl 0xc0(%rsp), %eax
pushq %rax
callq 0x19cf6
addq $0x20, %rsp
movsd %xmm0, (%rsp)
pushq $0x10
popq %rdi
callq 0x2170
movq %rax, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7404
movq (%rax), %rax
movq %rbx, 0x48(%rax)
incq %r15
jmp 0x89e9
movq 0x10(%rsp), %rdi
movq %r12, 0x40(%rsp)
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x150d3
movq %rax, %r15
xorl %ebx, %ebx
movq %rbp, 0x60(%rsp)
movq 0x8(%r15), %rax
subq (%r15), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x8b62
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %rbp
movq (%rbp), %rax
movq 0x8(%rbp), %r12
movq 0x48(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, (%rsp)
movq %r12, %rdi
callq 0x134b2
movq %rax, 0x28(%rsp)
movq 0x48(%r12), %rcx
movq %rcx, 0x48(%rax)
movq (%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7448
movq %rbp, %rdi
movq 0x60(%rsp), %rbp
pushq $0x18
popq %rsi
callq 0x2180
incq %rbx
jmp 0x8aec
movq %r15, %rdi
callq 0x165a8
movq 0x40(%rsp), %rdi
callq 0x165a8
movq %r13, %rdi
callq 0x832c
movq %r13, %rdi
pushq $0x18
popq %rsi
callq 0x2180
movq 0x2c452(%rip), %rbx     # 0x34fe0
movq 0x30(%rsp), %r15
leaq 0x1df1d(%rip), %r12     # 0x26ab7
movq 0x8(%rsp), %r13
jmp 0x8950
testq %r15, %r15
je 0x8bb1
movq %r15, %rdi
callq 0x2210
pushq $0x18
popq %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movq %rax, (%rsp)
movupd %xmm0, (%rax)
xorl %eax, %eax
leaq 0x28(%rsp), %r13
movq %rax, %rsi
movq 0x10(%rsp), %rdi
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
movq %rax, %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %rsi
jge 0x8d1f
movq %rsi, 0x8(%rsp)
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %rbp
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
movl %eax, %eax
movl %eax, %r12d
decl %r12d
js 0x8caa
movq %rax, 0x18(%rsp)
movl %r12d, %r15d
xorl %ebx, %ebx
movq (%rbp,%r15,8), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x8c61
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %rax
incq %rbx
movsd 0x20(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x8c2a
jmp 0x8c6b
movq 0x18(%rsp), %rax
decq %rax
jmp 0x8c14
xorl %ebx, %ebx
movq (%rbp,%r15,8), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x8caa
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x28(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
callq 0xd00a
incq %rbx
jmp 0x8c6d
movq 0x8(%rsp), %rcx
leaq 0x1(%rcx), %rax
testl %r12d, %r12d
jns 0x8bd1
movq 0x2c31d(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rcx, %r12
movq %rcx, %rsi
callq 0x7404
movq (%rax), %rax
movq (%rax), %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r8d
leaq 0x1de22(%rip), %rsi     # 0x26b2b
movq %rbx, %rdi
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
xorl %esi, %esi
pushq $0x10
popq %r13
subq %rcx, %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rsi
jge 0x8dea
movq %rsi, 0x20(%rsp)
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %rbx
xorl %r15d, %r15d
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x8dc4
xorl %r12d, %r12d
movq (%rbx,%r15,8), %rbp
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r12
jge 0x8d9d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %rdi
movq %r13, %rsi
callq 0x2180
incq %r12
jmp 0x8d60
cmpq %rax, %rcx
je 0x8dac
movq %rbp, %rdi
callq 0x165a8
jmp 0x8dbf
movq %rbp, %rdi
callq 0x832c
movq %rbp, %rdi
pushq $0x18
popq %rsi
callq 0x2180
incq %r15
jmp 0x8d4b
testq %rbx, %rbx
je 0x8dd1
movq %rbx, %rdi
callq 0x2210
movq 0x20(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rdi
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
jmp 0x8d25
movq (%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq