// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
callq 0x5060
movq %rax, 0xc8(%rsp)
movq 0x8(%r15), %rax
cmpq (%r15), %rax
je 0xea23
movq 0xa0(%rsp), %rax
movl (%rax), %eax
leal 0x1(,%rax,2), %ecx
addl %eax, %eax
movslq %ecx, %rbx
movslq %eax, %r12
movq 0x32a91(%rip), %r14     # 0x40fc0
leaq 0x2528f(%rip), %rsi     # 0x337c5
leaq 0x60(%rsp), %r13
leaq 0xd8(%rsp), %rbp
xorl %ecx, %ecx
movq %r15, 0x10(%rsp)
movl $0x19, %edx
movq %r14, %rdi
movq %rcx, %r14
callq 0x5420
movq (%r15), %rax
movq %r14, 0xd0(%rsp)
movq %r14, %r15
shlq $0x5, %r15
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq 0x32a44(%rip), %rdi     # 0x40fc0
callq 0x5420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %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 %r15, 0x18(%rsp)
addq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x7439
testl %eax, %eax
jne 0xea96
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), %r14
movq %r14, %rdi
callq 0x7710
movq 0x10(%rsp), %rax
movq (%rax), %rdi
addq 0x18(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x8076
testl %eax, %eax
jne 0xea41
xorl %r14d, %r14d
leaq 0x440(%rsp), %rdi
callq 0x5140
cmpl $-0x1, %eax
je 0xe88d
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), %r15
movq %r15, %rdi
callq 0x55f0
movq 0x10(%rsp), %rax
movq (%rax), %rsi
addq 0x18(%rsp), %rsi
leaq 0x2b8(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
movq %r15, %rcx
callq 0x6c5b
testl %eax, %eax
jne 0xe6fb
leaq 0x80(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8528
leaq 0x20(%rsp), %rdi
callq 0x6984
testl %eax, %eax
je 0xe74d
xorl %r15d, %r15d
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xe71a
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0xe73f
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r15b, %r15b
jne 0xe653
jmp 0xea41
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 %r13, %rsi
movl $0x4e, %edx
callq 0x56c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xe845
movq 0x68(%rsp), %rax
cmpq %rbx, %rax
setb %cl
cmpq %r12, %rax
sete %al
orb %cl, %al
jne 0xe789
xorl %r15d, %r15d
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x53e0
movq %rbp, %rdi
callq 0x68d8
movq %rbp, %rdi
leaq 0xb(%rsp), %rsi
callq 0x8a8b
movsbl 0xb(%rsp), %edx
movl 0xc(%rsp), %r8d
leaq 0xf8(%rsp), %rdi
movq %rbp, %rsi
movl %r14d, %ecx
callq 0xe3ba
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0xe830
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x53c0
incq %r15
movq 0x68(%rsp), %rax
subq %r12, %rax
cmpq %r15, %rax
ja 0xe7c8
jmp 0xe789
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xe861
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x53c0
incl %r14d
leaq 0x130(%rsp), %rdi
movq 0x32715(%rip), %rsi     # 0x40f88
callq 0x52c0
leaq 0x1b0(%rsp), %rdi
callq 0x5120
movb $0x1, %r15b
jmp 0xe6fe
leaq 0x2b8(%rsp), %rdi
callq 0x77fa
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 0xe054
testl %eax, %eax
leaq 0x40(%rsp), %rdi
jne 0xea34
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 0x8566
leaq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x2741e(%rip), %rsi     # 0x35d4e
callq 0x5710
movq 0xa0(%rsp), %rax
movl (%rax), %ecx
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
leaq 0x80(%rsp), %rdx
callq 0x9250
movl %eax, %r14d
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xe976
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x130(%rsp), %rdi
callq 0xdd56
leaq 0x3226e(%rip), %rax     # 0x40bf8
movq %rax, 0x440(%rsp)
leaq 0x32287(%rip), %rax     # 0x40c20
movq %rax, 0x450(%rsp)
movq $0x0, 0x448(%rsp)
leaq 0x2b8(%rsp), %rdi
leaq 0x3217c(%rip), %rsi     # 0x40b38
callq 0x7922
leaq 0x450(%rsp), %rdi
callq 0x5120
leaq 0xf8(%rsp), %rdi
callq 0xddf4
leaq 0x80(%rsp), %rdi
callq 0xab8e
testl %r14d, %r14d
jne 0xeaa3
movq 0xd0(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rax
subq (%r15), %rax
sarq $0x5, %rax
cmpq %rcx, %rax
movq 0x325aa(%rip), %r14     # 0x40fc0
leaq 0x24da8(%rip), %rsi     # 0x337c5
ja 0xe54a
movq 0xc8(%rsp), %rdi
callq 0x8320
xorl %eax, %eax
jmp 0xeaa8
leaq 0x130(%rsp), %rdi
callq 0xdd56
leaq 0x2b8(%rsp), %rdi
leaq 0x321a8(%rip), %rax     # 0x40bf8
movq %rax, 0x188(%rdi)
leaq 0x321c2(%rip), %rax     # 0x40c20
movq %rax, 0x198(%rdi)
movq $0x0, 0x190(%rdi)
leaq 0x320c1(%rip), %rsi     # 0x40b38
callq 0x7922
leaq 0x450(%rsp), %rdi
callq 0x5120
leaq 0xf8(%rsp), %rdi
callq 0xddf4
leaq 0x80(%rsp), %rdi
callq 0xab8e
movl $0x1, %eax
addq $0x558, %rsp            # imm = 0x558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xeabc
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xeae0
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x53c0
jmp 0xeae0
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0xdd56
jmp 0xebc7
movq %rax, %rbx
jmp 0xebd4
movq %rax, %rbx
jmp 0xebe1
jmp 0xeb0b
movq %rax, %rbx
jmp 0xeb65
jmp 0xeb13
movq %rax, %rbx
jmp 0xebc7
movq %rax, %rbx
jmp 0xeb86
jmp 0xeb1a
movq %rax, %rbx
jmp 0xeb49
jmp 0xeb21
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0xeb49
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xeb65
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x3241c(%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 0xeba2
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0xebc7
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x2b8(%rsp), %rdi
callq 0x7830
leaq 0xf8(%rsp), %rdi
callq 0xddf4
leaq 0x80(%rsp), %rdi
callq 0xab8e
movq %rbx, %rdi
callq 0x56a0