// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x2d435(%rip), %rdi     # 0x35fb8
leaq 0x1ca53(%rip), %rsi     # 0x255dd
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x10(%rbx), %rsi
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
pushq $0x38
popq %r15
pushq $0xe
popq %r12
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x8c0c
xorl %eax, %eax
movq %r15, %rcx
cmpq $0x48, %rcx
je 0x8bc8
addl (%rsi,%rcx), %eax
addq $0x4, %rcx
jmp 0x8bb9
cmpl (%r14), %eax
jge 0x8bdd
movq (%rsi), %r13
movq %rbx, %rdi
callq 0xa5e0
movq %r13, %rsi
jmp 0x8baf
movq %r12, %rcx
cmpq $0x12, %rcx
je 0x8bf6
movl (%rsi,%rcx,4), %edx
movl %edx, -0x10(%rsi,%rcx,4)
andl $0x0, (%rsi,%rcx,4)
incq %rcx
jmp 0x8be0
cvtsi2ss %eax, %xmm0
movss 0xc(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
jmp 0x8bac
movq 0x18(%rbx), %rsi
movq %rsi, %rcx
shrq %rcx
movl %esi, %eax
andl $0x1, %eax
orq %rcx, %rax
testq %rsi, %rsi
js 0x8c2a
cvtsi2ss %rsi, %xmm0
jmp 0x8c33
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
xorps %xmm1, %xmm1
je 0x8c42
movss 0xc(%rsp), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
movq 0x2d369(%rip), %rbx     # 0x35fb8
movq %rbx, %rdi
callq 0x5280
leaq 0x1c95a(%rip), %rsi     # 0x255b8
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x1c98b(%rip), %rsi     # 0x25600
movq %rbx, %rdi
callq 0x53f0
movq %rax, %rdi
movss 0xc(%rsp), %xmm0
callq 0x5300
movq %rax, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x51c0