// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18ac8, %rsp          # imm = 0x18AC8
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
testl %r9d, %r9d
js 0x11fa8
leal 0x1(%r9), %eax
xorl %ecx, %ecx
xorl %edx, %edx
testq %r15, %r15
je 0x11f93
movq $0x0, (%r15,%rdx,8)
testq %r14, %r14
je 0x11fa0
movq %rcx, (%r14,%rdx,8)
movq %rcx, (%r12,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x11f86
movq %r9, 0x18(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x12372
movb $0x0, 0x6(%rsp)
leaq 0x1b339(%rip), %rsi     # 0x2d2fd
movq %rbx, %rdi
callq 0x26d70
testb %al, %al
movq %rbx, 0x20(%rsp)
je 0x11fe1
movq 0x26ffc(%rip), %rax     # 0x38fd8
movq (%rax), %r13
jmp 0x11ff3
leaq 0x1ad20(%rip), %rsi     # 0x2cd08
movq %rbx, %rdi
callq 0x25d5a
movq %rax, %r13
movq %r13, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x122f9
movq %rax, %rcx
movabsq $0x100000000, %rbp      # imm = 0x100000000
xorl %ebx, %ebx
movq %r13, 0x28(%rsp)
imull $0x68c26139, %ebx, %eax # imm = 0x68C26139
rorl $0x6, %eax
cmpl $0x10c6, %eax           # imm = 0x10C6
movq %rcx, 0x8(%rsp)
ja 0x1203a
cmpb $0x1, 0x27198(%rip)     # 0x391cc
je 0x122d5
movq %rcx, %rdi
leaq 0x1b4f4(%rip), %rsi     # 0x2d538
leaq 0x420(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x2160
cmpl $0x3, %eax
jne 0x12386
movb 0x30(%rsp), %cl
leaq 0x31(%rsp), %rax
testb %cl, %cl
je 0x12086
addb $-0x30, %cl
cmpb $0xa, %cl
jae 0x1237c
movb (%rax), %cl
incq %rax
jmp 0x1206f
leaq 0x30(%rsp), %rdi
callq 0x2290
cmpl 0x18(%rsp), %eax
jg 0x12390
movl %eax, %r13d
leaq 0x420(%rsp), %rdi
leaq 0x7(%rsp), %rsi
callq 0x1239a
movl $0x3b9aca00, %edx       # imm = 0x3B9ACA00
xorl %ecx, %ecx
testb %al, %al
je 0x12218
movb 0x420(%rsp), %r9b
testb %r9b, %r9b
movsd 0x17f37(%rip), %xmm1    # 0x2a008
je 0x12220
movb 0x6(%rsp), %al
xorl %esi, %esi
xorl %ecx, %ecx
leal -0x3a(%r9), %edi
cmpb $-0xa, %dil
jae 0x120f0
movl %r9d, %r8d
xorl %edi, %edi
jmp 0x12137
movq %rsi, %r8
shlq $0x20, %r8
movq %rsi, %r10
leaq 0x421(%rsp), %rdi
addq %rdi, %r10
xorl %edi, %edi
addq %rbp, %r8
leal (%rdi,%rdi,4), %edi
addb $-0x30, %r9b
movzbl %r9b, %r9d
leal (%r9,%rdi,2), %edi
movb (%r10), %r9b
leal -0x3a(%r9), %r11d
incl %esi
incq %r10
cmpb $-0xb, %r11b
ja 0x12107
sarq $0x20, %r8
movb 0x420(%rsp,%r8), %r8b
cmpb $0x4d, %r9b
jne 0x12151
cmpl $0x0, 0x10(%rsp)
jle 0x1218a
cmpl %r13d, %edx
cmovgel %r13d, %edx
leal (%rdi,%r13), %ecx
decl %ecx
movslq %esi, %rsi
incq %rsi
leal -0x4d(%r8), %r9d
cmpb $0x2, %r9b
movl $0x0, %r9d
cmovbl %edi, %r9d
cmpb $0x44, %r8b
cmovel %edi, %r9d
addl %r9d, %r13d
movb 0x420(%rsp,%rsi), %r9b
testb %r9b, %r9b
jne 0x120df
jmp 0x12220
testq %r15, %r15
je 0x121b5
testl %edi, %edi
jle 0x121b5
leal (%rdi,%r13), %r10d
movslq %r13d, %r9
movslq %r10d, %r10
movsd (%r15,%r9,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%r9,8)
incq %r9
cmpq %r10, %r9
jl 0x1219d
testq %r14, %r14
je 0x12151
cmpb $0x46, %al
jne 0x121e9
testl %edi, %edi
jle 0x12151
leal (%rdi,%r13), %r10d
movslq %r13d, %r9
movslq %r10d, %r10
movsd (%r14,%r9,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%r9,8)
incq %r9
cmpq %r10, %r9
jl 0x121cc
jmp 0x12151
testl %edi, %edi
jle 0x12151
leal (%rdi,%r13), %r10d
movslq %r13d, %r9
movslq %r10d, %r10
movsd (%r12,%r9,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%r9,8)
incq %r9
cmpq %r10, %r9
jl 0x121fb
jmp 0x12151
movsd 0x17de8(%rip), %xmm1    # 0x2a008
cmpl $0x0, 0x10(%rsp)
movq 0x28(%rsp), %r13
jle 0x122b3
movb 0x6(%rsp), %sil
movq 0x10(%rsp), %rax
subl %eax, %ecx
incl %ecx
cmpb $0x46, %sil
cmovel %edx, %ecx
testl %ecx, %ecx
movl $0x1, %edx
cmovsl %edx, %ecx
addl %ecx, %eax
cltq
testq %r14, %r14
je 0x12293
movslq %ecx, %rdx
cmpb $0x46, %sil
jne 0x1227b
movsd (%r14,%rdx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jl 0x12261
jmp 0x12293
movsd (%r12,%rdx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jl 0x1227b
testq %r15, %r15
je 0x122b3
movslq %ecx, %rcx
movsd (%r15,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
cmpq %rax, %rcx
jl 0x1229b
incl %ebx
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x25c1e
movq %rax, %rcx
testq %rax, %rax
jne 0x12018
jmp 0x122f9
movq 0x26d04(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x1b1f7(%rip), %rsi     # 0x2d4dd
movl %ebx, %edx
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %rcx
jmp 0x1203a
leaq 0x1affd(%rip), %rsi     # 0x2d2fd
movq 0x20(%rsp), %rdi
callq 0x26d70
testb %al, %al
jne 0x12316
movq %r13, %rdi
callq 0x2170
movq 0x18b00(%rsp), %rax
movq 0x18(%rsp), %rdx
testl %edx, %edx
jle 0x1235e
movl %edx, %edx
xorpd %xmm0, %xmm0
testq %r15, %r15
je 0x1233e
movsd (%r15,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1235e
testq %r14, %r14
je 0x1234f
movsd (%r14,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1235e
leaq -0x1(%rdx), %rcx
cmpq $0x1, %rdx
movq %rcx, %rdx
jg 0x1232d
xorl %edx, %edx
movl %edx, (%rax)
addq $0x18ac8, %rsp          # imm = 0x18AC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x38d3
jmp 0x11fb8
movq 0x8(%rsp), %rdi
callq 0x3912
movq 0x8(%rsp), %rdi
callq 0x38ef
movq 0x8(%rsp), %rdi
callq 0x3935