// File: /workspace/github_cpp_makefile_star2_O1/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 $0x538, %rsp            # imm = 0x538
movq %rsi, %r15
movq %rdi, %r14
callq 0x5060
movq %rax, %rbx
leaq -0x350(%rbp), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x5720
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
leaq -0x558(%rbp), %rdi
callq 0x5410
leaq -0x558(%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x7f73
movl $0x1, %r14d
testl %eax, %eax
jne 0x19d31
leaq -0x558(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x96e2
movq %rbx, -0xf8(%rbp)
movq -0x98(%rbp), %rbx
subq -0xa0(%rbp), %rbx
shrq $0x5, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0x150(%rbp)
testl %ebx, %ebx
jle 0x19875
xorl %eax, %eax
leaq -0xe0(%rbp), %r14
leaq -0x150(%rbp), %r15
movq -0xd8(%rbp), %rsi
cmpq -0xd0(%rbp), %rsi
je 0x19858
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0xd8(%rbp)
jmp 0x19863
movq %r14, %rdi
movq %r15, %rdx
callq 0xe01e
movl -0x150(%rbp), %eax
incl %eax
movl %eax, -0x150(%rbp)
cmpl %ebx, %eax
jl 0x19839
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
je 0x198e2
movl $0x8, %r13d
xorl %r12d, %r12d
leaq 0x1aa1c(%rip), %r14     # 0x342b1
leaq -0x350(%rbp), %r15
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5420
movq -0xa0(%rbp), %rax
movq -0x8(%rax,%r13), %rsi
movq (%rax,%r13), %rdx
movq %r15, %rdi
callq 0x5420
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
incq %r12
sarq $0x5, %rax
addq $0x20, %r13
cmpq %r12, %rax
ja 0x1989c
movq -0x350(%rbp), %rax
leaq -0x350(%rbp), %rdi
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
leaq -0x350(%rbp), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rsp, -0xf0(%rbp)
leaq -0x120(%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 -0x5c(%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, -0x118(%rbp)
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
sarq $0x5, %rax
js 0x19992
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x199aa
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x19cce(%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, -0x110(%rbp)
movq %rdx, -0xe8(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0x108(%rbp)
leaq -0x558(%rbp), %r13
leaq -0x58(%rbp), %r14
movq %rbx, -0x100(%rbp)
movq -0x100(%rbp), %rbx
movq %r13, %rdi
movq -0xe8(%rbp), %rsi
movq -0x110(%rbp), %rdx
callq 0x5480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x19b97
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
movq -0xe8(%rbp), %rsi
movq -0x108(%rbp), %rdx
callq 0x862a
xorl %eax, %eax
movq %rax, -0x58(%rbp)
movl %eax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movl %eax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x89ef
movq %r13, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x19b31
movq %r13, %rdi
leaq -0x29(%rbp), %rsi
callq 0x5500
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x19b31
movq %r13, %rdi
movq %r12, %rsi
movq -0x118(%rbp), %rdx
callq 0x5480
movl -0x5c(%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 -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
addq %r12, %rdx
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x862a
movsbl -0x29(%rbp), %edx
leaq -0x150(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rcx
leaq -0xe0(%rbp), %r8
xorl %r9d, %r9d
pushq %rbx
pushq %rbx
callq 0xa08b
addq $0x10, %rsp
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x19a70
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x19a70
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
movl $0xa, %edx
callq 0x5220
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x19b6c
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %eax, %eax
movq %rax, -0x58(%rbp)
movl %eax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movl %eax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x199fe
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x199fe
movq -0x140(%rbp), %r15
testq %r15, %r15
leaq -0x350(%rbp), %r14
je 0x19cf4
leaq -0x58(%rbp), %rbx
leaq 0x1a6f8(%rip), %r12     # 0x342b1
leaq -0x2a(%rbp), %r13
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x859a
leaq -0xc0(%rbp), %rdi
movq %rbx, %rsi
callq 0x6bd6
leaq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x859a
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq %r14, %rdi
callq 0x5420
cmpq $0x0, -0x78(%rbp)
je 0x19c5e
xorl %r14d, %r14d
movl $0x1, %edx
leaq -0x350(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5420
movq -0x80(%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, -0x78(%rbp)
ja 0x19c23
movq -0x350(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x350(%rbp), %r14
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x19ca9
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x19ccb
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x19ce4
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x53c0
movq (%r15), %r15
testq %r15, %r15
leaq -0x58(%rbp), %rbx
jne 0x19bbd
movq -0xf8(%rbp), %rdi
callq 0x8354
movq -0xf0(%rbp), %rsp
leaq -0x150(%rbp), %rdi
callq 0xdf64
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x19d2e
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %r14d, %r14d
leaq -0x558(%rbp), %rdi
callq 0x50d0
leaq -0xa0(%rbp), %rdi
callq 0xabee
movq 0x27260(%rip), %rax     # 0x40fb0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x348(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x350(%rbp,%rcx)
callq 0x5330
leaq -0x258(%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 0x19db5
movq %rax, %rbx
jmp 0x19ebb
jmp 0x19dbd
movq %rax, %rbx
jmp 0x19eaf
jmp 0x19dbd
jmp 0x19db5
movq %rax, %rbx
jmp 0x19e27
movq %rax, %rbx
jmp 0x19e05
jmp 0x19db5
movq %rax, %rbx
jmp 0x19e88
movq %rax, %rbx
jmp 0x19e94
jmp 0x19e3a
jmp 0x19de9
jmp 0x19e3a
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x19e3d
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x19e3d
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x19e05
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x19e27
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x53c0
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x19e88
movq -0x48(%rbp), %rsi
jmp 0x19e80
movq %rax, %rbx
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x19e66
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %eax, %eax
movq %rax, -0x58(%rbp)
movl %eax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movl %eax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x19e88
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x53c0
leaq -0x150(%rbp), %rdi
callq 0xdf64
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x19eaf
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
leaq -0x558(%rbp), %rdi
callq 0x50d0
leaq -0xa0(%rbp), %rdi
callq 0xabee
leaq -0x350(%rbp), %rdi
callq 0x55a0
movq %rbx, %rdi
callq 0x56a0
nop