// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: write_decomposition_WAV(char*, int, char*, int, double, double, double, int, double)
void write_decomposition_WAV(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);

		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;

#define __UCHAR_MAPPABILITY__
#ifdef __DOUBLE_MAPPABILITY__
			// Load the mapability map signal profile, do filtering.
			mapability_signal_profile = load_per_nucleotide_binary_profile(mapability_signal_profile_fp, l_mapability_profile);

			// Do mapability aware median filtering on the current signal profile.
if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
			fprintf(stderr, "Scaling the mapability map with %d.\n", l_read_mapability_signal * 2);

			int mapability_scaling = l_read * 2;
			for(int i = 1; i <= l_mapability_profile; i++)
			{
				mapability_signal_profile[i] /= mapability_scaling;
			} // i loop.
#elif defined(__UCHAR_MAPPABILITY__)
			// Following loads the mappability signal profile from the char version of the multi-mappability profile.
			// Load the mapability map signal profile, do filtering.
			unsigned char* mapability_signal_char_profile = load_per_nucleotide_binary_uchar_profile(mapability_signal_profile_fp, l_mapability_profile);
			mapability_signal_profile = new double[l_mapability_profile + 2];
			for(int i = 1; i <= l_mapability_profile; i++)
			{
				unsigned char unsigned_char_val = (unsigned char)(mapability_signal_char_profile[i]);
				mapability_signal_profile[i] = (double)(unsigned_char_val);
				mapability_signal_profile[i] /= 100;

				if(mapability_signal_profile[i] < 0)
				{
					fprintf(stderr, "Sanity check failed.\n");
					exit(0);
				}
			} // i loop.
			delete [] mapability_signal_char_profile;
#else
			#error "Must define the type of mappability."
#endif

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, "Could not find the mapability map profile, 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.
		short int* merged_waveform_buffer = new short int[l_profile + 2];
		memset(merged_waveform_buffer, 0, sizeof(short int) * l_profile);
		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);

				int l_cur_last_index = MIN(l_cur_decomp, l_profile);
				for(int i = 1; i < l_cur_last_index; i++)
				{
					merged_waveform_buffer[i] += (short int)(cur_filtered_track[i]);
				} // i loop.

				// 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;

		fprintf(stderr, "Dumping WAV file.\n");

		// Amplify to get 
		short max_val = 0;
		short min_val = 60*1000;
		for(int i = 0; i < l_profile; i++)
		{
			if(max_val < merged_waveform_buffer[i])
			{
				max_val = merged_waveform_buffer[i];
			}

			if(min_val > merged_waveform_buffer[i])
			{
				min_val = merged_waveform_buffer[i];
			}
		} // i_loop.

		double norm_val = ((double)64000 / (double)(max_val - min_val));
		double half_val = (max_val + min_val) / 2;
		for(int i = 0; i < l_profile; i++)
		{
			double fraction_posn_per_cur_val = fabs((merged_waveform_buffer[i] - half_val)/half_val);
			double a = (1 - fraction_posn_per_cur_val);
			double warping_factor = pow((1 - a*a),.5) / (a+.05);
			//if(warping_factor > 1 || warping_factor < 0)
			//{
			//	fprintf(stderr, "Sanity check failed: %lf\n", warping_factor);
			//	exit(0);
			//}
			merged_waveform_buffer[i] *= (short)(warping_factor * norm_val * (merged_waveform_buffer[i] - half_val));
		} // i loop.

		char cur_chr_wav_fp[1000];
		sprintf(cur_chr_wav_fp, "%s.wav", chr_ids->at(i_chr));
		FILE * f_wave = wavfile_open(cur_chr_wav_fp);
		fprintf(stderr, "Opened WAV file.\n");
		if(f_wave == NULL) 
		{
			fprintf(stderr, "couldn't open %s for writing.", cur_chr_wav_fp);
			exit(0);
		}

		fprintf(stderr, "Writing WAV file.\n");
		wavfile_write(f_wave,merged_waveform_buffer,l_profile);
		fprintf(stderr, "Closing WAV file.\n");
		wavfile_close(f_wave);
		fprintf(stderr, "Done!\n");

		delete [] merged_waveform_buffer;
		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 $0x1058, %rsp           # imm = 0x1058
