// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc18, %rsp            # imm = 0xC18
movl %ecx, 0x34(%rsp)
movq %rdi, 0x40(%rsp)
testq %rsi, %rsi
movq %rdx, 0x48(%rsp)
je 0xf781
movq %rsi, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
callq 0x273e0
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
movq %r14, %rdi
callq 0xb476
jmp 0xf7ab
leaq 0x1c25c(%rip), %rsi     # 0x2b9e4
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x2613e
testq %rax, %rax
je 0xfc70
movq %rax, %r14
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x38(%rsp)
movq $0x0, 0x10(%rax)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xfc2b
xorl %ecx, %ecx
movq %r14, 0x20(%rsp)
cmpb $0x1, 0x2a9dc(%rip)     # 0x3a1ca
movq %rcx, 0x18(%rsp)
je 0xfb7a
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
jbe 0xfc58
movq (%rax,%rcx,8), %rdi
leaq 0x1c0e2(%rip), %rsi     # 0x2b8fd
movq %rcx, %r12
callq 0x2240
testl %eax, %eax
je 0xfb4e
movl $0x0, 0x14(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %r15
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbx
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
movq %r12, %rsi
jbe 0xfc4a
movq (%rax,%rsi,8), %rcx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0x1c1c4(%rip), %rsi     # 0x2ba3e
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl 0x34(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
movq %rbx, %r8
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0x1225e
cmpb $0x1, 0x2a91b(%rip)     # 0x3a1cb
je 0xfba9
cmpb $0x1, 0x2a90d(%rip)     # 0x3a1ca
movq 0x18(%rsp), %rsi
jne 0xf95d
movq 0x20(%rsp), %r14
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0xfc4a
movq (%rax,%rsi,8), %rdx
leaq 0x830(%rsp), %r12
movq %r12, %rdi
leaq 0x1e993(%rip), %rsi     # 0x2e28d
xorl %eax, %eax
callq 0x2080
movl 0x14(%rsp), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x1d6be
movq 0x18(%rsp), %rsi
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0xfc4a
movq (%rax,%rsi,8), %rdx
leaq 0x440(%rsp), %r14
movq %r14, %rdi
leaq 0x1e95e(%rip), %rsi     # 0x2e2a0
xorl %eax, %eax
callq 0x2080
movl 0x14(%rsp), %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1d6be
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0xfc4a
movq (%rax,%rsi,8), %rsi
movq 0x40(%rsp), %rdi
callq 0x1a008
movq %rax, %rdi
cmpb $0x1, 0x2a839(%rip)     # 0x3a1ca
movq %rax, 0x28(%rsp)
je 0xfbfc
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
xorps %xmm6, %xmm6
jle 0xfb09
movslq 0x14(%rsp), %rcx
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rdx, %rsi
je 0xfc4a
movq (%rax,%rsi,8), %r11
movq 0x48(%r11), %r8
movups %xmm6, 0x10(%r8)
movq $0x0, 0x20(%r8)
movslq 0xc(%r11), %r10
movl %r10d, %r9d
movslq 0x20(%r11), %r11
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
cmpl %r11d, %r10d
jg 0xfa66
leaq (%r15,%r10,8), %rbp
movl %r11d, %r14d
notl %r14d
addq %r9, %r14
xorpd %xmm3, %xmm3
xorl %r12d, %r12d
leaq (%r10,%r12), %r13
cmpq %rcx, %r13
jge 0xfa53
movsd (%rbp,%r12,8), %xmm4
addsd %xmm4, %xmm1
leal 0x1(%r12), %r13d
xorps %xmm2, %xmm2
cvtsi2sd %r13d, %xmm2
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
ucomisd 0x18(%r8), %xmm4
jbe 0xfa57
movsd %xmm4, 0x18(%r8)
leal (%r9,%r12), %r13d
movl %r13d, 0x10(%r8)
jmp 0xfa57
movapd %xmm3, %xmm2
incq %r12
movl %r14d, %r13d
movapd %xmm2, %xmm3
addl %r12d, %r13d
jne 0xfa12
xorpd %xmm3, %xmm3
cmpl %r10d, %r11d
jl 0xfaca
movl $0x1, %r14d
subl %r10d, %r14d
xorpd %xmm4, %xmm4
xorpd %xmm0, %xmm0
cmpq %rcx, %r11
jge 0xfab6
movsd (%rbx,%r11,8), %xmm5
addsd %xmm5, %xmm0
leal (%r14,%r11), %ebp
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
ucomisd 0x20(%r8), %xmm5
jbe 0xfaba
movsd %xmm5, 0x20(%r8)
movl %r11d, 0x14(%r8)
jmp 0xfaba
movapd %xmm4, %xmm3
leaq -0x1(%r11), %r12
movapd %xmm3, %xmm4
cmpq %r10, %r11
movq %r12, %r11
jg 0xfa80
unpcklpd %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0]
movsd %xmm1, 0x28(%r8)
movsd %xmm0, 0x30(%r8)
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
divpd %xmm0, %xmm3
cvttpd2dq %xmm3, %xmm0
movd %r9d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm0
movupd %xmm0, (%r8)
incq %rsi
cmpq %rdi, %rsi
jne 0xf9c4
movq %r15, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x28(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0xfb37
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x2190
movq 0x28(%rsp), %rax
movl $0x18, %esi
movq %rax, %rdi
callq 0x2190
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
incq %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
movq %r12, %rcx
jl 0xf7e7
jmp 0xfc2b
cmpq %rcx, %rdx
jbe 0xfc58
movq 0x2a456(%rip), %rdx     # 0x39fe0
movq (%rdx), %rdi
movq (%rax,%rcx,8), %rdx
leaq 0x1e003(%rip), %rsi     # 0x2db9b
xorl %eax, %eax
callq 0x2250
movq 0x18(%rsp), %rcx
jmp 0xf7f9
movl 0x2a615(%rip), %edx     # 0x3a1c4
movl 0x2a60b(%rip), %ecx     # 0x3a1c0
movq %r15, %rdi
callq 0x2ec3
movq %rax, %r14
movl 0x2a5fe(%rip), %edx     # 0x3a1c4
movl 0x2a5f4(%rip), %ecx     # 0x3a1c0
movq %rbx, %rdi
callq 0x2ec3
movq %rax, %rbp
movq %r15, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movl 0x2a5d3(%rip), %eax     # 0x3a1c0
movl %eax, 0x14(%rsp)
movq %rbp, %rbx
movq %r14, %r15
jmp 0xf8b6
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
movq 0x18(%rsp), %rax
cmpq %rax, %rdx
jbe 0xfc64
movq %rax, %rdx
callq 0x388e
movq 0x28(%rsp), %rdi
jmp 0xf99c
movq %r14, %rdi
callq 0x28cb4
movq 0x38(%rsp), %rax
addq $0xc18, %rsp            # imm = 0xC18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1dc85(%rip), %rdi     # 0x2d8d6
xorl %eax, %eax
callq 0x2270
leaq 0x1dc77(%rip), %rdi     # 0x2d8d6
movq %rcx, %rsi
jmp 0xfc51
leaq 0x1dc6b(%rip), %rdi     # 0x2d8d6
movq %rax, %rsi
jmp 0xfc51
callq 0x38bd