// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/kmers.cpp
// Function: printKmerFile(std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::array<int, 8ul>, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::array<int, 8ul>>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, float)
int printKmerFile(const std::unordered_map < std::string, std::array < int, 8 > > & mymap, const std::string outputfile, const int kmersize, float userminmaf){
	
	std::cout << "Writing split kmers to " << outputfile << std::endl;

	std::ofstream kmerfile(outputfile);
	if (kmerfile.fail()){
		std::cerr << std::endl << "Error: Failed to open " << outputfile << std::endl << std::endl;
		return 1;
	}
	kmerfile << "SKA v" << versionNumber << std::endl;
	kmerfile << kmersize << std::endl;
	std:: string samplename=splitFileName(outputfile);
	kmerfile << samplename.substr(0, samplename.find_last_of(".")) << std::endl;
	kmerfile << '"';
	for (std::unordered_map < std::string, std::array < int, 8 > >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it){
		std::string kmer=it->first;
		char base;
		bool basefound=false;
		int i=0;

		int filebasecoverage=0;
		int maxfilebasecoverage=0;
		char maxbase;
		for (std::array < int, 8 >::const_iterator it2=it->second.begin(); it2!=it->second.begin()+4; ++it2, ++i){
			filebasecoverage+=*it2;
			if (*it2>maxfilebasecoverage){
				maxfilebasecoverage=*it2;
				base=bases[i];
			}
		}

		float covcutoff=(1.0-userminmaf)*filebasecoverage;

		if (ascii_codons(kmer)){return 1;}
		if (maxfilebasecoverage>=covcutoff){	
			kmerfile << base << kmer;
		}
		else {
			kmerfile << "N" << kmer;
		}
	}
	kmerfile.close();
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movss %xmm0, (%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x2e2a1(%rip), %rdi     # 0x35fb8
leaq 0x1d6ea(%rip), %rsi     # 0x25408
callq 0x53f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
leaq 0x48(%rsp), %r15
pushq $0x10
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5740
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x68(%rsp,%rax)
je 0x7d95
movq 0x2e292(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x1d6b6(%rip), %rsi     # 0x25420
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
pushq $0x1
popq %rbx
jmp 0x7f21
leaq 0x1dfee(%rip), %rsi     # 0x25d8a
leaq 0x48(%rsp), %rdi
callq 0x53f0
leaq 0x2e773(%rip), %rsi     # 0x36520
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
leaq 0x48(%rsp), %rdi
movl %ebp, %esi
callq 0x56a0
movq %rax, %rdi
callq 0x51c0
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x7320
leaq 0x205e5(%rip), %rsi     # 0x283ca
leaq 0x28(%rsp), %r14
pushq $-0x1
popq %rdx
movq %r14, %rdi
callq 0x53d0
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x5420
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
leaq 0x8(%rsp), %rdi
callq 0x5778
leaq 0x48(%rsp), %rdi
pushq $0x22
popq %rsi
callq 0x5480
addq $0x10, %rbx
cvtss2sd (%rsp), %xmm0
movsd 0x1d422(%rip), %xmm1    # 0x25268
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
leaq 0x8(%rsp), %r14
leaq 0x2e67a(%rip), %rbp     # 0x364d5
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x7efc
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x5540
xorl %r12d, %r12d
movq %rbp, %rax
xorl %r13d, %r13d
pushq $0x28
popq %rcx
cmpq $0x38, %rcx
je 0x7e9f
movl (%rbx,%rcx), %edx
cmpl %r13d, %edx
jle 0x7e93
movb (%rax), %r15b
movl %edx, %r13d
addl %edx, %r12d
addq $0x4, %rcx
incq %rax
jmp 0x7e7f
movq %r14, %rdi
callq 0x6674
testl %eax, %eax
jne 0x7f0a
cvtsi2sd %r12d, %xmm0
mulsd (%rsp), %xmm0
cvtsi2ss %r13d, %xmm1
cvtsd2ss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jae 0x7ed6
leaq 0x48(%rsp), %rdi
leaq 0x1d568(%rip), %rsi     # 0x25437
callq 0x53f0
jmp 0x7ee4
movsbl %r15b, %esi
leaq 0x48(%rsp), %rdi
callq 0x5480
movq %rax, %rdi
movq %r14, %rsi
callq 0x53a0
movq %r14, %rdi
callq 0x5778
jmp 0x7e5b
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
callq 0x5560
jmp 0x7f17
leaq 0x8(%rsp), %rdi
callq 0x5778
pushq $0x1
popq %rbx
leaq 0x28(%rsp), %rdi
callq 0x5778
leaq 0x48(%rsp), %rdi
callq 0x55c0
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7f4c
jmp 0x7f47
jmp 0x7f4c
jmp 0x7f51
movq %rax, %rbx
jmp 0x7f68
movq %rax, %rbx
jmp 0x7f5e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5778
leaq 0x28(%rsp), %rdi
callq 0x5778
leaq 0x48(%rsp), %rdi
callq 0x55c0
movq %rbx, %rdi
callq 0x56c0