// File: /workspace/github_cpp_makefile_star2_O2/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 0x197ff(%rip), %rsi     # 0x272fd
callq 0x2230
testl %eax, %eax
je 0xdb1b
leaq 0x191fa(%rip), %rsi     # 0x26d08
movq %rbx, %rdi
callq 0x2027c
movq %rax, %rbp
jmp 0xdb25
movq 0x274b6(%rip), %rax     # 0x34fd8
movq (%rax), %rbp
testq %rbp, %rbp
je 0xddd0
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x18(%rsp)
andq $0x0, 0x10(%rax)
leaq 0x16e91(%rip), %rsi     # 0x249de
leaq 0x7d60(%rsp), %rbx
movq %rbx, %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rdx
xorl %eax, %eax
callq 0x2080
movq %rbx, %rdi
callq 0x20257
testb %al, %al
jne 0xddfc
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x2f40(%rsp), %rbx
leaq 0x440(%rsp), %r14
movq %rbp, %rdi
callq 0x2017f
testq %rax, %rax
je 0xdd29
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 0xdc80
cmpb $0x0, 0x50(%rsp)
je 0xdc80
movq %rbx, %r14
movq %r12, %rbx
movq %rbp, %r12
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0xf63c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x21152
movl %eax, %ebp
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cmpl %eax, %ebp
je 0xdc8d
movslq %ebp, %rsi
movq 0x18(%rsp), %rdi
callq 0x7386
movq (%rax), %rdi
testq %rdi, %rdi
movq %r12, %rbp
movq %rbx, %r12
movq %r14, %rbx
leaq 0x440(%rsp), %r14
je 0xdc80
movsbl 0x13(%rsp), %r8d
movl 0x14(%rsp), %r9d
cmpb $0x0, 0x24(%rsp)
je 0xdc69
leaq 0x19744(%rip), %rsi     # 0x2739e
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2240
jmp 0xdc80
leaq 0x19731(%rip), %rsi     # 0x273a1
movq %rbx, %rdx
movl %r8d, %ecx
movl %r9d, %r8d
xorl %eax, %eax
callq 0x2240
movq %r15, %rdi
callq 0x2210
jmp 0xdb9d
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x21062
movq %rax, 0x830(%rsp)
movq %r13, %rdi
leaq 0x830(%rsp), %rsi
callq 0x7d0a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x21152
movl %eax, %ebp
leaq 0x830(%rsp), %rdi
leaq 0x16d6a(%rip), %rsi     # 0x24a38
movq 0x28(%rsp), %rdx
leaq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x2080
leaq 0x830(%rsp), %rdi
leaq 0x16dba(%rip), %rsi     # 0x24aa8
callq 0x2027c
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xf520
movq 0x272d2(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x1967c(%rip), %rsi     # 0x27394
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
jmp 0xdc1b
leaq 0x16d78(%rip), %rsi     # 0x24aa8
leaq 0x7d60(%rsp), %rdi
callq 0x2027c
movq %rax, %rbx
leaq 0x19c76(%rip), %r15     # 0x279bd
xorl %r12d, %r12d
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0xdd7d
movq %r13, %rdi
movq %r12, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2240
incq %r12
jmp 0xdd4a
movq %rbx, %rdi
callq 0x2160
xorl %ebx, %ebx
movq 0x18(%rsp), %r14
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0xddb6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7386
movq (%rax), %rdi
callq 0x2160
incq %rbx
jmp 0xdd8c
movq %rbp, %rdi
callq 0x2160
addq $0x20408, %rsp          # imm = 0x20408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x27209(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0x19522(%rip), %rsi     # 0x27303
movq %rbx, %rdx
xorl %eax, %eax
addq $0x20408, %rsp          # imm = 0x20408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
movq %r12, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
leaq 0x7d60(%rsp), %rbx
movq %rbx, %rdi
callq 0x20089
movq %rax, %r13
movq 0x271c0(%rip), %rbp     # 0x34fe0
movq (%rbp), %rdi
leaq 0x19517(%rip), %rsi     # 0x27342
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2240
leaq 0x16bfc(%rip), %r14     # 0x24a38
leaq 0x2f40(%rsp), %r15
leaq 0x830(%rsp), %rbx
xorl %r12d, %r12d
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0xdecc
movq %r13, %rdi
movq %r12, %rsi
callq 0x7342
movq (%rax), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x2080
movq (%rbp), %rdi
leaq 0x194db(%rip), %rsi     # 0x27368
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
movq %r15, %rdi
callq 0x20257
testb %al, %al
je 0xdedb
movq %r15, %rdi
leaq 0x179c5(%rip), %rsi     # 0x25872
callq 0x2027c
movq %rax, 0x830(%rsp)
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0xf520
incq %r12
jmp 0xde4f
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %r12
jmp 0xdb8d
movq (%rbp), %rdi
leaq 0x1949b(%rip), %rsi     # 0x27381
leaq 0x2f40(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
leaq 0x16bac(%rip), %rsi     # 0x24aa8
leaq 0x7d60(%rsp), %rdi
callq 0x2027c
xorl %edi, %edi
callq 0x21d0