// File: 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
movq %rsp, %rbp
subq $0x640, %rsp            # imm = 0x640
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
callq 0x6080
movq %rax, -0x30(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x28920
movq -0x20(%rbp), %rax
movq (%rax), %rax
shlq %rax
addq $0x1, %rax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0xefe0
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf050
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xf010
testb $0x1, %al
jne 0x28242
jmp 0x28760
leaq -0x88(%rbp), %rdi
callq 0xf080
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x6230
jmp 0x2825f
movq 0x50d42(%rip), %rdi     # 0x78fa8
leaq 0x3101a(%rip), %rsi     # 0x59287
callq 0x6520
movq %rax, -0x5a8(%rbp)
jmp 0x2827b
movq -0x5a8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x64d0
movq %rax, -0x5b0(%rbp)
jmp 0x28297
movq -0x5b0(%rbp), %rdi
movq 0x50ceb(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0x282ac
leaq -0x360(%rbp), %rdi
callq 0x92d0
jmp 0x282ba
leaq -0xb0(%rbp), %rdi
leaq -0x360(%rbp), %rsi
movl $0x1, %edx
callq 0xa5e0
movl %eax, -0x5b4(%rbp)
jmp 0x282da
movl -0x5b4(%rbp), %eax
cmpl $0x0, %eax
je 0x28340
movl $0x1, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x28708
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x288fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x2874f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x28743
jmp 0x28342
leaq -0x1d8(%rbp), %rdi
callq 0x61c0
movl %eax, -0x5b8(%rbp)
jmp 0x28356
movl -0x5b8(%rbp), %eax
cmpl $-0x1, %eax
je 0x286f0
leaq -0x388(%rbp), %rdi
movq %rdi, -0x5c8(%rbp)
callq 0x6730
leaq -0x3a8(%rbp), %rdi
movq %rdi, -0x5d0(%rbp)
callq 0x6730
movq -0x5d0(%rbp), %rdx
movq -0x5c8(%rbp), %rcx
leaq -0x360(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x84b0
movl %eax, -0x5bc(%rbp)
jmp 0x283b4
movl -0x5bc(%rbp), %eax
cmpl $0x0, %eax
je 0x283ec
movl $0x1, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x28695
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x286d6
leaq -0x388(%rbp), %rdi
callq 0x7ff0
movl %eax, -0x5d4(%rbp)
jmp 0x28400
movl -0x5d4(%rbp), %eax
cmpl $0x0, %eax
je 0x28421
movl $0x1, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x28695
testb $0x1, -0x21(%rbp)
je 0x28464
movq -0x20(%rbp), %rax
movl (%rax), %esi
leaq -0x388(%rbp), %rdi
callq 0x8ee0
movl %eax, -0x5d8(%rbp)
jmp 0x28441
movl -0x5d8(%rbp), %eax
cmpl $0x0, %eax
je 0x28462
movl $0x1, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x28695
jmp 0x28464
movl -0x70(%rbp), %eax
incl %eax
movl %eax, -0x70(%rbp)
leaq -0x388(%rbp), %rdi
callq 0x67e0
movq %rax, %rcx
movl -0x78(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x78(%rbp)
leaq -0x530(%rbp), %rdi
callq 0x63a0
jmp 0x28491
leaq -0x520(%rbp), %rdi
leaq -0x388(%rbp), %rsi
callq 0x64d0
jmp 0x284a6
leaq -0x550(%rbp), %rdi
callq 0x6730
leaq -0x530(%rbp), %rdi
leaq -0x550(%rbp), %rsi
movl $0x4e, %edx
callq 0x6960
movq %rax, -0x5e0(%rbp)
jmp 0x284d3
movq -0x5e0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x6670
movb %al, -0x5e1(%rbp)
jmp 0x284f1
movb -0x5e1(%rbp), %al
testb $0x1, %al
jne 0x28500
jmp 0x28673
leaq -0x550(%rbp), %rdi
callq 0x67e0
movslq -0x6c(%rbp), %rcx
cmpq %rcx, %rax
jae 0x28545
jmp 0x284b2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x286ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
jmp 0x286be
movl $0x0, -0x554(%rbp)
movslq -0x554(%rbp), %rax
movq %rax, -0x5f0(%rbp)
leaq -0x550(%rbp), %rdi
callq 0x67e0
movq %rax, %rcx
movq -0x5f0(%rbp), %rax
movl -0x6c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x28662
movslq -0x554(%rbp), %rdx
movslq -0x6c(%rbp), %rcx
leaq -0x578(%rbp), %rdi
leaq -0x550(%rbp), %rsi
callq 0x6550
jmp 0x285a8
leaq -0x578(%rbp), %rdi
callq 0x7ec0
jmp 0x285b6
leaq -0x578(%rbp), %rdi
leaq -0x79(%rbp), %rsi
callq 0xbe60
movl %eax, -0x5f4(%rbp)
jmp 0x285ce
movl -0x5f4(%rbp), %eax
cmpl $0x0, %eax
je 0x2860f
movl $0x1, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x28634
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x578(%rbp), %rdi
callq 0x6a08
jmp 0x286be
movsbl -0x79(%rbp), %edx
leaq -0x68(%rbp), %rdi
leaq -0x578(%rbp), %rsi
movl $0x1, %ecx
callq 0xdd20
jmp 0x2862a
movl $0x0, -0x364(%rbp)
leaq -0x578(%rbp), %rdi
callq 0x6a08
movl -0x364(%rbp), %eax
testl %eax, %eax
jne 0x2867d
jmp 0x2864c
jmp 0x2864e
movl -0x554(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x554(%rbp)
jmp 0x2854f
movl -0x554(%rbp), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x284b2
movl $0x0, -0x364(%rbp)
leaq -0x550(%rbp), %rdi
callq 0x6a08
leaq -0x530(%rbp), %rdi
callq 0x6410
leaq -0x3a8(%rbp), %rdi
callq 0x6a08
leaq -0x388(%rbp), %rdi
callq 0x6a08
movl -0x364(%rbp), %eax
testl %eax, %eax
jne 0x28708
jmp 0x286b9
jmp 0x28342
leaq -0x550(%rbp), %rdi
callq 0x6a08
leaq -0x530(%rbp), %rdi
callq 0x6410
leaq -0x3a8(%rbp), %rdi
callq 0x6a08
leaq -0x388(%rbp), %rdi
callq 0x6a08
jmp 0x28743
leaq -0x360(%rbp), %rdi
callq 0x9400
jmp 0x286fe
movl $0x0, -0x364(%rbp)
leaq -0x360(%rbp), %rdi
callq 0x9480
leaq -0xb0(%rbp), %rdi
callq 0x6a08
movl -0x364(%rbp), %eax
testl %eax, %eax
jne 0x288e8
jmp 0x28730
jmp 0x28732
leaq -0x88(%rbp), %rdi
callq 0xf0a0
jmp 0x28216
leaq -0x360(%rbp), %rdi
callq 0x9480
leaq -0xb0(%rbp), %rdi
callq 0x6a08
jmp 0x288fd
movq 0x50841(%rip), %rdi     # 0x78fa8
leaq 0x3142b(%rip), %rsi     # 0x59b99
callq 0x6520
movq %rax, -0x600(%rbp)
jmp 0x2877c
movq -0x600(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x68d0
movq %rax, -0x608(%rbp)
jmp 0x28794
movq -0x608(%rbp), %rdi
leaq 0x313fe(%rip), %rsi     # 0x59ba0
callq 0x6520
movq %rax, -0x610(%rbp)
jmp 0x287b0
movq -0x610(%rbp), %rdi
movl -0x70(%rbp), %esi
callq 0x68d0
movq %rax, -0x618(%rbp)
jmp 0x287c8
movq -0x618(%rbp), %rdi
leaq 0x313d7(%rip), %rsi     # 0x59bad
callq 0x6520
movq %rax, -0x620(%rbp)
jmp 0x287e4
movq -0x620(%rbp), %rdi
movl -0x78(%rbp), %esi
callq 0x68d0
movq %rax, -0x628(%rbp)
jmp 0x287fc
movq -0x628(%rbp), %rdi
movq 0x50786(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0x28811
leaq -0x68(%rbp), %rdi
callq 0xfbb0
movq %rax, %rsi
movq 0x50784(%rip), %rdi     # 0x78fa8
callq 0x6120
movq %rax, -0x630(%rbp)
jmp 0x28832
movq -0x630(%rbp), %rdi
leaq 0x31389(%rip), %rsi     # 0x59bc9
callq 0x6520
movq %rax, -0x638(%rbp)
jmp 0x2884e
movq -0x638(%rbp), %rdi
movq 0x50734(%rip), %rsi     # 0x78f90
callq 0x6570
jmp 0x28863
movq -0x18(%rbp), %rsi
leaq -0x598(%rbp), %rdi
callq 0x6230
jmp 0x28875
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq -0x68(%rbp), %rdi
leaq -0x598(%rbp), %rsi
xorps %xmm0, %xmm0
callq 0xbf80
jmp 0x28890
leaq -0x598(%rbp), %rdi
callq 0x6a08
movq -0x30(%rbp), %rax
movq %rax, -0x5a0(%rbp)
movq -0x5a0(%rbp), %rdi
callq 0xa8f0
jmp 0x288b5
movl $0x0, -0x4(%rbp)
movl $0x1, -0x364(%rbp)
jmp 0x288e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0x598(%rbp), %rdi
callq 0x6a08
jmp 0x288fd
leaq -0x68(%rbp), %rdi
callq 0x28940
movl -0x4(%rbp), %eax
addq $0x640, %rsp            # imm = 0x640
popq %rbp
retq
leaq -0x68(%rbp), %rdi
callq 0x28940
movq -0xb8(%rbp), %rdi
callq 0x6900
nopw %cs:(%rax,%rax)
nopl (%rax)