// File: gersteinlab[P]MUSIC/src/ms_mapped_read_tools.cpp
// Function: buffer_per_nucleotide_profile_no_buffer(char*, int, double*, double*, double*, int, int&)
void buffer_per_nucleotide_profile_no_buffer(char* sorted_read_fp, const int l_extended_tag, 
	double* signal_profile_buffer, double* forward_strand_signal, double* reverse_strand_signal, 
	int l_buffer, int& l_data)
{
	for(int i = 0; i <= l_buffer; i++)
	{
		if(signal_profile_buffer != NULL)
		{
			signal_profile_buffer[i] = 0.0;
		}

		// If the per strand signal generation is requested, initilize the per strand signal.
		if(forward_strand_signal != NULL)
		{
			forward_strand_signal[i] = 0.0;
			reverse_strand_signal[i] = 0.0;
		}
	} // i loop.

	// Non-positive tag extension lengths are ignored and falls back to using the length in the CIGAR string entry.
	if(l_extended_tag <= 0)
	{
		fprintf(stderr, "Ignoring tag extension.\n");
	}

	char strand_char = 0;
	//char cur_fragment[100000];
	char mapping_map_str[100000];
	int chr_index;
	int n_processed_reads = 0;
	FILE* f_sorted_reads = NULL;
	if(t_string::compare_strings(sorted_read_fp, "stdin"))
	{
		f_sorted_reads = stdin;
	}
	else
	{
		f_sorted_reads = open_f(sorted_read_fp, "r");
	}

	while(1)
	{
		char* cur_read = getline(f_sorted_reads);

		if(cur_read == NULL)
		{
			break;
		}
		else if((n_processed_reads % (1000*1000)) == 0)
		{
if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
{
			fprintf(stderr, "Processing %d. read.                        \r", n_processed_reads);
}
		}	

		n_processed_reads++;

		// We need a check on the current read line to make sure what we have is a valid: index must be strictly numbers; mapping map string is validated
		// below.
		char chr_index_str[1000];

		if(sscanf(cur_read, "%s %c %s", mapping_map_str, &strand_char, chr_index_str) != 3)
		{
			fprintf(stderr, "Could not parse %s\n", cur_read);
			exit(0);
		}

		// Validate the string read as chromosome index.
		int char_i = 0;
		while(chr_index_str[char_i] != 0)
		{
			bool char_is_a_num = (chr_index_str[char_i] >= '0' && chr_index_str[char_i] <= '9');
			if(!char_is_a_num)
			{
				fprintf(stderr, "Chromosome index must be a number: %s\n", cur_read);
				exit(0);
			}

			char_i++;
		} // char_i loop.

		// This may be pointing to an error in read preprocessing.
		chr_index = atoi(chr_index_str);
		if(chr_index > l_buffer)
		{
			fprintf(stderr, "%s: The read mapped out of buffer.\n", cur_read);
			exit(0);
		}

		int i_mapp_map = 0;
		bool is_matching = false;
		char entry_type_char;

		// Parse the cigar string to get the fragments.
		bool is_read_spliced = false;
		bool mapping_map_str_valid = validate_mapping_map_str(mapping_map_str, is_read_spliced);

		int right_most_match_pos = 0;
		int left_most_match_pos = 1000*1000*1000;

		while(mapping_map_str_valid && 
			mapping_map_str[i_mapp_map] != 0)
		{
			int l_cur_entry;
			get_next_entry_per_mapp_map_string(mapping_map_str,
												i_mapp_map, 
												is_matching,
												l_cur_entry,
												entry_type_char);

			if(is_matching)
			{
				// Should we ignore the tag extension?
				if(l_extended_tag <= 0)
				{
					// Increase the height: The indexing is already 1 based, there is no conversion needed.
					if(signal_profile_buffer != NULL)
					{
						for(int i_cur = chr_index; i_cur <= chr_index+l_cur_entry-1; i_cur++)
						{
							signal_profile_buffer[i_cur]++;
						} // i_cur loop.
					}

					// Update the strand signals, if requested.
					if(forward_strand_signal != NULL)
					{
						if(strand_char == 'F')
						{
							// Update the forward strand signal.
							for(int i_cur = chr_index; i_cur <= chr_index+l_cur_entry-1; i_cur++)
							{
								forward_strand_signal[i_cur]++;
							} // i_cur loop.
						}
						else
						{
							// Update the reverse strand signal.
							for(int i_cur = chr_index; i_cur <= chr_index+l_cur_entry-1; i_cur++)
							{
								reverse_strand_signal[i_cur]++;
							} // i_cur loop.
						}
					} // strand signal check.
				}
				else // tag extension validation check.
				{
					left_most_match_pos = MIN(left_most_match_pos, chr_index);
					right_most_match_pos = (chr_index + l_cur_entry - 1);
				} // extension length check.
			} // match block check.

			// Update the base for the current entry.
			// Must check whether to update the mapping posn: Update only for D and M entries.
			/*if(entry_type_char == 'D' || 
				entry_type_char == 'M' ||
				entry_type_char == 'N' ||
				entry_type_char == 'H')*/
			if(check_genome_index_update_per_CIGAR_entry(entry_type_char))
			{
				chr_index += l_cur_entry;
			}
		} // mapping map string processing loop.

		// If tag extension is requested, utilize the leftmost and rightmost matching position for the mapped read.
		if(l_extended_tag > 0)
		{
			int ext_start = 0;
			int ext_end = 0;
			if(strand_char == 'F')
			{
				ext_start = left_most_match_pos;
			}
			else
			{
				//ext_start = (chr_index + l_cur_entry - 1) - (l_extended_tag - 1);
				ext_start = right_most_match_pos - (l_extended_tag - 1);
			}

			// Check for negative starts.
			if(ext_start < 0)
			{
				ext_start = 1;
			}

			ext_end = ext_start + l_extended_tag - 1;

			// Update profiles for the strands.
			if(forward_strand_signal != NULL)
			{
				if(strand_char == 'F')
				{
					// Update the forward strand signal.
					for(int i_cur = ext_start; i_cur <= ext_end; i_cur++)
					{
						forward_strand_signal[i_cur]++;
					} // i_cur loop.
				}
				else
				{
					// Update the reverse strand signal.
					for(int i_cur = ext_start; i_cur <= ext_end; i_cur++)
					{
						reverse_strand_signal[i_cur]++;
					} // i_cur loop.
				}
			} // strand signal check.

			// Update the profile.
			if(signal_profile_buffer != NULL)
			{
				for(int i_cur = ext_start; i_cur <= ext_end; i_cur++)
				{
					signal_profile_buffer[i_cur]++;
				} // i_cur loop.
			} // profile existence check.
		} // signal update check.

		delete [] cur_read;
	} // file reading loop.

	if(t_string::compare_strings(sorted_read_fp, "stdin"))
	{
	}
	else
	{
		fclose(f_sorted_reads);
	}

	// Get the length of the data.
	l_data = l_buffer;
	while(l_data > 0)
	{
		if(signal_profile_buffer != NULL && signal_profile_buffer[l_data] > 0.0)
		{
			break;
		}
		else if(forward_strand_signal != NULL && forward_strand_signal[l_data] > 0.0)
		{
			break;
		}
		else
		{
			l_data--;
		}
	} // l_data setting loop.
}

