// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: write_logR_profiles(char*, char*, int, char*, int, double, double, double, int, double)
void write_logR_profiles(char* chip_reads_dir,
							char* control_reads_dir, 
							int l_fragment,
							char* mapability_signal_dir,
							int l_read_mapability_signal,
							double base_scale_l_win,
							double end_scale_l_win,
							double log_step,
							int l_mapability_filtering_win,
							double max_normalized_mapability_signal)
{
	char chr_ids_fp[1000];
	sprintf(chr_ids_fp, "%s/chr_ids.txt", chip_reads_dir);
	vector<char*>* 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);
	}

	// Process each chromosome.
	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		fprintf(stderr, "Writing %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;
		//int l_read = l_read_mapability_signal;
		double* buffered_signal_profile = 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, 
			buffered_signal_profile, NULL, NULL,
			l_buffer, l_profile);

		// Check IP file.
		if(!check_file(cur_chr_chip_reads_fp))
		{
			fprintf(stderr, "Processed IP reads for %s does not exist @ %s. Skipping.\n", chr_ids->at(i_chr), cur_chr_chip_reads_fp);
			continue;
		}

		double* signal_profile = new double[l_profile + 2];	
		for(int i = 1; i <= l_profile; i++)
		{
			signal_profile[i] = buffered_signal_profile[i];
		} // i loop.
		delete [] buffered_signal_profile;

		// Generate the current control profile.
		l_buffer = 300*1000*1000;
		int l_control = 0;
		double* buffered_control_profile = new double[l_buffer + 2];
		char cur_chr_control_reads_fp[1000];
		sprintf(cur_chr_control_reads_fp, "%s/%s_mapped_reads.txt", control_reads_dir, chr_ids->at(i_chr));
		buffer_per_nucleotide_profile_no_buffer(cur_chr_control_reads_fp, l_fragment, 
			buffered_control_profile, NULL, NULL, 
			l_buffer, l_control);

		double* control_profile = new double[l_control + 2];
		for(int i = 1; i <= l_control; i++)
		{
			control_profile[i] = buffered_control_profile[i];
		} // i loop.
		delete [] buffered_control_profile;
		
//		// Load and process the mapability profile.
//		char mapability_signal_profile_fp[1000];
//		sprintf(mapability_signal_profile_fp, "%s/%s.bin", mapability_signal_dir, chr_ids->at(i_chr));
//
//		double* mapability_aware_smoothed_signal_profile = NULL;
//		int l_mapability_profile = 0;
//		if(check_file(mapability_signal_profile_fp))
//		{
//			double* mapability_signal_profile = NULL;
//
//if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
//			fprintf(stderr, "Smoothing the signal profile.\n");
//			mapability_aware_smoothed_signal_profile = mapability_aware_median_filter(signal_profile, l_profile,
//																					mapability_signal_profile, l_mapability_profile,
//																					max_normalized_mapability_signal,
//																					l_mapability_filtering_win);
//
//			delete [] mapability_signal_profile;
//		} // mapability based filtering check.
//		else
//		{
//			fprintf(stderr, "Mappability map is not found, skipping it.");
//			mapability_aware_smoothed_signal_profile = new double[l_profile + 2];
//			for(int i = 1; i <= l_profile; i++)
//			{
//				mapability_aware_smoothed_signal_profile[i] = signal_profile[i];
//			} // i loop.
//		}

		// Scale the control with respect to signal.
		double per_win_2DOF_lls_scaling_factor = 0;
		double per_win_1DOF_lls_scaling_factor = 0;
		double total_sig_scaling_factor = 0;
		get_per_window_scaling_factors_for_profile1_per_profile2(control_profile, l_control, signal_profile, l_profile, 10000,
																	per_win_2DOF_lls_scaling_factor,
																	per_win_1DOF_lls_scaling_factor,
																	total_sig_scaling_factor);

		double scaling_factor = per_win_1DOF_lls_scaling_factor;

