// File: /workspace/github_cpp_makefile_star2_O1/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 $0x20408, %rsp          # imm = 0x20408
movl %ecx, 0x24(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1cced(%rip), %rsi     # 0x2e2fd
callq 0x2240
testl %eax, %eax
je 0x1162d
leaq 0x1c6e8(%rip), %rsi     # 0x2dd08
movq %rbx, %rdi
callq 0x26387
movq %rax, %rbp
jmp 0x11637
movq 0x289a4(%rip), %rax     # 0x39fd8
movq (%rax), %rbp
testq %rbp, %rbp
je 0x11941
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rax)
leaq 0x1a380(%rip), %rsi     # 0x2b9e4
leaq 0x7d60(%rsp), %rbx
movq %rbx, %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x26362
testb %al, %al
jne 0x1196d
movl $0x18, %edi
callq 0x2180
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x2f40(%rsp), %rbx
leaq 0x440(%rsp), %r14
movq %rbp, %rdi
callq 0x26258
testq %rax, %rax
je 0x11865
movq %rax, %r15
movq %rbx, (%rsp)
movq %rax, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x4c(%rsp), %r8
leaq 0x13(%rsp), %r9
callq *%r12
cmpl $0x0, 0x14(%rsp)
jle 0x117b3
cmpb $0x0, 0x50(%rsp)
je 0x117b3
movq %rbx, %r14
movq %r12, %rbx
movq %rbp, %r12
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x12f1c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x274fc
movl %eax, %ebp
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cmpl %eax, %ebp
je 0x117c9
movslq %ebp, %rsi
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x11a85
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
movq %r12, %rbp
movq %rbx, %r12
movq %r14, %rbx
leaq 0x440(%rsp), %r14
je 0x117b3
movsbl 0x13(%rsp), %r8d
movl 0x14(%rsp), %r9d
cmpb $0x0, 0x24(%rsp)
je 0x1179c
leaq 0x1cc11(%rip), %rsi     # 0x2e39e
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2250
jmp 0x117b3
leaq 0x1cbfe(%rip), %rsi     # 0x2e3a1
movq %rbx, %rdx
movl %r8d, %ecx
movl %r9d, %r8d
xorl %eax, %eax
callq 0x2250
movq %r15, %rdi
callq 0x2220
testq %r15, %r15
jne 0x116b9
jmp 0x11865
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x273e0
movq %rax, 0x830(%rsp)
movq %r13, %rdi
leaq 0x830(%rsp), %rsi
callq 0xb476
movq %r13, %rdi
movq %rbp, %rsi
callq 0x274fc
movl %eax, %ebp
leaq 0x830(%rsp), %rdi
leaq 0x1a234(%rip), %rsi     # 0x2ba3e
movq 0x28(%rsp), %rdx
leaq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x2080
leaq 0x830(%rsp), %rdi
leaq 0x1a284(%rip), %rsi     # 0x2baae
callq 0x26387
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x12e0e
movq 0x28796(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x1cb40(%rip), %rsi     # 0x2e394
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x1173b
leaq 0x1a242(%rip), %rsi     # 0x2baae
leaq 0x7d60(%rsp), %rdi
callq 0x26387
movq %rax, %rbx
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x118d4
leaq 0x1d127(%rip), %r12     # 0x2e9bd
xorl %r15d, %r15d
cmpq %r15, %rdx
jbe 0x11a79
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 0x11899
movq %rbx, %rdi
callq 0x2170
movq 0x18(%rsp), %r14
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x11927
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0x11a6d
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 0x118f5
movq %rbp, %rdi
callq 0x2170
addq $0x20408, %rsp          # imm = 0x20408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28698(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x1c9b1(%rip), %rsi     # 0x2e303
movq %rbx, %rdx
xorl %eax, %eax
addq $0x20408, %rsp          # imm = 0x20408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2250
leaq 0x7d60(%rsp), %rbx
movq %rbx, %rdi
callq 0x2613e
movq %rax, %r13
movq 0x28659(%rip), %r15     # 0x39fe0
movq (%r15), %rdi
leaq 0x1c9b1(%rip), %rsi     # 0x2e342
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2250
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x116a9
movq %r12, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
leaq 0x1a07e(%rip), %r14     # 0x2ba3e
leaq 0x2f40(%rsp), %r12
leaq 0x830(%rsp), %rbp
xorl %ebx, %ebx
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x11a6d
movq (%rax,%rbx,8), %rcx
movq %r12, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq (%r15), %rdi
leaq 0x1c95e(%rip), %rsi     # 0x2e368
movq %r12, %rdx
xorl %eax, %eax
callq 0x2250
movq %r12, %rdi
callq 0x26362
testb %al, %al
je 0x11a93
movq %r12, %rdi
leaq 0x1ae4e(%rip), %rsi     # 0x2c878
callq 0x26387
movq %rax, 0x830(%rsp)
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x12e0e
movq 0x8(%r13), %rax
subq (%r13), %rax
incq %rbx
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jl 0x119d2
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %r12
jmp 0x116a9
leaq 0x1be62(%rip), %rdi     # 0x2d8d6
movq %rbx, %rsi
jmp 0x11a8c
leaq 0x1be56(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
jmp 0x11a8c
leaq 0x1be4a(%rip), %rdi     # 0x2d8d6
xorl %eax, %eax
callq 0x2270
leaq 0x2f40(%rsp), %rdi
leaq 0x7d60(%rsp), %rsi
callq 0x399c