// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_fasta.cpp
// Function: fastaToKmers(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&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, long const&, bool)
int fastaToKmers(const std::vector < std::string > & fastas, const std::string & outfilename, const long & kmerlen, const bool circularContigs)
{

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

	std::unordered_map < std::string, std::array < int, 8 > > kmerMap;//create a map to store the kmers for each base
	int substringlength=(kmerlen*2)+1;
	int numseqs=0;
	int numbases=0;
	int numreadbases=0;
	char base;

	for (std::vector < std::string >::const_iterator fastait=fastas.begin(); fastait!=fastas.end(); ++fastait){//for each fasta file
		
		std::string filename = *fastait;

		std::cout << "Reading " << filename << std::endl;
		igzstream gzfileStream;

		if(openGzFileStream(filename, gzfileStream)){return 1;}//open the fasta file. May be gzipped.

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

			std::string sequence;
			std::string name;

			if(readNextFastaSequence(gzfileStream, filename, name, sequence)){return 1;}//read the next sequence from the file

			if(IUPACToN(sequence)){return 1;}//convert any IUPAC characters to N and reject any unrecognised characters

			if (circularContigs){
				if(circulariseSequence(sequence, kmerlen)){return 1;}//if the contigs are circular, add an extra kmer lenght of sequence from the opposite end to each end
			}

			numseqs++;
			numreadbases += sequence.length();
			
			std::stringstream sequencestream;
			sequencestream << sequence;//convert the sequence to stringstream
			std::string subsequence;
			
			while (std::getline(sequencestream, subsequence, 'N')){//read each section of sequence between Ns
				
				if (subsequence.length() < substringlength){//if the subsequence is too short continue
					continue;
				}
				
				int i;

				for (i=0; i < subsequence.length() - (substringlength-1); ++i){//for each base in the subsequence

					std::string kmer = subsequence.substr(i,substringlength);//extract the kmer
					
					reverseComplementIfMin(kmer);//if the revcomp of the kmer is 'smaller' then revcomp it

					if(extractMiddleBase(kmer, base)){return 1;}//extract the middle base
					
					addKmerToBaseArrayMap(kmerMap, kmer, base, true);//add the kmer and base to the map

				}
				numbases += i;
    		}
		}
 		gzfileStream.close();//close the file
	}
	
	std::cout << "Added " << numbases << " kmers from " << numseqs << " sequences of total length " << numreadbases << std::endl;
	std::cout << kmerMap.size() << " unique kmers in map" << std::endl;
	
	printKmerFile(kmerMap, outfilename, kmerlen);//print the kmer file

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x568, %rsp            # imm = 0x568
movl %ecx, 0x14(%rsp)
movq %rdx, %rbx
movq %rsi, 0x48(%rsp)
movq %rdi, %r14
callq 0x5050
movq %rax, 0x40(%rsp)
leaq 0x118(%rsp), %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)
movq %rbx, 0x18(%rsp)
movl (%rbx), %eax
leal 0x1(,%rax,2), %ecx
addl %eax, %eax
movq %r14, 0x50(%rsp)
movq (%r14), %rsi
movslq %ecx, %r13
cltq
movq %rax, 0x60(%rsp)
pushq $0x1
popq %rbx
leaq 0x68(%rsp), %rbp
leaq 0x120(%rsp), %r15
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x50(%rsp), %rax
cmpq 0x8(%rax), %rsi
je 0x10501
leaq 0xa8(%rsp), %rdi
movq %rsi, 0x58(%rsp)
callq 0x5540
movq 0x25d26(%rip), %rdi     # 0x35fb8
leaq 0x14fe2(%rip), %rsi     # 0x2527b
callq 0x53f0
leaq 0x2c8(%rsp), %r14
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %r14, %rdi
callq 0x6d0a
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %r14d
movl %ebx, %edx
callq 0x74af
testl %eax, %eax
je 0x10314
xorl %ebx, %ebx
leaq 0x2c8(%rsp), %rdi
callq 0x6dea
leaq 0xa8(%rsp), %rdi
callq 0x5778
movq 0x58(%rsp), %rsi
addq $0x20, %rsi
testb %bl, %bl
movl %r14d, %ebx
jne 0x1026a
jmp 0x105d3
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
callq 0x5140
cmpl $-0x1, %eax
je 0x104ed
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
xorl %ecx, %ecx
movb %cl, 0x30(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
movb %cl, 0x98(%rsp)
leaq 0x2c8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x88(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0x67b1
testl %eax, %eax
jne 0x10397
leaq 0x20(%rsp), %rdi
callq 0x65ee
testl %eax, %eax
je 0x103bf
xorl %r12d, %r12d
leaq 0x88(%rsp), %rdi
callq 0x5778
leaq 0x20(%rsp), %rdi
callq 0x5778
testb %r12b, %r12b
jne 0x1031c
jmp 0x102df
cmpb $0x0, 0x14(%rsp)
je 0x103db
movq 0x18(%rsp), %rax
movl (%rax), %esi
leaq 0x20(%rsp), %rdi
callq 0x6c50
testl %eax, %eax
jne 0x10397
movl 0x8(%rsp), %eax
addl 0x28(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x52b0
leaq 0x150(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x53a0
incl 0x10(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
movb $0x0, 0x78(%rsp)
leaq 0x140(%rsp), %rdi
movq %rbp, %rsi
pushq $0x4e
popq %rdx
callq 0x56e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
setne %r12b
jne 0x104d3
movq 0x70(%rsp), %rax
cmpq %r13, %rax
jb 0x1041f
xorl %ebx, %ebx
subq 0x60(%rsp), %rax
cmpq %rbx, %rax
jbe 0x104b2
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x5420
movq %r15, %rdi
callq 0x6578
movq %r15, %rdi
leaq 0x7(%rsp), %rsi
callq 0x7c51
testl %eax, %eax
jne 0x104c3
movsbl 0x7(%rsp), %edx
leaq 0xe8(%rsp), %rdi
movq %r15, %rsi
movl %r14d, %ecx
callq 0x8a0f
movq %r15, %rdi
callq 0x5778
incq %rbx
movq 0x70(%rsp), %rax
jmp 0x10454
addl %ebx, 0xc(%rsp)
leaq 0x450(%rsp), %rbx
jmp 0x1041f
movq %r15, %rdi
callq 0x5778
leaq 0x450(%rsp), %rbx
movq %rbp, %rdi
callq 0x5778
leaq 0x140(%rsp), %rdi
callq 0x5320
jmp 0x1039a
movb $0x1, %bl
leaq 0x2c8(%rsp), %rdi
callq 0x6db4
jmp 0x102e1
movq 0x25ab0(%rip), %rdi     # 0x35fb8
leaq 0x1569a(%rip), %rsi     # 0x25ba9
callq 0x53f0
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x56a0
leaq 0x15689(%rip), %rsi     # 0x25bb0
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movl 0x10(%rsp), %esi
callq 0x56a0
leaq 0x1567b(%rip), %rsi     # 0x25bbd
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movl 0x8(%rsp), %esi
callq 0x56a0
movq %rax, %rdi
callq 0x51c0
movq 0x100(%rsp), %rsi
movq 0x25a4b(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x15660(%rip), %rsi     # 0x25bd9
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0xc8(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x5540
movq 0x18(%rsp), %rax
movl (%rax), %edx
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
xorps %xmm0, %xmm0
callq 0x7cf2
leaq 0xc8(%rsp), %rdi
callq 0x5778
xorl %ebx, %ebx
movq 0x40(%rsp), %rdi
callq 0x7657
leaq 0xe8(%rsp), %rdi
callq 0x10696
movl %ebx, %eax
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x1067c
jmp 0x1060a
movq %rax, %rbx
jmp 0x10643
jmp 0x1061f
movq %rax, %rbx
jmp 0x10681
jmp 0x10613
jmp 0x1061a
movq %rax, %rbx
jmp 0x10674
jmp 0x10624
movq %rax, %rbx
jmp 0x10667
movq %rax, %rbx
jmp 0x10650
movq %rax, %rbx
jmp 0x10639
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x5778
leaq 0x68(%rsp), %rdi
callq 0x5778
leaq 0x140(%rsp), %rdi
callq 0x5320
leaq 0x88(%rsp), %rdi
callq 0x5778
leaq 0x20(%rsp), %rdi
callq 0x5778
leaq 0x2c8(%rsp), %rdi
callq 0x6dea
leaq 0xa8(%rsp), %rdi
callq 0x5778
leaq 0xe8(%rsp), %rdi
callq 0x10696
movq %rbx, %rdi
callq 0x56c0