// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: filter_strand_uneven_peaks(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, char*, char*, int, double)
vector<t_annot_region*>* filter_strand_uneven_peaks(vector<t_annot_region*>* peak_regions,
		char* chrom,
		char* chip_reads_dir,
		int l_fragment,
		double min_forward_reverse_strand_signal_fraction)
{
	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*>();
	//vector<t_annot_region*>* removed_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(__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);
}

if(__DBG__)
{
		// Replace fore/rev signals.
		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;
}

		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++)
		{
			double total_forward_signal = 0.0;
			double total_reverse_signal = 0.0;
			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_reverse_signal += reverse_signal[i];
				}
			} // i loop.

			// Both strand signals much be larger than 0.
			if(total_forward_signal > 0 &&
				total_reverse_signal > 0 &&
				total_reverse_signal / total_forward_signal > min_forward_reverse_strand_signal_fraction &&
				total_forward_signal / total_reverse_signal > min_forward_reverse_strand_signal_fraction)
			{
				t_annot_region* new_region = duplicate_region(cur_chr_regs->at(i_reg));
				filtered_peaks->push_back(new_region);
				n_passing_peaks++;
			}
			else
			{
				//removed_peaks->push_back(cur_chr_regs->at(i_reg));
			}
		} // 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
movaps %xmm0, 0x40(%rsp)
movl %ecx, 0x1c(%rsp)
movq %rdx, %r15
movq %rdi, 0x58(%rsp)
testq %rsi, %rsi
je 0xcb5f
movq %rsi, %rbx
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rbx, %rdi
callq 0x21062
leaq 0x450(%rsp), %rsi
movq %rax, (%rsi)
movq %r13, %rdi
callq 0x7d0a
jmp 0xcb8f
leaq 0x17e78(%rip), %rsi     # 0x249de
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x20089
testq %rax, %rax
je 0xcf64
movq %rax, %r13
pushq $0x18
popq %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movq %rax, 0x38(%rsp)
movups %xmm0, (%rax)
movapd 0x40(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0x40(%rsp)
xorl %r14d, %r14d
movq %r15, 0x50(%rsp)
movq %r13, 0x30(%rsp)
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0xcf45
cmpb $0x1, 0x285e0(%rip)     # 0x351c2
je 0xcf17
movq %r13, %rdi
movq %r14, %rsi
callq 0x7342
movq (%rax), %rax
cmpb $0x4d, (%rax)
jne 0xcc06
cmpb $0x0, 0x1(%rax)
jne 0xcc06
incq %r14
jmp 0xcbc4
andl $0x0, 0xc(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %r14, %rbx
movq %rax, %r14
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbp
movq %r13, %rdi
movq %rbx, 0x10(%rsp)
movq %rbx, %rsi
callq 0x7342
movq (%rax), %rcx
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
leaq 0x17deb(%rip), %rsi     # 0x24a38
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
leaq 0xc(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movl 0x1c(%rsp), %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rbp, %r8
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0xe682
cmpb $0x1, 0x28541(%rip)     # 0x351c2
jne 0xccf1
movq %r13, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x7342
movq (%rax), %rdx
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1a5e8(%rip), %rsi     # 0x2728d
xorl %eax, %eax
callq 0x2080
movl 0xc(%rsp), %esi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x18dfd
movq %r13, %rdi
movq %r15, %rsi
callq 0x7342
movq (%rax), %rdx
leaq 0x840(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1a5c5(%rip), %rsi     # 0x272a0
xorl %eax, %eax
callq 0x2080
movl 0xc(%rsp), %esi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x18dfd
cmpb $0x1, 0x284cb(%rip)     # 0x351c3
jne 0xcd48
movl 0x284bc(%rip), %edx     # 0x351bc
movl 0x284b2(%rip), %ecx     # 0x351b8
movq %r14, %rdi
callq 0x8358
movq %rax, %rbx
movl 0x284a5(%rip), %edx     # 0x351bc
movl 0x2849b(%rip), %ecx     # 0x351b8
movq %rbp, %rdi
callq 0x8358
movq %rax, %r15
movq %r14, %rdi
callq 0x2210
movq %rbp, %rdi
callq 0x2210
movl 0x2847a(%rip), %eax     # 0x351b8
movl %eax, 0xc(%rsp)
movq %rbx, %r14
movq %r15, %rbp
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0x7342
movq (%rax), %rsi
movq 0x58(%rsp), %rdi
callq 0x15b31
movq %rax, %r12
cmpb $0x1, 0x28456(%rip)     # 0x351c2
jne 0xcdab
movq 0x2826b(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq 0x8(%r12), %r15
subq (%r12), %r15
shrq $0x3, %r15
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x7342
movq (%rax), %rcx
movq %rbx, %rdi
leaq 0x1a511(%rip), %rsi     # 0x272b2
movl %r15d, %edx
xorl %eax, %eax
callq 0x2240
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0xce9d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %r13
jg 0xce27
movslq 0xc(%rsp), %rax
cmpq %rax, %r13
movapd 0x20(%rsp), %xmm1
jge 0xce1c
movsd (%r14,%r13,8), %xmm0
movhpd (%rbp,%r13,8), %xmm0    # xmm0 = xmm0[0],mem[0]
addpd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
incq %r13
jmp 0xcde4
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm0
shufpd $0x1, %xmm2, %xmm0      # xmm0 = xmm0[1],xmm2[0]
movapd %xmm2, %xmm1
divpd %xmm0, %xmm1
movapd 0x40(%rsp), %xmm0
cmpltpd %xmm1, %xmm0
movmskpd %xmm0, %eax
cmpl $0x3, %eax
sete %al
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jbe 0xce95
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
ucomisd %xmm0, %xmm2
jbe 0xce95
testb %al, %al
je 0xce95
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x60(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x7448
incl %r15d
incq %rbx
jmp 0xcdb0
movq 0x2813c(%rip), %rax     # 0x34fe0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x7342
movq (%rax), %rdx
movq 0x8(%r12), %r8
subq (%r12), %r8
shrq $0x3, %r8
movq 0x20(%rsp), %rdi
leaq 0x1a22c(%rip), %rsi     # 0x27109
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2240
movq %r14, %rdi
movq %rbx, %r14
callq 0x2210
movq %rbp, %rdi
callq 0x2210
movq %r12, %rdi
callq 0x832c
movq %r12, %rdi
pushq $0x18
popq %rsi
callq 0x2180
movq 0x50(%rsp), %r15
jmp 0xcc01
movq 0x280c2(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x19c62(%rip), %rsi     # 0x26b9b
xorl %eax, %eax
callq 0x2240
jmp 0xcbe8
movq %r13, %rdi
callq 0x221f2
movq 0x38(%rsp), %rax
addq $0xc28, %rsp            # imm = 0xC28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28075(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x19be7(%rip), %rdi     # 0x26b5c
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
xorl %edi, %edi
callq 0x21d0
nop