// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: write_decomposition_bedGraphs(char*, int, char*, int, double, double, double, int, double)
void write_decomposition_bedGraphs(char* chip_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.

		// 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 $0x1438, %rsp           # imm = 0x1438
movsd %xmm3, 0x48(%rsp)
movl %r8d, 0x3c(%rsp)
movsd %xmm2, 0x68(%rsp)
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movl %esi, 0x40(%rsp)
movq %rdi, %rdx
leaq 0x1ddf4(%rip), %rsi     # 0x2a9de
leaq 0x1050(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, 0x70(%rsp)
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x25af8
testq %rax, %rax
je 0xd0fb
movq %rax, %r12
movq (%rax), %rbx
movq 0x8(%rax), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0xd0b7
xorl %ecx, %ecx
cmpq %rcx, %rdx
jbe 0xd0c9
movq 0x2c3a3(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq (%rbx,%rcx,8), %rdx
leaq 0x1ff48(%rip), %rsi     # 0x2cb93
xorl %eax, %eax
movq %rcx, 0x30(%rsp)
callq 0x2250
movq 0x30(%rsp), %rcx
movq (%r12), %rbx
movq 0x8(%r12), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
jbe 0xd0c9
movq (%rbx,%rcx,8), %rax
cmpb $0x4d, (%rax)
jne 0xcc88
cmpb $0x0, 0x1(%rax)
je 0xcfdd
movl $0x0, 0x2c(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
movq %rcx, %r15
callq 0x2040
movq %rax, %r14
movq (%rbx,%r15,8), %rcx
leaq 0x480(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1dd82(%rip), %rsi     # 0x2aa38
movq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
leaq 0x2c(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movl 0x40(%rsp), %esi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0x11f57
movq %rbx, %rdi
callq 0x25d35
testb %al, %al
je 0xcff1
movslq 0x2c(%rsp), %r15
cmpq $-0x2, %r15
leaq 0x10(,%r15,8), %rdi
movq $-0x1, %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r13
testq %r15, %r15
jle 0xcd32
leaq 0x8(%r13), %rdi
leaq 0x8(%r14), %rsi
shlq $0x3, %r15
movq %r15, %rdx
callq 0x2130
movq %r14, %rdi
callq 0x2220
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x30(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0xd0d5
movq (%rax,%rsi,8), %rcx
leaq 0xc60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1e225(%rip), %rsi     # 0x2af93
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x25d35
testb %al, %al
movq %r13, 0x88(%rsp)
je 0xd035
cmpb $0x1, 0x2c431(%rip)     # 0x391ca
leaq 0x90(%rsp), %rbx
je 0xd0a5
movl 0x2c(%rsp), %esi
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x48(%rsp), %xmm0
movl 0x3c(%rsp), %r8d
callq 0x14fd4
movq %rax, %r15
movl 0x2c(%rsp), %r14d
movl $0x18, %edi
callq 0x2180
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x1fe09(%rip), %rax     # 0x2cbf7
movq %rax, 0x18(%rsp)
movupd %xmm0, 0x8(%rsp)
movl $0x0, (%rsp)
movq %r15, 0x80(%rsp)
movq %r15, %rdi
movl %r14d, %esi
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movq %rbp, %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1401e
movq %rax, 0x78(%rsp)
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0xcf5c
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x1fda3(%rip), %rsi     # 0x2cbfc
leaq 0x1fd97(%rip), %rdx     # 0x2cbf7
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x25d35
testb %al, %al
je 0xcf42
movq %rbx, %rdi
leaq 0x44(%rsp), %rsi
callq 0x1d28c
movq %rax, %r15
movq %rbx, %rdi
callq 0x2310
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x30(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0xd0d5
movq (%rbp), %rcx
movq 0x8(%rbp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0xd0ea
movq %r12, %r13
movq (%rax,%rsi,8), %rbx
movsd (%rcx,%r14,8), %xmm0
callq 0x22c0
cvttsd2si %xmm0, %ecx
leaq 0x870(%rsp), %r12
movq %r12, %rdi
leaq 0x1fe07(%rip), %rsi     # 0x2ccf7
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2080
movq 0x30(%rsp), %r8
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0xd0de
movl 0x44(%rsp), %esi
movq (%rax,%r8,8), %rdx
movq %r15, %rdi
movq %r12, %rcx
callq 0x1d128
testq %r15, %r15
leaq 0x90(%rsp), %rbx
je 0xcf3f
movq %r15, %rdi
callq 0x2220
movq %r13, %r12
movq 0x8(%rbp), %rax
subq (%rbp), %rax
incq %r14
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0xce4f
movq 0x78(%rsp), %rbx
testq %rbx, %rbx
je 0xcf7a
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xcf7a
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %rbx, %rdi
callq 0x2190
movq (%rbp), %rdi
testq %rdi, %rdi
je 0xcf9c
movq 0x10(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movl $0x18, %esi
movq %rbp, %rdi
callq 0x2190
movq 0x88(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xcfc8
callq 0x2220
movq (%r12), %rbx
movq 0x8(%r12), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
movq 0x30(%rsp), %rcx
incq %rcx
movslq %edx, %rax
cmpq %rax, %rcx
jl 0xcc2d
jmp 0xd0b7
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x30(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0xd0d5
movq 0x2bfca(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%rsi,8), %rdx
leaq 0x1fc5f(%rip), %rsi     # 0x2cc83
leaq 0x480(%rsp), %rcx
xorl %eax, %eax
callq 0x2250
jmp 0xcfc8
movq 0x2bfa4(%rip), %rax     # 0x38fe0
movq (%rax), %rcx
movl $0x2a, %esi
movl $0x1, %edx
leaq 0x1fc7c(%rip), %rdi     # 0x2cccc
callq 0x2340
movslq 0x2c(%rsp), %r14
cmpq $-0x2, %r14
leaq 0x10(,%r14,8), %rdi
movq $-0x1, %rax
cmovlq %rax, %rdi
callq 0x2040
testq %r14, %r14
leaq 0x90(%rsp), %rbx
jle 0xd0af
movq %rax, %r15
movq %rax, %rdi
addq $0x8, %rdi
addq $0x8, %r13
movq %r14, %rdx
shlq $0x3, %rdx
movq %r13, %rsi
callq 0x2130
jmp 0xcdca
callq 0x346f
jmp 0xcda7
movq %rax, %r15
jmp 0xcdca
addq $0x1438, %rsp           # imm = 0x1438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f800(%rip), %rdi     # 0x2c8d0
movq %rcx, %rsi
jmp 0xd0f4
leaq 0x1f7f4(%rip), %rdi     # 0x2c8d0
jmp 0xd0f4
leaq 0x1f7eb(%rip), %rdi     # 0x2c8d0
movq %r8, %rsi
jmp 0xd0f4
leaq 0x1f7df(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
callq 0x3491