// File: /workspace/github_cpp_makefile_star2_O3/akdess[P]BAFExtract/src/main.cpp
// Function: main
int main(int argc, char* argv[])
{
	if (argc < 3)
	{
		fprintf(stderr, "USAGE: %s [Options] [Arguments]\n\
Options:\n\
	-preprocess_FASTA [FASTA file path] [Output firectory]\n\
	-generate_compressed_pileup_per_SAM [SAM file path (\"stdin\" for piping from samtools)] [Chromosome Ids/lengths file path] [Output directory] [Minimum mapping quality] [Minimum base quality]\n\
	-get_SNVs_per_pileup [chromosome info file path] [Pileup directory] [Binary sequences directory] [Min coverage per SNV (20)] [Min MAF covg per SNV (4)] [Min MAF (0.2)] [Output file path]\n", argv[0]);
		exit(0);
	}

	if (strcmp(argv[1], "-preprocess_FASTA") == 0)
	{
		if (argc != 4)
		{
			fprintf(stderr, "USAGE: %s -preprocess_FASTA [FASTA file path] [Output firectory]\n", argv[0]);
			exit(0);
		}

		char* fasta_fp = argv[2];
		char* bin_dir = argv[3];

		printf("Binarizing %s.\n", fasta_fp);
		FILE* f_fasta = open_f(fasta_fp, "r");

		char* cur_line = NULL;
		int buff_len = 2000 * 1000 * 1000;
		char* cur_entry_buffer = new char[buff_len];
		int cur_entry_i = 1;
		char cur_entry_id[1000];

		char chr_ids_fp[1000];
		sprintf(chr_ids_fp, "%s/chr_ids.txt", bin_dir);
		FILE* f_chr_ids = open_f(chr_ids_fp, "w");

		while (1)
		{
			// Process the current buffer.
			cur_line = getline(f_fasta);

			if (cur_line == NULL)
			{
				// File ended, dump the last entry if there are values in it.
				if (cur_entry_i > 1)
				{
					char cur_entry_bin_fp[1000];
					normalize_chr_id(cur_entry_id);
					sprintf(cur_entry_bin_fp, "%s/%s.bin", bin_dir, cur_entry_id);

					fprintf(f_chr_ids, "%s\n", cur_entry_id);

					FILE* f_bin = open_f(cur_entry_bin_fp, "wb");

					// Open the new binary file.
					fprintf(stderr, "Dumping %s (%d)\n", cur_entry_id, cur_entry_i);

					// Dump the sequence buffer.
					fwrite(&cur_entry_i, sizeof(int), 1, f_bin);
					fwrite(cur_entry_buffer, sizeof(char), cur_entry_i, f_bin);

					// Close current file.
					fclose(f_bin);
				}
				break;
			}
			else if (cur_line[0] == '>')
			{
				if (cur_entry_i > 1)
				{
					char cur_entry_bin_fp[1000];
					normalize_chr_id(cur_entry_id);
					sprintf(cur_entry_bin_fp, "%s/%s.bin", bin_dir, cur_entry_id);

					fprintf(f_chr_ids, "%s\n", cur_entry_id);

					FILE* f_bin = open_f(cur_entry_bin_fp, "wb");

					// Open the new binary file.
					fprintf(stderr, "Dumping %s (%d)\n", cur_entry_id, cur_entry_i);

					// Dump the sequence buffer.
					fwrite(&cur_entry_i, sizeof(int), 1, f_bin);
					fwrite(cur_entry_buffer, sizeof(char), cur_entry_i, f_bin);

					// Close current file.
					fclose(f_bin);
				}

				// Update the id, reset the counter.
				strcpy(cur_entry_id, &(cur_line[1]));
				cur_entry_i = 1;
			}
			else
			{
				// Concatenate the current sequence line.
				int l_cur_line = t_string::string_length(cur_line);
				for (int i = 0; i < l_cur_line; i++)
				{
					cur_entry_buffer[cur_entry_i] = cur_line[i];
					cur_entry_i++;
				} // i loop.
			}

			delete[] cur_line;
		} // file reading loop.

		fclose(f_fasta);
		fclose(f_chr_ids);
		delete[] cur_entry_buffer;
	} // -preprocess_FASTA option.	
	else if (t_string::compare_strings(argv[1], "-generate_compressed_pileup_per_SAM"))
	{
		if (argc != 7)
		{
			fprintf(stderr, "USAGE: %s -generate_compressed_pileup_per_SAM [SAM file path (\"stdin\" for piping from samtools)] [Chromosome Ids/lengths file path] [Output directory] [Minimum mapping quality] [Minimum base quality]\n", argv[0]);
			exit(0);
		}

		char* sam_fp = argv[2];
		char* chr_ids_lengths_list_fp = argv[3];
		char* op_dir = argv[4];
		int min_mapp_qual = atoi(argv[5]);
		int min_base_qual = atoi(argv[6]);

		vector<char*>* chr_ids = new vector<char*>();
		vector<int>* chr_lengths = new vector<int>();
		load_chromosome_lengths_per_tabbed_file(chr_ids_lengths_list_fp, chr_ids, chr_lengths);

		int n_processed_reads = 0;
		dump_nucleotide_pileup_per_SAM_file(sam_fp, chr_ids, chr_lengths, op_dir, min_mapp_qual, min_base_qual, n_processed_reads);
	} // -generate_compressed_pileup_per_SAM	
	else if (t_string::compare_strings(argv[1], "-get_SNVs_per_pileup"))
	{
		if (argc != 9)
		{
			fprintf(stderr, "USAGE: %s -get_SNVs_per_pileup [chromosome info file path] [Pileup directory] [Binary sequences directory] [Min coverage per SNV (20)] [Min MAF covg per SNV (4)] [Min MAF (0.2)] [Output file path]\n", argv[0]);
			exit(0);
		}

		char* chr_info_fp = argv[2];
		char* pileup_dir = argv[3];
		char* bin_seq_dir = argv[4];
		int min_covg = atoi(argv[5]);
		int min_alternate_covg = atoi(argv[6]);
		double min_alternate_freq = atof(argv[7]);
		char* op_fp = argv[8];

		fprintf(stderr, "Calling SNVs with minimum %d total and %d alternate read coverage and %.3f min alternate frequency.\n", min_covg, min_alternate_covg, min_alternate_freq);

		vector<char*>* chr_ids = new vector<char*>();
		vector<int>* chr_lengths = new vector<int>();

		int* nuc_2_num_array = get_per_char_as_nuc_2_num_coding_array();
		load_chromosome_lengths_per_tabbed_file(chr_info_fp, chr_ids, chr_lengths);

		FILE* f_op = open_f(op_fp, "w");
		for (int i_chr = 0; i_chr < chr_ids->size(); i_chr++)
		{
			fprintf(stderr, "Procesing %s\n", chr_ids->at(i_chr));

			int l_cur_chr_seq = 0;
			char cur_chr_bin_fp[1000];
			sprintf(cur_chr_bin_fp, "%s/%s.bin", bin_seq_dir, chr_ids->at(i_chr));
			char* cur_chr_seq = load_binary_sequence_file(cur_chr_bin_fp, l_cur_chr_seq);

			char cur_chr_pileup_fp[1000];
			sprintf(cur_chr_pileup_fp, "%s/%s_allele_counts.bin", pileup_dir, chr_ids->at(i_chr));
			if (!check_file(cur_chr_pileup_fp))
			{
				sprintf(cur_chr_pileup_fp, "%s/%s_allele_counts.bin.gz", pileup_dir, chr_ids->at(i_chr));
				if (!check_file(cur_chr_pileup_fp))
				{
					fprintf(stderr, "Could not find %s.\n", cur_chr_pileup_fp);
					exit(0);
				}
			}

			int l_pileup = 0;
			unsigned short** cur_chr_pileup = load_compressed_pileups(cur_chr_pileup_fp, l_pileup);
			fprintf(stderr, "Loaded %d long pileup.\n", l_pileup);

			char per_num_nucs[] = "ACGTN";

			for (int i = 1; i <= l_pileup; i++)
			{
				int cur_total_covg = 0;
				int max_alternate_cnt = 0;
				int max_alt_allele_i = -1;
				for (int i_all = 0; i_all < 4; i_all++)
				{
					cur_total_covg += cur_chr_pileup[i_all][i];

					bool is_alternate_allele = (nuc_2_num_array[cur_chr_seq[i]] != i_all);
					if (is_alternate_allele &&
						cur_chr_pileup[i_all][i] > min_alternate_covg &&
						max_alternate_cnt < cur_chr_pileup[i_all][i])
					{
						max_alternate_cnt = cur_chr_pileup[i_all][i];
						max_alt_allele_i = i_all;
					}
				} // i_all loop.

				if (cur_total_covg < min_covg)
				{
					continue;
				}

				double cur_alternate_freq = (double)(max_alternate_cnt) / (double)(cur_total_covg);
				if (cur_alternate_freq < min_alternate_freq)
				{
					continue;
				}

				fprintf(f_op, "%s\t%d\t%c\t%c\t%d\t%d\n", chr_ids->at(i_chr), i,
					cur_chr_seq[i], per_num_nucs[max_alt_allele_i],
					max_alternate_cnt, cur_total_covg);
			} // i loop.

			  // Free memory.
			delete_pileup(cur_chr_pileup);

			// Free sequence memory.
			delete[] cur_chr_seq;
		} // i_chr loop.

		fclose(f_op);
	} // -get_SNVs_per_pileup option.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc18, %rsp            # imm = 0xC18
movq %rsi, %rbx
cmpl $0x2, %edi
jle 0x3f9f
movl %edi, %ebp
movq 0x8(%rbx), %r14
leaq 0x6cc9(%rip), %rsi      # 0xa61f
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x3cf1
leaq 0x6d42(%rip), %rsi      # 0xa6af
movq %r14, %rdi
callq 0x589c
testb %al, %al
jne 0x3ece
movq 0x8(%rbx), %rdi
leaq 0x6e14(%rip), %rsi      # 0xa79c
callq 0x589c
testb %al, %al
je 0x3eba
cmpl $0x9, %ebp
jne 0x3fac
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rbx), %rbp
movq 0x20(%rbx), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rbx), %rdi
callq 0x2270
movl %eax, %r12d
movq 0x30(%rbx), %rdi
callq 0x2270
movl %eax, %r15d
movq 0x38(%rbx), %rdi
callq 0x21d0
movq 0x40(%rbx), %rbx
movq 0xb600(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6e8d(%rip), %rsi      # 0xa877
movl %r12d, 0x18(%rsp)
movl %r12d, %edx
movl %r15d, %ecx
movsd %xmm0, 0x48(%rsp)
movb $0x1, %al
callq 0x2230
movl $0x18, %edi
callq 0x2160
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %r13d, %r13d
movq %r13, 0x10(%rax)
movl $0x18, %edi
callq 0x2160
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %r13, 0x10(%rax)
callq 0x305d
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r14, %r12
callq 0x3134
leaq 0x6c37(%rip), %rsi      # 0xa692
movq %rbx, %rdi
callq 0x4b65
movq %rax, 0x28(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x3ce2
leaq 0x50(%rsp), %r13
xorl %edx, %edx
movq %rbp, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %rdx, 0x10(%rsp)
movq 0xb54e(%rip), %rcx      # 0xefe0
movq (%rcx), %rdi
movq (%rax,%rdx,8), %rdx
leaq 0x6e3c(%rip), %rsi      # 0xa8dc
xorl %eax, %eax
callq 0x2230
movq 0x10(%rsp), %rsi
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x3f91
movq (%rax,%rsi,8), %rcx
leaq 0x830(%rsp), %rbx
movq %rbx, %rdi
leaq 0x6bb9(%rip), %rsi      # 0xa694
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
leaq 0x440(%rsp), %rsi
callq 0x3883
movq 0x10(%rsp), %rsi
movq %rax, %r14
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x3f91
movq (%rax,%rsi,8), %rcx
movq %r13, %rdi
leaq 0x673b(%rip), %rsi      # 0xa261
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x4aa5
testb %al, %al
jne 0x3b82
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x10(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x3f91
movq (%rax,%rsi,8), %rcx
movq %r13, %rdi
leaq 0x6d82(%rip), %rsi      # 0xa8ea
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x4aa5
testb %al, %al
je 0x3fb4
movl $0x0, 0xc(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3215
movq %rax, %rbp
movq 0xb43f(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movl 0xc(%rsp), %r12d
leaq 0x6d69(%rip), %rsi      # 0xa919
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
testl %r12d, %r12d
jle 0x3ca2
incq %r12
movl $0x1, %r13d
movsbq (%r14,%r13), %r8
movq 0x20(%rsp), %rax
movl (%rax,%r8,4), %ecx
xorl %edx, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
xorl %r11d, %r11d
movq (%rbp,%rdx,8), %rsi
movzwl (%rsi,%r13,2), %esi
addl %esi, %r11d
cmpq %rdx, %rcx
setne %dil
cmpl %esi, %r15d
setl %r9b
cmpl %esi, %r10d
setl %bl
andb %r9b, %bl
testb %dil, %bl
cmovnel %esi, %r10d
cmovnel %edx, %eax
incq %rdx
cmpq $0x4, %rdx
jne 0x3be7
cmpl 0x18(%rsp), %r11d
jl 0x3c96
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r11d, %xmm1
divsd %xmm1, %xmm0
movsd 0x48(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3c96
movq 0x30(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq 0x10(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x3f91
movq (%rcx,%rsi,8), %rdx
cltq
leaq 0x6cbe(%rip), %rcx      # 0xa931
movsbl (%rax,%rcx), %r9d
movq 0x28(%rsp), %rdi
leaq 0x6cb3(%rip), %rsi      # 0xa937
movl %r13d, %ecx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2230
addq $0x10, %rsp
incq %r13
cmpq %r12, %r13
jne 0x3bcc
movq %rbp, %rdi
callq 0x38fc
movq %r14, %rdi
callq 0x21f0
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x30(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rdx, %rcx
movq 0x38(%rsp), %rbp
leaq 0x50(%rsp), %r13
ja 0x3a86
movq 0x28(%rsp), %rdi
callq 0x2150
jmp 0x3eba
cmpl $0x4, %ebp
jne 0x3fbe
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r12
leaq 0x696a(%rip), %rdi      # 0xa673
movq %r14, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x631c(%rip), %rsi      # 0xa036
movq %r14, %rdi
callq 0x4b65
movq %rax, %r14
movl $0x77359400, %edi       # imm = 0x77359400
callq 0x2040
movq %rax, %rbx
movl $0x1, 0xc(%rsp)
leaq 0x6942(%rip), %rsi      # 0xa683
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r12, 0x20(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x6933(%rip), %rsi      # 0xa692
movq %r15, %rdi
callq 0x4b65
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x498e
testq %rax, %rax
je 0x3ea0
movq %rax, %r15
movl $0x1, %ebp
leaq 0x830(%rsp), %r13
cmpb $0x3e, (%r15)
jne 0x3dab
cmpl $0x2, %ebp
jge 0x3df4
leaq 0x1(%r15), %rsi
movq %r13, %rdi
callq 0x2130
movl $0x1, %ebp
jmp 0x3dd3
movq %r15, %rdi
callq 0x5286
testl %eax, %eax
jle 0x3dd3
movslq %ebp, %rcx
movl %eax, %eax
addq %rbx, %rcx
xorl %edx, %edx
movb (%r15,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x3dc1
addl %edx, %ebp
movl %ebp, 0xc(%rsp)
movq %r15, %rdi
callq 0x21f0
movq %r14, %rdi
callq 0x498e
movq %rax, %r15
testq %rax, %rax
jne 0x3d8d
jmp 0x3e9b
movq %r13, %rdi
callq 0x30cc
leaq 0x440(%rsp), %r12
movq %r12, %rdi
leaq 0x6886(%rip), %rsi      # 0xa694
movq 0x20(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
movq 0x18(%rsp), %rdi
leaq 0x6587(%rip), %rsi      # 0xa3b0
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movq %r12, %rdi
leaq 0x628f(%rip), %rsi      # 0xa0cc
callq 0x4b65
movq %rax, %r12
movq 0xb194(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6848(%rip), %rsi      # 0xa69e
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2230
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %r12, %rcx
callq 0x22e0
movslq 0xc(%rsp), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rcx
callq 0x22e0
movq %r12, %rdi
callq 0x2150
jmp 0x3d98
cmpl $0x2, %ebp
jge 0x3ee1
movq %r14, %rdi
callq 0x2150
movq 0x18(%rsp), %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x21f0
xorl %eax, %eax
addq $0xc18, %rsp            # imm = 0xC18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
cmpl $0x7, %ebp
jne 0x3fa7
callq 0x2cbe
jmp 0x3eba
leaq 0x830(%rsp), %r15
movq %r15, %rdi
callq 0x30cc
leaq 0x679c(%rip), %rsi      # 0xa694
leaq 0x440(%rsp), %r13
movq %r13, %rdi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2060
leaq 0x6497(%rip), %rsi      # 0xa3b0
movq 0x18(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2230
leaq 0x619d(%rip), %rsi      # 0xa0cc
movq %r13, %rdi
callq 0x4b65
movq %rax, %r12
movq 0xb09f(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6753(%rip), %rsi      # 0xa69e
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2230
leaq 0xc(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x22e0
movslq (%r15), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rcx
callq 0x22e0
movq %r12, %rdi
callq 0x2150
jmp 0x3ea0
leaq 0x69cc(%rip), %rdi      # 0xa964
xorl %eax, %eax
callq 0x2250
movq %rbx, %rdi
callq 0x2d8f
callq 0x2c98
movq %rbx, %rdi
callq 0x2c4f
leaq 0x50(%rsp), %rdi
callq 0x2c75
movq %rbx, %rdi
callq 0x2d69