// File: /workspace/github_cpp_makefile_star2_O3/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, 0x34(%rsp)
movq %rdx, %r14
movq %rsi, 0xd0(%rsp)
movq %rdi, %r15
callq 0x5060
movq %rax, 0xc8(%rsp)
leaq 0x138(%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 (%r15), %rdx
movq %r15, 0xd8(%rsp)
cmpq 0x8(%r15), %rdx
movq %r14, 0x78(%rsp)
je 0x13591
movq (%r14), %rax
leal 0x1(,%rax,2), %ecx
addl %eax, %eax
movslq %ecx, %r14
cltq
movq %rax, 0x80(%rsp)
xorl %r13d, %r13d
movq 0x2de21(%rip), %r15     # 0x40fc0
leaq 0x38(%rsp), %rbp
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq (%rdx), %rsi
movq %rdx, 0xe0(%rsp)
movq 0x8(%rdx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x8566
movl $0x8, %edx
movq %r15, %rdi
leaq 0x2008a(%rip), %rsi     # 0x3327b
callq 0x5420
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq %r15, %rdi
callq 0x5420
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x2c8(%rsp), %r15
movq %r15, %rdi
callq 0x7710
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x8076
testl %eax, %eax
jne 0x134e2
jmp 0x13416
cmpb $0x0, 0x34(%rsp)
je 0x13281
movq 0x78(%rsp), %rax
movl (%rax), %esi
leaq 0x10(%rsp), %rdi
callq 0x7601
testl %eax, %eax
jne 0x13495
movl 0x4(%rsp), %eax
addl 0x18(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x5280
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x150(%rsp), %rdi
callq 0x5420
incl 0xc(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movb $0x0, 0x48(%rsp)
leaq 0x140(%rsp), %rdi
movq %rbp, %rsi
movl $0x4e, %edx
callq 0x56c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
setne %r15b
jne 0x133d4
movq 0x40(%rsp), %rax
cmpq %r14, %rax
jb 0x132cd
xorl %r12d, %r12d
cmpq 0x80(%rsp), %rax
je 0x133a5
xorl %r12d, %r12d
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x53e0
movq %rbx, %rdi
callq 0x68d8
movq %rbx, %rdi
leaq 0x3(%rsp), %rsi
callq 0x8a8b
testl %eax, %eax
jne 0x133af
movsbl 0x3(%rsp), %edx
leaq 0x108(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x1, %ecx
callq 0xa21e
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x1338c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
incq %r12
movq 0x40(%rsp), %rax
subq 0x80(%rsp), %rax
cmpq %r12, %rax
ja 0x13316
addl %r12d, 0x8(%rsp)
jmp 0x132cd
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x133d4
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x133f0
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x140(%rsp), %rdi
movq 0x2db89(%rip), %rsi     # 0x40f88
callq 0x52c0
leaq 0x1c0(%rsp), %rdi
callq 0x5120
jmp 0x13498
leaq 0x450(%rsp), %rdi
callq 0x5140
cmpl $-0x1, %eax
je 0x1357c
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movb %r13b, 0x20(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %r13, 0xb0(%rsp)
movb %r13b, 0xb8(%rsp)
leaq 0x2c8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x6c5b
testl %eax, %eax
jne 0x13495
leaq 0x10(%rsp), %rdi
callq 0x6984
testl %eax, %eax
je 0x13261
xorl %r15d, %r15d
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x134bd
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x134d9
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r15b, %r15b
jne 0x13416
xorl %r15d, %r15d
leaq 0x2d70c(%rip), %rax     # 0x40bf8
movq %rax, 0x450(%rsp)
leaq 0x2d725(%rip), %rax     # 0x40c20
movq %rax, 0x460(%rsp)
movq $0x0, 0x458(%rsp)
leaq 0x2c8(%rsp), %rdi
leaq 0x2d61a(%rip), %rsi     # 0x40b38
callq 0x7922
leaq 0x460(%rsp), %rdi
callq 0x5120
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x1354c
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r15b, %r15b
je 0x1370b
movq 0xe0(%rsp), %rdx
addq $0x20, %rdx
movq 0xd8(%rsp), %rax
cmpq 0x8(%rax), %rdx
movq 0x2da4c(%rip), %r15     # 0x40fc0
jne 0x131bc
jmp 0x135a9
movb $0x1, %r15b
leaq 0x2c8(%rsp), %rdi
callq 0x77fa
jmp 0x134e5
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x2da10(%rip), %rdi     # 0x40fc0
leaq 0x205d2(%rip), %rsi     # 0x33b89
movl $0x6, %edx
callq 0x5420
movq 0x2d9f8(%rip), %rdi     # 0x40fc0
movl 0x8(%rsp), %esi
callq 0x5690
movq %rax, %rbx
leaq 0x205b5(%rip), %rsi     # 0x33b90
movl $0xc, %edx
movq %rax, %rdi
callq 0x5420
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x5690
movq %rax, %rbx
leaq 0x2059f(%rip), %rsi     # 0x33b9d
movl $0x1b, %edx
movq %rax, %rdi
callq 0x5420
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x5690
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
movq 0x120(%rsp), %rsi
movq 0x2d970(%rip), %rdi     # 0x40fc0
callq 0x5240
movq %rax, %rbx
leaq 0x2055a(%rip), %rsi     # 0x33bb9
movl $0x14, %edx
movq %rax, %rdi
callq 0x5420
movq (%rbx), %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
leaq 0xf8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0xd0(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0xe8(%rsp), %rdi
callq 0x8566
movq 0x78(%rsp), %rax
movl (%rax), %edx
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorps %xmm0, %xmm0
callq 0x8b31
movq 0xe8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x136fa
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x53c0
xorl %ebx, %ebx
movq 0xc8(%rsp), %rdi
callq 0x8320
jmp 0x13710
movl $0x1, %ebx
leaq 0x108(%rsp), %rdi
callq 0x13868
movl %ebx, %eax
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0xe8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x13852
movq 0xf8(%rsp), %rsi
jmp 0x1384a
movq %rax, %r14
jmp 0x137c7
jmp 0x13779
jmp 0x13769
jmp 0x1375d
movq %rax, %r14
jmp 0x13852
jmp 0x13771
jmp 0x137a8
movq %rax, %r14
jmp 0x13836
movq %rax, %r14
jmp 0x13829
movq %rax, %r14
jmp 0x137e8
movq %rax, %r14
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x137ab
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
jmp 0x137ab
movq %rax, %r14
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x137c7
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x2d7ba(%rip), %rsi     # 0x40f88
leaq 0x140(%rsp), %rdi
callq 0x52c0
leaq 0x1c0(%rsp), %rdi
callq 0x5120
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x1380d
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x13829
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x2c8(%rsp), %rdi
callq 0x7830
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x13852
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x108(%rsp), %rdi
callq 0x13868
movq %r14, %rdi
callq 0x56a0
nop