// File: /workspace/github_cpp_makefile_star2_O1/CominLab[P]QCluster/freqs.cc
// Function: fill_overlap_count_vector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, double*, double*, int, double, double*, double**, double*, bool)
void fill_overlap_count_vector(string seq, string seq_qual, int K,
						double* freq, double *quality_vector, int normalize,
						double pc, double *avg_quality_1, double **avg_quality, 
						double *freq_1, bool redistribute)
{
	int L = seq.length();
	int *kmer = new int[K];
	int *qual = new int[K];
	double *prob = new double[K];
	double *freq_bases = new double[NUM_NT];
	
	//If K==1, freq_1 is NULL and freq_bases is initialized to 1/4 for each base
	if (K==1){
		for (int i=0; i<NUM_NT; i++) freq_bases[i] = 1.0/4.0;
	}//Otherwise, freq_bases is the normalization of freq_1
	else{
		double somma =0;
		for (int i=0; i<NUM_NT; i++) somma += freq_1[i];
		for (int i=0; i<NUM_NT; i++) freq_bases[i] = freq_1[i]/somma;
	}
	
	double readqual=1;
	for(int i=0; i<L; ++i){
		seq[i] = toupper(seq[i]);
	}
	int N = 1;  // number of K-mers
	for(int k=0; k<K; ++k){
		N *= NUM_NT;
	}
	for(int i=0; i<N; ++i){
		freq[i] = pc;  // initialize with the pseudocount
		quality_vector[i] = pc;
	}
	bool valid_kmer = true;
	int index_kmer = 0;
	
	for (int i=0; i<L-K+1; i++){
		valid_kmer = true;
		index_kmer = 0;
		readqual = 1;
		//Fill the kmer, qual and prob vectors
		for (int j=0; j<K; j++){
			if (!is_valid_nt(seq[i+j])){
					valid_kmer = false;
					break;
				}
			kmer[j] = nt2int(seq[i+j]);
			qual[j] = int(seq_qual[i+j])-base;
			prob[j] = (1.0 - pow(10.0,-(qual[j])/10.0));
			readqual *= prob[j];
			//Calculate vector index of the kmer
			index_kmer = NUM_NT*index_kmer + kmer[j];
		}

		if (!valid_kmer) continue;

		freq[index_kmer] += 1;
		quality_vector[index_kmer] += readqual;
		
		if (avg_quality != NULL)
			for (int j=0; j<K; j++) avg_quality[index_kmer][kmer[j]] += qual[j];
			
		if (avg_quality_1 != NULL) 
			avg_quality_1[kmer[0]] += qual[0];
		
		if (!redistribute) continue;
		
		//Redistributing quality: for each letter in the kmer...
		for (int j=0; j<K; j++){
			int original_letter = kmer[j];
			double original_prob = prob[j];
			//...we try to replace it with another
			for (int letter=0; letter<NUM_NT; letter++){
				//we must redistribute also to the same letter
				//if (letter == original_letter) continue;
				kmer[j] = letter;
				//Equally subdivided probability
				//prob[j] = (1.0-original_prob) / double(NUM_NT-1.0);
				//Probability subidivided basing on the letter frequency
				prob[j] = (1.0-original_prob) * double(freq_bases[letter]);
				
				//Recalculation of the new kmer probability
				readqual = 1;
				index_kmer = 0;
				for (int f=0; f<K; f++){
					readqual *= prob[j];
					index_kmer = NUM_NT*index_kmer + kmer[j];
				}
				//Add a little probability to that kmer
				quality_vector[index_kmer] += readqual;
			}
			kmer[j] = original_letter;
			prob[j] = original_prob;
		}
	}
	
	
	switch(normalize){
		case 1:	
			normalize_row(freq, N);
			normalize_row(quality_vector, N);
			break;
		case 2:
			normalize_row_against(freq, quality_vector, N);
			normalize_row(freq, N);
			break;
		case 3:
			normalize_row_against(freq, quality_vector, N);
			normalize_row(freq, N);
			normalize_row_projection(freq, quality_vector, N);
			break;
		default:
			break;
	}
	
	delete[] kmer;
	delete[] qual;
	delete[] prob;
	if (K==1) delete[] freq_bases;
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x40(%rsp)
movl %r9d, 0x64(%rsp)
movq %r8, %rbp
movq %rcx, 0x20(%rsp)
movl %edx, %ebx
movq %rsi, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xd0(%rsp), %r14
movb 0xd8(%rsp), %al
movb %al, 0x30(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x10(%rsp)
movslq %edx, %r13
leaq (,%r13,4), %r15
testl %r13d, %r13d
movq $-0x1, %rax
cmovsq %rax, %r15
leaq (,%r13,8), %r12
cmovsq %rax, %r12
movq %r15, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x28(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x48(%rsp)
movl $0x20, %edi
callq 0x2030
movq %rax, %r15
cmpl $0x1, %r13d
jne 0x5889
xorl %eax, %eax
movabsq $0x3fd0000000000000, %rcx # imm = 0x3FD0000000000000
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x587a
jmp 0x58b9
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%r14,%rax,8), %xmm0
incq %rax
cmpq $0x4, %rax
jne 0x588f
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x58a0
cmpl $0x0, 0x10(%rsp)
movq 0x50(%rsp), %rax
jle 0x58fc
movq 0x10(%rsp), %r14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq %r14, 0x58(%rsp)
xorl %r12d, %r12d
movq (%rax), %r13
movsbl (%r13,%r12), %edi
movq %rax, %r14
callq 0x2380
movb %al, (%r13,%r12)
movq %r14, %rax
incq %r12
cmpq %r12, 0x58(%rsp)
jne 0x58d9
movl $0x1, %ecx
testl %ebx, %ebx
jle 0x5920
movl %ebx, %eax
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
shll $0x2, %ecx
decl %eax
jne 0x5917
jmp 0x5930
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %rdx
movsd 0x40(%rsp), %xmm0
cmpl $0x1, %ecx
movl %ecx, 0x3c(%rsp)
movl %ecx, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
movsd %xmm0, (%rdx,%rcx,8)
movsd %xmm0, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x593e
movq 0x10(%rsp), %rax
subl %ebx, %eax
movq %rax, 0x10(%rsp)
jl 0x5c1e
cmpq $0x0, 0x68(%rsp)
sete %al
testl %ebx, %ebx
setle %dl
setg %sil
movq 0x10(%rsp), %rcx
incl %ecx
movq %rcx, 0x10(%rsp)
movl %ebx, %edi
orb %al, %dl
movb %dl, 0xf(%rsp)
andb 0x30(%rsp), %sil
movb %sil, 0xe(%rsp)
xorl %r14d, %r14d
movsd 0x266a(%rip), %xmm4     # 0x8008
movq %rdi, 0x30(%rsp)
testl %ebx, %ebx
movq 0x50(%rsp), %rax
movq %r14, 0x78(%rsp)
jle 0x5b09
xorl %r13d, %r13d
xorl %ecx, %ecx
movapd %xmm4, %xmm1
movsd %xmm1, 0x40(%rsp)
movq %rcx, 0x58(%rsp)
movq (%rax), %rax
movsbl (%rax,%r14), %r12d
movl %r12d, %edi
callq 0x2380
movsd 0x2627(%rip), %xmm4     # 0x8008
movl $0xff, %ecx
andl %ecx, %eax
addl $-0x41, %eax
cmpl $0x13, %eax
movq 0x28(%rsp), %r9
ja 0x5c06
movl $0x80045, %ecx          # imm = 0x80045
btl %eax, %ecx
jae 0x5c06
movl $0xff, %eax
andl %eax, %r12d
xorl %eax, %eax
cmpl $0x60, %r12d
jg 0x5a2b
cmpl $0x46, %r12d
jg 0x5a44
cmpl $0x41, %r12d
je 0x5a6f
cmpl $0x43, %r12d
je 0x5a3d
jmp 0x5a50
cmpl $0x66, %r12d
jg 0x5a57
cmpl $0x61, %r12d
je 0x5a6f
cmpl $0x63, %r12d
jne 0x5a50
movl $0x1, %eax
jmp 0x5a6f
cmpl $0x47, %r12d
je 0x5a6a
cmpl $0x54, %r12d
je 0x5a63
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5a6f
cmpl $0x67, %r12d
je 0x5a6a
cmpl $0x74, %r12d
jne 0x5a50
movl $0x3, %eax
jmp 0x5a6f
movl $0x2, %eax
movq 0x18(%rsp), %r12
movl %eax, (%r12,%r13,4)
movq 0x80(%rsp), %rax
movq (%rax), %rax
movsbl (%rax,%r14), %eax
leal -0x21(%rax), %ecx
movl %ecx, (%r9,%r13,4)
movl $0x21, %ecx
subl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x306b(%rip), %xmm0     # 0x8b10
divsd %xmm0, %xmm1
callq 0x2320
movsd 0x2552(%rip), %xmm4     # 0x8008
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
movq 0x48(%rsp), %rax
movsd %xmm1, (%rax,%r13,8)
movsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
movapd %xmm0, %xmm1
movslq 0x58(%rsp), %rax
movslq (%r12,%r13,4), %rcx
leaq (%rcx,%rax,4), %rcx
incq %r13
incq %r14
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
movq 0x50(%rsp), %rax
jne 0x59be
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %r9
jmp 0x5b14
movapd %xmm4, %xmm1
xorl %ecx, %ecx
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
addsd %xmm4, %xmm0
movsd %xmm0, (%rax,%rcx,8)
addsd (%rbp,%rcx,8), %xmm1
movsd %xmm1, (%rbp,%rcx,8)
cmpb $0x0, 0xf(%rsp)
jne 0x5b64
movq 0x68(%rsp), %rax
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sdl (%r9,%rcx,4), %xmm0
movslq (%rsi,%rcx,4), %rdx
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rcx
cmpq %rcx, %rdi
jne 0x5b45
movq 0x70(%rsp), %rcx
testq %rcx, %rcx
je 0x5b83
xorps %xmm0, %xmm0
cvtsi2sdl (%r9), %xmm0
movslq (%rsi), %rax
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
cmpb $0x0, 0xe(%rsp)
je 0x5c06
xorl %eax, %eax
movq 0x18(%rsp), %r8
movl (%r8,%rax,4), %ecx
movq 0x48(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm0
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
xorl %edx, %edx
movq 0x30(%rsp), %r10
movsd (%r15,%rdx,8), %xmm2
mulsd %xmm1, %xmm2
xorl %esi, %esi
movl %ebx, %edi
movapd %xmm4, %xmm3
mulsd %xmm2, %xmm3
leal (%rdx,%rsi,4), %esi
decl %edi
jne 0x5bc0
movslq %esi, %rsi
addsd (%rbp,%rsi,8), %xmm3
movsd %xmm3, (%rbp,%rsi,8)
incq %rdx
cmpq $0x4, %rdx
jne 0x5bae
movl $0x3, (%r8,%rax,4)
movq 0x48(%rsp), %rdx
movsd %xmm2, (%rdx,%rax,8)
movl %ecx, (%r8,%rax,4)
movsd %xmm0, (%rdx,%rax,8)
incq %rax
cmpq %r10, %rax
jne 0x5b8c
movq 0x78(%rsp), %r14
incq %r14
cmpq 0x10(%rsp), %r14
movq 0x30(%rsp), %rdi
jne 0x59a3
movl 0x64(%rsp), %eax
cmpl $0x1, %eax
je 0x5d41
cmpl $0x2, %eax
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rdi
movl 0x3c(%rsp), %edx
je 0x5cc6
cmpl $0x3, %eax
jne 0x5dc2
xorpd %xmm0, %xmm0
movl %edx, %eax
xorpd %xmm1, %xmm1
testl %edx, %edx
jle 0x5c68
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm1
incq %rcx
cmpq %rcx, %rax
jne 0x5c5b
testl %edx, %edx
jle 0x5c9d
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5c6e
testl %edx, %edx
jle 0x5c9d
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5c90
testl %edx, %edx
jle 0x5cb9
xorl %ecx, %ecx
movsd (%rdi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5ca3
movq %rbp, %rsi
callq 0x5758
jmp 0x5dc2
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
testl %edx, %edx
jle 0x5ce3
movl %edx, %eax
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm1
incq %rcx
cmpq %rcx, %rax
jne 0x5cd6
testl %edx, %edx
jle 0x5d1a
movl %edx, %eax
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5ceb
testl %edx, %edx
jle 0x5d1a
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5d0d
testl %edx, %edx
jle 0x5dc2
movl %edx, %eax
xorl %ecx, %ecx
movsd (%rdi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5d26
jmp 0x5dc2
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movl 0x3c(%rsp), %esi
testl %esi, %esi
movq 0x20(%rsp), %rdx
jle 0x5d67
movl %esi, %eax
xorl %ecx, %ecx
addsd (%rdx,%rcx,8), %xmm1
incq %rcx
cmpq %rcx, %rax
jne 0x5d5a
testl %esi, %esi
movq 0x18(%rsp), %r14
jle 0x5da2
movl %esi, %eax
xorl %ecx, %ecx
movsd (%rdx,%rcx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5d74
testl %esi, %esi
jle 0x5da2
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rbp,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5d94
testl %esi, %esi
jle 0x5dc2
movl %esi, %eax
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5daa
movq %r14, %rdi
callq 0x2270
movq 0x28(%rsp), %rdi
callq 0x2270
movq 0x48(%rsp), %rdi
callq 0x2270
cmpl $0x1, %ebx
jne 0x5dfc
movq %r15, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2270
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq