// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: set_per_strand_info_per_peaks(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, char*, char*, int)
vector<t_annot_region*>* set_per_strand_info_per_peaks(vector<t_annot_region*>* peak_regions,
		char* chrom,
		char* chip_reads_dir,
		int l_fragment)
{
	vector<char*>* chr_ids = NULL;

	if(chrom == NULL)
	{
		char chr_ids_fp[1000];
		sprintf(chr_ids_fp, "%s/chr_ids.txt", chip_reads_dir);
		chr_ids = buffer_file(chr_ids_fp);
		if(chr_ids == NULL)
		{
			fprintf(stderr, "Could not load the chromosome id's for the chip data.\n");
			exit(0);
		}
	}
	else
	{
		chr_ids = new vector<char*>();
		chr_ids->push_back(t_string::copy_me_str(chrom));
	}

	// Process each chromosome.
	vector<t_annot_region*>* filtered_peaks = new vector<t_annot_region*>();
	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
		fprintf(stderr, "%s..", chr_ids->at(i_chr));

		// Skip chromosome M for now.
		if(strcmp(chr_ids->at(i_chr), "M") == 0)
		{
			continue;
		}

		// Generate the current signal profile.
		int l_buffer = 300*1000*1000;
		int l_profile = 0;
		double* forward_signal = new double[l_buffer + 2];	
		double* reverse_signal = new double[l_buffer + 2];	
		char cur_chr_chip_reads_fp[1000];
		sprintf(cur_chr_chip_reads_fp, "%s/%s_mapped_reads.txt", chip_reads_dir, chr_ids->at(i_chr));
		buffer_per_nucleotide_profile_no_buffer(cur_chr_chip_reads_fp, l_fragment, 
			NULL, forward_signal, reverse_signal,
			l_buffer, l_profile);

if(__DBG__)
{
		double* new_f_signal_profile = replace_profiles_for_debug(forward_signal, l_profile, g_dbg_base_posn, g_dbg_l_new_profile);
		double* new_r_signal_profile = replace_profiles_for_debug(reverse_signal, l_profile, g_dbg_base_posn, g_dbg_l_new_profile);
		delete [] forward_signal;
		delete [] reverse_signal;
		forward_signal = new_f_signal_profile;
		reverse_signal = new_r_signal_profile;
		l_profile = g_dbg_l_new_profile;
}

if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
{
		char fore_strand_signal_fp[1000];
		sprintf(fore_strand_signal_fp, "%s_fore_strand.bin", chr_ids->at(i_chr));
		dump_per_nucleotide_binary_profile(forward_signal, l_profile, fore_strand_signal_fp);
		char rev_strand_signal_fp[1000];
		sprintf(rev_strand_signal_fp, "%s_rev_strand.bin", chr_ids->at(i_chr));
		dump_per_nucleotide_binary_profile(reverse_signal, l_profile, rev_strand_signal_fp);
}
			
		vector<t_annot_region*>* cur_chr_regs = get_regions_per_chromosome(peak_regions, chr_ids->at(i_chr));

if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
		fprintf(stderr, "Filtering %d regions on chromosome %s\n", (int)cur_chr_regs->size(), chr_ids->at(i_chr));
			
		//int n_passing_peaks = 0;
		for(int i_reg = 0; i_reg < (int)cur_chr_regs->size(); i_reg++)
		{
			t_ER_info* cur_peak_info = (t_ER_info*)(cur_chr_regs->at(i_reg)->data);

			double total_forward_signal = 0.0;
			double total_reverse_signal = 0.0;
			double total_fore_moment = 0;
			double total_rev_moment = 0;
			cur_peak_info->fore_max_val = 0;
			cur_peak_info->fore_climax_posn = 0;
			cur_peak_info->rev_max_val = 0;
			cur_peak_info->rev_climax_posn = 0;

			// Process forward strand info.
			for(int i = cur_chr_regs->at(i_reg)->start; i <= cur_chr_regs->at(i_reg)->end; i++)
			{
				if(i < l_profile)
				{
					total_forward_signal += forward_signal[i];
					total_fore_moment += (i - cur_chr_regs->at(i_reg)->start + 1) * forward_signal[i];

					if(cur_peak_info->fore_max_val < forward_signal[i])
					{
						cur_peak_info->fore_max_val = forward_signal[i];
						cur_peak_info->fore_climax_posn = i;
					}
				}
			} // i loop.

			// Process reverse strand info.
			for(int i = cur_chr_regs->at(i_reg)->end; i >= cur_chr_regs->at(i_reg)->start; i--)
			{
				if(i < l_profile)
				{
					total_reverse_signal += reverse_signal[i];
					total_rev_moment += (i - cur_chr_regs->at(i_reg)->start + 1) * reverse_signal[i];

					if(cur_peak_info->rev_max_val < reverse_signal[i])
					{
						cur_peak_info->rev_max_val = reverse_signal[i];
						cur_peak_info->rev_climax_posn = i;
					}
				}
			} // i loop.

			cur_peak_info->total_fore_mass = total_forward_signal;
			cur_peak_info->total_rev_mass = total_reverse_signal;

			cur_peak_info->fore_strand_mass_of_center = (int)(total_fore_moment / total_forward_signal) + cur_chr_regs->at(i_reg)->start;
			cur_peak_info->rev_strand_mass_of_center = (int)(total_rev_moment / total_reverse_signal) + cur_chr_regs->at(i_reg)->start;
		} // i_reg loop.

		//fprintf(stderr, "%s: %d peaks passed. (%d)\n", chr_ids->at(i_chr), n_passing_peaks, (int)cur_chr_regs->size());

		// For each peak, compute the fraction of read signal that is in mapable regions.
		delete [] forward_signal;
		delete [] reverse_signal;
		delete cur_chr_regs;
	} // chromosome loop.

	//dump_BED("filtered_peaks.bed", filtered_peaks);
	//dump_BED("removed_peaks.bed", removed_peaks);
	//delete removed_peaks;

	t_string::clean_string_list(chr_ids);

	return(filtered_peaks);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc28, %rsp            # imm = 0xC28
