// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_humanise.cpp
// Function: humaniseKmers(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int humaniseKmers(const std::string & kmerfile, const std::string & outputfile){
	
	const std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();//start the clock

	std::ofstream humanfile(outputfile);

	std::vector < std::string > sampleNames;
	int kmersize;
	std::ifstream fileStream;

	if (openFileStream(kmerfile, fileStream, false)){return 1;};//open the file
	
	readKmerHeader(fileStream, kmersize, sampleNames);//read the header to get the kmer size and sample names

	int numSamples=sampleNames.size();

	std::vector < int > fileInclude;
	for (int i=0; i<numSamples; ++i){ //put the index of all sample names that are to be included into a vector
		fileInclude.push_back(i);
	}

	for (int i=0; i<sampleNames.size(); ++i){//print the sample names with namesPerLine sample names per line
		humanfile << "\t" << sampleNames[i];
	}
	humanfile << std::endl;

	std::unordered_map < std::string, std::string > kmerMap;

	char base;
	char kmerbuffer[kmersize*2/3];
	char asciibuffer[int(ceil(float(sampleNames.size())/6))];

	while (fileStream.read(asciibuffer, sizeof(asciibuffer))){//read the next ascii bitstring
		std::string asciibits (asciibuffer, sizeof(asciibuffer));
		std::vector < bool > mybits;
		vectorbool_from_ascii(asciibits, mybits);//convert the ascii representation of the taxa to a vector of bools

		while (fileStream.peek()!='\n' && fileStream.get(base)){
			fileStream.read(kmerbuffer, sizeof(kmerbuffer));
			std::string kmer (kmerbuffer, kmersize*2/3);
			
			addKmerToStringMap(kmerMap, kmer, base, mybits, fileInclude, 0, numSamples, numSamples);
			
    	}
    	fileStream.ignore(256,'\n');//skip the end ofline character
    }

    for (std::unordered_map < std::string, std::string >::iterator it=kmerMap.begin(); it!=kmerMap.end(); ++it){
    	std::string asciikmer=it->first;
		std::string kmer=codons_from_ascii(asciikmer);
		std::string allbases=it->second;
		humanfile << kmer;

		for (int i=0; i<allbases.length(); ++i){
			humanfile << "\t" << allbases[i];
		}
		humanfile << std::endl;

	}

	printDuration(start);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x518, %rsp            # imm = 0x518