if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
		fprintf(stderr, "Scaling control with factor of %lf\n", scaling_factor);

		// Go over the whole control signal.
		for(int i = 0; i < l_control; i++)
		{
			control_profile[i] *= scaling_factor;
		} // i loop.

		int l_logR_profile = (l_control < l_profile)?(l_control):(l_profile);
		double* logR_profile = new double[l_logR_profile + 2];
		memset(logR_profile, 0, sizeof(double) * (l_logR_profile + 1));

		// Compute the log ratio signal.
		for(int i = 1; i <= l_logR_profile; i++)
		{
			if(control_profile[i] > 0 && signal_profile[i] > 0)
			{
				logR_profile[i] = log(signal_profile[i] / control_profile[i]);
			}
			else
			{
			}
		} // i loop.

		// Dump the profile.
		char cur_logR_profile_fp[1000];
		sprintf(cur_logR_profile_fp, "%s/%s_logR.bin", chip_reads_dir, chr_ids->at(i_chr));
		dump_per_nucleotide_binary_profile(logR_profile, l_logR_profile, cur_logR_profile_fp);

		//// Now, do the multiscale decomposition, 
		//vector<double>* scales_per_i_scale = new vector<double>();
		//vector<double*>* filtered_tracks = multiscale_median_filter_data(mapability_aware_smoothed_signal_profile, l_profile, 
		//																base_scale_l_win, end_scale_l_win, log_step, 
		//																scales_per_i_scale, 
		//																true,
		//																false, 
		//																false, 
		//																false, 
		//																NULL, 
		//																NULL,
		//																"temp");

		//// Dump the tracks.
		//for(int i_scale = 0; i_scale < (int)scales_per_i_scale->size(); i_scale++)
		//{
		//	char cur_decomp_fp[1000];
		//	sprintf(cur_decomp_fp, "%s_scale_%d.bin", "temp", i_scale);

		//	if(check_file(cur_decomp_fp))
		//	{
		//		int l_cur_decomp;
		//		double* cur_filtered_track = load_per_nucleotide_binary_profile(cur_decomp_fp, l_cur_decomp);

		//		// Delete the temp file.
		//		remove(cur_decomp_fp);

		//		char cur_bgr_fp[1000];
		//		sprintf(cur_bgr_fp, "chr%s_scale_%d.bgr", chr_ids->at(i_chr), (int)(floor(scales_per_i_scale->at(i_scale))));
		//		dump_bedGraph_per_per_nucleotide_binary_profile(cur_filtered_track, l_cur_decomp, chr_ids->at(i_chr), cur_bgr_fp);
		//		delete [] cur_filtered_track;
		//	}
		//} // i_scale loop.
		//delete filtered_tracks;

		//delete scales_per_i_scale;
		delete [] signal_profile;
		delete [] control_profile;
		//delete [] mapability_aware_smoothed_signal_profile;
	} // i_chr loop.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movl %edx, %ebp
