// File: 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
movq %rsp, %rbp
subq $0x610, %rsp            # imm = 0x610
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
callq 0x6080
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
shlq %rax
addq $0x1, %rax
movl %eax, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movslq -0x28(%rbp), %rax
movq %rax, -0x5b8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xb670
movq %rax, %rcx
movq -0x5b8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1d50e
movq 0x5c066(%rip), %rdi     # 0x78fa8
leaq 0x3c880(%rip), %rsi     # 0x597c9
callq 0x6520
movq %rax, -0x5c8(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0xf2d0
movq -0x5c8(%rbp), %rdi
movq %rax, %rsi
callq 0x64d0
movq %rax, %rdi
movq 0x5c015(%rip), %rsi     # 0x78f90
callq 0x6570
movl $0x0, -0x2c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x1ab40
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0xf2d0
movq %rax, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x8ce0
movl %eax, -0x5bc(%rbp)
jmp 0x1cfb1
movl -0x5bc(%rbp), %eax
cmpl $0x0, %eax
je 0x1cfe0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d4bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d503
leaq -0x90(%rbp), %rdi
callq 0x1abf0
movl $0x0, -0x94(%rbp)
leaq -0x338(%rbp), %rdi
callq 0x92d0
jmp 0x1d004
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0xf2d0
movq %rax, %rdi
leaq -0x338(%rbp), %rsi
movl $0x1, %edx
callq 0xa5e0
movl %eax, -0x5cc(%rbp)
jmp 0x1d02d
movl -0x5cc(%rbp), %eax
cmpl $0x0, %eax
je 0x1d06d
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d4a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d4f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d4eb
jmp 0x1d06f
leaq -0x1b0(%rbp), %rdi
callq 0x61c0
movl %eax, -0x5d0(%rbp)
jmp 0x1d083
movl -0x5d0(%rbp), %eax
cmpl $-0x1, %eax
je 0x1d397
leaq -0x358(%rbp), %rdi
callq 0x6730
leaq -0x378(%rbp), %rdi
movq %rdi, -0x5d8(%rbp)
callq 0x6730
movq -0x5d8(%rbp), %rdi
movl $0x2710, %esi           # imm = 0x2710
callq 0x6810
jmp 0x1d0c4
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0xf2d0
movq %rax, %rsi
leaq -0x338(%rbp), %rdi
leaq -0x358(%rbp), %rdx
leaq -0x378(%rbp), %rcx
callq 0x84b0
movl %eax, -0x5dc(%rbp)
jmp 0x1d0f6
movl -0x5dc(%rbp), %eax
cmpl $0x0, %eax
je 0x1d125
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d338
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d37a
leaq -0x48(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0xabd0
jmp 0x1d137
leaq -0x378(%rbp), %rdi
callq 0x7ff0
movl %eax, -0x5e0(%rbp)
jmp 0x1d14b
movl -0x5e0(%rbp), %eax
cmpl $0x0, %eax
je 0x1d169
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d338
leaq -0x500(%rbp), %rdi
callq 0x63a0
jmp 0x1d177
leaq -0x4f0(%rbp), %rdi
leaq -0x378(%rbp), %rsi
callq 0x64d0
jmp 0x1d18c
leaq -0x520(%rbp), %rdi
callq 0x6730
leaq -0x500(%rbp), %rdi
leaq -0x520(%rbp), %rsi
movl $0x4e, %edx
callq 0x6960
movq %rax, -0x5e8(%rbp)
jmp 0x1d1b9
movq -0x5e8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, -0x5e9(%rbp)
jmp 0x1d1d7
movb -0x5e9(%rbp), %al
testb $0x1, %al
jne 0x1d1e6
jmp 0x1d30a
leaq -0x520(%rbp), %rdi
callq 0x67e0
movslq -0x24(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1d21f
jmp 0x1d198
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d36e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d362
movl $0x0, -0x524(%rbp)
movslq -0x524(%rbp), %rax
movq %rax, -0x5f8(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x67e0
movq %rax, %rcx
movq -0x5f8(%rbp), %rax
movl -0x24(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x1d305
movslq -0x524(%rbp), %rdx
movslq -0x24(%rbp), %rcx
leaq -0x548(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x6550
jmp 0x1d282
leaq -0x548(%rbp), %rdi
callq 0x7ec0
jmp 0x1d290
leaq -0x548(%rbp), %rdi
leaq -0x549(%rbp), %rsi
callq 0xbe60
jmp 0x1d2a5
movsbl -0x549(%rbp), %edx
movl -0x94(%rbp), %ecx
movl -0x2c(%rbp), %r8d
leaq -0x90(%rbp), %rdi
leaq -0x548(%rbp), %rsi
callq 0x1cc70
jmp 0x1d2cb
leaq -0x548(%rbp), %rdi
callq 0x6a08
movl -0x524(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x524(%rbp)
jmp 0x1d229
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x548(%rbp), %rdi
callq 0x6a08
jmp 0x1d362
jmp 0x1d198
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x6a08
leaq -0x500(%rbp), %rdi
callq 0x6410
movl $0x0, -0x58(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x6a08
leaq -0x358(%rbp), %rdi
callq 0x6a08
movl -0x58(%rbp), %eax
testl %eax, %eax
jne 0x1d4a3
jmp 0x1d35d
jmp 0x1d06f
leaq -0x520(%rbp), %rdi
callq 0x6a08
leaq -0x500(%rbp), %rdi
callq 0x6410
leaq -0x378(%rbp), %rdi
callq 0x6a08
leaq -0x358(%rbp), %rdi
callq 0x6a08
jmp 0x1d4eb
leaq -0x338(%rbp), %rdi
callq 0x9400
jmp 0x1d3a5
leaq -0x588(%rbp), %rdi
movq %rdi, -0x608(%rbp)
callq 0x1ae50
movq -0x608(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x90(%rbp), %rdi
callq 0x1c6a0
movl %eax, -0x5fc(%rbp)
jmp 0x1d3d6
movl -0x5fc(%rbp), %eax
cmpl $0x0, %eax
je 0x1d405
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d497
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1d4df
movq -0x10(%rbp), %rdi
movslq -0x28(%rbp), %rsi
callq 0xf2d0
movq %rax, %rsi
leaq 0x3e8fa(%rip), %rdx     # 0x5bd16
leaq -0x5a8(%rbp), %rdi
callq 0x1ace0
jmp 0x1d42a
movq -0x18(%rbp), %rax
movl (%rax), %ecx
leaq -0x588(%rbp), %rdi
leaq -0x5a8(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0xc810
movl %eax, -0x60c(%rbp)
jmp 0x1d44f
leaq -0x5a8(%rbp), %rdi
callq 0x6a08
movl -0x60c(%rbp), %eax
cmpl $0x0, %eax
je 0x1d490
movl $0x1, -0x4(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x1d497
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x5a8(%rbp), %rdi
callq 0x6a08
jmp 0x1d4df
movl $0x0, -0x58(%rbp)
leaq -0x588(%rbp), %rdi
callq 0x1ae90
leaq -0x338(%rbp), %rdi
callq 0x9480
leaq -0x90(%rbp), %rdi
callq 0x1aeb0
leaq -0x48(%rbp), %rdi
callq 0xfe90
movl -0x58(%rbp), %eax
testl %eax, %eax
je 0x1d4cf
jmp 0x1d4cd
jmp 0x1d52c
jmp 0x1d4d1
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1cf14
leaq -0x588(%rbp), %rdi
callq 0x1ae90
leaq -0x338(%rbp), %rdi
callq 0x9480
leaq -0x90(%rbp), %rdi
callq 0x1aeb0
leaq -0x48(%rbp), %rdi
callq 0xfe90
jmp 0x1d538
movq -0x20(%rbp), %rax
movq %rax, -0x5b0(%rbp)
movq -0x5b0(%rbp), %rdi
callq 0xa8f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x610, %rsp            # imm = 0x610
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x6900
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)