// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_mapped_read_tools.cpp
// Function: preprocess_mapped_reads_file(char*, char*, void (*)(char*, char*, char*, int&, int&, char&, char*), bool)
void preprocess_mapped_reads_file(char* mrf_fp, char* parsed_reads_op_dir, void (preprocess_mapped_read_line)(char* cur_line, 
	char* read_id,
	char* chrom, 
	int& chr_index, int& sequenced_length, 
	char& strand_char, 
	char* mapping_quality_str),
	bool dump_read_id)
{
    // Divide SAM output with respect to chromosomes.
    FILE* f_mrf = NULL;
	t_file_buffer* mrf_file_buffer = NULL;
	if(strcmp(mrf_fp, "stdin") == 0)
	{
		f_mrf = stdin;
	}
	else
	{
		f_mrf = open_f(mrf_fp, "r");
	}

	if(f_mrf == NULL && mrf_file_buffer == NULL)
	{
		fprintf(stderr, "mapped read file pointer and file buffer are both NULL for %s\n", mrf_fp);
		return;
	}

    //char cur_line[100000];
    int n_frags = 0;
    //int n_total_frags = 0;

    vector<FILE*>* frag_f_ptrs = new vector<FILE*>();

	// Check chromosome id's list file.
	char chr_ids_fp[100000];
	sprintf(chr_ids_fp, "%s/chr_ids.txt", parsed_reads_op_dir);

	vector<char*>* chr_ids = NULL;
	if(check_file(chr_ids_fp))
	{
		chr_ids = buffer_file(chr_ids_fp);

		fprintf(stderr, "Found chromosome id's @ %s, pooling.\n", chr_ids_fp);

		// Open the files for appending.
		for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
		{
			char new_fn[1000];
			sprintf(new_fn, "%s/%s_mapped_reads.txt", parsed_reads_op_dir, chr_ids->at(i_chr));
			fprintf(stderr, "Opening %s for pooling.\n", new_fn);
			if(!check_file(new_fn))
			{
				fprintf(stderr, "Could not open %s\n", new_fn);
				open_f(chr_ids_fp, "w");
				exit(0);
			}
			else
			{
				frag_f_ptrs->push_back(open_f(new_fn, "a"));
			}
		} // i_chr loop.
	}
	else
	{
		// The chromosomes will be added now.
		chr_ids = new vector<char*>();
	}

	while(1)
	{
		//char* cur_line = getline(f_mrf);
		char* cur_line = NULL;
		if(f_mrf != NULL)
		{
			cur_line = getline(f_mrf);
		}

		if(cur_line == NULL)
		{
			break;
		}
		
		// Load the mapping info based on the file type.
		char chrom[1000];
		char read_id[1000];
		int chr_index;
		int sequenced_length;
		char strand_char; 
		char mapping_quality_str[20000];
		preprocess_mapped_read_line(cur_line,
									read_id,
									chrom, 
									chr_index, sequenced_length, 
									strand_char, 
									mapping_quality_str);

		// Make sure that the line is valid.
		if(chr_index >= 1 &&
			chrom[0] != 0)
		{
			// Normalize the chromosome id to comply with the 
			normalize_chr_id(chrom);

			// Get the chromosome index.
			int i_chr = t_string::get_i_str(chr_ids, chrom);

			// If the chromosome does not exist in the list opened and accumulated so far, add the id to the list and also open the processed read file.
			if(i_chr == (int)chr_ids->size())
			{
				// Add the chromosome id.
				chr_ids->push_back(t_string::copy_me_str(chrom));
				i_chr = t_string::get_i_str(chr_ids, chrom);

				char new_fn[10000];
				sprintf(new_fn, "%s/%s_mapped_reads.txt", parsed_reads_op_dir, chrom);

				// Does the file exist? If so, use the file, do not overwrite.
				frag_f_ptrs->push_back(open_f(new_fn, "w"));

				fprintf(stderr, "Added %s\n", chrom);
			}

			FILE* cur_frag_file = frag_f_ptrs->at(i_chr);

			if(cur_frag_file == NULL)
			{
					//printf("Could not resolve file pointer for fragment with file name %s\n", chr_fn);
			}
			else
			{
				if(dump_read_id)
				{
					fprintf(cur_frag_file, "%s %s %c %d\n", read_id, mapping_quality_str, strand_char, chr_index);
				}
				else
				{
					fprintf(cur_frag_file, "%s %c %d\n", mapping_quality_str, strand_char, chr_index);
				}
				n_frags++;
			}
		} // check if the line corresponds to a valid mapped nucleotide.

		delete [] cur_line;
	} // file reading loop.

	// (Re)Dump the chromosome id list.
	FILE* f_chrs = open_f(chr_ids_fp, "w");
	for(int i_chr = 0; i_chr< (int)chr_ids->size(); i_chr++)
	{
		fprintf(f_chrs, "%s\n", chr_ids->at(i_chr));
	} // i_chr loop.

	fclose(f_chrs);

	// Close fragment file pointers.
	for(int i_f = 0; i_f < (int)frag_f_ptrs->size(); i_f++)
	{
		fclose(frag_f_ptrs->at(i_f));
	}

	// Unload/close the mapped read file.
	if(f_mrf != NULL)
	{
		fclose(f_mrf);
	}
	else if(mrf_file_buffer != NULL)
	{
		unload_file(mrf_file_buffer);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x203f8, %rsp          # imm = 0x203F8
movl %ecx, 0x1c(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1bfcb(%rip), %rsi     # 0x2d2fd
callq 0x2240
testl %eax, %eax
je 0x1134c
leaq 0x1b9c6(%rip), %rsi     # 0x2cd08
movq %rbx, %rdi
callq 0x25d5a
jmp 0x11356
movq 0x27c85(%rip), %rax     # 0x38fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1164c
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x19658(%rip), %rsi     # 0x2a9de
leaq 0x7d50(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x25d35
testb %al, %al
movq %r15, 0x20(%rsp)
jne 0x11678
movl $0x18, %edi
callq 0x2180
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x11573
movq %rax, %rbp
leaq 0x2f30(%rsp), %r12
leaq 0x430(%rsp), %r15
movq %r12, (%rsp)
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
movq %r15, %rdx
leaq 0xc(%rsp), %rcx
leaq 0x3c(%rsp), %r8
leaq 0xb(%rsp), %r9
callq *0x28(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x114c1
cmpb $0x0, 0x430(%rsp)
je 0x114c1
movq %r15, %rdi
callq 0x12c04
movq %r13, %rdi
movq %r15, %rsi
callq 0x26ed0
movl %eax, %ebx
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cmpl %eax, %ebx
je 0x114e2
movslq %ebx, %rsi
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x1177f
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %rbx
je 0x114c1
movsbl 0xb(%rsp), %r8d
movl 0xc(%rsp), %r9d
cmpb $0x0, 0x1c(%rsp)
je 0x114aa
leaq 0x1bf05(%rip), %rsi     # 0x2d39e
leaq 0x40(%rsp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2250
jmp 0x114c1
leaq 0x1bef0(%rip), %rsi     # 0x2d3a1
movq %r12, %rdx
movl %r8d, %ecx
movl %r9d, %r8d
xorl %eax, %eax
callq 0x2250
movq %rbp, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x25c1e
movq %rax, %rbp
testq %rax, %rax
jne 0x113f4
jmp 0x11573
movq %r15, %rdi
callq 0x26db4
movq %rax, 0x820(%rsp)
movq %r13, %rdi
leaq 0x820(%rsp), %rsi
callq 0xb27c
movq %r13, %rdi
movq %r15, %rsi
callq 0x26ed0
movl %eax, %ebx
leaq 0x820(%rsp), %rdi
leaq 0x1951a(%rip), %rsi     # 0x2aa38
movq 0x20(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2080
leaq 0x820(%rsp), %rdi
leaq 0x1956c(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movq %rax, 0x30(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x12aee
movq 0x27a86(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x1be30(%rip), %rsi     # 0x2d394
movq %r15, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x11458
leaq 0x1952e(%rip), %rsi     # 0x2aaa8
leaq 0x7d50(%rsp), %rdi
callq 0x25d5a
movq %rax, %rbx
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x115e2
leaq 0x1c419(%rip), %r12     # 0x2d9bd
xorl %r15d, %r15d
cmpq %r15, %rdx
jbe 0x11767
movq (%rax,%r15,8), %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x2250
incq %r15
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
jl 0x115a7
movq %rbx, %rdi
callq 0x2170
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x11630
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0x11773
movq (%rax,%rbx,8), %rdi
callq 0x2170
incq %rbx
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x115fe
movq 0x10(%rsp), %rdi
callq 0x2170
addq $0x203f8, %rsp          # imm = 0x203F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2798d(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x1bca6(%rip), %rsi     # 0x2d303
movq %rbx, %rdx
xorl %eax, %eax
addq $0x203f8, %rsp          # imm = 0x203F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2250
leaq 0x7d50(%rsp), %rbx
movq %rbx, %rdi
callq 0x25af8
movq %rax, %r13
movq 0x2794e(%rip), %rbp     # 0x38fe0
movq (%rbp), %rdi
leaq 0x1bca5(%rip), %rsi     # 0x2d342
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2250
movq (%r13), %rax
movq 0x8(%r13), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x113cb
leaq 0x2f30(%rsp), %r12
leaq 0x820(%rsp), %rbx
xorl %r15d, %r15d
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x11767
movq (%rax,%r15,8), %rcx
movq %r12, %rdi
leaq 0x19346(%rip), %rsi     # 0x2aa38
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq (%rbp), %rdi
leaq 0x1bc5f(%rip), %rsi     # 0x2d368
movq %r12, %rdx
xorl %eax, %eax
callq 0x2250
movq %r12, %rdi
callq 0x25d35
testb %al, %al
je 0x1178d
movq %r12, %rdi
leaq 0x1a149(%rip), %rsi     # 0x2b872
callq 0x25d5a
movq %rax, 0x820(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x12aee
incq %r15
movq (%r13), %rax
movq 0x8(%r13), %rdx
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r15
jl 0x116d4
jmp 0x113cb
leaq 0x1b162(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0x11786
leaq 0x1b156(%rip), %rdi     # 0x2c8d0
movq %rbx, %rsi
jmp 0x11786
leaq 0x1b14a(%rip), %rdi     # 0x2c8d0
xorl %eax, %eax
callq 0x2270
leaq 0x2f30(%rsp), %rdi
leaq 0x7d50(%rsp), %rsi
callq 0x387b