// File: /workspace/github_cpp_makefile_star2_O1/simonrharris[P]SKA/src/sk_alleles.cpp
// Function: allelesToKmers(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, long const&)
int allelesToKmers(const std::vector < std::string > & alleles, const long & kmerlen)
{

	const std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();//start the clock

	int substringlength=(kmerlen*2)+1;

	for (int s = 0; s < alleles.size(); ++s){

		std::cout << "Creating split kmers for " << alleles[s] << std::endl;

		int numAlleles=0;
		std::vector < std::string > alleleNames;	

		if(countSequencesinFasta(alleles[s], numAlleles)){return 1;}//count the number of sequences in the file

		// Create the kmer map
		std::unordered_map < std::string, std::string > kmerMap;

		int alleleNumber=0;
		
		igzstream gzfileStream;
		if(openGzFileStream(alleles[s], gzfileStream)){return 1;}

		while (gzfileStream.peek()!=EOF){

			std::string alleleName;
			std::string sequence;
			sequence.reserve(10000);

			if(readNextFastaSequence(gzfileStream, alleles[s], alleleName, sequence)){return 1;}

			alleleNames.push_back(alleleName);

			if(IUPACToN(sequence)){return 1;}//convert any IUPAC charaxcters to N and reject any unrecognised characters
			
			std::stringstream sequencestream;
			sequencestream << sequence;//convert the sequence to stringstream
			std::string subsequence;
			
			while (std::getline(sequencestream, subsequence, 'N')){
				
				if (subsequence.length()<substringlength){
					continue;
				}

				for (int i=0; i<subsequence.length()-(substringlength-1); ++i){
					std::string kmer=subsequence.substr(i,substringlength);
					
					reverseComplementIfMin(kmer);
					
					char base;
					extractMiddleBase(kmer, base);
					
					addKmerToStringMap(kmerMap, kmer, base, alleleNumber, numAlleles);
				}
		
    		}
    		alleleNumber++;
		}
 		gzfileStream.close();//close the file
	
 		std::unordered_map < std::vector < bool >,  std::vector < std::string > > revKmerMap;

		if(reverseKmerMap(kmerMap, revKmerMap, numAlleles)){return 1;}

		if(printMergedKmerFile(revKmerMap, alleles[s]+".skf", alleleNames, kmerlen)){return 1;}

	}

	printDuration(start);//stop the clock
	
	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x558, %rsp            # imm = 0x558
movq %rsi, 0xa0(%rsp)
movq %rdi, %r13
callq 0x5060
movq %rax, 0xc8(%rsp)
movq 0x8(%r13), %rax
cmpq (%r13), %rax
je 0xebe5
movq 0xa0(%rsp), %rax
movl (%rax), %eax
leal 0x1(,%rax,2), %ecx
addl %eax, %eax
movslq %ecx, %rbp
movslq %eax, %r14
movq 0x328cc(%rip), %rbx     # 0x40fc0
leaq 0x250ca(%rip), %rsi     # 0x337c5
leaq 0x60(%rsp), %r15
leaq 0xd8(%rsp), %r12
xorl %ecx, %ecx
movq %r13, 0x10(%rsp)
movl $0x19, %edx
movq %rbx, %rdi
movq %rcx, %rbx
callq 0x5420
movq (%r13), %rax
movq %rbx, 0xd0(%rsp)
movq %rbx, %r13
shlq $0x5, %r13
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
movq 0x3287e(%rip), %rdi     # 0x40fc0
callq 0x5420
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
callq 0x5250
movl $0x0, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r13, 0x18(%rsp)
addq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x746b
testl %eax, %eax
jne 0xec51
leaq 0x128(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq $0x1, 0x100(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0x118(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x2b8(%rsp), %rbx
movq %rbx, %rdi
callq 0x7742
movq 0x10(%rsp), %rax
movq (%rax), %rdi
addq 0x18(%rsp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x80aa
testl %eax, %eax
jne 0xec03
xorl %ebx, %ebx
leaq 0x440(%rsp), %rdi
callq 0x5140
cmpl $-0x1, %eax
je 0xea50
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
xorl %ecx, %ecx
movq %rcx, 0xb0(%rsp)
movb %cl, 0xb8(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movb %cl, 0x30(%rsp)
movl $0x2710, %esi           # imm = 0x2710
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x55f0
movq 0x10(%rsp), %rax
movq (%rax), %rsi
addq 0x18(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
movq %r13, %rcx
callq 0x6ca1
testl %eax, %eax
jne 0xe8c0
leaq 0x80(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x855c
leaq 0x20(%rsp), %rdi
callq 0x69ab
testl %eax, %eax
je 0xe912
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xe8df
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0xe904
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r13b, %r13b
jne 0xe818
jmp 0xec03
leaq 0x130(%rsp), %rdi
callq 0x5280
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x140(%rsp), %rdi
callq 0x5420
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq $0x0, 0x68(%rsp)
movb $0x0, 0x70(%rsp)
leaq 0x130(%rsp), %rdi
movq %r15, %rsi
movl $0x4e, %edx
callq 0x56c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xea09
movq 0x68(%rsp), %rax
cmpq %rbp, %rax
setb %cl
cmpq %r14, %rax
sete %al
orb %cl, %al
jne 0xe94e
xorl %r13d, %r13d
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x53e0
movq %r12, %rdi
callq 0x6905
movq %r12, %rdi
leaq 0xb(%rsp), %rsi
callq 0x8ae7
movsbl 0xb(%rsp), %edx
movl 0xc(%rsp), %r8d
leaq 0xf8(%rsp), %rdi
movq %r12, %rsi
movl %ebx, %ecx
callq 0xe57e
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0xe9f4
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x53c0
incq %r13
movq 0x68(%rsp), %rax
subq %r14, %rax
cmpq %r13, %rax
ja 0xe98d
jmp 0xe94e
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xea25
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x53c0
incl %ebx
leaq 0x130(%rsp), %rdi
movq 0x32552(%rip), %rsi     # 0x40f88
callq 0x52c0
leaq 0x1b0(%rsp), %rdi
callq 0x5120
movb $0x1, %r13b
jmp 0xe8c3
leaq 0x2b8(%rsp), %rdi
callq 0x782c
leaq 0x160(%rsp), %rax
movq %rax, 0x130(%rsp)
movq $0x1, 0x138(%rsp)
leaq 0x140(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x3f800000, 0x150(%rsp) # imm = 0x3F800000
movups %xmm0, 0x18(%rax)
movl 0xc(%rsp), %edx
leaq 0xf8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xe1c4
testl %eax, %eax
leaq 0x40(%rsp), %rdi
jne 0xebf6
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq 0x50(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq 0x18(%rsp), %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
callq 0x859a
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
leaq 0x2726b(%rip), %rsi     # 0x35d5e
callq 0x5710
movq 0xa0(%rsp), %rax
movl (%rax), %ecx
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
leaq 0x80(%rsp), %rdx
callq 0x92ac
movl %eax, %ebx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xeb38
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x130(%rsp), %rdi
callq 0xdec6
leaq 0x320ac(%rip), %rax     # 0x40bf8
movq %rax, 0x440(%rsp)
leaq 0x320c5(%rip), %rax     # 0x40c20
movq %rax, 0x450(%rsp)
movq $0x0, 0x448(%rsp)
leaq 0x2b8(%rsp), %rdi
leaq 0x31fba(%rip), %rsi     # 0x40b38
callq 0x7954
leaq 0x450(%rsp), %rdi
callq 0x5120
leaq 0xf8(%rsp), %rdi
callq 0xdf64
leaq 0x80(%rsp), %rdi
callq 0xabee
testl %ebx, %ebx
jne 0xec5e
movq 0xd0(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rax
subq (%r13), %rax
sarq $0x5, %rax
cmpq %rcx, %rax
movq 0x323e8(%rip), %rbx     # 0x40fc0
leaq 0x24be6(%rip), %rsi     # 0x337c5
ja 0xe70f
movq 0xc8(%rsp), %rdi
callq 0x8354
xorl %eax, %eax
jmp 0xec63
leaq 0x130(%rsp), %rdi
callq 0xdec6
leaq 0x450(%rsp), %rbx
leaq 0x31fe6(%rip), %rax     # 0x40bf8
movq %rax, -0x10(%rbx)
leaq 0x32003(%rip), %rax     # 0x40c20
movq %rax, (%rbx)
movq $0x0, -0x8(%rbx)
leaq 0x31f09(%rip), %rsi     # 0x40b38
leaq 0x2b8(%rsp), %rdi
callq 0x7954
movq %rbx, %rdi
callq 0x5120
leaq 0xf8(%rsp), %rdi
callq 0xdf64
leaq 0x80(%rsp), %rdi
callq 0xabee
movl $0x1, %eax
addq $0x558, %rsp            # imm = 0x558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xec77
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xec9b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x53c0
jmp 0xec9b
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0xdec6
jmp 0xed82
movq %rax, %rbx
jmp 0xed8f
movq %rax, %rbx
jmp 0xed9c
jmp 0xecc6
movq %rax, %rbx
jmp 0xed20
jmp 0xecce
movq %rax, %rbx
jmp 0xed82
movq %rax, %rbx
jmp 0xed41
jmp 0xecd5
movq %rax, %rbx
jmp 0xed04
jmp 0xecdc
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0xed04
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xed20
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x32261(%rip), %rsi     # 0x40f88
leaq 0x130(%rsp), %rdi
callq 0x52c0
leaq 0x1b0(%rsp), %rdi
callq 0x5120
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xed5d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0xed82
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x2b8(%rsp), %rdi
callq 0x7862
leaq 0xf8(%rsp), %rdi
callq 0xdf64
leaq 0x80(%rsp), %rdi
callq 0xabee
movq %rbx, %rdi
callq 0x56a0