// File: /workspace/github_c_makefile_star2_O2/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)
andq $0x0, 0x40(%rsp)
pushq $0x1
popq %r12
pushq $-0x1
popq %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
leaq 0x1b94f(%rip), %r14     # 0x20194
movl $0x0, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movl %ebx, %edi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x3090
movl %eax, %r13d
shll $0x18, %r13d
js 0x48c5
cmpl $0x72000000, %r13d      # imm = 0x72000000
je 0x48a4
cmpl $0x70000000, %r13d      # imm = 0x70000000
je 0x488f
cmpl $0x66000000, %r13d      # imm = 0x66000000
jne 0x48b8
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x48b8
movq 0x27722(%rip), %rax     # 0x2bfb8
movq (%rax), %rdi
callq 0x35b0
movl %eax, 0x20(%rsp)
jmp 0x48b8
movq 0x2770d(%rip), %rax     # 0x2bfb8
movq (%rax), %rdi
callq 0x35b0
movq %rax, 0x28(%rsp)
cmpl $0x3f000000, %r13d      # imm = 0x3F000000
cmovel %r15d, %r12d
jmp 0x4855
movl 0x20(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x34(%rsp)
testl %r12d, %r12d
je 0x4dfc
movq 0x28(%rsp), %rdi
movl %edi, %ecx
xorl $0x1, %ecx
movq 0x276e9(%rip), %r13     # 0x2bfd8
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 0x4928
testb %cl, %cl
je 0x4dfc
cltq
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x1(%rax), %rdi
leaq 0x1b795(%rip), %rbx     # 0x200d4
leaq 0x1baa7(%rip), %r14     # 0x203ed
movq %rbx, %rsi
movl $0x130, %edx            # imm = 0x130
movq %r14, %rcx
callq 0x1e045
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 %r14, %rcx
callq 0x1e045
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 0x1e045
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 0x1e045
movq %rax, %r14
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdi
callq 0x3100
leaq 0x5(%rax), %rdi
leaq 0x1b6ee(%rip), %rsi     # 0x200d4
movl $0x134, %edx            # imm = 0x134
movq %rbx, %rcx
callq 0x1e045
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 0x1b9e2(%rip), %rsi     # 0x203fb
movq %r12, 0x18(%rsp)
movq %r12, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1b9ca(%rip), %rsi     # 0x20402
movq %r15, 0x50(%rsp)
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1b9b2(%rip), %rsi     # 0x20409
movq %r14, 0x60(%rsp)
movq %r14, %rdi
xorl %eax, %eax
callq 0x3070
movslq (%r13), %rax
movq (%rbp,%rax,8), %rdx
leaq 0x1b99a(%rip), %rsi     # 0x20410
movq %rbx, 0x58(%rsp)
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3070
callq 0x31c0
movq %rax, %rbx
leaq 0x1b982(%rip), %rdx     # 0x20416
pushq $0x3
popq %rsi
leaq 0x1b94f(%rip), %rdi     # 0x203ed
xorl %eax, %eax
callq 0xc78e
movq 0x28(%rsp), %rcx
decl %ecx
leaq 0x1b56d(%rip), %rax     # 0x20020
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 0x20(%rsp), %rdi
movq %rdi, %r12
movq 0x18(%rsp), %rsi
callq 0x866a
jmp 0x4b60
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, %r12
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x8992
movslq (%r13), %rax
movq (%rbp,%rax,8), %rsi
movq %r15, %rdi
callq 0x32c0
jmp 0x4b60
movq 0x20(%rsp), %rdi
movq %rdi, %r12
movq 0x18(%rsp), %rsi
callq 0x89e5
jmp 0x4b60
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, %r12
movq 0x18(%rsp), %rsi
callq 0x8806
callq 0x31c0
subq %rbx, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b4a7(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
leaq 0x1b8b6(%rip), %r14     # 0x20436
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r14, %rsi
movb $0x1, %al
callq 0xc872
leaq 0x1b857(%rip), %r13     # 0x203ed
leaq 0x1d281(%rip), %rdx     # 0x21e1e
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0xcacc
movq %rax, %rbx
callq 0x31c0
movq %rax, %r15
leaq 0x1b884(%rip), %rdx     # 0x20440
movq %r13, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0xc78e
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xf629
callq 0x31c0
subq %r15, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b432(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
movl %ebp, %edi
movq %r14, %rsi
movb $0x1, %al
callq 0xc872
movq %rbx, %rdi
callq 0xd044
callq 0x31c0
movq %rax, %rbx
leaq 0x1b84c(%rip), %rdx     # 0x2045d
movq %r13, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0xc78e
movq 0x50(%rsp), %rdi
callq 0x43a7
movq %rax, %r15
movq 0x60(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xe53f
movq %r15, %rdi
callq 0xe901
callq 0x31c0
subq %rbx, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b3c5(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
movl %ebp, %edi
movq %r14, %rsi
movb $0x1, %al
callq 0xc872
callq 0x31c0
movq %rax, %rbx
leaq 0x1b814(%rip), %rdx     # 0x2048a
movq %r13, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0xc78e
movq %r12, %rdi
callq 0xe802
movq %rax, %r15
movq %rax, %rdi
callq 0x45cc
movq %r12, %rdi
movq %r15, %rsi
callq 0xe53f
movq %r15, %rdi
callq 0xe901
callq 0x31c0
subq %rbx, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b35f(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
movl %ebp, %edi
movq %r14, %rsi
movq %r14, %r13
movb $0x1, %al
callq 0xc872
leaq 0x1b716(%rip), %r14     # 0x203ed
movq %r14, %rdi
movq 0x18(%rsp), %rsi
leaq 0x1d138(%rip), %rdx     # 0x21e1e
callq 0xcacc
movq %rax, %rbx
callq 0x31c0
movq %rax, %r15
leaq 0x1b79e(%rip), %rdx     # 0x2049b
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0xc78e
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0xf629
callq 0x31c0
subq %r15, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b2eb(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
movl %ebp, %edi
movq %r13, %rsi
movb $0x1, %al
callq 0xc872
movq %rbx, %rdi
callq 0xd044
callq 0x31c0
movq %rax, %rbx
leaq 0x1b76c(%rip), %rdx     # 0x204c4
leaq 0x1b68e(%rip), %rdi     # 0x203ed
movl %ebp, %esi
xorl %eax, %eax
callq 0xc78e
movq %r12, %rdi
callq 0xe802
movq %rax, %r15
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0xd65b
movq 0x58(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0xe5b5
movq %r15, %rdi
callq 0xe901
callq 0x31c0
subq %rbx, %rax
cvtsi2ss %rax, %xmm0
divss 0x1b271(%rip), %xmm0    # 0x2001c
cvtss2sd %xmm0, %xmm0
movl %ebp, %edi
leaq 0x1b67e(%rip), %rsi     # 0x20436
movb $0x1, %al
callq 0xc872
movq %r14, %rdi
callq 0x3390
movq 0x18(%rsp), %rdi
callq 0x3390
movq 0x50(%rsp), %rdi
callq 0x3390
movq %r12, %rdi
callq 0x3390
movq %r13, %rdi
callq 0x3390
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x271cd(%rip), %r15     # 0x2bfd0
movq (%r15), %rsi
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x3410
movq (%r15), %rcx
leaq 0x1b380(%rip), %rdi     # 0x2019a
pushq $0x43
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b3ab(%rip), %rdi     # 0x201de
pushq $0xa
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b3a1(%rip), %rdi     # 0x201e9
pushq $0x3f
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b3cc(%rip), %rdi     # 0x20229
pushq $0x1b
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b3d3(%rip), %rdi     # 0x20245
pushq $0x62
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b421(%rip), %rdi     # 0x202a8
pushq $0x61
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b46e(%rip), %rdi     # 0x2030a
pushq $0x50
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b4aa(%rip), %rdi     # 0x2035b
pushq $0x24
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b4ba(%rip), %rdi     # 0x20380
pushq $0xb
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b4b1(%rip), %rdi     # 0x2038c
pushq $0x33
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rcx
leaq 0x1b4d0(%rip), %rdi     # 0x203c0
pushq $0x2c
popq %rsi
movq %r14, %rdx
callq 0x3790
movq (%r15), %rsi
movl %ebx, %edi
callq 0x3410
pushq $0x1
popq %rax
jmp 0x4ded