// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %rdi, %rbx
movq 0x20(%rdi), %rdi
leaq 0x38b1(%rip), %rsi      # 0x15131
callq 0x4400
testq %rax, %rax
je 0x11b45
movq %rax, %r14
leaq 0x48a0(%rip), %r15      # 0x16138
leaq -0x10a0(%rbp), %r12
xorl %eax, %eax
movl %eax, 0x70(%rbx)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x46a0
cmpl $0x1, %eax
jne 0x118c0
movl 0x70(%rbx), %eax
incl %eax
jmp 0x118a1
movq %r14, %rdi
callq 0x4350
cmpq $0x0, 0x8(%rbx)
je 0x11a66
movq (%rbx), %rdi
leaq 0x3854(%rip), %rsi      # 0x15131
callq 0x4400
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x11b7c
leaq 0x40(%rbx), %r15
leaq 0x48(%rbx), %rax
movq %rax, -0x78(%rbp)
leaq -0xca0(%rbp), %r12
leaq -0x98(%rbp), %r14
movq %rbx, -0x38(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x4ae0(%rip), %rsi      # 0x163f8
movq %r12, %rdx
leaq -0x10a0(%rbp), %rcx
xorl %eax, %eax
callq 0x46a0
cmpl $0x2, %eax
jne 0x11a5d
movq %r12, %rdi
pushq $0x5f
popq %rsi
callq 0x43b0
testq %rax, %rax
je 0x11a78
movq %rax, %r13
movq %r12, %rsi
movb $0x0, (%rax)
movq 0x78(%rbx), %r12
movq %r14, %rdi
movq %rsi, %rbx
leaq -0x8a0(%rbp), %rdx
callq 0x4140
movq %r12, %rdi
movq %r14, %rsi
callq 0x11c20
movq %rax, %r12
movq %r14, %rdi
callq 0x4180
movq %r12, -0x30(%rbp)
testq %r12, %r12
je 0x11a4a
movq %r13, %r12
incq %r12
movq %r12, %rdi
callq 0x4580
testl %eax, %eax
je 0x11ac7
movq %r15, %r13
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x120e2
movq %rax, %r12
cmpq -0x78(%rbp), %rax
leaq -0x8a0(%rbp), %r14
jne 0x11a20
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0x68(%rbp), %rsi
movq %rsi, -0x58(%rbp)
movq %rsi, -0x50(%rbp)
andq $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x8a0(%rbp)
leaq -0x890(%rbp), %rdi
callq 0xa422
movq %r13, %rdi
movq %r14, %rsi
callq 0x1213e
leaq -0x898(%rbp), %rdi
callq 0x725c
leaq -0x70(%rbp), %rdi
callq 0x725c
movq %r13, %rdi
leaq -0x30(%rbp), %rsi
callq 0x120e2
movq %rax, %r12
movq %r14, %rdi
leaq -0x10a0(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x4140
addq $0x28, %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x1230c
movq %r14, %rdi
callq 0x4180
movq %rbx, %r12
leaq -0x98(%rbp), %r14
movq -0x38(%rbp), %rbx
jmp 0x1190d
movq -0x40(%rbp), %rdi
callq 0x4350
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rbx), %rcx
leaq 0x497b(%rip), %rsi      # 0x163fe
leaq -0x8a0(%rbp), %rbx
leaq -0xca0(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4080
pushq $0x10
popq %rdi
callq 0x41b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x46e0
movq 0xd510(%rip), %rsi      # 0x1efc8
movq 0xd4f9(%rip), %rdx      # 0x1efb8
movq %r14, %rdi
callq 0x4620
leaq -0x70(%rbp), %rdi
leaq -0xca0(%rbp), %rsi
leaq -0x8a0(%rbp), %rdx
callq 0x4140
movb $0x5f, (%r13)
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, (%rsp)
leaq 0x494d(%rip), %rsi      # 0x1644b
leaq -0x8a0(%rbp), %rbx
leaq -0xca0(%rbp), %rdx
movq %rbx, %rdi
movq %r12, %r9
xorl %eax, %eax
callq 0x4080
pushq $0x10
popq %rdi
callq 0x41b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x46e0
movq 0xd492(%rip), %rsi      # 0x1efc8
movq 0xd47b(%rip), %rdx      # 0x1efb8
movq %r14, %rdi
callq 0x4620
movq 0x20(%rbx), %rdx
leaq 0x4891(%rip), %rsi      # 0x163e1
leaq -0x8a0(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4080
pushq $0x10
popq %rdi
callq 0x41b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x46e0
jmp 0x11ab1
movq (%rbx), %rdx
leaq 0x485b(%rip), %rsi      # 0x163e1
leaq -0x8a0(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4080
pushq $0x10
popq %rdi
callq 0x41b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x46e0
jmp 0x11ab1
jmp 0x11be8
jmp 0x11be8
movq %rax, %rbx
jmp 0x11bc6
movq %rax, %rbx
movq %r14, %rdi
callq 0x4260
leaq -0x70(%rbp), %rdi
jmp 0x11c0d
movq %rax, %rbx
leaq -0x898(%rbp), %rdi
callq 0x725c
leaq -0x70(%rbp), %rdi
callq 0x725c
jmp 0x11c17
jmp 0x11c14
movq %rax, %rbx
movq %r14, %rdi
callq 0x4260
jmp 0x11c17
jmp 0x11c14
movq %rax, %rbx
leaq -0x8a0(%rbp), %rdi
jmp 0x11c0d
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x4180
jmp 0x11c17
movq %rax, %rbx
movq %rbx, %rdi
callq 0x4660
nop