// File: 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:
subq $0x1108, %rsp           # imm = 0x1108
movq %rdi, 0x1100(%rsp)
movq %rsi, 0x10f8(%rsp)
movq %rdx, 0x10f0(%rsp)
movl %ecx, 0x10ec(%rsp)
movsd %xmm0, 0x10e0(%rsp)
movq $0x0, 0x10d8(%rsp)
cmpq $0x0, 0x10f8(%rsp)
jne 0x1a4f5
leaq 0xcf0(%rsp), %rdi
movq 0x10f0(%rsp), %rdx
leaq 0x3852d(%rip), %rsi     # 0x529da
movb $0x0, %al
callq 0x2090
leaq 0xcf0(%rsp), %rdi
callq 0x452c0
movq %rax, 0x10d8(%rsp)
cmpq $0x0, 0x10d8(%rsp)
jne 0x1a4f3
movq 0x4ab05(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x3aa3d(%rip), %rsi     # 0x54f22
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x1a56d
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0xa0(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0xa0(%rsp), %rdi
callq 0xc630
movq 0xa0(%rsp), %rax
movq %rax, 0x10d8(%rsp)
movq 0x10d8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x10f8(%rsp), %rdi
callq 0x47b60
movq 0xa8(%rsp), %rdi
movq %rax, 0xce8(%rsp)
leaq 0xce8(%rsp), %rsi
callq 0xc640
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x98(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x98(%rsp), %rdi
callq 0xc670
movq 0x98(%rsp), %rax
movq %rax, 0xce0(%rsp)
movl $0x0, 0xcdc(%rsp)
movl 0xcdc(%rsp), %eax
movl %eax, 0x94(%rsp)
movq 0x10d8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x94(%rsp), %eax
cmpl %ecx, %eax
jge 0x1ac38
testb $0x1, 0x4abf0(%rip)     # 0x651da
je 0x1a62c
movq 0x4a9ed(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x88(%rsp), %rdi
movq (%rax), %rdx
leaq 0x3a93c(%rip), %rsi     # 0x54f61
movb $0x0, %al
callq 0x2250
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
leaq 0x3825a(%rip), %rsi     # 0x528a5
callq 0x2240
cmpl $0x0, %eax
jne 0x1a65a
jmp 0x1ac22
movl $0x11e1a300, 0xcd8(%rsp) # imm = 0x11E1A300
movl $0x0, 0xcd4(%rsp)
movl 0xcd8(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0xcc8(%rsp)
movl 0xcd8(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0xcc0(%rsp)
leaq 0x8d0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x10f0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rdx
movq (%rax), %rcx
leaq 0x38311(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
leaq 0x8d0(%rsp), %rdi
movl 0x10ec(%rsp), %esi
movq 0xcc8(%rsp), %rcx
movq 0xcc0(%rsp), %r8
movl 0xcd8(%rsp), %r9d
xorl %eax, %eax
movl %eax, %edx
leaq 0xcd4(%rsp), %rax
movq %rax, (%rsp)
callq 0x1edf0
testb $0x1, 0x4aa6d(%rip)     # 0x651da
je 0x1a81b
leaq 0x4e0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x68(%rsp), %rdi
movq (%rax), %rdx
leaq 0x3aeaf(%rip), %rsi     # 0x55653
movb $0x0, %al
callq 0x2090
movq 0xcc8(%rsp), %rdi
movl 0xcd4(%rsp), %esi
leaq 0x4e0(%rsp), %rdx
callq 0x35140
leaq 0xf0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x70(%rsp), %rdi
movq (%rax), %rdx
leaq 0x3ae6e(%rip), %rsi     # 0x55666
movb $0x0, %al
callq 0x2090
movq 0xcc0(%rsp), %rdi
movl 0xcd4(%rsp), %esi
leaq 0xf0(%rsp), %rdx
callq 0x35140
testb $0x1, 0x4a9b9(%rip)     # 0x651db
je 0x1a8df
movq 0xcc8(%rsp), %rdi
movl 0xcd4(%rsp), %esi
movl 0x4a997(%rip), %edx     # 0x651d4
movl 0x4a98d(%rip), %ecx     # 0x651d0
callq 0x12390
movq %rax, 0xe8(%rsp)
movq 0xcc0(%rsp), %rdi
movl 0xcd4(%rsp), %esi
movl 0x4a96f(%rip), %edx     # 0x651d4
movl 0x4a965(%rip), %ecx     # 0x651d0
callq 0x12390
movq %rax, 0xe0(%rsp)
movq 0xcc8(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, %rax
je 0x1a895
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0xcc0(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x1a8b2
movq 0x58(%rsp), %rdi
callq 0x2220
movq 0xe8(%rsp), %rax
movq %rax, 0xcc8(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0xcc0(%rsp)
movl 0x4a8f8(%rip), %eax     # 0x651d0
movl %eax, 0xcd4(%rsp)
movq 0x1100(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x50(%rsp), %rdi
movq (%rax), %rsi
callq 0x2e1e0
movq %rax, 0xd8(%rsp)
testb $0x1, 0x4a8bd(%rip)     # 0x651da
je 0x1a96e
movq 0x4a6ba(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc680
movl %eax, 0x4c(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq 0x40(%rsp), %rdi
movl 0x4c(%rsp), %edx
movq (%rax), %rcx
leaq 0x3ad11(%rip), %rsi     # 0x55678
movb $0x0, %al
callq 0x2250
movl $0x0, 0xd4(%rsp)
movl $0x0, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1ab57
xorps %xmm0, %xmm0
movsd %xmm0, 0xc8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0xd8(%rsp), %rdi
movslq 0xd0(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0xd8(%rsp), %rdi
movslq 0xd0(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x38(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x1aa8a
movl 0xbc(%rsp), %eax
cmpl 0xcd4(%rsp), %eax
jge 0x1aa72
movq 0xcc8(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0xcc0(%rsp), %rax
movslq 0xbc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x1aa74
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x1a9e5
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1ab3d
movsd 0xc0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1ab3d
movsd 0xc0(%rsp), %xmm0
divsd 0xc8(%rsp), %xmm0
ucomisd 0x10e0(%rsp), %xmm0
jbe 0x1ab3d
movsd 0xc8(%rsp), %xmm0
divsd 0xc0(%rsp), %xmm0
ucomisd 0x10e0(%rsp), %xmm0
jbe 0x1ab3d
movq 0xd8(%rsp), %rdi
movslq 0xd0(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0xb0(%rsp)
movq 0xce0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xc8c0
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x1ab3f
jmp 0x1ab3f
jmp 0x1ab41
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x1a984
movq 0x4a482(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x10d8(%rsp), %rdi
movslq 0xcdc(%rsp), %rsi
callq 0xc240
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc680
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movl 0x2c(%rsp), %ecx
movl %eax, %r8d
leaq 0x3a91c(%rip), %rsi     # 0x554cf
movb $0x0, %al
callq 0x2250
movq 0xcc8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x1abd7
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0xcc0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x1abf4
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x1ac20
movq 0x8(%rsp), %rdi
callq 0xc970
movq 0x8(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
jmp 0x1ac22
movl 0xcdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcdc(%rsp)
jmp 0x1a5b6
movq 0x10d8(%rsp), %rdi
callq 0x49db0
movq 0xce0(%rsp), %rax
addq $0x1108, %rsp           # imm = 0x1108
retq
nopw %cs:(%rax,%rax)
nop