// File: 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
subq $0x670, %rsp            # imm = 0x670
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
callq 0x6080
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x10, %edx
callq 0x69b0
leaq -0x238(%rbp), %rdi
callq 0x1ab40
leaq -0x448(%rbp), %rdi
callq 0x65f0
jmp 0x32d1b
movq -0x10(%rbp), %rdi
leaq -0x448(%rbp), %rsi
xorl %edx, %edx
callq 0xa4f0
movl %eax, -0x5d4(%rbp)
jmp 0x32d35
movl -0x5d4(%rbp), %eax
cmpl $0x0, %eax
je 0x32d84
movl $0x1, -0x4(%rbp)
movl $0x1, -0x458(%rbp)
jmp 0x334f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x3352d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x33521
leaq -0x448(%rbp), %rdi
leaq -0x23c(%rbp), %rsi
leaq -0x238(%rbp), %rdx
callq 0xcd40
jmp 0x32da0
leaq -0x238(%rbp), %rdi
callq 0xb670
movl %eax, -0x45c(%rbp)
leaq -0x478(%rbp), %rdi
callq 0x1ac10
movl $0x0, -0x47c(%rbp)
movl -0x47c(%rbp), %eax
cmpl -0x45c(%rbp), %eax
jge 0x32e15
leaq -0x478(%rbp), %rdi
leaq -0x47c(%rbp), %rsi
callq 0x1ac30
jmp 0x32deb
jmp 0x32ded
movl -0x47c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x47c(%rbp)
jmp 0x32dc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x334e7
movl $0x0, -0x480(%rbp)
movslq -0x480(%rbp), %rax
movq %rax, -0x5e0(%rbp)
leaq -0x238(%rbp), %rdi
callq 0xb670
movq %rax, %rcx
movq -0x5e0(%rbp), %rax
cmpq %rcx, %rax
jae 0x32e9b
leaq 0x27462(%rip), %rsi     # 0x5a2b1
leaq -0x220(%rbp), %rdi
callq 0x6520
movq %rax, -0x5e8(%rbp)
jmp 0x32e64
movslq -0x480(%rbp), %rsi
leaq -0x238(%rbp), %rdi
callq 0x1ab60
movq -0x5e8(%rbp), %rdi
movq %rax, %rsi
callq 0x64d0
jmp 0x32e88
jmp 0x32e8a
movl -0x480(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x480(%rbp)
jmp 0x32e1f
movq 0x460ee(%rip), %rsi     # 0x78f90
leaq -0x220(%rbp), %rdi
callq 0x6570
jmp 0x32eb0
leaq -0x4b8(%rbp), %rdi
callq 0x1abf0
movl -0x23c(%rbp), %eax
addl %eax, %eax
cltq
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movq %rax, -0x600(%rbp)
movq %rsp, %rcx
movq %rcx, -0x4c8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x5f8(%rbp)
movq %rcx, %rsp
movq %rax, -0x4d0(%rbp)
leaq -0x238(%rbp), %rdi
callq 0xb670
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x5f0(%rbp)
testq %rax, %rax
movss %xmm0, -0x5ec(%rbp)
js 0x32f5a
movss -0x5f0(%rbp), %xmm0
movss %xmm0, -0x5ec(%rbp)
movss -0x5ec(%rbp), %xmm0
movss 0x26712(%rip), %xmm1    # 0x5967c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x60b0
cvttsd2si %xmm0, %eax
movq %rax, -0x610(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x608(%rbp)
movq %rcx, %rsp
movq %rax, -0x4d8(%rbp)
movq -0x610(%rbp), %rdx
movq -0x608(%rbp), %rsi
leaq -0x448(%rbp), %rdi
callq 0x6680
movq %rax, -0x618(%rbp)
jmp 0x32fc7
movq -0x618(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, -0x619(%rbp)
jmp 0x32fe5
movb -0x619(%rbp), %al
testb $0x1, %al
jne 0x32ff4
jmp 0x33297
leaq -0x4f9(%rbp), %rdi
movq %rdi, -0x628(%rbp)
callq 0x6910
movq -0x608(%rbp), %rsi
movq -0x610(%rbp), %rdx
movq -0x628(%rbp), %rcx
leaq -0x4f8(%rbp), %rdi
callq 0x65e0
jmp 0x3302a
leaq -0x4f9(%rbp), %rdi
callq 0x65a0
leaq -0x528(%rbp), %rdi
movq %rdi, -0x630(%rbp)
callq 0x1acc0
movq -0x630(%rbp), %rsi
leaq -0x4f8(%rbp), %rdi
callq 0xbd50
jmp 0x3305e
jmp 0x33060
leaq -0x448(%rbp), %rdi
callq 0x61c0
movl %eax, -0x634(%rbp)
jmp 0x33074
movl -0x634(%rbp), %ecx
xorl %eax, %eax
cmpl $0xa, %ecx
movb %al, -0x635(%rbp)
je 0x330cf
leaq -0x448(%rbp), %rdi
leaq -0x4b9(%rbp), %rsi
callq 0x6720
movq %rax, -0x640(%rbp)
jmp 0x330a3
movq -0x640(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, -0x641(%rbp)
jmp 0x330c1
movb -0x641(%rbp), %al
movb %al, -0x635(%rbp)
jmp 0x330cf
movb -0x635(%rbp), %al
testb $0x1, %al
jne 0x330de
jmp 0x33245
movq -0x600(%rbp), %rdx
movq -0x5f8(%rbp), %rsi
leaq -0x448(%rbp), %rdi
callq 0x6680
jmp 0x330fa
movl -0x23c(%rbp), %eax
addl %eax, %eax
cltq
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
cltq
movq %rax, -0x658(%rbp)
leaq -0x549(%rbp), %rdi
movq %rdi, -0x650(%rbp)
callq 0x6910
movq -0x5f8(%rbp), %rsi
movq -0x658(%rbp), %rdx
movq -0x650(%rbp), %rcx
leaq -0x548(%rbp), %rdi
callq 0x65e0
jmp 0x33157
leaq -0x549(%rbp), %rdi
callq 0x65a0
movsbl -0x4b9(%rbp), %edx
movl -0x45c(%rbp), %ecx
subq $0x10, %rsp
movq %rsp, %rax
movl %ecx, 0x8(%rax)
movl %ecx, (%rax)
leaq -0x4b8(%rbp), %rdi
leaq -0x548(%rbp), %rsi
leaq -0x528(%rbp), %rcx
leaq -0x478(%rbp), %r8
xorl %r9d, %r9d
callq 0xda00
addq $0x10, %rsp
jmp 0x331a6
leaq -0x548(%rbp), %rdi
callq 0x6a08
jmp 0x33060
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x334db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
leaq -0x4f9(%rbp), %rdi
callq 0x65a0
jmp 0x334db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x3327a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
leaq -0x549(%rbp), %rdi
callq 0x65a0
jmp 0x3327a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
leaq -0x548(%rbp), %rdi
callq 0x6a08
jmp 0x3327a
leaq -0x448(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
movl $0xa, %edx
callq 0x6340
jmp 0x3325d
leaq -0x528(%rbp), %rdi
callq 0xf720
leaq -0x4f8(%rbp), %rdi
callq 0x6a08
jmp 0x32fa4
leaq -0x528(%rbp), %rdi
callq 0xf720
leaq -0x4f8(%rbp), %rdi
callq 0x6a08
jmp 0x334db
leaq -0x4b8(%rbp), %rdi
callq 0xff80
movq %rax, -0x558(%rbp)
leaq -0x4b8(%rbp), %rdi
callq 0xf7c0
movq %rax, -0x560(%rbp)
leaq -0x558(%rbp), %rdi
leaq -0x560(%rbp), %rsi
callq 0xf790
testb $0x1, %al
jne 0x332d9
jmp 0x3348d
leaq -0x558(%rbp), %rdi
callq 0xf880
movq %rax, %rsi
leaq -0x580(%rbp), %rdi
callq 0x6230
jmp 0x332f6
leaq -0x5a0(%rbp), %rdi
leaq -0x580(%rbp), %rsi
callq 0x8290
jmp 0x3330b
leaq -0x558(%rbp), %rdi
callq 0xf880
movq %rax, %rsi
addq $0x20, %rsi
leaq -0x5c0(%rbp), %rdi
callq 0x6230
jmp 0x3332c
leaq -0x220(%rbp), %rdi
leaq -0x5a0(%rbp), %rsi
callq 0x64d0
jmp 0x33341
movl $0x0, -0x5c4(%rbp)
movslq -0x5c4(%rbp), %rax
movq %rax, -0x660(%rbp)
leaq -0x5c0(%rbp), %rdi
callq 0x67e0
movq %rax, %rcx
movq -0x660(%rbp), %rax
cmpq %rcx, %rax
jae 0x33429
leaq 0x26f32(%rip), %rsi     # 0x5a2b1
leaq -0x220(%rbp), %rdi
callq 0x6520
movq %rax, -0x668(%rbp)
jmp 0x33394
movslq -0x5c4(%rbp), %rsi
leaq -0x5c0(%rbp), %rdi
callq 0x69e0
movq %rax, -0x670(%rbp)
jmp 0x333b0
movq -0x668(%rbp), %rdi
movq -0x670(%rbp), %rax
movsbl (%rax), %esi
callq 0x6620
jmp 0x333c8
jmp 0x333ca
movl -0x5c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c4(%rbp)
jmp 0x3334b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x3347f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x33473
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
leaq -0x5c0(%rbp), %rdi
callq 0x6a08
jmp 0x33473
movq 0x45b60(%rip), %rsi     # 0x78f90
leaq -0x220(%rbp), %rdi
callq 0x6570
jmp 0x3343e
leaq -0x5c0(%rbp), %rdi
callq 0x6a08
leaq -0x5a0(%rbp), %rdi
callq 0x6a08
leaq -0x580(%rbp), %rdi
callq 0x6a08
leaq -0x558(%rbp), %rdi
callq 0x1a9d0
jmp 0x332aa
leaq -0x5a0(%rbp), %rdi
callq 0x6a08
leaq -0x580(%rbp), %rdi
callq 0x6a08
jmp 0x334db
movq -0x20(%rbp), %rax
movq %rax, -0x5d0(%rbp)
movq -0x5d0(%rbp), %rdi
callq 0xa8f0
jmp 0x334a6
movl $0x0, -0x4(%rbp)
movl $0x1, -0x458(%rbp)
movq -0x4c8(%rbp), %rax
movq %rax, %rsp
leaq -0x4b8(%rbp), %rdi
callq 0x1aeb0
leaq -0x478(%rbp), %rdi
callq 0x1aa00
jmp 0x334f5
leaq -0x4b8(%rbp), %rdi
callq 0x1aeb0
leaq -0x478(%rbp), %rdi
callq 0x1aa00
jmp 0x33521
leaq -0x448(%rbp), %rdi
callq 0x6140
leaq -0x238(%rbp), %rdi
callq 0xfe90
leaq -0x220(%rbp), %rdi
callq 0x67d0
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
leaq -0x448(%rbp), %rdi
callq 0x6140
leaq -0x238(%rbp), %rdi
callq 0xfe90
leaq -0x220(%rbp), %rdi
callq 0x67d0
movq -0x450(%rbp), %rdi
callq 0x6900
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)