// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq %rdi, %rbx
testl %r9d, %r9d
js 0x122af
leal 0x1(%r9), %eax
xorl %ecx, %ecx
xorl %edx, %edx
testq %r15, %r15
je 0x1229a
movq $0x0, (%r15,%rdx,8)
testq %r14, %r14
je 0x122a7
movq %rcx, (%r14,%rdx,8)
movq %rcx, (%r12,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1228d
movq %r9, 0x20(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x12670
movb $0x0, 0x6(%rsp)
leaq 0x1c032(%rip), %rsi     # 0x2e2fd
movq %rbx, %rdi
callq 0x2738a
testb %al, %al
movq %rbx, 0x28(%rsp)
je 0x122e8
movq 0x27cf5(%rip), %rax     # 0x39fd8
movq (%rax), %rax
jmp 0x122f7
leaq 0x1ba19(%rip), %rsi     # 0x2dd08
movq %rbx, %rdi
callq 0x26387
movq %rax, 0x10(%rsp)
leaq 0x30(%rsp), %r13
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
callq 0x26258
movq %rax, %rbx
testq %rax, %rax
je 0x125c1
imull $0x68c26139, %ebp, %eax # imm = 0x68C26139
addl $0x218c0, %eax          # imm = 0x218C0
rorl $0x6, %eax
cmpl $0x10c6, %eax           # imm = 0x10C6
ja 0x1233b
cmpb $0x1, 0x27e97(%rip)     # 0x3a1cc
je 0x125cc
movq %rbx, %rdi
leaq 0x1c1f3(%rip), %rsi     # 0x2e538
leaq 0x420(%rsp), %rdx
leaq 0x6(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2160
cmpl $0x3, %eax
jne 0x1268a
movb 0x30(%rsp), %cl
leaq 0x31(%rsp), %rax
testb %cl, %cl
je 0x12385
addb $-0x30, %cl
cmpb $0xa, %cl
jae 0x1267a
movb (%rax), %cl
incq %rax
jmp 0x1236e
movl %ebp, 0x1c(%rsp)
movq %r13, %rbp
movq %r13, %rdi
callq 0x2290
cmpl 0x20(%rsp), %eax
jg 0x12682
movl %eax, %r13d
movb $0x0, 0x7(%rsp)
leaq 0x420(%rsp), %rdi
leaq 0x7(%rsp), %rsi
callq 0x12692
testb %al, %al
je 0x12513
xorl %ecx, %ecx
movl $0x3b9aca00, %edx       # imm = 0x3B9ACA00
movb 0x6(%rsp), %sil
xorl %edi, %edi
movsd 0x18c32(%rip), %xmm1    # 0x2b008
movslq %edi, %r9
cmpb $0x0, 0x420(%rsp,%r9)
je 0x12522
movb 0x420(%rsp,%r9), %r10b
leal -0x3a(%r10), %r11d
xorl %r8d, %r8d
cmpb $-0xa, %r11b
jb 0x1242a
movslq %edi, %r9
xorl %r8d, %r8d
leal (%r8,%r8,4), %r8d
addb $-0x30, %r10b
movzbl %r10b, %r10d
leal (%r10,%r8,2), %r8d
movb 0x421(%rsp,%r9), %r10b
incq %r9
leal -0x3a(%r10), %r11d
incl %edi
cmpb $-0xb, %r11b
ja 0x12403
cmpb $0x4d, 0x420(%rsp,%r9)
jne 0x12449
cmpl $0x0, 0x8(%rsp)
jle 0x12480
cmpl %r13d, %edx
cmovgel %r13d, %edx
leal (%r8,%r13), %ecx
decl %ecx
movslq %edi, %r9
incl %edi
movb 0x420(%rsp,%r9), %r9b
leal -0x4d(%r9), %r10d
cmpb $0x2, %r10b
movl $0x0, %r10d
cmovbl %r8d, %r10d
cmpb $0x44, %r9b
cmovel %r8d, %r10d
addl %r10d, %r13d
testb %al, %al
jne 0x123d6
jmp 0x12522
testq %r15, %r15
je 0x124ac
testl %r8d, %r8d
jle 0x124ac
leal (%r8,%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 0x12494
testq %r14, %r14
je 0x12449
cmpb $0x46, %sil
jne 0x124e3
testl %r8d, %r8d
jle 0x12449
leal (%r8,%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 0x124c6
jmp 0x12449
testl %r8d, %r8d
jle 0x12449
leal (%r8,%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 0x124f6
jmp 0x12449
movl $0x3b9aca00, %edx       # imm = 0x3B9ACA00
xorl %ecx, %ecx
movsd 0x18ae6(%rip), %xmm1    # 0x2b008
cmpl $0x0, 0x8(%rsp)
movq %rbp, %r13
movl 0x1c(%rsp), %ebp
jle 0x125b7
movb 0x6(%rsp), %sil
movq 0x8(%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 0x12597
movslq %ecx, %rdx
cmpb $0x46, %sil
jne 0x1257f
movsd (%r14,%rdx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jl 0x12565
jmp 0x12597
movsd (%r12,%rdx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jl 0x1257f
testq %r15, %r15
je 0x125b7
movslq %ecx, %rcx
movsd (%r15,%rcx,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
cmpq %rax, %rcx
jl 0x1259f
incl %ebp
movq %rbx, %rdi
callq 0x2220
testq %rbx, %rbx
jne 0x12303
jmp 0x125eb
movq 0x27a0d(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x1bf00(%rip), %rsi     # 0x2e4dd
movl %ebp, %edx
xorl %eax, %eax
callq 0x2250
jmp 0x1233b
leaq 0x1bd0b(%rip), %rsi     # 0x2e2fd
movq 0x28(%rsp), %rdi
callq 0x2738a
testb %al, %al
jne 0x1260a
movq 0x10(%rsp), %rdi
callq 0x2170
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
jle 0x12654
movl %ecx, %ecx
xorpd %xmm0, %xmm0
movq 0x18b00(%rsp), %rdx
testq %r15, %r15
je 0x12632
movsd (%r15,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1265c
testq %r14, %r14
je 0x12643
movsd (%r14,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1265c
leaq -0x1(%rcx), %rax
cmpq $0x1, %rcx
movq %rax, %rcx
jg 0x12621
xorl %ecx, %ecx
jmp 0x1265c
movq 0x18b00(%rsp), %rdx
movl %ecx, (%rdx)
addq $0x18ac8, %rsp          # imm = 0x18AC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x39f4
jmp 0x122bf
movq %rbx, %rdi
callq 0x3a33
movq %rbx, %rdi
callq 0x3a56
movq %rbx, %rdi
callq 0x3a10