// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x36b5c(%rip), %r15     # 0x40fc0
leaq 0x29182(%rip), %rsi     # 0x335ed
movl $0x22, %edx
movq %r15, %rdi
callq 0x5420
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq 0x10(%rbx), %rsi
xorps %xmm0, %xmm0
testq %rsi, %rsi
je 0xa517
movss %xmm0, 0x4(%rsp)
movl $0x38, %ecx
xorl %eax, %eax
addl (%rsi,%rcx), %eax
addq $0x4, %rcx
cmpq $0x48, %rcx
jne 0xa4b8
cmpl (%r14), %eax
jge 0xa4da
movq (%rsi), %r15
movq %rbx, %rdi
callq 0xc01e
movq %r15, %rsi
jmp 0xa510
movl $0xe, %ecx
movl (%rsi,%rcx,4), %edx
movl %edx, -0x10(%rsi,%rcx,4)
movl $0x0, (%rsi,%rcx,4)
incq %rcx
cmpq $0x12, %rcx
jne 0xa4df
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x4(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
movq (%rsi), %rsi
testq %rsi, %rsi
jne 0xa4b1
jmp 0xa51d
movss %xmm0, 0x4(%rsp)
movq 0x18(%rbx), %r14
movq %r14, %rax
shrq %rax
movl %r14d, %r12d
andl $0x1, %r12d
orq %rax, %r12
movq 0x36a88(%rip), %rbx     # 0x40fc0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5240
movq %rax, %r15
leaq 0x2907b(%rip), %rsi     # 0x335c8
movl $0x24, %edx
movq %rax, %rdi
callq 0x5420
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x29088(%rip), %rsi     # 0x33610
movl $0x16, %edx
movq %rbx, %rdi
callq 0x5420
testq %r14, %r14
js 0xa5a4
xorps %xmm1, %xmm1
cvtsi2ss %r14, %xmm1
jmp 0xa5b0
xorps %xmm1, %xmm1
cvtsi2ss %r12, %xmm1
addss %xmm1, %xmm1
xorps %xmm0, %xmm0
je 0xa5c3
movss 0x4(%rsp), %xmm0
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x5610
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x5250