movsd %xmm3, 0x68(%rsp)
movl %r8d, 0x4c(%rsp)
movsd %xmm2, 0x88(%rsp)
movsd %xmm1, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movl %esi, 0x50(%rsp)
movq %rdi, %r12
leaq 0x1bb07(%rip), %rsi     # 0x249de
leaq 0xc70(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x20089
testq %rax, %rax
je 0x9531
movq 0x2c0dc(%rip), %rbp     # 0x34fe0
leaq 0x28(%rsp), %r15
leaq 0xa0(%rsp), %r13
xorl %r14d, %r14d
movq %rax, 0x30(%rsp)
movq 0x8(%rax), %rcx
subq (%rax), %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jge 0x9504
movq (%rbp), %rbx
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x1dc44(%rip), %rsi     # 0x26b93
xorl %eax, %eax
callq 0x2240
movq %rbp, %rdi
movq 0x2c080(%rip), %rbp     # 0x34fe0
movq %r14, %rsi
callq 0x7342
movq (%rax), %rax
cmpb $0x4d, (%rax)
jne 0x8f80
cmpb $0x0, 0x1(%rax)
jne 0x8f80
incq %r14
movq 0x30(%rsp), %rax
jmp 0x8f19
andl $0x0, 0x28(%rsp)
movl $0x8f0d1810, %edi       # imm = 0x8F0D1810
callq 0x2040
movq %rax, %rbx
movq 0x30(%rsp), %rdi
movq %r14, 0x40(%rsp)
movq %r14, %rsi
callq 0x7342
movq (%rax), %rcx
movq %r15, %r14
leaq 0x880(%rsp), %r15
movq %r15, %rdi
leaq 0x1ba7c(%rip), %rsi     # 0x24a38
movq %r12, 0x60(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0x2080
movq %r14, (%rsp)
movq %r15, %rdi
movl 0x50(%rsp), %esi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x11e1a300, %r9d       # imm = 0x11E1A300
callq 0xe682
movslq 0x28(%rsp), %r14
cmpq $-0x2, %r14
leaq 0x10(,%r14,8), %rdi
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r12
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
leaq 0x1bf77(%rip), %r14     # 0x24f93
xorl %ecx, %ecx
movq 0x70(%rsp), %r15
cmpq %rcx, %rax
je 0x903a
movsd 0x8(%rbx,%rcx,8), %xmm0
movsd %xmm0, 0x8(%r12,%rcx,8)
incq %rcx
jmp 0x9023
movq %rbx, %rdi
callq 0x2210
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x7342
movq (%rax), %rcx
leaq 0x490(%rsp), %rdi
movq %r14, %rsi
movq %rdi, %rbx
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
andl $0x0, 0x2c(%rsp)
movq %rbx, %rdi
callq 0x20257
testb %al, %al
je 0x90e1
movq %rbx, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x18624
movq %rax, %rbx
movslq 0x2c(%rsp), %r15
cmpq $-0x2, %r15
leaq 0x10(,%r15,8), %rdi
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r14
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
xorl %ecx, %ecx
movsd 0x1af5d(%rip), %xmm1    # 0x24020
cmpq %rcx, %rax
je 0x913c
movzbl 0x1(%rbx,%rcx), %edx
cvtsi2sd %edx, %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%r14,%rcx,8)
incq %rcx
jmp 0x90c3
movq (%rbp), %rcx
leaq 0x1dad3(%rip), %rdi     # 0x26bbf
pushq $0x37
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
movslq 0x28(%rsp), %r14
cmpq $-0x2, %r14
leaq 0x10(,%r14,8), %rdi
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %rbx
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x9196
movsd 0x8(%r12,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rbx,%rcx,8)
incq %rcx
jmp 0x9125
testq %rbx, %rbx
je 0x9149
movq %rbx, %rdi
callq 0x2210
cmpb $0x1, 0x2c072(%rip)     # 0x351c2
jne 0x9168
movq (%rbp), %rcx
leaq 0x1da43(%rip), %rdi     # 0x26ba0
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %ecx
movq %r12, %rdi
movq %r14, %rdx
movsd 0x68(%rsp), %xmm0
movl 0x4c(%rsp), %r8d
callq 0x10ea4
movq %rax, %rbx
movq %r14, %rdi
callq 0x2210
movl 0x28(%rsp), %r14d
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x1da42(%rip), %rax     # 0x26bf7
movq %rax, 0x18(%rsp)
movupd %xmm0, 0x8(%rsp)
andl $0x0, (%rsp)
xorl %ebp, %ebp
movq %rbx, 0x58(%rsp)
movq %rbx, %rdi
movl %r14d, %esi
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq %r15, %rdx
pushq $0x1
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x103f1
movq %rax, 0x38(%rsp)
movslq 0x28(%rsp), %rbx
cmpq $-0x2, %rbx
leaq 0x4(%rbx,%rbx), %rdi
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x2040
movq %rax, %r14
addq %rbx, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20f0
movq 0x8(%r15), %rax
subq (%r15), %rax
shrq $0x3, %rax
cmpl %eax, %ebp
jge 0x92b4
movq %r13, %rdi
leaq 0x1d9b7(%rip), %rsi     # 0x26bfc
leaq 0x1d9ab(%rip), %rdx     # 0x26bf7
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2080
movq %r13, %rdi
callq 0x20257
testb %al, %al
je 0x92ad
movq %r13, %rdi
leaq 0x54(%rsp), %rsi
callq 0x18e5d
movq %rax, %rbx
movl 0x54(%rsp), %eax
movl 0x28(%rsp), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movslq %ecx, %rax
pushq $0x1
popq %rcx
cmpq %rax, %rcx
jge 0x9298
cvttsd2si (%rbx,%rcx,8), %edx
addw %dx, (%r14,%rcx,2)
incq %rcx
jmp 0x9284
movq %r13, %rdi
callq 0x2300
testq %rbx, %rbx
je 0x92ad
movq %rbx, %rdi
callq 0x2210
incl %ebp
jmp 0x922c
movq 0x38(%rsp), %rbx
testq %rbx, %rbx
je 0x92c6
movq %rbx, %rdi
callq 0xd106
movq %rbx, %rdi
pushq $0x18
popq %rsi
callq 0x2180
movq 0x2bd08(%rip), %rbp     # 0x34fe0
movq (%rbp), %rcx
leaq 0x1d929(%rip), %rdi     # 0x26c0c
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
movl 0x28(%rsp), %ecx
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movw $0xea60, %si            # imm = 0xEA60
xorl %edi, %edi
cmpq %rax, %rdx
je 0x9323
movzwl (%r14,%rax,2), %r8d
cmpw %r8w, %di
cmovlel %r8d, %edi
cmpw %r8w, %si
cmovgel %r8d, %esi
incq %rax
jmp 0x9304
movzwl %di, %edx
movswl %si, %eax
movl %edx, %esi
subl %eax, %esi
cvtsi2sd %esi, %xmm0
movsd 0x1d677(%rip), %xmm1    # 0x269b0
divsd %xmm0, %xmm1
movsd %xmm1, 0x90(%rsp)
addl %edx, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x38(%rsp)
xorl %ebx, %ebx
movslq %ecx, %rax
cmpq %rax, %rbx
jge 0x9400
movswl (%r14,%rbx,2), %eax
cvtsi2sd %eax, %xmm0
movsd 0x38(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd %xmm1, %xmm0
andpd 0x1d65b(%rip), %xmm0    # 0x269e0
movsd 0x1ac7b(%rip), %xmm2    # 0x24008
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x98(%rsp)
mulsd %xmm1, %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd 0x1aca6(%rip), %xmm1    # 0x24058
callq 0x22c0
movsd 0x98(%rsp), %xmm1
addsd 0x1ac88(%rip), %xmm1    # 0x24050
divsd %xmm1, %xmm0
mulsd 0x90(%rsp), %xmm0
movswl (%r14,%rbx,2), %eax
cvtsi2sd %eax, %xmm1
subsd 0x38(%rsp), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
imull %eax, %ecx
movw %cx, (%r14,%rbx,2)
incq %rbx
movl 0x28(%rsp), %ecx
jmp 0x935a
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x7342
movq (%rax), %rdx
movq %r13, %rdi
leaq 0x1d803(%rip), %rsi     # 0x26c1f
xorl %eax, %eax
callq 0x2080
movq %r13, %rdi
callq 0x23ccc
movq %rbp, %rcx
movq %rax, %rbp
movq (%rcx), %rcx
leaq 0x1d7eb(%rip), %rdi     # 0x26c26
pushq $0x11
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2330
movq 0x2bb90(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
testq %rbp, %rbp
je 0x9516
leaq 0x1d7f3(%rip), %rdi     # 0x26c56
pushq $0x12
popq %rsi
movq %rbx, %rdx
callq 0x2330
movl 0x28(%rsp), %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x23d41
movq 0x2bb5c(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x1d7db(%rip), %rdi     # 0x26c69
pushq $0x12
popq %rsi
movq %rbx, %rdx
callq 0x2330
movq %rbp, %rdi
callq 0x23d55
movq 0x2bb38(%rip), %rbp     # 0x34fe0
movq (%rbp), %rcx
leaq 0x1d7c9(%rip), %rdi     # 0x26c7c
pushq $0x6
popq %rsi
movq %rbx, %rdx
callq 0x2330
movq %r14, %rdi
callq 0x2210
movq %r15, %rdi
callq 0xd13a
movq %r15, %rdi
pushq $0x18
popq %rsi
callq 0x2180
movq %r12, %rdi
callq 0x2210
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x94f0
callq 0x2210
movq 0x60(%rsp), %r12
leaq 0x28(%rsp), %r15
movq 0x40(%rsp), %r14
jmp 0x8f76
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d71b(%rip), %rsi     # 0x26c38
leaq 0xa0(%rsp), %rdx
movq %rcx, %rdi
xorl %eax, %eax
callq 0x2240
jmp 0x954d
movq 0x2baa8(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x1d61a(%rip), %rdi     # 0x26b5c
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
xorl %edi, %edi
callq 0x21d0