movq %rsi, %r14
movq %rdi, %rbx
callq 0x5050
movq %rax, %r15
leaq -0x338(%rbp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x5740
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
leaq -0x540(%rbp), %rdi
callq 0x5450
leaq -0x540(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x73fe
testl %eax, %eax
je 0x14649
pushq $0x1
popq %rbx
jmp 0x149cf
leaq -0x540(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x832b
movq -0x48(%rbp), %r12
subq -0x50(%rbp), %r12
shrq $0x5, %r12
leaq -0x100(%rbp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
xorl %eax, %eax
leaq -0x138(%rbp), %r14
movl %eax, -0x138(%rbp)
cmpl %r12d, %eax
jge 0x146a4
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbe78
movl -0x138(%rbp), %eax
incl %eax
jmp 0x14684
movq %r15, -0xb0(%rbp)
xorl %r15d, %r15d
leaq 0x11c0c(%rip), %rbx     # 0x262c1
leaq -0x338(%rbp), %r14
xorl %r13d, %r13d
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x5, %rax
cmpq %r13, %rax
jbe 0x146f3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53f0
movq -0x50(%rbp), %rsi
addq %r15, %rsi
movq %rax, %rdi
callq 0x53a0
incq %r13
addq $0x20, %r15
jmp 0x146bf
leaq -0x338(%rbp), %rdi
callq 0x51c0
movq %rsp, -0xa8(%rbp)
leaq -0x108(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movl -0x30(%rbp), %eax
addl %eax, %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
movl %eax, %r14d
leaq 0xf(%r14), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
sarq $0x5, %rax
js 0x1475f
cvtsi2ss %rax, %xmm0
jmp 0x14774
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x10ef4(%rip), %xmm0    # 0x25670
callq 0x5220
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0xc0(%rbp)
movq %rdx, -0x58(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0xb8(%rbp)
leaq -0x540(%rbp), %rbx
leaq -0xa0(%rbp), %r13
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
movq -0xc0(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x148ec
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x7828
andq $0x0, -0x80(%rbp)
andl $0x0, -0x78(%rbp)
andq $0x0, -0x70(%rbp)
andl $0x0, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x7bb6
movq %rbx, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x148c2
movq %rbx, %rdi
leaq -0x29(%rbp), %rsi
callq 0x5550
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x148c2
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x54c0
movl -0x30(%rbp), %eax
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
addq %r15, %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x7828
movsbl -0x29(%rbp), %edx
leaq -0x138(%rbp), %rdi
movq %r13, %rsi
leaq -0x80(%rbp), %rcx
leaq -0x100(%rbp), %r8
xorl %r9d, %r9d
pushq %r12
pushq %r12
callq 0x88cb
popq %rax
popq %rcx
movq %r13, %rdi
callq 0x5778
jmp 0x1482b
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
pushq $0xa
popq %rdx
callq 0x5260
leaq -0x80(%rbp), %rdi
callq 0x908a
leaq -0xe0(%rbp), %rdi
callq 0x5778
jmp 0x147ba
movq -0x128(%rbp), %r15
leaq -0xe0(%rbp), %r14
leaq -0x338(%rbp), %r12
leaq 0x119b9(%rip), %r13     # 0x262c1
testq %r15, %r15
je 0x149a2
leaq 0x8(%r15), %rsi
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x5540
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6714
leaq 0x28(%r15), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x5540
movq %r12, %rdi
movq %r14, %rsi
callq 0x53a0
xorl %ebx, %ebx
cmpq %rbx, -0x98(%rbp)
jbe 0x14975
movq %r12, %rdi
movq %r13, %rsi
callq 0x53f0
movq -0xa0(%rbp), %rcx
movsbl (%rcx,%rbx), %esi
movq %rax, %rdi
callq 0x5480
incq %rbx
jmp 0x14949
movq %r12, %rdi
callq 0x51c0
leaq -0xa0(%rbp), %rdi
callq 0x5778
movq %r14, %rdi
callq 0x5778
leaq -0x80(%rbp), %rdi
callq 0x5778
movq (%r15), %r15
jmp 0x14908
movq -0xb0(%rbp), %rdi
callq 0x7657
movq -0xa8(%rbp), %rsp
leaq -0x138(%rbp), %rdi
callq 0xbfa8
leaq -0x100(%rbp), %rdi
callq 0xc052
xorl %ebx, %ebx
leaq -0x540(%rbp), %rdi
callq 0x50e0
leaq -0x50(%rbp), %rdi
callq 0x911e
leaq -0x338(%rbp), %rdi
callq 0x55c0
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14a25
jmp 0x14a2e
movq %rax, %rbx
jmp 0x14aa3
movq %rax, %rbx
jmp 0x14a97
movq %rax, %rbx
jmp 0x14a76
movq %rax, %rbx
jmp 0x14a6a
jmp 0x14a25
jmp 0x14a25
jmp 0x14a2e
movq %rax, %rbx
jmp 0x14a7f
jmp 0x14a5b
jmp 0x14a46
movq %rax, %rbx
jmp 0x14a8b
jmp 0x14a46
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x5778
jmp 0x14a49
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x908a
leaq -0xe0(%rbp), %rdi
jmp 0x14a7a
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x5778
leaq -0xe0(%rbp), %rdi
callq 0x5778
leaq -0x80(%rbp), %rdi
callq 0x5778
leaq -0x138(%rbp), %rdi
callq 0xbfa8
leaq -0x100(%rbp), %rdi
callq 0xc052
leaq -0x540(%rbp), %rdi
callq 0x50e0
leaq -0x50(%rbp), %rdi
callq 0x911e
leaq -0x338(%rbp), %rdi
callq 0x55c0
movq %rbx, %rdi
callq 0x56c0