// File: 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:
subq $0x208b8, %rsp          # imm = 0x208B8
movb %cl, %al
movq %rdi, 0x208b0(%rsp)
movq %rsi, 0x208a8(%rsp)
movq %rdx, 0x208a0(%rsp)
andb $0x1, %al
movb %al, 0x2089f(%rsp)
movq $0x0, 0x20890(%rsp)
movq $0x0, 0x20888(%rsp)
movq 0x208b0(%rsp), %rdi
leaq 0x37b2c(%rip), %rsi     # 0x556bd
callq 0x2240
cmpl $0x0, %eax
jne 0x1dbaf
movq 0x47436(%rip), %rax     # 0x64fd8
movq (%rax), %rax
movq %rax, 0x20890(%rsp)
jmp 0x1dbcb
movq 0x208b0(%rsp), %rdi
leaq 0x37510(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x20890(%rsp)
cmpq $0x0, 0x20890(%rsp)
jne 0x1dc06
cmpq $0x0, 0x20888(%rsp)
jne 0x1dc06
movq 0x473f8(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x208b0(%rsp), %rdx
leaq 0x37ac9(%rip), %rsi     # 0x556c3
movb $0x0, %al
callq 0x2250
jmp 0x1e1d4
movl $0x0, 0x20884(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x68(%rsp), %rdi
callq 0xc280
movq 0x68(%rsp), %rax
movq %rax, 0x20878(%rsp)
leaq 0x81d0(%rsp), %rdi
movq 0x208a8(%rsp), %rdx
leaq 0x34d7d(%rip), %rsi     # 0x529da
movb $0x0, %al
callq 0x2090
movq $0x0, 0x81c8(%rsp)
leaq 0x81d0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x1dc86
jmp 0x1ddf8
leaq 0x81d0(%rsp), %rdi
callq 0x452c0
movq %rax, 0x81c8(%rsp)
movq 0x4733e(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x81d0(%rsp), %rdx
leaq 0x37a4e(%rip), %rsi     # 0x55702
movb $0x0, %al
callq 0x2250
movl $0x0, 0x81c4(%rsp)
movl 0x81c4(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x81c8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x1ddf6
leaq 0x7dd0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x208a8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x81c8(%rsp), %rdi
movslq 0x81c4(%rsp), %rsi
callq 0xc240
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rdx
movq (%rax), %rcx
leaq 0x34d04(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
movq 0x472a2(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x7dd0(%rsp), %rdx
leaq 0x379d8(%rip), %rsi     # 0x55728
movb $0x0, %al
callq 0x2250
leaq 0x7dd0(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x1dda3
movq 0x47271(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x7dd0(%rsp), %rdx
leaq 0x379c0(%rip), %rsi     # 0x55741
movb $0x0, %al
callq 0x2250
leaq 0x81d0(%rsp), %rdi
leaq 0x34d0d(%rip), %rsi     # 0x52aa4
callq 0x455f0
xorl %edi, %edi
callq 0x21f0
movq 0x20878(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x7dd0(%rsp), %rdi
leaq 0x35b0e(%rip), %rsi     # 0x538cd
callq 0x455f0
movq 0x48(%rsp), %rdi
movq %rax, 0x7dc8(%rsp)
leaq 0x7dc8(%rsp), %rsi
callq 0x1fcf0
jmp 0x1dde0
movl 0x81c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x81c4(%rsp)
jmp 0x1dcc6
jmp 0x1de2d
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x40(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x40(%rsp), %rdi
callq 0xc630
movq 0x40(%rsp), %rax
movq %rax, 0x81c8(%rsp)
jmp 0x1de2f
movq $0x0, 0x7dc0(%rsp)
cmpq $0x0, 0x20890(%rsp)
je 0x1de5b
movq 0x20890(%rsp), %rdi
callq 0x45400
movq %rax, 0x7dc0(%rsp)
cmpq $0x0, 0x7dc0(%rsp)
jne 0x1de6b
jmp 0x1e0c7
movq 0x208a0(%rsp), %rax
movq 0x7dc0(%rsp), %rdi
leaq 0x75e0(%rsp), %rsi
leaq 0x79d0(%rsp), %rdx
leaq 0x27b0(%rsp), %r10
leaq 0x75dc(%rsp), %rcx
leaq 0x75d8(%rsp), %r8
leaq 0x75d7(%rsp), %r9
movq %r10, (%rsp)
callq *%rax
cmpl $0x1, 0x75dc(%rsp)
jl 0x1e0a5
movsbl 0x79d0(%rsp), %eax
cmpl $0x0, %eax
je 0x1e0a5
leaq 0x79d0(%rsp), %rdi
callq 0x22350
movq 0x81c8(%rsp), %rdi
leaq 0x79d0(%rsp), %rsi
callq 0x47cf0
movl %eax, 0x27ac(%rsp)
movl 0x27ac(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x81c8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jne 0x1dff1
movq 0x81c8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x79d0(%rsp), %rdi
callq 0x47b60
movq 0x28(%rsp), %rdi
movq %rax, 0x27a0(%rsp)
leaq 0x27a0(%rsp), %rsi
callq 0xc640
movq 0x81c8(%rsp), %rdi
leaq 0x79d0(%rsp), %rsi
callq 0x47cf0
movl %eax, 0x27ac(%rsp)
leaq 0x90(%rsp), %rdi
movq 0x208a8(%rsp), %rdx
leaq 0x79d0(%rsp), %rcx
leaq 0x34aa5(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
movq 0x20878(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x34af2(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq 0x30(%rsp), %rdi
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rsi
callq 0x1fcf0
movq 0x47008(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x79d0(%rsp), %rdx
leaq 0x3776a(%rip), %rsi     # 0x55754
movb $0x0, %al
callq 0x2250
movq 0x20878(%rsp), %rdi
movslq 0x27ac(%rsp), %rsi
callq 0xc300
movq (%rax), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x1e021
jmp 0x1e0a3
testb $0x1, 0x2089f(%rsp)
je 0x1e064
movq 0x80(%rsp), %rdi
leaq 0x75e0(%rsp), %rdx
leaq 0x27b0(%rsp), %rcx
movsbl 0x75d7(%rsp), %r8d
movl 0x75dc(%rsp), %r9d
leaq 0x37703(%rip), %rsi     # 0x5575e
movb $0x0, %al
callq 0x2250
jmp 0x1e092
movq 0x80(%rsp), %rdi
leaq 0x27b0(%rsp), %rdx
movsbl 0x75d7(%rsp), %ecx
movl 0x75dc(%rsp), %r8d
leaq 0x376d6(%rip), %rsi     # 0x55761
movb $0x0, %al
callq 0x2250
movl 0x20884(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20884(%rsp)
jmp 0x1e0a5
movq 0x7dc0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x1e0c2
movq 0x20(%rsp), %rdi
callq 0x2220
jmp 0x1de2f
leaq 0x81d0(%rsp), %rdi
leaq 0x349ce(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x81c8(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1e147
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x81c8(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc240
movq 0x10(%rsp), %rdi
movq (%rax), %rdx
leaq 0x37c4a(%rip), %rsi     # 0x55d7d
movb $0x0, %al
callq 0x2250
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x1e0e8
movq 0x78(%rsp), %rdi
callq 0x2180
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x20878(%rsp), %rdi
callq 0xc450
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x1e1a0
movq 0x20878(%rsp), %rdi
movslq 0x70(%rsp), %rsi
callq 0xc300
movq (%rax), %rdi
callq 0x2180
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x1e159
cmpq $0x0, 0x20890(%rsp)
je 0x1e1ba
movq 0x20890(%rsp), %rdi
callq 0x2180
jmp 0x1e1d4
cmpq $0x0, 0x20888(%rsp)
je 0x1e1d2
movq 0x20888(%rsp), %rdi
callq 0x451b0
jmp 0x1e1d4
addq $0x208b8, %rsp          # imm = 0x208B8
retq
nopl (%rax)