// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_merge.cpp
// Function: mergeKmerFiles(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&, 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 mergeKmerFiles(const std::string & outfile, const std::vector < std::string > & kmerfiles, 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();
	
	// Create the kmer map
	std::unordered_map < std::string, std::vector < bool > > kmerMap;
	
	std::vector< int > kmerCounts;
	int kmersize=0;
	std::vector < std::string > sampleNames;

	if (collectSampleNames(kmerfiles, sampleNames)!=0){return 1;}

	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 it=include.begin(); it!=include.end(); ++it){//make a vector of all included sample names to help printing output files later
		if (*it){
			includedSampleNames.push_back(sampleNames[distance(include.begin(), it)]);
		}
	}

	//int numSamples=sampleNames.size();
	int numSamples = std::count(include.begin(), include.end(), true);

	std::vector < std::string > fileSampleNames;

	int sampleNum=0;
	int includedSampleNum=0;
	std::ifstream fileStream;

	for (int s = 0; s < kmerfiles.size(); ++s){

		if (openFileStream(kmerfiles[s], fileStream)){return 1;};

		int newkmersize;
		
		readKmerHeader(fileStream, newkmersize, fileSampleNames);
		
		if (s==0){
			kmersize=newkmersize;
		}

		if (newkmersize!=kmersize){
			std::cout << "kmer files have different kmer sizes" << std::endl << std::endl;
			return 0;
		}

		std::vector < int > fileInclude;

		for (int i=0; i<fileSampleNames.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);
			}
		}
		if (fileInclude.size()==0){ // if no sample names in the file are going to be included then don't read the file
			fileStream.close();
			sampleNum+=int(fileSampleNames.size());
			fileSampleNames.clear();
			continue;
		}

		//int numFileSamples=fileSampleNames.size();
		int numFileSamples = fileInclude.size();

		char basebuffer[1];
		char kmerbuffer[(kmersize*2/3)+1];
		char asciibuffer[int(std::ceil(float(fileSampleNames.size())/6))];

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

			int mybitcount=0;
			for (int i=0; i< fileInclude.size(); ++i){
				if (mybits[fileInclude[i]]){
					mybitcount++;
				}
			}

			if (mybitcount==0){
				std::string line;
				std::getline(fileStream, line);
				continue;
			}

			while (fileStream.peek()!='\n' && fileStream.read(kmerbuffer, sizeof(kmerbuffer))){
				std::string kmer (kmerbuffer, (kmersize*2/3)+1);
				
				std::unordered_map < std::string, std::vector < bool > >::iterator it = kmerMap.find(kmer);//check if the kmer is in the map
				if ( it != kmerMap.end() ){//if the kmer is in the map
					for (int i=0; i<fileInclude.size(); ++i){
						it->second[i+includedSampleNum]=mybits[fileInclude[i]];
					}
				}
				else {//if the kmer is not in the hash
					std::vector < bool > taxonBitset(numSamples, false);//create a new vector of bools for all samples
					for (int i=0; i<fileInclude.size(); ++i){ //change the bools to true based for taxa in the file containing the kmer
						taxonBitset[i+includedSampleNum]=mybits[fileInclude[i]];
					}
					kmerMap.insert(std::make_pair(kmer, taxonBitset));//add the new vector to the map
				}
	    	}
	    	fileStream.ignore(256,'\n');//skip the end of line character
    	}
    	sampleNum+=numFileSamples; //add the number of samples in the file to the count of total samples
    	includedSampleNum+=fileInclude.size();
    	fileSampleNames.clear();
		fileStream.close();
	}

	std::cout << kmerMap.size() << " kmers identified from " << includedSampleNum << " samples in " << numfiles << " files" << std::endl;
	
	std::cout << "Merging..." << std::endl;

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

	reverseVectorBoolKmerMap(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, outfile, includedSampleNames, kmersize)){return 1;}
	
	printDuration(start);//stop the clock
	
	return 0;
		
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
callq 0x5050
movq %rax, %r14
movq 0x8(%r13), %r12
subq (%r13), %r12
leaq -0x278(%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)
movaps %xmm0, -0x230(%rbp)
andq $0x0, -0x220(%rbp)
leaq -0xb0(%rbp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
pushq $0x1
popq %rdx
movq %r13, %rdi
movl %edx, -0x2c(%rbp)
callq 0x8436
testl %eax, %eax
jne 0x1580e
movq -0xa8(%rbp), %rsi
subq -0xb0(%rbp), %rsi
sarq $0x5, %rsi
leaq -0x188(%rbp), %rdi
leaq -0x4b0(%rbp), %rdx
callq 0x9076
movq %r15, -0x100(%rbp)
movq %r14, -0x108(%rbp)
leaq -0xb0(%rbp), %rsi
leaq -0x188(%rbp), %rdx
movq %rbx, %rdi
callq 0x8605
shrq $0x5, %r12
movq %r12, -0x110(%rbp)
xorps %xmm0, %xmm0
leaq -0x210(%rbp), %rbx
andq $0x0, 0x10(%rbx)
movaps %xmm0, (%rbx)
xorl %r15d, %r15d
movq -0x188(%rbp), %r12
xorl %r14d, %r14d
movq -0x178(%rbp), %rdx
cmpq %rdx, %r12
jne 0x150b2
cmpl -0x170(%rbp), %r14d
je 0x150fd
movq (%r12), %rax
btq %r14, %rax
jae 0x150e3
movl %r14d, %esi
movq %r12, %rax
subq -0x188(%rbp), %rax
shlq $0x8, %rax
addq -0xb0(%rbp), %rax
shlq $0x5, %rsi
addq %rax, %rsi
movq %rbx, %rdi
callq 0x77fc
xorl %eax, %eax
cmpl $0x3f, %r14d
leal 0x1(%r14), %ecx
sete %al
leaq (%r12,%rax,8), %r12
cmovel %r15d, %ecx
movl %ecx, %r14d
jmp 0x1509d
movq -0x188(%rbp), %rdi
leaq -0x4b0(%rbp), %r8
movb $0x1, (%r8)
xorl %esi, %esi
movl %r14d, %ecx
callq 0xc10d
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0x5450
movslq %ebx, %rax
movq %rax, -0x140(%rbp)
xorl %r14d, %r14d
leaq -0x4b0(%rbp), %r15
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
leaq -0xf8(%rbp), %r12
leaq -0x1f8(%rbp), %rbx
movl $0x0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movq %r13, -0x118(%rbp)
movq (%r13), %rdi
movq 0x8(%r13), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x15667
movq %r14, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x73fe
testl %eax, %eax
jne 0x157e1
movq %r15, %rdi
leaq -0x7c(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x832b
movq %r14, -0x88(%rbp)
testq %r14, %r14
sete %al
movl -0x7c(%rbp), %r14d
cmpl %r14d, -0x34(%rbp)
sete %cl
orb %al, %cl
je 0x1579a
movl %r14d, -0x34(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
xorl %eax, %eax
movl %eax, -0xf8(%rbp)
movslq %eax, %rcx
movq -0x68(%rbp), %rdx
subq -0x70(%rbp), %rdx
sarq $0x5, %rdx
cmpq %rcx, %rdx
jbe 0x15236
addl -0x38(%rbp), %eax
movslq %eax, %rsi
leaq -0x188(%rbp), %rdi
callq 0x903c
testq %rdx, (%rax)
je 0x1522c
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0xbe78
movl -0xf8(%rbp), %eax
incl %eax
jmp 0x151ef
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rax
subq %rax, %rsi
je 0x1528e
movq %rsp, -0x120(%rbp)
addl %r14d, %r14d
movl %r14d, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movl %eax, %r14d
leal 0x1(%r14), %eax
movq %rax, -0x150(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
js 0x152ba
cvtsi2ss %rax, %xmm0
jmp 0x152cf
movq %r15, %rdi
callq 0x50a0
movq -0x68(%rbp), %r14
subq -0x70(%rbp), %r14
sarq $0x5, %r14
leaq -0x70(%rbp), %rdi
callq 0xbe96
movq %r14, %rax
movq -0x88(%rbp), %r14
jmp 0x15653
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
sarq $0x2, %rsi
movq %rsi, -0x128(%rbp)
divss 0x1038e(%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, -0x138(%rbp)
movq %rdx, -0x90(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0x130(%rbp)
movslq %r14d, %rax
movq -0x78(%rbp), %rcx
addq %rcx, %rax
incq %rax
movq %rax, -0x148(%rbp)
movslq -0x30(%rbp), %r13
movq %r15, %rdi
movq -0x90(%rbp), %rsi
movq -0x138(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x15609
leaq -0x240(%rbp), %rax
movq %rax, -0x250(%rbp)
leaq -0x250(%rbp), %r14
movq %r14, %rdi
movq -0x90(%rbp), %rsi
movq -0x130(%rbp), %rdx
callq 0x7828
andq $0x0, -0x1f8(%rbp)
andl $0x0, -0x1f0(%rbp)
andq $0x0, -0x1e8(%rbp)
andl $0x0, -0x1e0(%rbp)
andq $0x0, -0x1d8(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7bb6
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0x153e7
movslq (%rax,%r14,4), %rsi
movq %rbx, %rdi
callq 0x903c
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbl $-0x1, %r15d
incq %r14
jmp 0x153b7
testl %r15d, %r15d
leaq -0x1d0(%rbp), %r14
je 0x155b9
leaq -0x4b0(%rbp), %r15
movq %r15, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x155a7
movq %r15, %rdi
movq -0x78(%rbp), %rsi
movq -0x150(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x155a7
leaq -0x1c0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq %r14, %rdi
movq -0x78(%rbp), %rsi
movq -0x148(%rbp), %rdx
callq 0x7828
leaq -0x2a8(%rbp), %rdi
movq %r14, %rsi
callq 0x15a68
movq %rax, %r14
testq %rax, %rax
je 0x154d3
addq $0x28, %r14
xorl %r15d, %r15d
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
jbe 0x15593
movslq (%rax,%r15,4), %rsi
movq %rbx, %rdi
callq 0x903c
movq %rax, -0xf8(%rbp)
movq %rdx, -0xf0(%rbp)
leaq (%r15,%r13), %rsi
movq %r14, %rdi
callq 0x903c
movq %rax, -0x1b0(%rbp)
movq %rdx, -0x1a8(%rbp)
leaq -0x1b0(%rbp), %rdi
movq %r12, %rsi
callq 0x9896
incq %r15
jmp 0x15473
movb $0x0, -0xf8(%rbp)
leaq -0x1b0(%rbp), %rdi
movq -0x140(%rbp), %rsi
movq %r12, %rdx
leaq -0x160(%rbp), %rcx
callq 0x9140
xorl %r14d, %r14d
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0x1555a
movslq (%rax,%r14,4), %rsi
movq %rbx, %rdi
callq 0x903c
movq %rax, -0xf8(%rbp)
movq %rdx, -0xf0(%rbp)
leaq (%r14,%r13), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x903c
movq %rax, -0x160(%rbp)
movq %rdx, -0x158(%rbp)
leaq -0x160(%rbp), %rdi
movq %r12, %rsi
callq 0x9896
incq %r14
jmp 0x154fa
movq %r12, %rdi
leaq -0x1d0(%rbp), %rsi
leaq -0x1b0(%rbp), %rdx
callq 0x1598e
leaq -0x2a8(%rbp), %rdi
movq %r12, %rsi
callq 0x15b38
movq %r12, %rdi
callq 0x15978
leaq -0x1b0(%rbp), %rdi
callq 0x908a
leaq -0x1d0(%rbp), %r14
movq %r14, %rdi
callq 0x5778
jmp 0x153f7
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
pushq $0xa
popq %rdx
callq 0x5260
jmp 0x155f0
leaq -0xe8(%rbp), %rax
movq %rax, -0xf8(%rbp)
andq $0x0, -0xf0(%rbp)
movb $0x0, -0xe8(%rbp)
leaq -0x4b0(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5080
movq %r12, %rdi
callq 0x5778
movq %rbx, %rdi
callq 0x908a
leaq -0x250(%rbp), %rdi
callq 0x5778
jmp 0x1532d
movq %r15, %r13
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %r14
leaq -0x70(%rbp), %rdi
callq 0xbe96
movq %r13, %rdi
callq 0x50a0
subq %r15, %r14
shrq $0x2, %r14
addl %r14d, -0x30(%rbp)
movq -0x120(%rbp), %rsp
movq -0x118(%rbp), %r13
movq -0x88(%rbp), %r14
leaq -0x4b0(%rbp), %r15
movq -0x128(%rbp), %rax
addl %eax, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xc052
incq %r14
jmp 0x15178
movq -0x290(%rbp), %rsi
movq 0x20943(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x100f4(%rip), %rsi     # 0x25775
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x56a0
leaq 0xfe05(%rip), %rsi      # 0x254a0
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movq -0x110(%rbp), %rsi
callq 0x56a0
leaq 0x1140c(%rip), %rsi     # 0x26ac5
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq 0x208e8(%rip), %rdi     # 0x35fb8
leaq 0x10cb6(%rip), %rsi     # 0x2638d
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq -0xc8(%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 -0x2a8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8ca0
movq -0x108(%rbp), %r14
movq -0x100(%rbp), %rbx
movq -0xe0(%rbp), %rsi
movq 0x20880(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x1006b(%rip), %rsi     # 0x257af
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq -0x270(%rbp), %rdi
movq %rbx, %rsi
callq 0x5540
leaq -0xf8(%rbp), %rdi
leaq -0x270(%rbp), %rsi
leaq -0x210(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0x80d4
movl %eax, %ebx
leaq -0x270(%rbp), %rdi
callq 0x5778
testl %ebx, %ebx
je 0x157c6
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x157d5
movq 0x20817(%rip), %rdi     # 0x35fb8
leaq 0xfcd3(%rip), %rsi      # 0x2547b
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
movl $0x0, -0x2c(%rbp)
jmp 0x157e1
movl $0x0, -0x2c(%rbp)
movq %r14, %rdi
callq 0x7657
leaq -0xf8(%rbp), %rdi
callq 0xbf2a
leaq -0x4b0(%rbp), %rdi
callq 0x50e0
leaq -0x70(%rbp), %rdi
callq 0x911e
leaq -0x210(%rbp), %rdi
callq 0x911e
leaq -0x188(%rbp), %rdi
callq 0x908a
leaq -0xb0(%rbp), %rdi
callq 0x911e
leaq -0x230(%rbp), %rdi
callq 0xc052
leaq -0x2a8(%rbp), %rdi
callq 0x159c4
movl -0x2c(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x270(%rbp), %rdi
callq 0x5778
jmp 0x1586e
jmp 0x1588f
movq %rax, %rbx
jmp 0x1592b
jmp 0x15897
movq %rax, %rbx
jmp 0x15940
jmp 0x1587f
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0xbf2a
jmp 0x1591f
movq %rax, %rbx
jmp 0x1594c
jmp 0x1588f
jmp 0x158b5
jmp 0x158b5
jmp 0x1588f
movq %rax, %rbx
jmp 0x1591f
movq %rax, %rbx
jmp 0x15934
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
jmp 0x158f9
jmp 0x158d5
jmp 0x158b5
jmp 0x158d5
jmp 0x158b5
jmp 0x158b5
movq %rax, %rbx
jmp 0x15916
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x15978
jmp 0x158df
jmp 0x158dc
jmp 0x158ef
jmp 0x158ef
jmp 0x158d5
jmp 0x158d5
movq %rax, %rbx
jmp 0x158fe
jmp 0x158dc
movq %rax, %rbx
leaq -0x1b0(%rbp), %rdi
callq 0x908a
jmp 0x158f2
jmp 0x158ef
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x5778
leaq -0x1f8(%rbp), %rdi
callq 0x908a
leaq -0x250(%rbp), %rdi
callq 0x5778
leaq -0x50(%rbp), %rdi
callq 0xc052
leaq -0x4b0(%rbp), %rdi
callq 0x50e0
leaq -0x70(%rbp), %rdi
callq 0x911e
leaq -0x210(%rbp), %rdi
callq 0x911e
leaq -0x188(%rbp), %rdi
callq 0x908a
leaq -0xb0(%rbp), %rdi
callq 0x911e
leaq -0x230(%rbp), %rdi
callq 0xc052
leaq -0x2a8(%rbp), %rdi
callq 0x159c4
movq %rbx, %rdi
callq 0x56c0