// Assembly:
subq $0x18b28, %rsp          # imm = 0x18B28
movq 0x18b30(%rsp), %rax
movq %rdi, 0x18b20(%rsp)
movl %esi, 0x18b1c(%rsp)
movq %rdx, 0x18b10(%rsp)
movq %rcx, 0x18b08(%rsp)
movq %r8, 0x18b00(%rsp)
movl %r9d, 0x18afc(%rsp)
movl $0x0, 0x18af8(%rsp)
movl 0x18af8(%rsp), %eax
cmpl 0x18afc(%rsp), %eax
jg 0x1eebf
cmpq $0x0, 0x18b10(%rsp)
je 0x1ee6c
movq 0x18b10(%rsp), %rax
movslq 0x18af8(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x18b08(%rsp)
je 0x1eea7
movq 0x18b08(%rsp), %rax
movslq 0x18af8(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x18b00(%rsp), %rax
movslq 0x18af8(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x1eea9
movl 0x18af8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18af8(%rsp)
jmp 0x1ee39
cmpl $0x0, 0x18b1c(%rsp)
jg 0x1eee1
movq 0x46110(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x36a05(%rip), %rsi     # 0x558df
movb $0x0, %al
callq 0x2250
movb $0x0, 0x18af7(%rsp)
movl $0x0, 0x448(%rsp)
movq $0x0, 0x440(%rsp)
movq 0x18b20(%rsp), %rdi
leaq 0x367ae(%rip), %rsi     # 0x556bd
callq 0x47ac0
testb $0x1, %al
jne 0x1ef1a
jmp 0x1ef2e
movq 0x460b7(%rip), %rax     # 0x64fd8
movq (%rax), %rax
movq %rax, 0x440(%rsp)
jmp 0x1ef4a
movq 0x18b20(%rsp), %rdi
leaq 0x36191(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x440(%rsp)
jmp 0x1ef4c
movq 0x440(%rsp), %rdi
callq 0x45400
movq %rax, 0x438(%rsp)
cmpq $0x0, 0x438(%rsp)
jne 0x1ef71
jmp 0x1f47d
movl 0x448(%rsp), %eax
movl $0xf4240, %ecx          # imm = 0xF4240
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1efaf
testb $0x1, 0x46250(%rip)     # 0x651dc
je 0x1efad
movq 0x4604b(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x448(%rsp), %edx
leaq 0x368f7(%rip), %rsi     # 0x5589d
movb $0x0, %al
callq 0x2250
jmp 0x1efaf
jmp 0x1efb1
movl 0x448(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x448(%rsp)
movq 0x438(%rsp), %rdi
leaq 0x450(%rsp), %rdx
leaq 0x50(%rsp), %r8
leaq 0x3691a(%rip), %rsi     # 0x558f8
leaq 0x18af7(%rsp), %rcx
movb $0x0, %al
callq 0x2170
cmpl $0x3, %eax
je 0x1f019
movq 0x45fe7(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x438(%rsp), %rdx
leaq 0x368c0(%rip), %rsi     # 0x558cb
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x0, 0x4c(%rsp)
movslq 0x4c(%rsp), %rax
movsbl 0x50(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x1f09e
movslq 0x4c(%rsp), %rax
movsbl 0x50(%rsp,%rax), %ecx
xorl %eax, %eax
cmpl $0x30, %ecx
movb %al, 0x13(%rsp)
jl 0x1f059
movslq 0x4c(%rsp), %rax
movsbl 0x50(%rsp,%rax), %eax
cmpl $0x39, %eax
setle %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
andb $0x1, %al
movb %al, 0x4b(%rsp)
testb $0x1, 0x4b(%rsp)
jne 0x1f091
movq 0x45f6f(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x438(%rsp), %rdx
leaq 0x3687e(%rip), %rsi     # 0x55901
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x1f021
leaq 0x50(%rsp), %rdi
callq 0x2290
movl %eax, 0x44c(%rsp)
movl 0x44c(%rsp), %eax
cmpl 0x18afc(%rsp), %eax
jle 0x1f0e6
movq 0x45f1a(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x438(%rsp), %rdx
leaq 0x36850(%rip), %rsi     # 0x55928
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x0, 0x44(%rsp)
movb $0x0, 0x43(%rsp)
movb $0x0, 0x41(%rsp)
leaq 0x450(%rsp), %rdi
leaq 0x41(%rsp), %rsi
callq 0x1f540
andb $0x1, %al
movb %al, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl $0x3b9aca00, 0x38(%rsp) # imm = 0x3B9ACA00
xorl %eax, %eax
testb $0x1, 0x40(%rsp)
movb %al, 0x12(%rsp)
je 0x1f144
movslq 0x44(%rsp), %rax
movsbl 0x450(%rsp,%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0x12(%rsp)
movb 0x12(%rsp), %al
testb $0x1, %al
jne 0x1f151
jmp 0x1f308
leaq 0x450(%rsp), %rdi
leaq 0x44(%rsp), %rsi
leaq 0x43(%rsp), %rdx
leaq 0x34(%rsp), %rcx
leaq 0x42(%rsp), %r8
callq 0x1f6f0
testb $0x1, 0x43(%rsp)
je 0x1f2e1
cmpl $0x0, 0x18b1c(%rsp)
jg 0x1f2a3
cmpq $0x0, 0x18b10(%rsp)
je 0x1f1e5
movl 0x44c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl 0x44c(%rsp), %ecx
addl 0x34(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x1f1e3
movq 0x18b10(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsd 0x32e4c(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x1f1a1
jmp 0x1f1e5
cmpq $0x0, 0x18b08(%rsp)
je 0x1f2a1
movsbl 0x18af7(%rsp), %eax
cmpl $0x46, %eax
jne 0x1f250
movl 0x44c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl 0x44c(%rsp), %ecx
addl 0x34(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x1f24e
movq 0x18b08(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd 0x32de1(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1f20c
jmp 0x1f29f
movl 0x44c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl 0x44c(%rsp), %ecx
addl 0x34(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x1f29d
movq 0x18b00(%rsp), %rax
movslq 0x28(%rsp), %rcx
movsd 0x32d92(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x1f25b
jmp 0x1f29f
jmp 0x1f2a1
jmp 0x1f2df
movl 0x38(%rsp), %eax
cmpl 0x44c(%rsp), %eax
jge 0x1f2ba
movl 0x38(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x1f2c5
movl 0x44c(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x44c(%rsp), %eax
addl 0x34(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1f2e1
movsbl 0x42(%rsp), %edi
callq 0x1f7e0
testb $0x1, %al
jne 0x1f2f1
jmp 0x1f303
movl 0x34(%rsp), %eax
addl 0x44c(%rsp), %eax
movl %eax, 0x44c(%rsp)
jmp 0x1f120
cmpl $0x0, 0x18b1c(%rsp)
jle 0x1f45d
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movsbl 0x18af7(%rsp), %eax
cmpl $0x46, %eax
jne 0x1f33d
movl 0x38(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x1f351
movl 0x3c(%rsp), %eax
movl 0x18b1c(%rsp), %ecx
subl $0x1, %ecx
subl %ecx, %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jge 0x1f360
movl $0x1, 0x24(%rsp)
movl 0x24(%rsp), %eax
addl 0x18b1c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x20(%rsp)
cmpq $0x0, 0x18b08(%rsp)
je 0x1f410
movsbl 0x18af7(%rsp), %eax
cmpl $0x46, %eax
jne 0x1f3ce
movl 0x24(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x1f3cc
movq 0x18b08(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x32c63(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x1f396
jmp 0x1f40e
movl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x1f40c
movq 0x18b00(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd 0x32c23(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x1f3d6
jmp 0x1f40e
jmp 0x1f410
cmpq $0x0, 0x18b10(%rsp)
je 0x1f45b
movl 0x24(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x1f459
movq 0x18b10(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsd 0x32bd6(%rip), %xmm0    # 0x52018
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x1f423
jmp 0x1f45b
jmp 0x1f45d
movq 0x438(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x1f478
movq (%rsp), %rdi
callq 0x2220
jmp 0x1ef4c
movq 0x18b20(%rsp), %rdi
leaq 0x36231(%rip), %rsi     # 0x556bd
callq 0x47ac0
testb $0x1, %al
jne 0x1f497
jmp 0x1f499
jmp 0x1f4a6
movq 0x440(%rsp), %rdi
callq 0x2180
movl 0x18afc(%rsp), %ecx
movq 0x18b30(%rsp), %rax
movl %ecx, (%rax)
movq 0x18b30(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x1f533
cmpq $0x0, 0x18b10(%rsp)
je 0x1f4f2
movq 0x18b10(%rsp), %rax
movq 0x18b30(%rsp), %rcx
movslq (%rcx), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1f4f2
jmp 0x1f533
cmpq $0x0, 0x18b08(%rsp)
je 0x1f520
movq 0x18b08(%rsp), %rax
movq 0x18b30(%rsp), %rcx
movslq (%rcx), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1f520
jmp 0x1f533
movq 0x18b30(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x1f531
jmp 0x1f4b7
addq $0x18b28, %rsp          # imm = 0x18B28
retq
nopl (%rax,%rax)