movl %ecx, 0x44(%rsp)
movq %rdx, %r14
movq %rdi, 0x50(%rsp)
testq %rsi, %rsi
je 0xbf42
movq %rsi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rbx, %rdi
callq 0x21062
leaq 0x60(%rsp), %rsi
movq %rax, (%rsi)
movq %r12, %rdi
callq 0x7d0a
jmp 0xbf6f
leaq 0x18a95(%rip), %rsi     # 0x249de
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x20089
testq %rax, %rax
je 0xc40f
movq %rax, %r12
pushq $0x18
popq %rdi
callq 0x2170
xorpd %xmm0, %xmm0
andq $0x0, 0x10(%rax)
movq %rax, 0x48(%rsp)
movupd %xmm0, (%rax)
xorl %r13d, %r13d
movq %r14, 0x58(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0xc3f0
cmpb $0x1, 0x2920d(%rip)     # 0x351c2
je 0xc382
movq %r12, %rdi
movq %r13, %rsi
callq 0x7342
movq (%rax), %rax
cmpb $0x4d, (%rax)
jne 0xbfd8
cmpb $0x0, 0x1(%rax)
je 0xc37a
andl $0x0, 0xc(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %r15
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbx
movq %r12, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rsi
callq 0x7342
movq (%rax), %rcx
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x18a1f(%rip), %rsi     # 0x24a38
movq %r14, %rdx
xorl %eax, %eax
callq 0x2080
leaq 0xc(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movl 0x44(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
movq %rbx, %r8
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0xe682
cmpb $0x1, 0x29176(%rip)     # 0x351c3
jne 0xc0a2
movl 0x29167(%rip), %edx     # 0x351bc
movl 0x2915d(%rip), %ecx     # 0x351b8
movq %r15, %rdi
callq 0x8358
movq %rax, %r14
movl 0x29150(%rip), %edx     # 0x351bc
movl 0x29146(%rip), %ecx     # 0x351b8
movq %rbx, %rdi
callq 0x8358
movq %rax, %r13
movq %r15, %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x2210
movl 0x29125(%rip), %eax     # 0x351b8
movl %eax, 0xc(%rsp)
movq %r13, %rbx
movq 0x20(%rsp), %r12
movq %r14, %r15
cmpb $0x1, 0x29119(%rip)     # 0x351c2
movq 0x28(%rsp), %r13
jne 0xc11c
movq %r12, %rdi
movq %r13, %rsi
callq 0x7342
movq (%rax), %rdx
leaq 0x840(%rsp), %r14
movq %r14, %rdi
leaq 0x1b1bd(%rip), %rsi     # 0x2728d
xorl %eax, %eax
callq 0x2080
movl 0xc(%rsp), %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x18dfd
movq %r12, %rdi
movq %r13, %rsi
callq 0x7342
movq (%rax), %rdx
leaq 0x450(%rsp), %r14
movq %r14, %rdi
leaq 0x1b19a(%rip), %rsi     # 0x272a0
xorl %eax, %eax
callq 0x2080
movl 0xc(%rsp), %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x18dfd
movq %r12, %rdi
movq %r13, %rsi
callq 0x7342
movq (%rax), %rsi
movq 0x50(%rsp), %rdi
callq 0x15b31
movq %rax, %r14
cmpb $0x1, 0x29084(%rip)     # 0x351c2
je 0xc3b0
xorl %r13d, %r13d
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0xc348
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x48(%rax), %r12
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%r12)
andq $0x0, 0x20(%r12)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rax
xorpd %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbp
jg 0xc230
movslq 0xc(%rsp), %rax
cmpq %rax, %rbp
jl 0xc1cb
incq %rbp
jmp 0xc1a5
movsd 0x38(%rsp), %xmm0
addsd (%r15,%rbp,8), %xmm0
movsd %xmm0, 0x38(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rcx
leaq 0x1(%rbp), %rax
movl 0xc(%rcx), %ecx
movl %ebp, %edx
subl %ecx, %edx
incl %edx
cvtsi2sd %edx, %xmm1
movsd (%r15,%rbp,8), %xmm0
mulsd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x30(%rsp)
ucomisd 0x18(%r12), %xmm0
jbe 0xc1a2
movsd %xmm0, 0x18(%r12)
movl %ebp, 0x10(%r12)
jmp 0xc1a2
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rax
cmpq %rax, %rbp
jl 0xc2d1
movslq 0xc(%rsp), %rax
cmpq %rax, %rbp
jge 0xc2c3
movsd 0x10(%rsp), %xmm0
addsd (%rbx,%rbp,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl %ebp, %ecx
subl 0xc(%rax), %ecx
incl %ecx
cvtsi2sd %ecx, %xmm0
movsd (%rbx,%rbp,8), %xmm1
mulsd %xmm1, %xmm0
addsd 0x18(%rsp), %xmm0
ucomisd 0x20(%r12), %xmm1
jbe 0xc2c9
movsd %xmm1, 0x20(%r12)
movl %ebp, 0x14(%r12)
jmp 0xc2c9
movsd 0x18(%rsp), %xmm0
decq %rbp
jmp 0xc24c
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x28(%r12)
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x30(%r12)
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %ebp
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
addl 0xc(%rax), %ebp
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x8(%r12)
movsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
cvttsd2si %xmm0, %ebp
movq %r14, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
addl 0xc(%rax), %ebp
cvtsi2sd %ebp, %xmm0
movsd %xmm0, (%r12)
incq %r13
jmp 0xc147
movq %r15, %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x2210
movq %r14, %rdi
callq 0x832c
movq %r14, %rdi
pushq $0x18
popq %rsi
callq 0x2180
movq 0x58(%rsp), %r14
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %r13
incq %r13
jmp 0xbf96
movq 0x28c57(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x1a7f7(%rip), %rsi     # 0x26b9b
xorl %eax, %eax
callq 0x2240
jmp 0xbfbb
movq 0x28c29(%rip), %rax     # 0x34fe0
movq (%rax), %r12
movq 0x8(%r14), %r13
subq (%r14), %r13
shrq $0x3, %r13
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x7342
movq (%rax), %rcx
movq %r12, %rdi
leaq 0x1aed1(%rip), %rsi     # 0x272b2
movl %r13d, %edx
xorl %eax, %eax
callq 0x2240
jmp 0xc144
movq %r12, %rdi
callq 0x221f2
movq 0x48(%rsp), %rax
addq $0xc28, %rsp            # imm = 0xC28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28bca(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x1a73c(%rip), %rdi     # 0x26b5c
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
xorl %edi, %edi
callq 0x21d0