// File: 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:
subq $0x1528, %rsp           # imm = 0x1528
movq %rdi, 0x1520(%rsp)
movl %esi, 0x151c(%rsp)
movq %rdx, 0x1510(%rsp)
movl %ecx, 0x150c(%rsp)
movsd %xmm0, 0x1500(%rsp)
movsd %xmm1, 0x14f8(%rsp)
movsd %xmm2, 0x14f0(%rsp)
movl %r8d, 0x14ec(%rsp)
movsd %xmm3, 0x14e0(%rsp)
leaq 0x10f0(%rsp), %rdi
movq 0x1520(%rsp), %rdx
leaq 0x3db52(%rip), %rsi     # 0x529da
movb $0x0, %al
callq 0x2090
leaq 0x10f0(%rsp), %rdi
callq 0x452c0
movq %rax, 0x10e8(%rsp)
cmpq $0x0, 0x10e8(%rsp)
jne 0x14ece
movq 0x5012a(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40062(%rip), %rsi     # 0x54f22
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x0, 0x10e4(%rsp)
movl 0x10e4(%rsp), %eax
movl %eax, 0xbc(%rsp)
movq 0x10e8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0xbc(%rsp), %eax
cmpl %ecx, %eax
jge 0x155d7
movq 0x500d3(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq 0xb0(%rsp), %rdi
movq (%rax), %rdx
leaq 0x4001a(%rip), %rsi     # 0x54f59
movb $0x0, %al
callq 0x2250
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
leaq 0x3d940(%rip), %rsi     # 0x528a5
callq 0x2240
cmpl $0x0, %eax
jne 0x14f74
jmp 0x155c1
movl $0x11e1a300, 0x10e0(%rsp) # imm = 0x11E1A300
movl $0x0, 0x10dc(%rsp)
movl 0x10e0(%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, 0x10d0(%rsp)
leaq 0xce0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x1520(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3da26(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
leaq 0xce0(%rsp), %rdi
movl 0x151c(%rsp), %esi
movq 0x10d0(%rsp), %rdx
movl 0x10e0(%rsp), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0x10dc(%rsp), %rax
movq %r8, %rcx
movq %rax, (%rsp)
callq 0x1edf0
leaq 0xce0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x150ab
movq 0x4ff7b(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq 0x98(%rsp), %rdi
movq (%rax), %rdx
leaq 0xce0(%rsp), %rcx
leaq 0x3ffaa(%rip), %rsi     # 0x55049
movb $0x0, %al
callq 0x2250
jmp 0x155c1
movl 0x10dc(%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, 0xcd8(%rsp)
movl $0x1, 0xcd4(%rsp)
movl 0xcd4(%rsp), %eax
cmpl 0x10dc(%rsp), %eax
jg 0x15138
movq 0x10d0(%rsp), %rax
movslq 0xcd4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xcd8(%rsp), %rax
movslq 0xcd4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xcd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcd4(%rsp)
jmp 0x150eb
movq 0x10d0(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, %rax
je 0x1515b
movq 0x90(%rsp), %rdi
callq 0x2220
leaq 0x8e0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1510(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3dde5(%rip), %rsi     # 0x52f8f
movb $0x0, %al
callq 0x2090
movq $0x0, 0x8d8(%rsp)
movl $0x0, 0x8d4(%rsp)
leaq 0x8e0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x151de
jmp 0x15269
movq $0x0, 0x8c8(%rsp)
testb $0x1, 0x4ffe9(%rip)     # 0x651da
je 0x1520b
movq 0x4fde6(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x3fd62(%rip), %rsi     # 0x54f66
movb $0x0, %al
callq 0x2250
movq 0xcd8(%rsp), %rdi
movl 0x10dc(%rsp), %esi
movq 0x8c8(%rsp), %rdx
movl 0x8d4(%rsp), %ecx
movsd 0x14e0(%rsp), %xmm0
movl 0x14ec(%rsp), %r8d
callq 0x25380
movq %rax, 0x8d8(%rsp)
movq 0x8c8(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x15264
movq 0x78(%rsp), %rdi
callq 0x2220
jmp 0x15310
movq 0x4fd70(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x3fe18(%rip), %rsi     # 0x55092
movb $0x0, %al
callq 0x2250
movl 0x10dc(%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, 0x8d8(%rsp)
movl $0x1, 0x8c4(%rsp)
movl 0x8c4(%rsp), %eax
cmpl 0x10dc(%rsp), %eax
jg 0x1530e
movq 0xcd8(%rsp), %rax
movslq 0x8c4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x8d8(%rsp), %rax
movslq 0x8c4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x8c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c4(%rsp)
jmp 0x152c1
jmp 0x15310
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x70(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x70(%rsp), %rdi
callq 0xc840
movq 0x70(%rsp), %rax
movq %rax, 0x8b8(%rsp)
movq 0x8d8(%rsp), %rdi
movl 0x10dc(%rsp), %esi
movsd 0x1500(%rsp), %xmm0
movsd 0x14f8(%rsp), %xmm1
movsd 0x14f0(%rsp), %xmm2
movq 0x8b8(%rsp), %rdx
movl $0x1, %ecx
xorl %r9d, %r9d
xorl %eax, %eax
leaq 0x3fc35(%rip), %rax     # 0x54fbd
movl %r9d, %r8d
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x23eb0
movq %rax, 0x8b0(%rsp)
movl $0x0, 0x8ac(%rsp)
movl 0x8ac(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0x8b8(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1552d
leaq 0x4c0(%rsp), %rdi
movl 0x8ac(%rsp), %ecx
leaq 0x3fbc4(%rip), %rsi     # 0x54fc2
leaq 0x3fbb8(%rip), %rdx     # 0x54fbd
movb $0x0, %al
callq 0x2090
leaq 0x4c0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x15422
jmp 0x15515
leaq 0x4c0(%rsp), %rdi
leaq 0x4bc(%rsp), %rsi
callq 0x351c0
movq %rax, 0x4b0(%rsp)
leaq 0x4c0(%rsp), %rdi
callq 0x2330
leaq 0xc0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x8b8(%rsp), %rdi
movslq 0x8ac(%rsp), %rsi
callq 0xc930
movsd (%rax), %xmm0
callq 0x22e0
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
cvttsd2si %xmm0, %ecx
leaq 0x3fc14(%rip), %rsi     # 0x550bd
movb $0x0, %al
callq 0x2090
movq 0x4b0(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0x4bc(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x10e8(%rsp), %rdi
movslq 0x10e4(%rsp), %rsi
callq 0xc240
movq 0x50(%rsp), %rdi
movl 0x5c(%rsp), %esi
movq (%rax), %rdx
leaq 0xc0(%rsp), %rcx
callq 0x34bd0
movq 0x4b0(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, %rax
je 0x15513
movq 0x60(%rsp), %rdi
callq 0x2220
jmp 0x15515
jmp 0x15517
movl 0x8ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8ac(%rsp)
jmp 0x153c1
movq 0x8b0(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x15559
movq 0x38(%rsp), %rdi
callq 0x1acf0
movq 0x38(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
movq 0x8b8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x15585
movq 0x30(%rsp), %rdi
callq 0x1ad50
movq 0x30(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
movq 0xcd8(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x155a2
movq 0x28(%rsp), %rdi
callq 0x2220
movq 0x8d8(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x155bf
movq 0x20(%rsp), %rdi
callq 0x2220
jmp 0x155c1
movl 0x10e4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10e4(%rsp)
jmp 0x14ed9
addq $0x1528, %rsp           # imm = 0x1528
retq
nop