// File: /workspace/github_cpp_makefile_star2_O3/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 0x36bc1(%rip), %r15     # 0x40fc0
leaq 0x291e7(%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 0xa4ac
movss %xmm0, 0x4(%rsp)
movdqu 0x38(%rsi), %xmm0
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %eax
cmpl (%r14), %eax
jge 0xa47c
movq (%rsi), %r15
movq %rbx, %rdi
callq 0xbfc6
movq %r15, %rsi
jmp 0xa4a5
movups 0x38(%rsi), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movups %xmm0, 0x28(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rsi)
movss 0x4(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movq (%rsi), %rsi
testq %rsi, %rsi
jne 0xa44c
jmp 0xa4b2
movss %xmm0, 0x4(%rsp)
movq 0x18(%rbx), %r14
movq %r14, %rax
shrq %rax
movl %r14d, %r12d
andl $0x1, %r12d
orq %rax, %r12
movq 0x36af3(%rip), %rbx     # 0x40fc0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5240
movq %rax, %r15
leaq 0x290e6(%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 0x290f3(%rip), %rsi     # 0x33610
movl $0x16, %edx
movq %rbx, %rdi
callq 0x5420
testq %r14, %r14
js 0xa539
xorps %xmm1, %xmm1
cvtsi2ss %r14, %xmm1
jmp 0xa545
xorps %xmm1, %xmm1
cvtsi2ss %r12, %xmm1
addss %xmm1, %xmm1
xorps %xmm0, %xmm0
je 0xa558
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