// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc28, %rsp            # imm = 0xC28
movq %rsi, %rbx
cmpl $0x2, %edi
jle 0x3b25
movl %edi, %ebp
movq 0x8(%rbx), %r14
leaq 0x6172(%rip), %rsi      # 0x960f
movq %r14, %rdi
callq 0x2220
testl %eax, %eax
je 0x3566
leaq 0x61eb(%rip), %rsi      # 0x969f
movq %r14, %rdi
callq 0x522c
testb %al, %al
je 0x3721
cmpl $0x7, %ebp
jne 0x3b51
movq 0x10(%rbx), %r15
movq 0x18(%rbx), %r12
movq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rbx), %rdi
callq 0x2280
movl %eax, %ebp
movq 0x30(%rbx), %rdi
callq 0x2280
movl %eax, %ebx
pushq $0x18
popq %r13
movq %r13, %rdi
callq 0x2170
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r13, %rdi
callq 0x2170
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x252a
leaq 0x450(%rsp), %rax
andl $0x0, (%rax)
subq $0x8, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movl %ebp, %r8d
movl %ebx, %r9d
pushq %rax
callq 0x2c3a
popq %rax
popq %rcx
jmp 0x3739
cmpl $0x4, %ebp
jne 0x3b3b
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r12
leaq 0x60e5(%rip), %rdi      # 0x9663
movq %r14, %rsi
xorl %eax, %eax
callq 0x2050
leaq 0x5a97(%rip), %rsi      # 0x9026
movq %r14, %rdi
callq 0x471a
movq %rax, %r14
movl $0x77359400, %edi       # imm = 0x77359400
callq 0x2040
movq %rax, %rbx
leaq 0xc(%rsp), %rax
movl $0x1, (%rax)
leaq 0x60ba(%rip), %rsi      # 0x9673
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r12, 0x20(%rsp)
movq %r12, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x60ab(%rip), %rsi      # 0x9682
movq %r15, %rdi
callq 0x471a
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rbp
leaq 0x450(%rsp), %r13
movq %r14, %rdi
callq 0x459e
testq %rax, %rax
je 0x3700
movq %rax, %r15
cmpb $0x3e, (%rax)
jne 0x3620
cmpl $0x2, %ebp
jge 0x365c
movq %r15, %rsi
incq %rsi
movq %r13, %rdi
callq 0x2140
pushq $0x1
popq %rbp
jmp 0x364e
movq %r15, %rdi
callq 0x4eb4
movslq %ebp, %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
addq %rbx, %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x364c
movb (%r15,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
jmp 0x363a
addl %edx, %ebp
movl %ebp, 0xc(%rsp)
movq %r15, %rdi
callq 0x2200
jmp 0x35ef
movq %r13, %rdi
callq 0x24c2
leaq 0x840(%rsp), %r12
movq %r12, %rdi
leaq 0x600e(%rip), %rsi      # 0x9684
movq 0x20(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
movq 0x10(%rsp), %rdi
leaq 0x5d0f(%rip), %rsi      # 0x93a0
movq %r13, %rdx
xorl %eax, %eax
callq 0x2240
movq %r12, %rdi
leaq 0x5a17(%rip), %rsi      # 0x90bc
callq 0x471a
movq %rax, %r12
movq 0xb92c(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x5fd0(%rip), %rsi      # 0x968e
movq %r13, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq %r12, %rcx
callq 0x22f0
movslq 0xc(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rcx
callq 0x22f0
movq %r12, %rdi
callq 0x2160
jmp 0x360d
cmpl $0x2, %ebp
jge 0x374d
movq %r14, %rdi
callq 0x2160
movq 0x10(%rsp), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x2200
jmp 0x3739
movq 0x8(%rbx), %rdi
leaq 0x6060(%rip), %rsi      # 0x978c
callq 0x522c
testb %al, %al
jne 0x37fb
xorl %eax, %eax
addq $0xc28, %rsp            # imm = 0xC28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x450(%rsp), %r15
movq %r15, %rdi
callq 0x24c2
leaq 0x5f20(%rip), %rsi      # 0x9684
leaq 0x840(%rsp), %r13
movq %r13, %rdi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2060
leaq 0x5c1b(%rip), %rsi      # 0x93a0
movq 0x10(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
leaq 0x5921(%rip), %rsi      # 0x90bc
movq %r13, %rdi
callq 0x471a
movq %rax, %r12
movq 0xb833(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x5ed7(%rip), %rsi      # 0x968e
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
leaq 0xc(%rsp), %r15
pushq $0x4
popq %rsi
pushq $0x1
popq %r13
movq %r15, %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x22f0
movslq (%r15), %rdx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x22f0
movq %r12, %rdi
callq 0x2160
jmp 0x3705
cmpl $0x9, %ebp
jne 0x3b67
movq 0x10(%rbx), %r14
movq 0x18(%rbx), %r13
movq 0x20(%rbx), %rax
movq %rax, 0x50(%rsp)
movq 0x28(%rbx), %rdi
callq 0x2280
movl %eax, %ebp
movq 0x30(%rbx), %rdi
callq 0x2280
movl %eax, %r15d
movq 0x38(%rbx), %rdi
callq 0x21e0
movq 0x40(%rbx), %rbx
movq 0xb7a0(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x601d(%rip), %rsi      # 0x9867
movl %ebp, 0x1c(%rsp)
movl %ebp, %edx
movl %r15d, %ecx
movsd %xmm0, 0x58(%rsp)
movb $0x1, %al
callq 0x2240
pushq $0x18
popq %r12
movq %r12, %rdi
callq 0x2170
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r12, %rdi
callq 0x2170
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
callq 0x245a
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x252a
leaq 0x5dcf(%rip), %rsi      # 0x9682
movq %rbx, %rdi
callq 0x471a
movq %rax, 0x30(%rsp)
leaq 0x60(%rsp), %r12
xorl %ecx, %ecx
movq %r13, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
movq 0x8(%rbp), %rax
subq (%rbp), %rax
sarq $0x3, %rax
cmpq %rcx, %rax
jbe 0x3b16
movq 0xb6f3(%rip), %r14      # 0xefe0
movq (%r14), %rbx
movq %rbp, %rdi
movq %rcx, %r14
movq %rcx, %rsi
callq 0x3be2
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x5fc1(%rip), %rsi      # 0x98cc
xorl %eax, %eax
callq 0x2240
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3be2
movq (%rax), %rcx
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
leaq 0x5d52(%rip), %rsi      # 0x9684
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
leaq 0x840(%rsp), %rsi
callq 0x33bf
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
movq %r14, 0x28(%rsp)
movq %r14, %rsi
callq 0x3be2
movq (%rax), %rcx
movq %r12, %rdi
leaq 0x58e1(%rip), %rsi      # 0x9251
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq %r12, %rdi
callq 0x4676
testb %al, %al
jne 0x39ba
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x3be2
movq (%rax), %rcx
movq %r12, %rdi
leaq 0x5f3a(%rip), %rsi      # 0x98da
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq %r12, %rdi
callq 0x4676
testb %al, %al
je 0x3b7d
andl $0x0, 0xc(%rsp)
movq %r12, %rdi
leaq 0xc(%rsp), %rsi
callq 0x25f6
movq %rax, %r12
movq 0xb60a(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movl 0xc(%rsp), %ebx
leaq 0x5f25(%rip), %rsi      # 0x9909
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
incl %ebx
movq %rbx, 0x20(%rsp)
pushq $0x1
popq %rbp
movl 0x1c(%rsp), %r9d
movq 0x38(%rsp), %r10
pushq $-0x1
popq %r11
cmpq 0x20(%rsp), %rbp
je 0x3ae8
movq 0x10(%rsp), %rax
movsbq (%rax,%rbp), %rax
movl (%r10,%rax,4), %eax
xorl %ecx, %ecx
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %r11d, %ebx
cmpq $0x4, %rcx
je 0x3a6b
movq (%r12,%rcx,8), %rdx
movzwl (%rdx,%rbp,2), %edx
addl %edx, %r14d
cmpq %rcx, %rax
setne %sil
cmpl %edx, %r15d
setl %dil
cmpl %edx, %r13d
setl %r8b
andb %dil, %r8b
testb %sil, %r8b
cmovnel %edx, %r13d
cmovnel %ecx, %ebx
incq %rcx
jmp 0x3a33
cmpl %r9d, %r14d
jl 0x3ae0
cvtsi2sd %r13d, %xmm0
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movsd 0x58(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3ae0
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3be2
movq (%rax), %rdx
movq 0x10(%rsp), %rax
movsbl (%rax,%rbp), %r8d
movslq %ebx, %rax
leaq 0x5e71(%rip), %rcx      # 0x9921
movsbl (%rax,%rcx), %r9d
movq 0x30(%rsp), %rdi
leaq 0x5e66(%rip), %rsi      # 0x9927
movl %ebp, %ecx
xorl %eax, %eax
pushq %r14
pushq %r13
callq 0x2240
pushq $-0x1
popq %r11
movq 0x48(%rsp), %r10
movl 0x2c(%rsp), %r9d
addq $0x10, %rsp
incq %rbp
jmp 0x3a0f
movq %r12, %rdi
callq 0x3434
movq 0x10(%rsp), %rdi
callq 0x2200
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
leaq 0x60(%rsp), %r12
jmp 0x38d1
movq 0x30(%rsp), %rdi
callq 0x2160
jmp 0x3739
movq 0xb4b4(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x58f9(%rip), %rsi      # 0x9432
jmp 0x3b93
movq 0xb49e(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x5ad2(%rip), %rsi      # 0x9621
jmp 0x3b93
movq 0xb488(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x5b5e(%rip), %rsi      # 0x96c3
jmp 0x3b93
movq 0xb472(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x5c26(%rip), %rsi      # 0x97a1
jmp 0x3b93
movq 0xb45c(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x5d67(%rip), %rsi      # 0x98f5
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21c0
nop