// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bwtindex.c
// Function: command_index
int command_index(int argc, char *argv[]) {
	bwt_t *bwt;
	char * prefix, * proName, * pacName, * bwtName, * saName;
	gzFile fp;
	char c;
	int indexType, valid;
	IndexHeader indexHeader;
	clock_t t;

	// Parse arguments
	valid = 1;
	indexType = -1;
	memset(&indexHeader, 0, sizeof(indexHeader));

	while ((c = getopt(argc, argv, "fr:p:")) >= 0) {
		if (c == 'f') indexHeader.multiFrame = 1;
		if (c == 'p') indexHeader.referenceType = atoi(optarg);
		if (c == 'r') indexType = atoi(optarg);
		if (c == '?') valid = 0;
	}

	// Check for valid combinations
	if (valid) {
		valid = 0;
		if ((indexType == 1) && (argc - optind == 2)) valid = 1;
		if ((indexType == 2) && (argc - optind == 1)) valid = 1;
		if ((indexType == 3) && (argc - optind == 1)) valid = 1;
		if ((indexType == 4) && (argc - optind == 1)) valid = 1;
	}

	if (!valid) {
		fprintf(stderr, "\n");
		fprintf(stderr, "Usage: paladin index [options] <reference.fasta> [annotation.gff]\n\n");
		fprintf(stderr, "Options:\n\n");
		fprintf(stderr, "    -f     Enable indexing all frames in nucleotide references\n");
		fprintf(stderr, "    -r<#>  Reference type:\n");
		fprintf(stderr, "              1: Reference contains nucleotide sequences (requires corresponding .gff annotation)\n");
		fprintf(stderr, "              2: Reference contains nucleotide sequences (coding only, eg curated transcriptome)\n");
		fprintf(stderr, "              3: Reference contains protein sequences (UniProt or other source)\n");
		fprintf(stderr, "              4: Development tests\n\n");
		fprintf(stderr, "Examples:\n\n");
		fprintf(stderr, "   paladin index -r1 reference.fasta reference.gff\n");
		fprintf(stderr, "   paladin index -r3 uniprot_sprot.fasta.gz\n");
		fprintf(stderr, "\n");
		return 1;
	}    
   
	// Setup filenames
	prefix = malloc(strlen(argv[optind]) + 1);
	proName = malloc(strlen(argv[optind]) + 5);
	pacName = malloc(strlen(argv[optind]) + 5);
	bwtName = malloc(strlen(argv[optind]) + 5);
	saName = malloc(strlen(argv[optind]) + 5);

	sprintf(prefix, "%s", argv[optind]);
	sprintf(proName, "%s.pro", argv[optind]);
	sprintf(pacName, "%s.pac", argv[optind]);
	sprintf(bwtName, "%s.bwt", argv[optind]);
	sprintf(saName, "%s.sa", argv[optind]);

	// Create Protein Sequence
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Translating protein sequence...");

	switch (indexType) {
		case 1:
			// Nucleotide sequence and annotation
			writeIndexProtein(prefix, proName, argv[optind+1], indexHeader); break;
		case 2:
			// Nucleotide sequence, coding only
			writeIndexCodingProtein(prefix, proName, indexHeader); break;
		case 3:
			// Protein sequence
			writeIndexDirectProtein(prefix, proName, indexHeader);
			sprintf(proName, "%s", argv[optind]);
			break;
		case 4:
			// Testing
			writeIndexTestProtein(prefix, proName); break;
	}

	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);

	// Pack FASTA
	fp = xzopen(proName, "r");
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Packing protein sequence... ");
	bns_fasta2bntseq(fp, prefix, 0);
	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
	err_gzclose(fp);

	// Construct BWT
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Constructing BWT for the packed sequence... ");
	bwt = bwt_pac2bwt(pacName);
	bwt_dump_bwt(bwtName, bwt);
	bwt_destroy(bwt);
	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);

	// Update BWT
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Updating BWT... ");
	bwt = bwt_restore_bwt(bwtName);
	bwt_bwtupdate_core(bwt);
	bwt_dump_bwt(bwtName, bwt);
	bwt_destroy(bwt);
	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);

	// Pack Forward-Only FASTA
	fp = xzopen(proName, "r");
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Packing forward-only protein squence... ");
	bns_fasta2bntseq(fp, prefix, 1);
	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
	err_gzclose(fp);

	// Construct Suffix Array from FM-Index and Occurrences
	t = clock();
	logMessage(__func__, LOG_LEVEL_MESSAGE, "Constructing suffix array... ");
	bwt = bwt_restore_bwt(bwtName);
	bwt_cal_sa(bwt, 32);
	bwt_dump_sa(saName, bwt);
	bwt_destroy(bwt);
	logMessageRaw(LOG_LEVEL_MESSAGE, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);

	free(prefix);
	free(proName);
	free(pacName);
	free(bwtName);
	free(saName);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbp
