// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_align.cpp
// Function: alignKmers(float const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, 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&, bool const&, bool const&, 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&)
int alignKmers(const float & minproportion, const std::string & outputprefix, const std::vector < std::string > & kmerfiles, const bool & variantonly, const bool & printkmers, const std::vector < std::string > & sample)
{

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

	int numfiles=kmerfiles.size();//record the number of files

	std::vector < std::string > sampleNames;
	if (collectSampleNames(kmerfiles, sampleNames)!=0){return 1;}//get the total number of samples in all input files

	std::vector < bool > include (sampleNames.size());
	getSubsample(sample, sampleNames, include);//get a vector of bools representing which samples to include based on a provided sample file. This also removed duplicate samples in the input files

	std::vector < std::string > includedSampleNames;
	for (std::vector < bool >::iterator vbit=include.begin(); vbit!=include.end(); ++vbit){//make a vector of all included sample names to help printing output files later
		if (*vbit){
			includedSampleNames.push_back(sampleNames[std::distance(include.begin(), vbit)]);
		}
	}

	int numSamples = std::count(include.begin(), include.end(), true);//count the number of included samples

	std::cout << numSamples << " samples will be included in the alignment" << std::endl;
	
	float maxmissing=round((1.0-minproportion)*numSamples);//calculate the maximum number of samples that can be missing for the site to be included in the alignment

	float minrequired=numSamples-maxmissing;//calculate the minimum number of samples that must contain a kmer for the site to be included in the alignment
	
	std::cout << "Keeping variants for which at least " << int(minrequired) << " samples include kmer matches" << std::endl << std::endl;
	
	// Create the kmer map
	std::unordered_map < std::string, std::string > kmerMap;
	int oldkmersize=0;
	int sampleNum=0;
	int includedSampleNum=0;
	char base;
	int kmersize;

	std::ifstream fileStream;
	for (int s = 0; s < kmerfiles.size(); ++s){//read each file and make a map of the kmers which stores the bases for each included sample
		
		if (openFileStream(kmerfiles[s], fileStream)){return 1;};

		
		std::vector < std::string > names;
		
		readKmerHeader(fileStream, kmersize, names);//read the header from the kmer file to get the kmer size and sample names
		
		if (s==0){ //If it's the first file, set the oldkmersize
			oldkmersize=kmersize;
		}
		else if (kmersize!=oldkmersize){ //if the file kmer size isn't the same as the other files then ditch out
			std::cerr << "kmer files have different kmer sizes" << std::endl << std::endl;
			return 1;
		}

		std::vector < int > fileInclude;

		for (int i=0; i<names.size(); ++i){ //put the index of all sample names that are to be included into a vector
			if (include[sampleNum+i]){
				fileInclude.push_back(i);
			}
		}

		int fileIncludeSize=fileInclude.size();

		if (fileIncludeSize==0){ // if no sample names in the file are going to be included then don't read the file
			fileStream.close();
			sampleNum+=int(names.size());
			names.clear();
			continue;
		}

		char kmerbuffer[kmersize*2/3];
		char asciibuffer[int(ceil(float(names.size())/6))];

		while (fileStream.read(asciibuffer, sizeof(asciibuffer))){//read the next ascii bitstring
			std::string asciibits (asciibuffer, sizeof(asciibuffer));
			std::vector < bool > mybits;
			vectorbool_from_ascii(asciibits, mybits);//convert the ascii representation of the taxa to a vector of bools

			while (fileStream.peek()!='\n' && fileStream.get(base)){
				fileStream.read(kmerbuffer, sizeof(kmerbuffer));
				std::string kmer (kmerbuffer, kmersize*2/3);
				
				addKmerToStringMap(kmerMap, kmer, base, mybits, fileInclude, includedSampleNum, numSamples, maxmissing);
				
	    	}
	    	fileStream.ignore(256,'\n');//skip the end ofline character
	    }
	    sampleNum+=int(names.size()); //add the number of samples in the file to the count of total samples
	    includedSampleNum+=fileIncludeSize;
		fileStream.close();
	}
	
	std::cout << kmerMap.size() << " kmers identified from " << numSamples << " samples in " << numfiles << " files" << std::endl;
	
	std::vector < int > constantBases (4,0);
	
	float filteredsites=0;
	float variantsites=0;

	filterAlignment(kmerMap, constantBases, numSamples, minrequired, variantonly, filteredsites, variantsites);

	if(calculateMissedSNPs(filteredsites, variantsites, kmersize)){return 1;};
	
	printAlignment(outputprefix+".aln", kmerMap, constantBases, includedSampleNames, variantonly);

	if (printkmers){

		std::cout << "Printing aligned split kmers" << std::endl;

		std::unordered_map < std::vector < bool >,  std::vector < std::string > > revKmerMap;

		reverseStringKmerMap(kmerMap, revKmerMap);//reverse the map so that we have a new map of kmers for each combination of samples

		std::cout << revKmerMap.size() << " unique taxon combinations in map" << std::endl;
		
		if(printMergedKmerFile(revKmerMap, outputprefix+".skf", includedSampleNames, kmersize)){return 1;}
	}

	printDuration(start);
	
	return 0;
	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %r9, %r14
movq %r8, %r13
movq %rcx, -0x130(%rbp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, -0x40(%rbp)
callq 0x5050
movq %rax, %r15
movq 0x8(%rbx), %rax
subq (%rbx), %rax
movq %rax, -0xa8(%rbp)
xorps %xmm0, %xmm0
leaq -0x110(%rbp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
pushq $0x1
popq %rdx
movq %rbx, -0x148(%rbp)
movq %rbx, %rdi
movl %edx, -0x34(%rbp)
callq 0x8436
testl %eax, %eax
jne 0xbce2
movq -0x108(%rbp), %rsi
subq -0x110(%rbp), %rsi
sarq $0x5, %rsi
leaq -0x198(%rbp), %rdi
leaq -0x448(%rbp), %rdx
callq 0x9076
movq %r13, -0x120(%rbp)
movq %r12, -0x128(%rbp)
movq %r15, -0x118(%rbp)
leaq -0x110(%rbp), %rsi
leaq -0x198(%rbp), %rdx
movq %r14, %rdi
callq 0x8605
shrq $0x5, -0xa8(%rbp)
xorps %xmm0, %xmm0
leaq -0x1d0(%rbp), %r14
andq $0x0, 0x10(%r14)
movaps %xmm0, (%r14)
xorl %r12d, %r12d
movq -0x198(%rbp), %r13
xorl %r15d, %r15d
movq -0x188(%rbp), %rdx
cmpq %rdx, %r13
jne 0xb549
cmpl -0x180(%rbp), %r15d
je 0xb595
movq (%r13), %rax
btq %r15, %rax
jae 0xb57a
movl %r15d, %esi
movq %r13, %rax
subq -0x198(%rbp), %rax
shlq $0x8, %rax
addq -0x110(%rbp), %rax
shlq $0x5, %rsi
addq %rax, %rsi
movq %r14, %rdi
callq 0x77fc
xorl %eax, %eax
cmpl $0x3f, %r15d
leal 0x1(%r15), %ecx
sete %al
leaq (%r13,%rax,8), %r13
cmovel %r12d, %ecx
movl %ecx, %r15d
jmp 0xb534
movq -0x198(%rbp), %rdi
leaq -0x448(%rbp), %r8
movb $0x1, (%r8)
xorl %esi, %esi
movl %r15d, %ecx
callq 0xc10d
movq %rax, %rbx
movq 0x2a9fd(%rip), %rdi     # 0x35fb8
movl %ebx, %esi
callq 0x56a0
leaq 0x1a13e(%rip), %rsi     # 0x25707
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq -0x40(%rbp), %rax
cvtss2sd (%rax), %xmm0
movsd 0x19c7f(%rip), %xmm1    # 0x25268
subsd %xmm0, %xmm1
cvtsi2sd %ebx, %xmm0
mulsd %xmm1, %xmm0
callq 0x5180
movsd %xmm0, -0x40(%rbp)
movq 0x2a9b2(%rip), %rdi     # 0x35fb8
leaq 0x1a125(%rip), %rsi     # 0x25732
callq 0x53f0
cvtsd2ss -0x40(%rbp), %xmm1
cvtsi2ss %ebx, %xmm0
movss %xmm1, -0x40(%rbp)
subss %xmm1, %xmm0
cvttss2si %xmm0, %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x56a0
leaq 0x1a11c(%rip), %rsi     # 0x25757
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
movl %r14d, -0x64(%rbp)
movq %rbx, -0xb8(%rbp)
leaq -0x1f0(%rbp), %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)
leaq -0x448(%rbp), %rdi
callq 0x5450
cvttss2si -0x40(%rbp), %eax
movq %rax, -0x168(%rbp)
xorl %r14d, %r14d
leaq -0x448(%rbp), %r13
pushq $0x1
popq %rax
movl %eax, -0x34(%rbp)
leaq -0x60(%rbp), %r15
leaq -0x29(%rbp), %rbx
leaq -0x1b8(%rbp), %r12
movl $0x0, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x148(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0xba33
movq %r14, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x73fe
testl %eax, %eax
jne 0xbcb2
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
movq %r13, %rdi
leaq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x832b
movl -0x30(%rbp), %eax
testq %r14, %r14
leaq -0x198(%rbp), %r15
je 0xb73e
cmpl -0x44(%rbp), %eax
je 0xb741
jmp 0xbc86
movl %eax, -0x44(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
andq $0x0, -0xc0(%rbp)
xorl %eax, %eax
movl %eax, -0xa0(%rbp)
movslq %eax, %rcx
movq -0x58(%rbp), %rdx
subq -0x60(%rbp), %rdx
sarq $0x5, %rdx
cmpq %rcx, %rdx
jbe 0xb79f
addl -0x48(%rbp), %eax
movslq %eax, %rsi
movq %r15, %rdi
callq 0x903c
testq %rdx, (%rax)
je 0xb795
leaq -0xd0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xbe78
movl -0xa0(%rbp), %eax
incl %eax
jmp 0xb755
movq %r14, -0x150(%rbp)
movq -0xc8(%rbp), %rax
subq -0xd0(%rbp), %rax
shrq $0x2, %rax
testl %eax, %eax
je 0xb802
movq %rax, -0x140(%rbp)
movq %rsp, -0x138(%rbp)
movl -0x30(%rbp), %eax
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movq %rax, -0x170(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
sarq $0x5, %rax
js 0xb823
cvtsi2ss %rax, %xmm0
jmp 0xb838
movq %r13, %rdi
callq 0x50a0
movq -0x58(%rbp), %r14
subq -0x60(%rbp), %r14
leaq -0x60(%rbp), %r15
movq %r15, %rdi
callq 0xbe96
jmp 0xba08
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x19e30(%rip), %xmm0    # 0x25670
callq 0x5220
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0x160(%rbp)
movq %rdx, -0xb0(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0x158(%rbp)
movq %r13, %rdi
movq -0xb0(%rbp), %rsi
movq -0x160(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xb9e0
movq %r13, %r14
movq %r12, %r13
movq %rbx, %r12
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xf0(%rbp), %rbx
movq %rbx, %rdi
movq -0xb0(%rbp), %rsi
movq -0x158(%rbp), %rdx
callq 0x7828
andq $0x0, -0xa0(%rbp)
andl $0x0, -0x98(%rbp)
andq $0x0, -0x90(%rbp)
andl $0x0, -0x88(%rbp)
andq $0x0, -0x80(%rbp)
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x7bb6
movq %r12, %rbx
movq %r13, %r12
movq %r14, %r13
leaq -0x220(%rbp), %r14
movq %r13, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0xb9b3
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5550
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xb9b3
movq %r13, %rdi
movq %r15, %rsi
movq -0x170(%rbp), %rdx
callq 0x54c0
movl -0x30(%rbp), %eax
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq -0x1a8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
addq %r15, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x7828
movsbl -0x29(%rbp), %edx
movq %r14, %rdi
movq %r12, %rsi
leaq -0xa0(%rbp), %rcx
leaq -0xd0(%rbp), %r8
movl -0x40(%rbp), %r9d
pushq -0x168(%rbp)
pushq -0xb8(%rbp)
callq 0x88cb
popq %rax
popq %rcx
movq %r12, %rdi
callq 0x5778
jmp 0xb911
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
pushq $0xa
popq %rdx
callq 0x5260
leaq -0xa0(%rbp), %rdi
callq 0x908a
leaq -0xf0(%rbp), %rdi
callq 0x5778
jmp 0xb873
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r14
movq %r13, %rdi
callq 0x50a0
subq %r15, %r14
movq -0x140(%rbp), %rax
addl %eax, -0x40(%rbp)
movq -0x138(%rbp), %rsp
leaq -0x60(%rbp), %r15
shrq $0x5, %r14
addl %r14d, -0x48(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xc052
movq %r15, %rdi
callq 0x911e
movq -0x150(%rbp), %r14
incq %r14
jmp 0xb6cf
movq -0x208(%rbp), %rsi
movq 0x2a577(%rip), %rdi     # 0x35fb8
callq 0x5280
movq -0xb8(%rbp), %r13
leaq 0x19d21(%rip), %rsi     # 0x25775
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movl %r13d, %esi
callq 0x56a0
leaq 0x19a32(%rip), %rsi     # 0x254a0
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movq -0xa8(%rbp), %rsi
callq 0x56a0
leaq 0x1b039(%rip), %rsi     # 0x26ac5
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq -0xa0(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0xf0(%rbp), %rdi
pushq $0x4
popq %rsi
leaq -0x1b8(%rbp), %rcx
callq 0xbe22
movq -0x128(%rbp), %rbx
movq -0x120(%rbp), %r14
movq -0x130(%rbp), %r12
leaq -0x1b8(%rbp), %r9
andl $0x0, (%r9)
andl $0x0, -0x60(%rbp)
movzbl (%r12), %r8d
subq $0x8, %rsp
leaq -0x220(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl %r13d, %edx
movl -0x64(%rbp), %ecx
pushq %r15
callq 0xafc8
popq %rax
popq %rcx
movss -0x1b8(%rbp), %xmm0
movss -0x60(%rbp), %xmm1
movl -0x30(%rbp), %edi
callq 0x76d1
pushq $0x1
popq %rcx
movl %ecx, -0x34(%rbp)
testl %eax, %eax
jne 0xbc78
leaq 0x19c5d(%rip), %rdx     # 0x2578d
leaq -0xa0(%rbp), %rdi
movq %rbx, %rsi
callq 0xbe9e
leaq -0x1e8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0xbed6
movzbl (%r12), %r8d
leaq -0xa0(%rbp), %rdi
leaq -0x220(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
leaq -0x1e8(%rbp), %rcx
callq 0xb14d
leaq -0x1e8(%rbp), %rdi
callq 0x911e
leaq -0xa0(%rbp), %rdi
callq 0x5778
cmpb $0x1, (%r14)
jne 0xbc65
movq 0x2a417(%rip), %rdi     # 0x35fb8
leaq 0x19bea(%rip), %rsi     # 0x25792
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq -0x70(%rbp), %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)
leaq -0x220(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x8db9
movq -0x88(%rbp), %rsi
movq 0x2a3c0(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x19bab(%rip), %rsi     # 0x257af
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x1c107(%rip), %rdx     # 0x27d22
leaq -0x240(%rbp), %rdi
movq %rbx, %rsi
callq 0xbe9e
movl -0x30(%rbp), %ecx
leaq -0xa0(%rbp), %rdi
leaq -0x240(%rbp), %rsi
leaq -0x1d0(%rbp), %rdx
callq 0x80d4
movl %eax, %ebx
leaq -0x240(%rbp), %rdi
callq 0x5778
leaq -0xa0(%rbp), %rdi
callq 0xbf2a
testl %ebx, %ebx
jne 0xbc78
movl $0x0, -0x34(%rbp)
movq -0x118(%rbp), %rdi
callq 0x7657
leaq -0xf0(%rbp), %rdi
callq 0xc052
jmp 0xbcb2
movq 0x2a363(%rip), %rdi     # 0x35ff0
leaq 0x197e7(%rip), %rsi     # 0x2547b
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
leaq -0x60(%rbp), %rdi
callq 0x911e
leaq -0x448(%rbp), %rdi
callq 0x50e0
leaq -0x220(%rbp), %rdi
callq 0xbfa8
leaq -0x1d0(%rbp), %rdi
callq 0x911e
leaq -0x198(%rbp), %rdi
callq 0x908a
leaq -0x110(%rbp), %rdi
callq 0x911e
movl -0x34(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
callq 0x5778
jmp 0xbd38
movq %rax, %rbx
leaq -0x1e8(%rbp), %rdi
callq 0x911e
jmp 0xbd25
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x5778
jmp 0xbd61
jmp 0xbd5e
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0xbf2a
jmp 0xbd61
jmp 0xbd84
jmp 0xbd7f
movq %rax, %rbx
jmp 0xbdea
jmp 0xbd89
movq %rax, %rbx
jmp 0xbe02
jmp 0xbd6f
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0xc052
jmp 0xbdde
movq %rax, %rbx
jmp 0xbe0e
jmp 0xbd84
jmp 0xbd89
jmp 0xbd89
jmp 0xbd94
movq %rax, %rbx
jmp 0xbdd5
movq %rax, %rbx
jmp 0xbdde
movq %rax, %rbx
jmp 0xbdf6
jmp 0xbd94
jmp 0xbd94
jmp 0xbd94
movq %rax, %rbx
jmp 0xbdc9
jmp 0xbdae
jmp 0xbdae
movq %rax, %rbx
leaq -0x1b8(%rbp), %rdi
callq 0x5778
jmp 0xbdb1
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x908a
leaq -0xf0(%rbp), %rdi
callq 0x5778
leaq -0xd0(%rbp), %rdi
callq 0xc052
leaq -0x60(%rbp), %rdi
callq 0x911e
leaq -0x448(%rbp), %rdi
callq 0x50e0
leaq -0x220(%rbp), %rdi
callq 0xbfa8
leaq -0x1d0(%rbp), %rdi
callq 0x911e
leaq -0x198(%rbp), %rdi
callq 0x908a
leaq -0x110(%rbp), %rdi
callq 0x911e
movq %rbx, %rdi
callq 0x56c0