// File: /workspace/github_cpp_makefile_star2_O3/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 $0x528, %rsp            # imm = 0x528
movq %rsi, %r15
movq %rdi, %r14
callq 0x5060
movq %rax, %rbx
leaq -0x340(%rbp), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x5720
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
leaq -0x548(%rbp), %rdi
callq 0x5410
leaq -0x548(%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x7f3f
movl $0x1, %r14d
testl %eax, %eax
jne 0x19b38
leaq -0x548(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x9684
movq %rbx, -0xe8(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rbx
subq %rax, %rbx
shrq $0x5, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movq $0x0, -0xc0(%rbp)
movl $0x0, -0x140(%rbp)
testl %ebx, %ebx
jle 0x1968c
xorl %eax, %eax
leaq -0xd0(%rbp), %r14
leaq -0x140(%rbp), %r15
movq -0xc8(%rbp), %rsi
cmpq -0xc0(%rbp), %rsi
je 0x19667
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0xc8(%rbp)
jmp 0x19672
movq %r14, %rdi
movq %r15, %rdx
callq 0xdeae
movl -0x140(%rbp), %eax
incl %eax
movl %eax, -0x140(%rbp)
cmpl %ebx, %eax
jl 0x19648
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpq %rax, %rcx
je 0x196e5
movl $0x8, %r13d
xorl %r12d, %r12d
leaq 0x1ac00(%rip), %r14     # 0x342a1
leaq -0x340(%rbp), %r15
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5420
movq -0x70(%rbp), %rax
movq -0x8(%rax,%r13), %rsi
movq (%rax,%r13), %rdx
movq %r15, %rdi
callq 0x5420
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
incq %r12
sarq $0x5, %rax
addq $0x20, %r13
cmpq %r12, %rax
ja 0x196a8
movq -0x340(%rbp), %rax
leaq -0x340(%rbp), %rdi
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
leaq -0x340(%rbp), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rsp, -0xe0(%rbp)
leaq -0x110(%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
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rax
shrq $0x3f, %rax
shrq $0x20, %rcx
addl %eax, %ecx
movq %rcx, -0x108(%rbp)
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
js 0x1978f
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x197a7
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x19ed1(%rip), %xmm0    # 0x33680
callq 0x51e0
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0x100(%rbp)
movq %rdx, -0xd8(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0xf8(%rbp)
leaq -0x548(%rbp), %r13
leaq -0xb8(%rbp), %r14
movq %rbx, -0xf0(%rbp)
movq -0xf0(%rbp), %rbx
movq %r13, %rdi
movq -0xd8(%rbp), %rsi
movq -0x100(%rbp), %rdx
callq 0x5480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1998f
leaq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq -0xd8(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0x85fc
xorl %eax, %eax
movq %rax, -0xb8(%rbp)
movl %eax, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
movl %eax, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x8993
movq %r13, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x1993d
movq %r13, %rdi
leaq -0x29(%rbp), %rsi
callq 0x5500
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1993d
movq %r13, %rdi
movq %r12, %rsi
movq -0x108(%rbp), %rdx
callq 0x5480
movl -0x30(%rbp), %eax
addl %eax, %eax
cltq
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movslq %eax, %rdx
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
addq %r12, %rdx
leaq -0x50(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x85fc
movsbl -0x29(%rbp), %edx
leaq -0x140(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rcx
leaq -0xd0(%rbp), %r8
xorl %r9d, %r9d
pushq %rbx
pushq %rbx
callq 0xa00a
addq $0x10, %rsp
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1987c
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x1987c
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
movl $0xa, %edx
callq 0x5220
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x1996a
movq -0x98(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x197fe
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x197fe
movq -0x130(%rbp), %r15
testq %r15, %r15
leaq -0x340(%rbp), %r14
je 0x19afb
leaq -0xb8(%rbp), %rbx
leaq 0x1a8ed(%rip), %r12     # 0x342a1
leaq -0x2a(%rbp), %r13
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x8566
leaq -0x90(%rbp), %rdi
movq %rbx, %rsi
callq 0x6b7e
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x8566
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq %r14, %rdi
callq 0x5420
cmpq $0x0, -0x48(%rbp)
je 0x19a5f
xorl %r14d, %r14d
movl $0x1, %edx
leaq -0x340(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5420
movq -0x50(%rbp), %rax
movb (%rax,%r14), %al
movb %al, -0x2a(%rbp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5420
incq %r14
cmpq %r14, -0x48(%rbp)
ja 0x19a24
movq -0x340(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x340(%rbp), %r14
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x19aaa
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x19ac6
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x19ae8
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x53c0
movq (%r15), %r15
testq %r15, %r15
leaq -0xb8(%rbp), %rbx
jne 0x199b8
movq -0xe8(%rbp), %rdi
callq 0x8320
movq -0xe0(%rbp), %rsp
leaq -0x140(%rbp), %rdi
callq 0xddf4
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x19b35
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %r14d, %r14d
leaq -0x548(%rbp), %rdi
callq 0x50d0
leaq -0x70(%rbp), %rdi
callq 0xab8e
movq 0x2745c(%rip), %rax     # 0x40fb0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x338(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x340(%rbp,%rcx)
callq 0x5330
leaq -0x248(%rbp), %rdi
callq 0x5120
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x19bb9
movq %rax, %rbx
jmp 0x19cb9
jmp 0x19bc1
movq %rax, %rbx
jmp 0x19cad
jmp 0x19bc1
jmp 0x19bb9
movq %rax, %rbx
jmp 0x19c25
movq %rax, %rbx
jmp 0x19c09
jmp 0x19bb9
movq %rax, %rbx
jmp 0x19c86
movq %rax, %rbx
jmp 0x19c92
jmp 0x19c41
jmp 0x19bed
jmp 0x19c41
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x19c44
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x19c44
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x19c09
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x19c25
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x19c86
movq -0xa8(%rbp), %rsi
jmp 0x19c7e
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x19c6a
movq -0x98(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
movq $0x0, -0xb8(%rbp)
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x19c86
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
leaq -0x140(%rbp), %rdi
callq 0xddf4
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x19cad
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
leaq -0x548(%rbp), %rdi
callq 0x50d0
leaq -0x70(%rbp), %rdi
callq 0xab8e
leaq -0x340(%rbp), %rdi
callq 0x55a0
movq %rbx, %rdi
callq 0x56a0
nop