// File: 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:
subq $0x10f8, %rsp           # imm = 0x10F8
movq %rdi, 0x10f0(%rsp)
movq %rsi, 0x10e8(%rsp)
movl %edx, 0x10e4(%rsp)
movq %rcx, 0x10d8(%rsp)
movl %r8d, 0x10d4(%rsp)
movsd %xmm0, 0x10c8(%rsp)
movsd %xmm1, 0x10c0(%rsp)
movsd %xmm2, 0x10b8(%rsp)
movl %r9d, 0x10b4(%rsp)
movsd %xmm3, 0x10a8(%rsp)
leaq 0xcc0(%rsp), %rdi
movq 0x10f0(%rsp), %rdx
leaq 0x3e2e9(%rip), %rsi     # 0x529da
movb $0x0, %al
callq 0x2090
leaq 0xcc0(%rsp), %rdi
callq 0x452c0
movq %rax, 0xcb8(%rsp)
cmpq $0x0, 0xcb8(%rsp)
jne 0x14737
movq 0x508c1(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x407f9(%rip), %rsi     # 0x54f22
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x0, 0xcb4(%rsp)
movl 0xcb4(%rsp), %eax
movl %eax, 0x7c(%rsp)
movq 0xcb8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x7c(%rsp), %eax
cmpl %ecx, %eax
jge 0x14e0d
movq 0x50870(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x70(%rsp)
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq 0x70(%rsp), %rdi
movq (%rax), %rdx
leaq 0x407bd(%rip), %rsi     # 0x54f59
movb $0x0, %al
callq 0x2250
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
leaq 0x3e0e3(%rip), %rsi     # 0x528a5
callq 0x2240
cmpl $0x0, %eax
jne 0x147d1
jmp 0x14df7
movl $0x11e1a300, 0xcb0(%rsp) # imm = 0x11E1A300
movl $0x0, 0xcac(%rsp)
movl 0xcb0(%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, 0xca0(%rsp)
leaq 0x8b0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x10f0(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3e1d5(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
leaq 0x8b0(%rsp), %rdi
movl 0x10e4(%rsp), %esi
movq 0xca0(%rsp), %rdx
movl 0xcb0(%rsp), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0xcac(%rsp), %rax
movq %r8, %rcx
movq %rax, (%rsp)
callq 0x1edf0
leaq 0x8b0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x148f6
movq 0x5072a(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq 0x58(%rsp), %rdi
movq (%rax), %rdx
leaq 0x8b0(%rsp), %rcx
leaq 0x4075f(%rip), %rsi     # 0x55049
movb $0x0, %al
callq 0x2250
jmp 0x14df7
movl 0xcac(%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, 0x8a8(%rsp)
movl $0x1, 0x8a4(%rsp)
movl 0x8a4(%rsp), %eax
cmpl 0xcac(%rsp), %eax
jg 0x14983
movq 0xca0(%rsp), %rax
movslq 0x8a4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x8a8(%rsp), %rax
movslq 0x8a4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x8a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8a4(%rsp)
jmp 0x14936
movq 0xca0(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x149a0
movq 0x50(%rsp), %rdi
callq 0x2220
movl $0x11e1a300, 0xcb0(%rsp) # imm = 0x11E1A300
movl $0x0, 0x8a0(%rsp)
movl 0xcb0(%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, 0x898(%rsp)
leaq 0x4b0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x10e8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3e006(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
leaq 0x4b0(%rsp), %rdi
movl 0x10e4(%rsp), %esi
movq 0x898(%rsp), %rdx
movl 0xcb0(%rsp), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0x8a0(%rsp), %rax
movq %r8, %rcx
movq %rax, (%rsp)
callq 0x1edf0
movl 0x8a0(%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, 0x4a8(%rsp)
movl $0x1, 0x4a4(%rsp)
movl 0x4a4(%rsp), %eax
cmpl 0x8a0(%rsp), %eax
jg 0x14afa
movq 0x898(%rsp), %rax
movslq 0x4a4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x4a8(%rsp), %rax
movslq 0x4a4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x4a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4a4(%rsp)
jmp 0x14aad
movq 0x898(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x14b17
movq 0x38(%rsp), %rdi
callq 0x2220
xorps %xmm0, %xmm0
movsd %xmm0, 0x498(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x490(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x488(%rsp)
movq 0x4a8(%rsp), %rdi
movl 0x8a0(%rsp), %esi
movq 0x8a8(%rsp), %rdx
movl 0xcac(%rsp), %ecx
movl $0x2710, %r8d           # imm = 0x2710
leaq 0x498(%rsp), %r9
leaq 0x490(%rsp), %r10
leaq 0x488(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x3e300
movsd 0x490(%rsp), %xmm0
movsd %xmm0, 0x480(%rsp)
testb $0x1, 0x5063c(%rip)     # 0x651da
je 0x14bc1
movq 0x50439(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0x480(%rsp), %xmm0
leaq 0x3f901(%rip), %rsi     # 0x544bb
movb $0x1, %al
callq 0x2250
movl $0x0, 0x47c(%rsp)
movl 0x47c(%rsp), %eax
cmpl 0x8a0(%rsp), %eax
jge 0x14c12
movsd 0x480(%rsp), %xmm0
movq 0x4a8(%rsp), %rax
movslq 0x47c(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x47c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x47c(%rsp)
jmp 0x14bcc
movl 0x8a0(%rsp), %eax
cmpl 0xcac(%rsp), %eax
jge 0x14c2f
movl 0x8a0(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x14c3a
movl 0xcac(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x478(%rsp)
movl 0x478(%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, 0x470(%rsp)
movq 0x470(%rsp), %rdi
movl 0x478(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2110
movl $0x1, 0x46c(%rsp)
movl 0x46c(%rsp), %eax
cmpl 0x478(%rsp), %eax
jg 0x14d55
movq 0x4a8(%rsp), %rax
movslq 0x46c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14d3b
movq 0x8a8(%rsp), %rax
movslq 0x46c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14d3b
movq 0x8a8(%rsp), %rax
movslq 0x46c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x4a8(%rsp), %rax
movslq 0x46c(%rsp), %rcx
divsd (%rax,%rcx,8), %xmm0
callq 0x2140
movq 0x470(%rsp), %rax
movslq 0x46c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x14d3d
jmp 0x14d3d
jmp 0x14d3f
movl 0x46c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x46c(%rsp)
jmp 0x14ca5
leaq 0x80(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x10f0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xcb8(%rsp), %rdi
movslq 0xcb4(%rsp), %rsi
callq 0xc240
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq (%rax), %rcx
leaq 0x402eb(%rip), %rsi     # 0x55083
movb $0x0, %al
callq 0x2090
movq 0x470(%rsp), %rdi
movl 0x478(%rsp), %esi
leaq 0x80(%rsp), %rdx
callq 0x35140
movq 0x8a8(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x14dd8
movq 0x28(%rsp), %rdi
callq 0x2220
movq 0x4a8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x14df5
movq 0x10(%rsp), %rdi
callq 0x2220
jmp 0x14df7
movl 0xcb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcb4(%rsp)
jmp 0x14742
addq $0x10f8, %rsp           # imm = 0x10F8
retq
nopw %cs:(%rax,%rax)