// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18ae8, %rsp          # imm = 0x18AE8
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movl %r9d, 0x14(%rsp)
movslq %r9d, %rax
xorl %ecx, %ecx
cmpq %rax, %rcx
jg 0xe6d1
testq %r14, %r14
je 0xe6bd
andq $0x0, (%r14,%rcx,8)
testq %rbx, %rbx
je 0xe6cc
andq $0x0, (%rbx,%rcx,8)
andq $0x0, (%r15,%rcx,8)
incq %rcx
jmp 0xe6ae
cmpl $0x0, 0x20(%rsp)
jle 0xea4f
movb $0x0, 0xa(%rsp)
leaq 0x18c15(%rip), %rsi     # 0x272fd
movq %r12, %rdi
callq 0x2100e
testb %al, %al
movq %r12, 0x48(%rsp)
je 0xe705
movq 0x268d8(%rip), %rax     # 0x34fd8
movq (%rax), %rax
jmp 0xe714
leaq 0x185fc(%rip), %rsi     # 0x26d08
movq %r12, %rdi
callq 0x2027c
movq %rax, 0x28(%rsp)
leaq 0x50(%rsp), %r13
xorl %ebp, %ebp
movq 0x28(%rsp), %rdi
callq 0x2017f
testq %rax, %rax
je 0xe9d7
movq %rax, %r12
movl %ebp, %eax
xorl %edx, %edx
movl $0xf4240, %ecx          # imm = 0xF4240
divl %ecx
testl %edx, %edx
jne 0xe752
cmpb $0x1, 0x26a78(%rip)     # 0x351c4
je 0xe9b8
movq %r12, %rdi
leaq 0x18ddc(%rip), %rsi     # 0x27538
leaq 0x440(%rsp), %rdx
leaq 0xa(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2150
cmpl $0x3, %eax
jne 0xea92
incl %ebp
movq %r13, %rax
movb (%rax), %cl
testb %cl, %cl
je 0xe797
addb $-0x30, %cl
incq %rax
cmpb $0xa, %cl
jb 0xe781
jmp 0xea70
movl %ebp, 0x34(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, %rdi
callq 0x2280
cmpl 0x14(%rsp), %eax
jg 0xeaa5
movl %eax, %ebp
andl $0x0, 0x1c(%rsp)
movb $0x0, 0x9(%rsp)
leaq 0x440(%rsp), %rdi
leaq 0x13(%rsp), %rsi
callq 0xeabd
movl %eax, %r13d
movq $0x0, 0x40(%rsp)
movb 0xa(%rsp), %al
movb %al, 0x8(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x3b9aca00, %r12d      # imm = 0x3B9ACA00
movsd 0x1580e(%rip), %xmm1    # 0x24008
testb %r13b, %r13b
je 0xe8ff
movslq 0x1c(%rsp), %rax
cmpb $0x0, 0x440(%rsp,%rax)
je 0xe907
leaq 0x440(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x9(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0xb(%rsp), %r8
callq 0xeb1a
cmpb $0x1, 0x9(%rsp)
jne 0xe862
cmpl $0x0, 0x20(%rsp)
movsd 0x157bd(%rip), %xmm1    # 0x24008
jle 0xe88c
cmpl %ebp, %r12d
cmovgel %ebp, %r12d
movl 0xc(%rsp), %eax
addl %ebp, %eax
decl %eax
movl %eax, 0x18(%rsp)
jmp 0xe86a
movsd 0x1579e(%rip), %xmm1    # 0x24008
movb 0xb(%rsp), %al
leal -0x4d(%rax), %ecx
cmpb $0x2, %cl
movl 0xc(%rsp), %ecx
movl $0x0, %edx
cmovbl %ecx, %edx
cmpb $0x44, %al
cmovel %ecx, %edx
addl %edx, %ebp
jmp 0xe7fa
testq %r14, %r14
je 0xe8b6
movslq %ebp, %rax
movslq 0xc(%rsp), %rcx
addq %rax, %rcx
cmpq %rcx, %rax
jge 0xe8b6
movsd (%r14,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rax,8)
incq %rax
jmp 0xe89c
testq %rbx, %rbx
je 0xe86a
movslq 0xc(%rsp), %rax
movslq %ebp, %rcx
addq %rcx, %rax
cmpb $0x46, 0x8(%rsp)
jne 0xe8e5
cmpq %rax, %rcx
jge 0xe86a
movsd (%rbx,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0xe8cd
cmpq %rax, %rcx
jge 0xe86a
movsd (%r15,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
jmp 0xe8e5
movl $0x3b9aca00, %r12d      # imm = 0x3B9ACA00
jmp 0xe910
movl 0x18(%rsp), %eax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x20(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %r13
movl 0x34(%rsp), %ebp
movq 0x40(%rsp), %rdx
movb 0x8(%rsp), %sil
jle 0xe9ae
movq 0x20(%rsp), %rax
subl %eax, %edx
incl %edx
cmpb $0x46, %sil
cmovel %r12d, %edx
testl %edx, %edx
pushq $0x1
popq %rcx
cmovsl %ecx, %edx
addl %edx, %eax
cltq
testq %rbx, %rbx
je 0xe98c
movslq %edx, %rcx
cmpb $0x46, %sil
jne 0xe972
cmpq %rax, %rcx
jge 0xe98c
movsd (%rbx,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0xe95a
cmpq %rax, %rcx
jge 0xe98c
movsd (%r15,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
jmp 0xe972
testq %r14, %r14
je 0xe9ae
movslq %edx, %rcx
cmpq %rax, %rcx
jge 0xe9ae
movsd (%r14,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rcx,8)
incq %rcx
jmp 0xe994
callq 0x2210
jmp 0xe720
movq 0x26621(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x18b14(%rip), %rsi     # 0x274dd
movl %ebp, %edx
xorl %eax, %eax
callq 0x2240
jmp 0xe752
leaq 0x1891f(%rip), %rsi     # 0x272fd
movq 0x48(%rsp), %rdi
callq 0x2100e
testb %al, %al
jne 0xe9f6
movq 0x28(%rsp), %rdi
callq 0x2160
movl 0x14(%rsp), %edx
movl %edx, %eax
movl %edx, %ecx
sarl $0x1f, %ecx
andl %edx, %ecx
xorpd %xmm0, %xmm0
testl %eax, %eax
jle 0xea31
testq %r14, %r14
je 0xea1c
movsd (%r14,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0xea33
testq %rbx, %rbx
je 0xea2c
movsd (%rbx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0xea33
decq %rax
jmp 0xea07
movl %ecx, %eax
movq 0x18b20(%rsp), %rcx
movl %eax, (%rcx)
addq $0x18ae8, %rsp          # imm = 0x18AE8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2658a(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x18abf(%rip), %rdi     # 0x2751f
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
jmp 0xe6dc
movq 0x26569(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x18ac0(%rip), %rsi     # 0x27541
movq %r12, %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq 0x26547(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x18a68(%rip), %rsi     # 0x2750b
jmp 0xea81
movq 0x26534(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x18ab2(%rip), %rsi     # 0x27568
movq 0x38(%rsp), %rdx
jmp 0xea84