// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rsi, 0xd0(%rsp)
movq %rdi, %r14
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 (%r14), %rdx
movq %r14, 0xd8(%rsp)
cmpq 0x8(%r14), %rdx
sete %r14b
movq %rbx, 0x78(%rsp)
je 0x13712
movq (%rbx), %rax
leal 0x1(,%rax,2), %ecx
addl %eax, %eax
movslq %ecx, %r13
cltq
movq %rax, 0x80(%rsp)
xorl %ebx, %ebx
leaq 0x2c8(%rsp), %r15
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 0x859a
movl $0x8, %edx
movq 0x2dc67(%rip), %rdi     # 0x40fc0
leaq 0x1ff1b(%rip), %rsi     # 0x3327b
callq 0x5420
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x2dc4a(%rip), %rdi     # 0x40fc0
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
movb %r14b, 0x2(%rsp)
movq %r15, %rdi
callq 0x7742
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x80aa
testl %eax, %eax
je 0x13461
xorl %r14d, %r14d
leaq 0x2d822(%rip), %rax     # 0x40bf8
movq %rax, 0x450(%rsp)
leaq 0x2d83b(%rip), %rax     # 0x40c20
movq %rax, 0x460(%rsp)
movq $0x0, 0x458(%rsp)
movq %r15, %rdi
leaq 0x2d735(%rip), %rsi     # 0x40b38
callq 0x7954
leaq 0x460(%rsp), %rdi
callq 0x5120
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x13431
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r14b, %r14b
je 0x1372c
movq 0xe0(%rsp), %rdx
addq $0x20, %rdx
movq 0xd8(%rsp), %rax
cmpq 0x8(%rax), %rdx
sete %r14b
jne 0x13327
jmp 0x13731
leaq 0x450(%rsp), %r14
movq %r14, %rdi
callq 0x5140
cmpl $-0x1, %eax
je 0x13702
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movb %bl, 0x20(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rbx, 0xb0(%rsp)
movb %bl, 0xb8(%rsp)
movq %r15, %rdi
leaq 0x58(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x6ca1
testl %eax, %eax
jne 0x134d8
leaq 0x10(%rsp), %rdi
callq 0x69ab
testl %eax, %eax
je 0x13532
xorl %r15d, %r15d
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x13500
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1351c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
testb %r15b, %r15b
leaq 0x2c8(%rsp), %r15
jne 0x13469
jmp 0x133cc
cmpb $0x0, 0x34(%rsp)
je 0x1354e
movq 0x78(%rsp), %rax
movl (%rax), %esi
leaq 0x10(%rsp), %rdi
callq 0x7633
testl %eax, %eax
jne 0x134d8
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
leaq 0x38(%rsp), %rsi
movl $0x4e, %edx
callq 0x56c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
setne %r15b
jne 0x136c0
cmpq %r13, 0x40(%rsp)
jb 0x1359a
xorl %r12d, %r12d
movq 0x80(%rsp), %rax
cmpq %rax, 0x40(%rsp)
setne %r14b
je 0x13680
xorl %r12d, %r12d
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x53e0
movq %rbp, %rdi
callq 0x6905
movq %rbp, %rdi
leaq 0x3(%rsp), %rsi
callq 0x8ae7
testl %eax, %eax
jne 0x13699
movsbl 0x3(%rsp), %edx
leaq 0x108(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x1, %ecx
callq 0xa289
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x13663
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
incq %r12
movq 0x40(%rsp), %rax
subq 0x80(%rsp), %rax
cmpq %r12, %rax
seta %r14b
ja 0x135eb
addl %r12d, 0x8(%rsp)
testb $0x1, %r14b
leaq 0x450(%rsp), %r14
je 0x1359a
jmp 0x136c0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x13685
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
jmp 0x13685
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x136dc
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x140(%rsp), %rdi
movq 0x2d89d(%rip), %rsi     # 0x40f88
callq 0x52c0
leaq 0x1c0(%rsp), %rdi
callq 0x5120
jmp 0x134db
movb $0x1, %r14b
movq %r15, %rdi
callq 0x782c
jmp 0x133cf
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x13731
movb 0x2(%rsp), %r14b
movl $0x1, %ebx
testb $0x1, %r14b
je 0x138a0
movq 0x2d879(%rip), %rdi     # 0x40fc0
leaq 0x2044b(%rip), %rsi     # 0x33b99
movl $0x6, %edx
callq 0x5420
movq 0x2d861(%rip), %rdi     # 0x40fc0
movl 0x8(%rsp), %esi
callq 0x5690
movq %rax, %rbx
leaq 0x2042e(%rip), %rsi     # 0x33ba0
movl $0xc, %edx
movq %rax, %rdi
callq 0x5420
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x5690
movq %rax, %rbx
leaq 0x20418(%rip), %rsi     # 0x33bad
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 0x2d7d9(%rip), %rdi     # 0x40fc0
callq 0x5240
movq %rax, %rbx
leaq 0x203d3(%rip), %rsi     # 0x33bc9
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 0x859a
movq 0x78(%rsp), %rax
movl (%rax), %edx
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorps %xmm0, %xmm0
callq 0x8b8d
movq 0xe8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x13891
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x53c0
xorl %ebx, %ebx
movq 0xc8(%rsp), %rdi
callq 0x8354
leaq 0x108(%rsp), %rdi
callq 0x139f8
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 0x139e2
movq 0xf8(%rsp), %rsi
jmp 0x139da
movq %rax, %r14
jmp 0x13957
jmp 0x13909
jmp 0x138ed
jmp 0x138f9
movq %rax, %r14
jmp 0x139e2
jmp 0x13901
jmp 0x13938
movq %rax, %r14
jmp 0x139c6
movq %rax, %r14
jmp 0x139b9
movq %rax, %r14
jmp 0x13978
movq %rax, %r14
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x1393b
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x53c0
jmp 0x1393b
movq %rax, %r14
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x13957
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x2d62a(%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 0x1399d
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x139b9
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x2c8(%rsp), %rdi
callq 0x7862
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x139e2
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x108(%rsp), %rdi
callq 0x139f8
movq %r14, %rdi
callq 0x56a0
nop