// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc18, %rsp            # imm = 0xC18
movapd %xmm0, 0x30(%rsp)
movl %ecx, %r13d
movq %rdx, %r15
movq %rdi, 0x48(%rsp)
testq %rsi, %rsi
je 0x1039a
movq %rsi, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rbx, %rdi
callq 0x26db4
leaq 0x440(%rsp), %rsi
movq %rax, (%rsi)
movq %r12, %rdi
callq 0xb27c
jmp 0x103ca
leaq 0x1a63d(%rip), %rsi     # 0x2a9de
leaq 0x440(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x25af8
testq %rax, %rax
je 0x1087e
movq %rax, %r12
movl $0x18, %edi
callq 0x2180
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x10820
movapd 0x30(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0x30(%rsp)
xorl %r8d, %r8d
movq %r15, 0x40(%rsp)
movq %r12, 0x20(%rsp)
movl %r13d, 0x2c(%rsp)
cmpb $0x1, 0x28d9f(%rip)     # 0x391ca
je 0x107eb
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0x10861
movq (%rax,%r8,8), %rsi
cmpb $0x4d, (%rsi)
jne 0x10463
cmpb $0x0, 0x1(%rsi)
jne 0x10463
incq %r8
movslq %edx, %rsi
cmpq %rsi, %r8
jl 0x10424
jmp 0x10820
movq %r15, %rbx
movl $0x0, 0x14(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
movq %r8, 0x18(%rsp)
callq 0x2040
movq %rax, %r14
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq 0x18(%rsp), %rsi
movq %rax, %r15
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x10858
movq (%rax,%rsi,8), %rcx
leaq 0x440(%rsp), %r13
movq %r13, %rdi
leaq 0x1a577(%rip), %rsi     # 0x2aa38
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2080
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movl 0x2c(%rsp), %esi
xorl %edx, %edx
movq %r14, %rcx
movq %r15, %r8
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0x11f57
cmpb $0x1, 0x28cd5(%rip)     # 0x391ca
movq 0x20(%rsp), %rbx
jne 0x1058d
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x10858
movq (%rax,%rsi,8), %rdx
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x1cd5e(%rip), %rsi     # 0x2d28d
xorl %eax, %eax
callq 0x2080
movl 0x14(%rsp), %esi
movq %r14, %rdi
movq %r12, %rdx
callq 0x1d226
movq 0x18(%rsp), %rsi
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x10858
movq (%rax,%rsi,8), %rdx
leaq 0x830(%rsp), %r12
movq %r12, %rdi
leaq 0x1cd29(%rip), %rsi     # 0x2d2a0
xorl %eax, %eax
callq 0x2080
movl 0x14(%rsp), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0x1d226
cmpb $0x1, 0x28c37(%rip)     # 0x391cb
jne 0x105e9
movl 0x28c28(%rip), %edx     # 0x391c4
movl 0x28c1e(%rip), %ecx     # 0x391c0
movq %r14, %rdi
callq 0x2ec3
movq %rax, %rbx
movl 0x28c11(%rip), %edx     # 0x391c4
movl 0x28c07(%rip), %ecx     # 0x391c0
movq %r15, %rdi
callq 0x2ec3
movq %rax, %r12
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movl 0x28be6(%rip), %eax     # 0x391c0
movl %eax, 0x14(%rsp)
movq %rbx, %r14
movq 0x20(%rsp), %rbx
movq %r12, %r15
movq 0x18(%rsp), %rsi
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x10858
movq (%rax,%rsi,8), %rsi
movq 0x48(%rsp), %rdi
callq 0x19bcb
movq %rax, %r12
cmpb $0x1, 0x28bad(%rip)     # 0x391ca
jne 0x10646
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
movq 0x18(%rsp), %rax
cmpq %rax, %rdx
jbe 0x1086d
movq %r12, %rdi
movq %rax, %rdx
callq 0x37e4
movq (%r12), %rax
movq 0x8(%r12), %rdx
movq %rdx, %r8
subq %rax, %r8
sarq $0x3, %r8
xorl %ebx, %ebx
testl %r8d, %r8d
jle 0x10752
xorl %r13d, %r13d
xorl %ebx, %ebx
xorpd %xmm3, %xmm3
cmpq %r13, %r8
jbe 0x1083d
movq (%rax,%r13,8), %rdi
movslq 0xc(%rdi), %rcx
movl 0x20(%rdi), %esi
xorpd %xmm0, %xmm0
cmpl %esi, %ecx
jg 0x106ae
movslq 0x14(%rsp), %r8
subl %ecx, %esi
incl %esi
cmpq %r8, %rcx
jge 0x106a7
movsd (%r14,%rcx,8), %xmm1
movhpd (%r15,%rcx,8), %xmm1    # xmm1 = xmm1[0],mem[0]
addpd %xmm1, %xmm0
incq %rcx
decl %esi
jne 0x10692
movapd %xmm0, %xmm1
shufpd $0x1, %xmm0, %xmm1      # xmm1 = xmm1[1],xmm0[0]
movapd %xmm0, %xmm2
divpd %xmm1, %xmm2
movapd 0x30(%rsp), %xmm1
cmpltpd %xmm2, %xmm1
movmskpd %xmm1, %ecx
cmpl $0x3, %ecx
sete %cl
ucomisd %xmm3, %xmm0
jbe 0x10732
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
ucomisd %xmm3, %xmm0
jbe 0x10732
testb %cl, %cl
je 0x10732
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0x1084c
callq 0x16f1f
movq %rax, 0x50(%rsp)
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0x10716
movq %rax, (%rsi)
addq $0x8, 0x8(%rbp)
jmp 0x10723
movq %rbp, %rdi
leaq 0x50(%rsp), %rdx
callq 0xb74c
xorpd %xmm3, %xmm3
incl %ebx
movq (%r12), %rax
movq 0x8(%r12), %rdx
incq %r13
movq %rdx, %rcx
subq %rax, %rcx
movq %rcx, %r8
sarq $0x3, %r8
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0x1066d
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x10858
movq 0x28866(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%rsi,8), %rdx
leaq 0x1c981(%rip), %rsi     # 0x2d109
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2250
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq (%r12), %rdi
testq %rdi, %rdi
je 0x107b7
movq 0x10(%r12), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %r12, %rdi
callq 0x2190
movq 0x20(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x40(%rsp), %r15
movq 0x18(%rsp), %r8
jmp 0x10453
cmpq %r8, %rdx
jbe 0x10861
movq 0x287e9(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%r8,8), %rdx
leaq 0x1c396(%rip), %rsi     # 0x2cb9b
xorl %eax, %eax
movq %r8, %r14
callq 0x2250
movq %r14, %r8
movq (%r12), %rax
movq 0x8(%r12), %rcx
jmp 0x10431
movq %r12, %rdi
callq 0x2859a
movq %rbp, %rax
addq $0xc18, %rsp            # imm = 0xC18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c08c(%rip), %rdi     # 0x2c8d0
movq %r13, %rsi
movq %r8, %rdx
jmp 0x10877
leaq 0x1c07d(%rip), %rdi     # 0x2c8d0
movq %r13, %rsi
jmp 0x10877
leaq 0x1c071(%rip), %rdi     # 0x2c8d0
jmp 0x10877
leaq 0x1c068(%rip), %rdi     # 0x2c8d0
movq %r8, %rsi
jmp 0x10877
leaq 0x1c05c(%rip), %rdi     # 0x2c8d0
movq %rax, %rsi
xorl %eax, %eax
callq 0x2270
callq 0x3813
nop