// File: /workspace/github_c_makefile_star2_O1/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 0x1f376(%rip), %rdx     # 0x25194
callq 0x3090
movl %eax, %r12d
xorl %r13d, %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll $0x18, %r12d
js 0x5ec2
movq %rax, 0x28(%rsp)
movl $0x1, %r15d
leaq 0x1f34a(%rip), %r14     # 0x25194
cmpl $0x72000000, %r12d      # imm = 0x72000000
je 0x5e84
cmpl $0x70000000, %r12d      # imm = 0x70000000
je 0x5e6f
cmpl $0x66000000, %r12d      # imm = 0x66000000
jne 0x5e98
movl $0x1, 0x34(%rsp)
jmp 0x5e98
movq 0x2a142(%rip), %rax     # 0x2ffb8
movq (%rax), %rdi
callq 0x35b0
movl %eax, 0x38(%rsp)
jmp 0x5e98
movq 0x2a12d(%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 0x5e4a
testl %r15d, %r15d
sete %r13b
jmp 0x5ec7
movq %rax, 0x28(%rsp)
testb %r13b, %r13b
jne 0x6417
movq 0x28(%rsp), %rsi
movl %esi, %eax
movq 0x2a0fa(%rip), %r13     # 0x2ffd8
subl (%r13), %ebx
xorl $0x1, %eax
movl %ebx, %ecx
xorl $0x2, %ecx
orl %eax, %ecx
setne %al
cmpl $0x1, %ebx
setne %cl
movl %esi, %edx
andl $-0x2, %edx
cmpl $0x2, %edx
setne %dl
cmpl $0x4, %esi
setne %sil
andb %dl, %sil
orb %cl, %sil
cmpb $0x1, %sil
jne 0x5f1b
testb %al, %al
jne 0x6417
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
leaq 0x1f1a0(%rip), %rbx     # 0x250d4
leaq 0x1f4b2(%rip), %r12     # 0x253ed
movq %rbx, %rsi
movl $0x130, %edx            # imm = 0x130
movq %r12, %rcx
callq 0x22d2b
movq %rax, 0x20(%rsp)
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x131, %edx            # imm = 0x131
movq %r12, %rcx
callq 0x22d2b
movq %rax, 0x50(%rsp)
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x132, %edx            # imm = 0x132
movq %r12, %rcx
callq 0x22d2b
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 %r12, %rcx
movq %r12, %r14
callq 0x22d2b
movq %rax, %r12
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
movq %rbx, %rsi
movl $0x134, %edx            # imm = 0x134
movq %r14, %rcx
movq %r13, %r14
callq 0x22d2b
movq %rax, %rbx
movslq (%r13), %rax
movq (%rbp,%rax,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x32c0
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f3ec(%rip), %rsi     # 0x253fb
movq 0x50(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r14), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f3d5(%rip), %rsi     # 0x25402
movq %r15, 0x48(%rsp)
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r14), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f3be(%rip), %rsi     # 0x25409
movq %r12, 0x58(%rsp)
movq %r12, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r14), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1f3a7(%rip), %rsi     # 0x25410
movq %rbx, 0x60(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3070
callq 0x31c0
movq %rax, %rbx
leaq 0x1f38f(%rip), %rdx     # 0x25416
leaq 0x1f35f(%rip), %rdi     # 0x253ed
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq 0x28(%rsp), %rcx
decl %ecx
cmpl $0x3, %ecx
ja 0x6145
leaq 0x1ef6f(%rip), %rax     # 0x25020
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movslq (%r14), %rax
movq 0x8(%rbp,%rax,8), %rdx
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x9a29
jmp 0x6145
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x9dfe
movslq (%r14), %rax
movq (%rbp,%rax,8), %rsi
movq %r13, %rdi
callq 0x32c0
jmp 0x6145
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x9e54
jmp 0x6145
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x9bd3
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1eebf(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
leaq 0x1f2ce(%rip), %r15     # 0x25436
movl $0x3, %edi
movq %r15, %rsi
movb $0x1, %al
callq 0xdccf
leaq 0x1f26f(%rip), %rbp     # 0x253ed
leaq 0x20c99(%rip), %rdx     # 0x26e1e
movq %rbp, %rdi
movq %r13, %rsi
callq 0xdf35
movq %rax, %rbx
callq 0x31c0
movq %r13, %r12
movq %rax, %r13
leaq 0x1f29b(%rip), %rdx     # 0x25440
movq %rbp, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x10e8d
callq 0x31c0
subq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ee41(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %r15, %rsi
movq %r15, %r14
movb $0x1, %al
callq 0xdccf
movq %rbx, %rdi
callq 0xe4b9
callq 0x31c0
movq %rax, %rbx
leaq 0x1f255(%rip), %rdx     # 0x2545d
movq %rbp, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq 0x48(%rsp), %rdi
callq 0x5984
movq %rax, %r13
movq 0x58(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0xfc75
movq %r13, %rdi
callq 0x1004d
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1edc8(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %r14, %rsi
movb $0x1, %al
callq 0xdccf
callq 0x31c0
movq %rax, %rbx
leaq 0x1f214(%rip), %rdx     # 0x2548a
movq %rbp, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq %r15, %rdi
callq 0xff47
movq %rax, %r13
movq %rax, %rdi
callq 0x5ba2
movq %r15, %rdi
movq %r13, %rsi
callq 0xfc75
movq %r13, %rdi
callq 0x1004d
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ed59(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %r14, %rsi
movq %r14, %rbp
movb $0x1, %al
callq 0xdccf
leaq 0x1f10d(%rip), %r14     # 0x253ed
movq %r14, %rdi
movq %r12, %rsi
leaq 0x20b31(%rip), %rdx     # 0x26e1e
callq 0xdf35
movq %rax, %rbx
callq 0x31c0
movq %rax, %r13
leaq 0x1f197(%rip), %rdx     # 0x2549b
movq %r14, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
movl $0x1, %edx
callq 0x10e8d
callq 0x31c0
subq %r13, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ecdc(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
movq %rbp, %rsi
movb $0x1, %al
callq 0xdccf
movq %rbx, %rdi
callq 0xe4b9
callq 0x31c0
movq %rax, %rbx
leaq 0x1f15a(%rip), %rdx     # 0x254c4
leaq 0x1f07c(%rip), %rdi     # 0x253ed
movl $0x3, %esi
xorl %eax, %eax
callq 0xdbeb
movq %r15, %rdi
callq 0xff47
movq %rax, %r13
movq %rax, %rdi
movl $0x20, %esi
callq 0xeaeb
movq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0xfcf2
movq %r13, %rdi
callq 0x1004d
callq 0x31c0
subq %rbx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1ec57(%rip), %xmm0    # 0x2501c
cvtss2sd %xmm0, %xmm0
movl $0x3, %edi
leaq 0x1f061(%rip), %rsi     # 0x25436
movb $0x1, %al
callq 0xdccf
movq %r14, %rdi
callq 0x3390
movq %r12, %rdi
callq 0x3390
movq 0x48(%rsp), %rdi
callq 0x3390
movq %r15, %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 0x6408