// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc08, %rsp            # imm = 0xC08
movq %rsi, %rbx
cmpl $0x2, %edi
jle 0x3f74
movl %edi, %ebp
movq 0x8(%rbx), %r14
leaq 0x6cf4(%rip), %rsi      # 0xa61f
movq %r14, %rdi
callq 0x2210
testl %eax, %eax
je 0x3cb8
leaq 0x6d6d(%rip), %rsi      # 0xa6af
movq %r14, %rdi
callq 0x5860
testb %al, %al
jne 0x3ea2
movq 0x8(%rbx), %rdi
leaq 0x6e3f(%rip), %rsi      # 0xa79c
callq 0x5860
testb %al, %al
je 0x3e8e
cmpl $0x9, %ebp
jne 0x3f81
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rbx), %rbp
movq 0x20(%rbx), %rax
movq %rax, 0x30(%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 0xb62b(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6eb8(%rip), %rsi      # 0xa877
movl %r12d, 0x8(%rsp)
movl %r12d, %edx
movl %r15d, %ecx
movsd %xmm0, 0x38(%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 0x301d
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r14, %r12
callq 0x30f4
leaq 0x6c62(%rip), %rsi      # 0xa692
movq %rbx, %rdi
callq 0x4b1e
movq %rax, 0x10(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x3ca9
leaq 0x430(%rsp), %r13
xorl %ebx, %ebx
movq %rbp, 0x28(%rsp)
movq %r12, 0x20(%rsp)
movq 0xb57b(%rip), %rcx      # 0xefe0
movq (%rcx), %rdi
movq (%rax,%rbx,8), %rdx
leaq 0x6e69(%rip), %rsi      # 0xa8dc
xorl %eax, %eax
callq 0x2230
movl $0x0, 0x40(%rsp)
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x3f63
movq (%rax,%rbx,8), %rcx
leaq 0x820(%rsp), %r14
movq %r14, %rdi
leaq 0x6be3(%rip), %rsi      # 0xa694
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x2060
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x384b
movq %rax, %r14
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x3f63
movq (%rax,%rbx,8), %rcx
movq %r13, %rdi
leaq 0x676d(%rip), %rsi      # 0xa261
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x4a5e
testb %al, %al
jne 0x3b4b
movq (%r12), %rax
movq 0x8(%r12), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x3f63
movq (%rax,%rbx,8), %rcx
movq %r13, %rdi
leaq 0x6db9(%rip), %rsi      # 0xa8ea
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x4a5e
testb %al, %al
je 0x3f89
movl $0x0, 0x4(%rsp)
movq %r13, %rdi
leaq 0x4(%rsp), %rsi
callq 0x31c6
movq %rax, %rbp
movq 0xb476(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movl 0x4(%rsp), %r12d
leaq 0x6da0(%rip), %rsi      # 0xa919
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
testl %r12d, %r12d
jle 0x3c6b
incq %r12
movl $0x1, %r13d
movsbq (%r14,%r13), %rax
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,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 %r8b
cmpl %esi, %r10d
setl %r9b
andb %r8b, %r9b
testb %dil, %r9b
cmovnel %esi, %r10d
cmovnel %edx, %eax
incq %rdx
cmpq $0x4, %rdx
jne 0x3baf
cmpl 0x8(%rsp), %r11d
jl 0x3c5f
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r11d, %xmm1
divsd %xmm1, %xmm0
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3c5f
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x3f63
movq (%rcx,%rbx,8), %rdx
movsbl (%r14,%r13), %r8d
cltq
leaq 0x6cf5(%rip), %rcx      # 0xa931
movsbl (%rax,%rcx), %r9d
movq 0x10(%rsp), %rdi
leaq 0x6cea(%rip), %rsi      # 0xa937
movl %r13d, %ecx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2230
addq $0x10, %rsp
incq %r13
cmpq %r12, %r13
jne 0x3b95
movq %rbp, %rdi
callq 0x38c4
movq %r14, %rdi
callq 0x21f0
incq %rbx
movq 0x20(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
movq 0x28(%rsp), %rbp
leaq 0x430(%rsp), %r13
ja 0x3a5e
movq 0x10(%rsp), %rdi
callq 0x2150
jmp 0x3e8e
cmpl $0x4, %ebp
jne 0x3f96
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r12
leaq 0x69a3(%rip), %rdi      # 0xa673
movq %r14, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x6355(%rip), %rsi      # 0xa036
movq %r14, %rdi
callq 0x4b1e
movq %rax, %r14
movl $0x77359400, %edi       # imm = 0x77359400
callq 0x2040
movq %rax, %rbx
movl $0x1, 0x4(%rsp)
leaq 0x697b(%rip), %rsi      # 0xa683
leaq 0x430(%rsp), %r15
movq %r15, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x6969(%rip), %rsi      # 0xa692
movq %r15, %rdi
callq 0x4b1e
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x4954
testq %rax, %rax
je 0x3e6b
movq %rax, %rbp
leaq 0x820(%rsp), %r13
cmpb $0x3e, (%rbp)
jne 0x3d76
movl 0x4(%rsp), %r15d
cmpl $0x2, %r15d
jge 0x3dc6
leaq 0x1(%rbp), %rsi
movq %r13, %rdi
callq 0x2130
movl $0x1, %ecx
jmp 0x3da5
movq %rbp, %rdi
callq 0x5260
movl 0x4(%rsp), %ecx
testl %eax, %eax
jle 0x3da5
movslq %ecx, %rdx
movl %eax, %esi
addq %rbx, %rdx
xorl %eax, %eax
movb (%rbp,%rax), %dil
movb %dil, (%rdx,%rax)
incq %rax
cmpq %rax, %rsi
jne 0x3d90
addl %ecx, %eax
movl %eax, %ecx
movl %ecx, 0x4(%rsp)
movq %rbp, %rdi
callq 0x21f0
movq %r14, %rdi
callq 0x4954
movq %rax, %rbp
testq %rax, %rax
jne 0x3d52
jmp 0x3e6b
movq %r13, %rdi
callq 0x308c
leaq 0x40(%rsp), %r12
movq %r12, %rdi
leaq 0x68b7(%rip), %rsi      # 0xa694
movq 0x18(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
movq 0x8(%rsp), %rdi
leaq 0x65b8(%rip), %rsi      # 0xa3b0
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movq %r12, %rdi
leaq 0x62c0(%rip), %rsi      # 0xa0cc
callq 0x4b1e
movq %rax, %r12
movq 0xb1c5(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6879(%rip), %rsi      # 0xa69e
movq %r13, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2230
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %r12, %rcx
callq 0x22e0
movslq 0x4(%rsp), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq %r12, %rcx
callq 0x22e0
movq %r12, %rdi
callq 0x2150
jmp 0x3d63
movl 0x4(%rsp), %ebp
cmpl $0x2, %ebp
jge 0x3eb5
movq %r14, %rdi
callq 0x2150
movq 0x8(%rsp), %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x21f0
xorl %eax, %eax
addq $0xc08, %rsp            # imm = 0xC08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
cmpl $0x7, %ebp
jne 0x3f7c
callq 0x2c88
jmp 0x3e8e
leaq 0x820(%rsp), %r13
movq %r13, %rdi
callq 0x308c
leaq 0x67c8(%rip), %rsi      # 0xa694
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
leaq 0x64c6(%rip), %rsi      # 0xa3b0
movq 0x8(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
leaq 0x61cc(%rip), %rsi      # 0xa0cc
movq %r15, %rdi
callq 0x4b1e
movq %rax, %r15
movq 0xb0ce(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6782(%rip), %rsi      # 0xa69e
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2230
leaq 0x4(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x22e0
movslq (%r12), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq %r15, %rcx
callq 0x22e0
movq %r15, %rdi
callq 0x2150
jmp 0x3e74
leaq 0x69fa(%rip), %rdi      # 0xa964
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2250
movq %rbx, %rdi
callq 0x2d59
callq 0x2c62
movq %rbx, %rdi
callq 0x2c19
leaq 0x430(%rsp), %rdi
callq 0x2c3f
movq %rbx, %rdi
callq 0x2d33