movl %edi, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x1f34c(%rip), %rdx     # 0x25194
callq 0x3090
movl %eax, %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll $0x18, %r12d
js 0x5f0d
movq %rax, 0x28(%rsp)
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x1f320(%rip), %r14     # 0x25194
movl $0x0, 0x20(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x72000000, %r12d      # imm = 0x72000000
je 0x5ebe
cmpl $0x70000000, %r12d      # imm = 0x70000000
je 0x5ea9
cmpl $0x66000000, %r12d      # imm = 0x66000000
jne 0x5ed2
movl $0x1, 0x20(%rsp)
jmp 0x5ed2
movq 0x2a108(%rip), %rax     # 0x2ffb8
movq (%rax), %rdi
callq 0x35b0
movl %eax, 0x18(%rsp)
jmp 0x5ed2
movq 0x2a0f3(%rip), %rax     # 0x2ffb8
movq (%rax), %rdi
callq 0x35b0
movq %rax, 0x28(%rsp)
cmpl $0x3f000000, %r12d      # imm = 0x3F000000
cmovel %r13d, %r15d
movl %ebx, %edi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x3090
movl %eax, %r12d
shll $0x18, %r12d
jns 0x5e84
movl 0x18(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x34(%rsp)
testl %r15d, %r15d
jne 0x5f12
jmp 0x645d
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movl %edi, %ecx
xorl $0x1, %ecx
movq 0x2a0b5(%rip), %r13     # 0x2ffd8
movl (%r13), %eax
subl %eax, %ebx
movl %ebx, %edx
xorl $0x2, %edx
orl %ecx, %edx
sete %cl
cmpl $0x1, %ebx
sete %dl
movl %edi, %esi
andl $-0x2, %esi
cmpl $0x2, %esi
sete %sil
cmpl $0x4, %edi
sete %dil
orb %sil, %dil
testb %dl, %dil
jne 0x5f5c
testb %cl, %cl
je 0x645d
cltq
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
leaq 0x1f161(%rip), %rbx     # 0x250d4
leaq 0x1f473(%rip), %r14     # 0x253ed
movq %rbx, %rsi
movl $0x130, %edx            # imm = 0x130
movq %r14, %rcx
callq 0x23213
movq %rax, 0x18(%rsp)
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x131, %edx            # imm = 0x131
movq %r14, %rcx
callq 0x23213
movq %rax, %r12
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x132, %edx            # imm = 0x132
movq %r14, %rcx
callq 0x23213
movq %rax, %r15
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x133, %edx            # imm = 0x133
movq %r14, %rcx
movq %r14, %rbx
callq 0x23213
movq %rax, %r14
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
leaq 0x1f0ba(%rip), %rsi     # 0x250d4
movl $0x134, %edx            # imm = 0x134
movq %rbx, %rcx
callq 0x23213
movq %rax, %rbx
movslq (%r13), %rax
movq (%rbp,%rax,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x32c0
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f3ae(%rip), %rsi     # 0x253fb
movq %r12, 0x20(%rsp)
movq %r12, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f396(%rip), %rsi     # 0x25402
movq %r15, 0x50(%rsp)
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f37e(%rip), %rsi     # 0x25409
movq %r14, 0x60(%rsp)
movq %r14, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f366(%rip), %rsi     # 0x25410
movq %rbx, 0x58(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3070
callq 0x31c0
movq %rax, %rbx
leaq 0x1f34e(%rip), %rdx     # 0x25416
leaq 0x1f31e(%rip), %rdi     # 0x253ed
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq 0x28(%rsp), %rcx
decl %ecx
leaq 0x1ef37(%rip), %rax     # 0x25020
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movslq (%r13), %rax
movq 0x8(%rbp,%rax,8), %rdx
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, %r12
movq 0x20(%rsp), %rsi
callq 0x9c71
jmp 0x6196
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, %r12
movq 0x20(%rsp), %r15
movq %r15, %rsi
callq 0xa018
movslq (%r13), %rax
movq (%rbp,%rax,8), %rsi
movq %r15, %rdi
callq 0x32c0
jmp 0x6196
movq 0x18(%rsp), %rdi
movq %rdi, %r12
movq 0x20(%rsp), %rsi
callq 0xa06e
jmp 0x6196
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, %r12
movq 0x20(%rsp), %rsi
callq 0x9e2b
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ee6e(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
leaq 0x1f27d(%rip), %rbp     # 0x25436
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdf64
leaq 0x1f21e(%rip), %r14     # 0x253ed
leaq 0x20c94(%rip), %rdx     # 0x26e6a
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0xe1c4
movq %rax, %rbx
callq 0x31c0
movq %rax, %r13
leaq 0x1f24b(%rip), %rdx     # 0x25440
movq %r14, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x11164
callq 0x31c0
subq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1edf3(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdf64
movq %rbx, %rdi
callq 0xe747
callq 0x31c0
movq %rax, %rbx
leaq 0x1f20a(%rip), %rdx     # 0x2545d
movq %r14, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq 0x50(%rsp), %rdi
callq 0x59b5
movq %rax, %r13
movq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xffaa
movq %r13, %rdi
callq 0x10378
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ed7d(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdf64
callq 0x31c0
movq %rax, %rbx
leaq 0x1f1c9(%rip), %rdx     # 0x2548a
movq %r14, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq %r12, %rdi
callq 0x10272
movq %rax, %r13
movq %rax, %rdi
callq 0x5bd3
movq %r12, %rdi
movq %r13, %rsi
callq 0xffaa
movq %r13, %rdi
callq 0x10378
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ed0e(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdf64
movq %r14, %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
leaq 0x20b37(%rip), %rdx     # 0x26e6a
callq 0xe1c4
movq %rax, %rbx
callq 0x31c0
movq %rax, %r13
leaq 0x1f151(%rip), %rdx     # 0x2549b
movq %r14, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq %rbx, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movl $0x1, %edx
callq 0x11164
callq 0x31c0
subq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ec96(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdf64
movq %rbx, %rdi
callq 0xe747
callq 0x31c0
movq %rax, %rbx
leaq 0x1f114(%rip), %rdx     # 0x254c4
leaq 0x1f036(%rip), %rdi     # 0x253ed
movl $0x3, %esi
xorl %eax, %eax
callq 0xde80
movq %r12, %rdi
callq 0x10272
movq %rax, %r13
movq %rax, %rdi
movl $0x20, %esi
callq 0xed83
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x10027
movq %r13, %rdi
callq 0x10378
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ec11(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
leaq 0x1f01b(%rip), %rsi     # 0x25436
movb $0x1, %al
callq 0xdf64
movq %r14, %rdi
callq 0x3390
movq %r15, %rdi
callq 0x3390
movq 0x50(%rsp), %rdi
callq 0x3390
movq %r12, %rdi
callq 0x3390
movq %rbp, %rdi
callq 0x3390
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x37f8
movl $0x1, %eax
jmp 0x644e