movq %rsi, 0x48(%rsp)
movq %rdi, %rdx
leaq 0x1f010(%rip), %rsi     # 0x2b9e4
leaq 0xc30(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0x28(%rsp)
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x2613e
testq %rax, %rax
je 0xce1e
movq %rax, %r12
movq (%rax), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xcdfb
movq 0x2d5c3(%rip), %r15     # 0x39fe0
leaq 0x2116f(%rip), %rbx     # 0x2db93
xorl %r13d, %r13d
movl %ebp, 0x24(%rsp)
movq %r12, 0x40(%rsp)
cmpq %r13, %rdx
jbe 0xce0d
movq (%r15), %rdi
movq (%rax,%r13,8), %rdx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2250
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0xce0d
movq (%rax,%r13,8), %rdi
leaq 0x1ee8f(%rip), %rsi     # 0x2b8fd
callq 0x2240
testl %eax, %eax
je 0xcd79
movl $0x0, 0x14(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbx
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0xce0d
movq (%rax,%r13,8), %rcx
leaq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x1ef82(%rip), %rsi     # 0x2ba3e
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0x1225e
movq %r14, %rdi
callq 0x26362
testb %al, %al
je 0xcda1
movslq 0x14(%rsp), %r15
cmpq $-0x2, %r15
leaq 0x10(,%r15,8), %rdi
movq $-0x1, %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r14
testq %r15, %r15
jle 0xcb39
leaq 0x8(%r14), %rdi
movq %rbx, %rsi
addq $0x8, %rsi
shlq $0x3, %r15
movq %r15, %rdx
callq 0x2130
movq %r14, 0x18(%rsp)
movq %rbx, %rdi
callq 0x2220
movl $0x0, 0x10(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbx
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0xce0d
movq (%rax,%r13,8), %rcx
leaq 0x840(%rsp), %r14
movq %r14, %rdi
leaq 0x1eeb4(%rip), %rsi     # 0x2ba3e
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0x1225e
movslq 0x10(%rsp), %r15
cmpq $-0x2, %r15
leaq 0x10(,%r15,8), %rdi
movq $-0x1, %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %rbp
testq %r15, %r15
jle 0xcbf7
leaq 0x8(%rbp), %rdi
movq %rbx, %rsi
addq $0x8, %rsi
shlq $0x3, %r15
movq %r15, %rdx
callq 0x2130
movq %rbx, %rdi
callq 0x2220
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x50(%rsp)
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %ecx
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movl $0x2710, %r8d           # imm = 0x2710
leaq 0x58(%rsp), %r9
callq 0x231dc
movsd 0x30(%rsp), %xmm0
cmpb $0x1, 0x2d57a(%rip)     # 0x3a1ca
je 0xcdd2
movslq 0x10(%rsp), %rax
testq %rax, %rax
jle 0xcc7a
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xcc62
movl 0x14(%rsp), %ebx
cmpl %ebx, %eax
cmovll %eax, %ebx
movslq %ebx, %r14
leaq 0x10(,%r14,8), %rdi
cmpl $-0x2, %r14d
movq $-0x1, %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r15
leaq 0x8(,%r14,8), %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20f0
testl %r14d, %r14d
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %rax
jle 0xcd08
leal 0x1(%rbx), %r12d
movl $0x1, %r14d
movsd (%rbp,%r14,8), %xmm1
ucomisd %xmm2, %xmm1
jbe 0xcd00
movsd (%rax,%r14,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0xcd00
divsd %xmm1, %xmm0
callq 0x2120
movq 0x18(%rsp), %rax
xorpd %xmm2, %xmm2
movsd %xmm0, (%r15,%r14,8)
incq %r14
cmpq %r14, %r12
jne 0xcccf
movq 0x40(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0xce0d
movq (%rax,%r13,8), %rcx
leaq 0x450(%rsp), %r14
movq %r14, %rdi
leaq 0x20f81(%rip), %rsi     # 0x2dcbd
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x1d6be
movq 0x18(%rsp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movl 0x24(%rsp), %ebp
movq 0x2d26e(%rip), %r15     # 0x39fe0
leaq 0x20e1a(%rip), %rbx     # 0x2db93
incq %r13
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0xca30
jmp 0xcdfb
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0xce0d
movq (%r15), %rdi
movq (%rax,%r13,8), %rdx
leaq 0x20ebf(%rip), %rsi     # 0x2dc83
leaq 0x60(%rsp), %rcx
xorl %eax, %eax
callq 0x2250
jmp 0xcd72
movq 0x2d207(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x202e0(%rip), %rsi     # 0x2d0c3
movb $0x1, %al
movsd %xmm0, 0x38(%rsp)
callq 0x2250
movsd 0x38(%rsp), %xmm0
jmp 0xcc56
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20ac2(%rip), %rdi     # 0x2d8d6
movq %r13, %rsi
xorl %eax, %eax
callq 0x2270
callq 0x34cd