// File: CK7[P]abawaca/src/SCGdb.cpp
// Function: SCGdb::load_scgs()
void SCGdb::load_scgs()
{
	FILE* fin = fopen(scg_list_file.c_str(), "r");
	if(fin == NULL) {
		char	s[2048];
		sprintf(s, "Failed to read file %s", scg_list_file.c_str());
		throw invalid_argument(s);
	}

	char	scg[1024], gene[1024];

	total_num_scgs = 0;
	while(fscanf(fin, "%s", scg) == 1) {
		total_num_scgs++;
	}
	fclose(fin);

	// An empty SCG database
	if(gene2scg_file.size() == 0)
		return;

	fin = fopen(gene2scg_file.c_str(), "r");
	if(fin == NULL) {
		char	s[2048];
		sprintf(s, "Failed to read file %s", gene2scg_file.c_str());
		throw invalid_argument(s);
	}

	while(fscanf(fin, "%s %s", gene, scg) == 2) {
		char* p = strrchr(gene, '_');
		if(p == NULL) {
			char	s[2048];
			sprintf(s, "Unexpected gene name %s, expected prodigal format: <name>_<integer>\nfile: %s", gene, scg_list_file.c_str());
			throw invalid_argument(s);
		}

		*p = 0;
		size_t scaf = scaf_db.scaf_name2id(gene);
		// The following can happen if a sequence is in the file that was used for the SCG analysis but was not in the file that was binned.
		// We are fine with that.
		if(scaf == 0) {
			continue;
		}

		int gene_id = atoi(p+1);

		if(gene_id == 0) {
			string scaf_name = gene;
			*p = '_';
			char	s[2048];
			sprintf(s, "Could not identify gene id in %s (recovered scaf=%s (%lu), gene_id=%s). Expecting prodigal format gene names: <name>_<integer>\nfile: %s",
				gene, scaf_name.c_str(), scaf, p+1, scg_list_file.c_str());
			throw invalid_argument(s);
		}
		auto it = scaf2scgs.find(scaf);
		if(it == scaf2scgs.end()) {
			scaf2scgs.insert(pair<size_t, set<string> >(scaf, set<string>()));
			it = scaf2scgs.find(scaf);
		}
		it->second.insert(scg);
	}
	fclose(fin);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a30, %rsp           # imm = 0x2A30
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x2968(%rbp)
addq $0x20, %rdi
callq 0x5190
movq %rax, %rdi
leaq 0x8e80(%rip), %rsi      # 0x3514d
callq 0x54b0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2c372
movq -0x2968(%rbp), %rdi
addq $0x20, %rdi
callq 0x5190
movq %rax, %rdx
leaq 0xa139(%rip), %rsi      # 0x36434
xorl %eax, %eax
leaq -0x810(%rbp), %rdi
movq %rdi, -0x2978(%rbp)
callq 0x50b0
movl $0x10, %edi
callq 0x51e0
movq -0x2978(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2970(%rbp)
callq 0x57f0
jmp 0x2c335
movq -0x2970(%rbp), %rdi
movq 0x20c85(%rip), %rsi     # 0x4cfc8
movq 0x20c6e(%rip), %rdx     # 0x4cfb8
callq 0x5700
movq -0x2970(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
callq 0x52d0
jmp 0x2ca44
movq -0x2968(%rbp), %rax
movl $0x0, 0x70(%rax)
movq -0x10(%rbp), %rdi
leaq -0xc20(%rbp), %rdx
leaq 0x9dce(%rip), %rsi      # 0x36160
movb $0x0, %al
callq 0x57a0
cmpl $0x1, %eax
jne 0x2c3b0
movq -0x2968(%rbp), %rax
movl 0x70(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x70(%rax)
jmp 0x2c380
movq -0x10(%rbp), %rdi
callq 0x5400
movq -0x2968(%rbp), %rdi
callq 0x52c0
cmpq $0x0, %rax
jne 0x2c3d0
jmp 0x2ca3b
movq -0x2968(%rbp), %rdi
callq 0x5190
movq %rax, %rdi
leaq 0x8d67(%rip), %rsi      # 0x3514d
callq 0x54b0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2c487
movq -0x2968(%rbp), %rdi
callq 0x5190
movq %rax, %rdx
leaq 0xa024(%rip), %rsi      # 0x36434
xorl %eax, %eax
leaq -0x1820(%rbp), %rdi
movq %rdi, -0x2988(%rbp)
callq 0x50b0
movl $0x10, %edi
callq 0x51e0
movq -0x2988(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2980(%rbp)
callq 0x57f0
jmp 0x2c44a
movq -0x2980(%rbp), %rdi
movq 0x20b70(%rip), %rsi     # 0x4cfc8
movq 0x20b59(%rip), %rdx     # 0x4cfb8
callq 0x5700
movq -0x2980(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
callq 0x52d0
jmp 0x2ca44
jmp 0x2c489
movq -0x10(%rbp), %rdi
leaq -0x1020(%rbp), %rdx
leaq -0xc20(%rbp), %rcx
leaq 0x9fa9(%rip), %rsi      # 0x3644b
movb $0x0, %al
callq 0x57a0
cmpl $0x2, %eax
jne 0x2ca32
leaq -0x1020(%rbp), %rdi
movl $0x5f, %esi
callq 0x5470
movq %rax, -0x1828(%rbp)
cmpq $0x0, -0x1828(%rbp)
jne 0x2c570
movq -0x2968(%rbp), %rdi
addq $0x20, %rdi
callq 0x5190
movq %rax, %rcx
leaq 0x9f5f(%rip), %rsi      # 0x36451
xorl %eax, %eax
leaq -0x2030(%rbp), %rdi
movq %rdi, -0x2998(%rbp)
leaq -0x1020(%rbp), %rdx
callq 0x50b0
movl $0x10, %edi
callq 0x51e0
movq -0x2998(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2990(%rbp)
callq 0x57f0
jmp 0x2c533
movq -0x2990(%rbp), %rdi
movq 0x20a87(%rip), %rsi     # 0x4cfc8
movq 0x20a70(%rip), %rdx     # 0x4cfb8
callq 0x5700
movq -0x2990(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
callq 0x52d0
jmp 0x2ca44
movq -0x2968(%rbp), %rax
movq -0x1828(%rbp), %rcx
movb $0x0, (%rcx)
movq 0x78(%rax), %rax
movq %rax, -0x29a8(%rbp)
leaq -0x2059(%rbp), %rdi
movq %rdi, -0x29a0(%rbp)
callq 0x5760
movq -0x29a0(%rbp), %rdx
leaq -0x2058(%rbp), %rdi
leaq -0x1020(%rbp), %rsi
callq 0x55b0
jmp 0x2c5bb
movq -0x29a8(%rbp), %rdi
leaq -0x2058(%rbp), %rsi
callq 0x2ccd0
movq %rax, -0x29b0(%rbp)
jmp 0x2c5d7
leaq -0x2058(%rbp), %rdi
callq 0x5250
leaq -0x2059(%rbp), %rdi
callq 0x5460
movq -0x29b0(%rbp), %rax
movq %rax, -0x2038(%rbp)
cmpq $0x0, -0x2038(%rbp)
jne 0x2c64f
jmp 0x2c489
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
jmp 0x2c63e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
leaq -0x2058(%rbp), %rdi
callq 0x5250
leaq -0x2059(%rbp), %rdi
callq 0x5460
jmp 0x2ca44
movq -0x1828(%rbp), %rdi
addq $0x1, %rdi
callq 0x5670
movl %eax, -0x2060(%rbp)
cmpl $0x0, -0x2060(%rbp)
jne 0x2c7e4
leaq -0x2081(%rbp), %rdi
movq %rdi, -0x29b8(%rbp)
callq 0x5760
movq -0x29b8(%rbp), %rdx
leaq -0x2080(%rbp), %rdi
leaq -0x1020(%rbp), %rsi
callq 0x55b0
jmp 0x2c6a1
leaq -0x2081(%rbp), %rdi
callq 0x5460
movq -0x1828(%rbp), %rax
movb $0x5f, (%rax)
leaq -0x2080(%rbp), %rdi
callq 0x5190
movq -0x2968(%rbp), %rdi
movq %rax, -0x29e0(%rbp)
movq -0x2038(%rbp), %rax
movq %rax, -0x29d8(%rbp)
movq -0x1828(%rbp), %rax
incq %rax
movq %rax, -0x29d0(%rbp)
addq $0x20, %rdi
callq 0x5190
movq -0x29e0(%rbp), %rcx
movq -0x29d8(%rbp), %r8
movq -0x29d0(%rbp), %r9
movq %rax, %rdx
movq %rsp, %rax
movq %rdx, (%rax)
leaq 0x9d80(%rip), %rsi      # 0x3649e
xorl %eax, %eax
leaq -0x2890(%rbp), %rdi
movq %rdi, -0x29c8(%rbp)
leaq -0x1020(%rbp), %rdx
callq 0x50b0
movl $0x10, %edi
callq 0x51e0
movq -0x29c8(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x29c0(%rbp)
callq 0x57f0
jmp 0x2c75f
movq -0x29c0(%rbp), %rdi
movq 0x2085b(%rip), %rsi     # 0x4cfc8
movq 0x20844(%rip), %rdx     # 0x4cfb8
callq 0x5700
jmp 0x2ca50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
leaq -0x2081(%rbp), %rdi
callq 0x5460
jmp 0x2ca44
movq -0x29c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
callq 0x52d0
jmp 0x2c7d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
leaq -0x2080(%rbp), %rdi
callq 0x5250
jmp 0x2ca44
movq -0x2968(%rbp), %rdi
addq $0x40, %rdi
leaq -0x2038(%rbp), %rsi
callq 0x2cd50
movq -0x2968(%rbp), %rdi
movq %rax, -0x2898(%rbp)
addq $0x40, %rdi
callq 0x2cdb0
movq %rax, -0x28a0(%rbp)
leaq -0x2898(%rbp), %rdi
leaq -0x28a0(%rbp), %rsi
callq 0x2cd80
testb $0x1, %al
jne 0x2c835
jmp 0x2c953
movq -0x2968(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x29f0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x28f0(%rbp)
movaps %xmm0, -0x2900(%rbp)
movaps %xmm0, -0x2910(%rbp)
leaq -0x2910(%rbp), %rdi
movq %rdi, -0x29e8(%rbp)
callq 0x2ca50
movq -0x29e8(%rbp), %rdx
leaq -0x28d8(%rbp), %rdi
leaq -0x2038(%rbp), %rsi
callq 0x2ce20
jmp 0x2c88e
movq -0x29f0(%rbp), %rdi
leaq -0x28d8(%rbp), %rsi
callq 0x2cde0
movb %dl, -0x29f9(%rbp)
movq %rax, -0x29f8(%rbp)
jmp 0x2c8b0
movb -0x29f9(%rbp), %al
movq -0x29f8(%rbp), %rcx
movq %rcx, -0x2920(%rbp)
movb %al, -0x2918(%rbp)
leaq -0x28d8(%rbp), %rdi
callq 0x2ce60
leaq -0x2910(%rbp), %rdi
callq 0xb300
movq -0x2968(%rbp), %rdi
addq $0x40, %rdi
leaq -0x2038(%rbp), %rsi
callq 0x2cd50
movq %rax, -0x2928(%rbp)
movq -0x2928(%rbp), %rax
movq %rax, -0x2898(%rbp)
jmp 0x2c953
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
jmp 0x2c942
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
leaq -0x28d8(%rbp), %rdi
callq 0x2ce60
leaq -0x2910(%rbp), %rdi
callq 0xb300
jmp 0x2ca44
leaq -0x2898(%rbp), %rdi
callq 0x2ce80
addq $0x8, %rax
movq %rax, -0x2a10(%rbp)
leaq -0x2949(%rbp), %rdi
movq %rdi, -0x2a08(%rbp)
callq 0x5760
movq -0x2a08(%rbp), %rdx
leaq -0x2948(%rbp), %rdi
leaq -0xc20(%rbp), %rsi
callq 0x55b0
jmp 0x2c999
movq -0x2a10(%rbp), %rdi
leaq -0x2948(%rbp), %rsi
callq 0x2cea0
movb %dl, -0x2a19(%rbp)
movq %rax, -0x2a18(%rbp)
jmp 0x2c9bb
movb -0x2a19(%rbp), %al
movq -0x2a18(%rbp), %rcx
movq %rcx, -0x2960(%rbp)
movb %al, -0x2958(%rbp)
leaq -0x2948(%rbp), %rdi
callq 0x5250
leaq -0x2949(%rbp), %rdi
callq 0x5460
jmp 0x2c489
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
jmp 0x2ca24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x818(%rbp)
movl %eax, -0x81c(%rbp)
leaq -0x2948(%rbp), %rdi
callq 0x5250
leaq -0x2949(%rbp), %rdi
callq 0x5460
jmp 0x2ca44
movq -0x10(%rbp), %rdi
callq 0x5400
addq $0x2a30, %rsp           # imm = 0x2A30
popq %rbp
retq
movq -0x818(%rbp), %rdi
callq 0x5750