// File: 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
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rsi, -0x2d0(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movss %xmm0, -0x20(%rbp)
movq 0x6cfff(%rip), %rdi     # 0x78fa8
leaq 0x4d468(%rip), %rsi     # 0x59418
callq 0x6520
movq -0x2d0(%rbp), %rsi
movq %rax, %rdi
callq 0x64d0
movq %rax, %rdi
movq 0x6cfc2(%rip), %rsi     # 0x78f90
callq 0x6570
movq -0x2d0(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x10, %edx
callq 0x69b0
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x6030
movb %al, -0x2c5(%rbp)
jmp 0xc00b
movb -0x2c5(%rbp), %al
testb $0x1, %al
jne 0xc01a
jmp 0xc0cc
movq 0x6cfcf(%rip), %rdi     # 0x78ff0
movq 0x6cf68(%rip), %rsi     # 0x78f90
callq 0x6570
movq %rax, -0x2d8(%rbp)
jmp 0xc036
movq -0x2d8(%rbp), %rdi
leaq 0x4d3ec(%rip), %rsi     # 0x59430
callq 0x6520
movq %rax, -0x2e0(%rbp)
jmp 0xc052
movq -0x2d0(%rbp), %rsi
movq -0x2e0(%rbp), %rdi
callq 0x64d0
movq %rax, -0x2e8(%rbp)
jmp 0xc06e
movq -0x2e8(%rbp), %rdi
movq 0x6cf14(%rip), %rsi     # 0x78f90
callq 0x6570
movq %rax, -0x2f0(%rbp)
jmp 0xc08a
movq -0x2f0(%rbp), %rdi
movq 0x6cef8(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0xc09f
movl $0x1, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0xc4de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0xc4f6
leaq 0x4dca7(%rip), %rsi     # 0x59d7a
leaq -0x220(%rbp), %rdi
callq 0x6520
movq %rax, -0x2f8(%rbp)
jmp 0xc0e8
movq -0x2f8(%rbp), %rdi
leaq 0x6d56a(%rip), %rsi     # 0x79660
callq 0x64d0
movq %rax, -0x300(%rbp)
jmp 0xc104
movq -0x300(%rbp), %rdi
movq 0x6ce7e(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0xc119
movl -0x1c(%rbp), %esi
leaq -0x220(%rbp), %rdi
callq 0x68d0
movq %rax, -0x308(%rbp)
jmp 0xc131
movq -0x308(%rbp), %rdi
movq 0x6ce51(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0xc146
movq -0x2d0(%rbp), %rsi
leaq -0x250(%rbp), %rdi
callq 0xa370
jmp 0xc15b
leaq 0x5025c(%rip), %rsi     # 0x5c3be
leaq -0x250(%rbp), %rdi
movq %rdi, -0x310(%rbp)
movq $-0x1, %rdx
callq 0x64f0
movq -0x310(%rbp), %rsi
movq %rax, %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x270(%rbp), %rdi
callq 0x6550
jmp 0xc198
leaq -0x220(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x64d0
movq %rax, -0x318(%rbp)
jmp 0xc1b4
movq -0x318(%rbp), %rdi
movq 0x6cdce(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0xc1c9
leaq -0x270(%rbp), %rdi
callq 0x6a08
leaq -0x220(%rbp), %rdi
movl $0x22, %esi
callq 0x6620
jmp 0xc1e8
movq -0x10(%rbp), %rdi
callq 0xeeb0
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rdi
callq 0xef10
movq %rax, -0x280(%rbp)
leaq -0x278(%rbp), %rdi
leaq -0x280(%rbp), %rsi
callq 0xeee0
testb $0x1, %al
jne 0xc224
jmp 0xc4a3
leaq -0x278(%rbp), %rdi
callq 0xef40
movq %rax, %rsi
leaq -0x2a0(%rbp), %rdi
callq 0x6230
jmp 0xc241
movb $0x0, -0x2a2(%rbp)
movl $0x0, -0x2a8(%rbp)
movl $0x0, -0x2ac(%rbp)
movl $0x0, -0x2b0(%rbp)
leaq -0x278(%rbp), %rdi
callq 0xef40
movq %rax, %rdi
addq $0x20, %rdi
callq 0xef70
movq %rax, -0x2c0(%rbp)
movq -0x2c0(%rbp), %rax
movq %rax, -0x320(%rbp)
leaq -0x278(%rbp), %rdi
callq 0xef40
movq %rax, %rdi
addq $0x20, %rdi
callq 0xef70
movq %rax, %rcx
movq -0x320(%rbp), %rax
addq $0x10, %rcx
cmpq %rcx, %rax
je 0xc372
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
addl -0x2ac(%rbp), %eax
movl %eax, -0x2ac(%rbp)
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
cmpl -0x2b0(%rbp), %eax
jle 0xc34a
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2b0(%rbp)
movslq -0x2a8(%rbp), %rcx
leaq 0x6d310(%rip), %rax     # 0x79615
movb (%rax,%rcx), %al
movb %al, -0x2a1(%rbp)
jmp 0xc34a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0xc4d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x6a08
jmp 0xc4d0
jmp 0xc34c
movq -0x2c0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2c0(%rbp)
movl -0x2a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a8(%rbp)
jmp 0xc285
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x4cee5(%rip), %xmm0    # 0x59268
subsd %xmm1, %xmm0
cvtsi2sdl -0x2ac(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2c4(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x8100
movl %eax, -0x324(%rbp)
jmp 0xc3b3
movl -0x324(%rbp), %eax
cmpl $0x0, %eax
je 0xc3f7
movl $0x1, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0xc478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x6a08
jmp 0xc4d0
cvtsi2ssl -0x2b0(%rbp), %xmm0
ucomiss -0x2c4(%rbp), %xmm0
jb 0xc43b
movsbl -0x2a1(%rbp), %esi
leaq -0x220(%rbp), %rdi
callq 0x6620
movq %rax, -0x330(%rbp)
jmp 0xc424
movq -0x330(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x64d0
jmp 0xc439
jmp 0xc46e
leaq 0x4d005(%rip), %rsi     # 0x59447
leaq -0x220(%rbp), %rdi
callq 0x6520
movq %rax, -0x338(%rbp)
jmp 0xc457
movq -0x338(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x64d0
jmp 0xc46c
jmp 0xc46e
movl $0x0, -0x230(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x6a08
movl -0x230(%rbp), %eax
testl %eax, %eax
jne 0xc4c2
jmp 0xc490
jmp 0xc492
leaq -0x278(%rbp), %rdi
callq 0xef90
jmp 0xc1f8
leaq -0x220(%rbp), %rdi
callq 0x6740
jmp 0xc4b1
movl $0x0, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x6a08
jmp 0xc4de
leaq -0x250(%rbp), %rdi
callq 0x6a08
jmp 0xc4f6
leaq -0x220(%rbp), %rdi
callq 0x67d0
movl -0x4(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x67d0
movq -0x228(%rbp), %rdi
callq 0x6900
nop