// File: simonrharris[P]SKA/src/kmers.cpp
// Function: applyFinalKmerArrayMapFilters(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>>>>&, int const&)
void applyFinalKmerArrayMapFilters(std::unordered_map < std::string, std::array < int, 8 > > & kmerMap, const int & usercovcutoff){

	std::cout << "Filtering kmers for total coverage" << std::endl;

	int basecoverage;
	int maxbasecoverage;
	//float covcutoff;
	float totalcoverage=0;

	std::unordered_map < std::string, std::array < int, 8 > >::iterator it = kmerMap.begin();
	std::unordered_map < std::string, std::array < int, 8 > >::iterator endIter = kmerMap.end();
	for (; it!=endIter; ){

		basecoverage=0;
		maxbasecoverage=0;
		for (std::array < int, 8 >::iterator it2=it->second.begin()+4; it2!=it->second.end(); ++it2){
			basecoverage+=*it2;
			if (*it2>maxbasecoverage){
				maxbasecoverage=*it2;
			}
		}

		//remove kmers with coverage lower than the user defined cutoff
		if (basecoverage<usercovcutoff){
			kmerMap.erase(it++);
		}
		else{
			//filter bases that don't meet the coverage cutoff - don't do this any more
			int j=0;
			for (int i=4; i<8; ++i, ++j){
				/*if (it->second[i]>=covcutoff){
					it->second[j]=it->second[i];
				}*/
				it->second[j]=it->second[i];
				it->second[i]=0;
			}
			totalcoverage+=basecoverage;
			++it;
		}
		
	}

	float meancoverage;
	if (kmerMap.size()>0){
		meancoverage=totalcoverage/kmerMap.size();
	}
	else{
		meancoverage=0;
	}
	std::cout << kmerMap.size() << " unique kmers in map after filtering" << std::endl;
	std::cout << "Mean kmer coverage is " << meancoverage << std::endl;

	//return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x6aeee(%rip), %rdi     # 0x78fa8
leaq 0x4b52c(%rip), %rsi     # 0x595ed
callq 0x6520
movq %rax, %rdi
movq 0x6aec0(%rip), %rsi     # 0x78f90
callq 0x6570
xorps %xmm0, %xmm0
movss %xmm0, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
callq 0xfac0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0xf990
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xeee0
testb $0x1, %al
jne 0xe10d
jmp 0xe269
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xf9c0
movq %rax, %rdi
addq $0x20, %rdi
callq 0xfaf0
addq $0x10, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xf9c0
movq %rax, %rdi
addq $0x20, %rdi
callq 0xfbd0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
je 0xe191
movq -0x38(%rbp), %rax
movl (%rax), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
cmpl -0x18(%rbp), %eax
jle 0xe181
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x18(%rbp)
jmp 0xe183
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
jmp 0xe138
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xe1c9
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0xfb50
movq -0x68(%rbp), %rdi
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
callq 0xfb10
movq %rax, -0x48(%rbp)
jmp 0xe264
movl $0x0, -0x4c(%rbp)
movl $0x4, -0x50(%rbp)
cmpl $0x8, -0x50(%rbp)
jge 0xe24c
leaq -0x28(%rbp), %rdi
callq 0xf9c0
movq %rax, %rdi
addq $0x20, %rdi
movslq -0x50(%rbp), %rsi
callq 0xf9f0
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xf9c0
movq %rax, %rdi
addq $0x20, %rdi
movslq -0x4c(%rbp), %rsi
callq 0xf9f0
movl -0x6c(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x28(%rbp), %rdi
callq 0xf9c0
movq %rax, %rdi
addq $0x20, %rdi
movslq -0x50(%rbp), %rsi
callq 0xf9f0
movl $0x0, (%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xe1d7
cvtsi2ssl -0x14(%rbp), %xmm0
addss -0x1c(%rbp), %xmm0
movss %xmm0, -0x1c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xfb80
jmp 0xe0f7
movq -0x8(%rbp), %rdi
callq 0xfbb0
cmpq $0x0, %rax
jbe 0xe2d5
movss -0x1c(%rbp), %xmm0
movss %xmm0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
callq 0xfbb0
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x74(%rbp)
testq %rax, %rax
movss %xmm0, -0x70(%rbp)
js 0xe2c0
movss -0x74(%rbp), %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x78(%rbp), %xmm0
movss -0x70(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x54(%rbp)
jmp 0xe2dd
xorps %xmm0, %xmm0
movss %xmm0, -0x54(%rbp)
movq -0x8(%rbp), %rdi
callq 0xfbb0
movq %rax, %rsi
movq 0x6acb8(%rip), %rdi     # 0x78fa8
callq 0x6120
movq %rax, %rdi
leaq 0x4b2c9(%rip), %rsi     # 0x595c8
callq 0x6520
movq %rax, %rdi
movq 0x6ac82(%rip), %rsi     # 0x78f90
callq 0x6570
movq 0x6ac8e(%rip), %rdi     # 0x78fa8
leaq 0x4b2ef(%rip), %rsi     # 0x59610
callq 0x6520
movq %rax, %rdi
movss -0x54(%rbp), %xmm0
callq 0x63f0
movq %rax, %rdi
movq 0x6ac53(%rip), %rsi     # 0x78f90
callq 0x6570
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)