// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movl %edx, %r13d
movq %rsi, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xd0(%rsp), %rbx
movb 0xd8(%rsp), %al
movb %al, 0x58(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x8(%rdi), %rax
movq %rax, 0x8(%rsp)
movslq %edx, %r14
leaq (,%r14,4), %r15
testl %r14d, %r14d
movq $-0x1, %rax
cmovsq %rax, %r15
leaq (,%r14,8), %r12
cmovsq %rax, %r12
movq %r15, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x48(%rsp)
movl $0x20, %edi
callq 0x2030
movq %rax, %r15
cmpl $0x1, %r14d
movl %r13d, 0x14(%rsp)
jne 0x5899
movapd 0x33e4(%rip), %xmm0     # 0x8c70
movupd %xmm0, (%r15)
movupd %xmm0, 0x10(%r15)
jmp 0x58cc
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%rbx,%rax,8), %xmm0
incq %rax
cmpq $0x4, %rax
jne 0x589f
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorl %eax, %eax
movupd (%rbx,%rax,8), %xmm1
divpd %xmm0, %xmm1
movupd %xmm1, (%r15,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x58b3
cmpl $0x0, 0x8(%rsp)
movq 0x38(%rsp), %r13
jle 0x5900
movq 0x8(%rsp), %rbx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq (%r13), %r12
movsbl (%r12,%r14), %edi
callq 0x2380
movb %al, (%r12,%r14)
incq %r14
cmpq %r14, %rbx
jne 0x58e6
movl 0x14(%rsp), %r14d
movl $0x1, %ecx
testl %r14d, %r14d
jle 0x5935
movl %r14d, %eax
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rbx
movsd 0x40(%rsp), %xmm0
shll $0x2, %ecx
decl %eax
jne 0x592c
jmp 0x594f
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rbx
movsd 0x40(%rsp), %xmm0
cmpl $0x1, %ecx
movl %ecx, 0x2c(%rsp)
movl %ecx, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
movsd %xmm0, (%rsi,%rcx,8)
movsd %xmm0, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x595d
movq 0x8(%rsp), %rax
subl %r14d, %eax
movq %rax, 0x8(%rsp)
jl 0x5c2b
cmpq $0x0, 0x68(%rsp)
sete %al
testl %r14d, %r14d
setg %dl
setle %r8b
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, 0x8(%rsp)
movl %r14d, %ecx
movq %rcx, 0x50(%rsp)
orb %al, %r8b
movb %r8b, 0x6(%rsp)
andb 0x58(%rsp), %dl
movb %dl, 0x7(%rsp)
xorl %r13d, %r13d
movsd 0x2643(%rip), %xmm4     # 0x8008
testl %r14d, %r14d
movq %r13, 0x78(%rsp)
jle 0x5b26
xorl %r14d, %r14d
xorl %eax, %eax
movapd %xmm4, %xmm1
movq %rax, 0x58(%rsp)
movsd %xmm1, 0x40(%rsp)
movq (%rbx), %rax
movsbl (%rax,%r13), %r12d
movl %r12d, %edi
callq 0x2380
movsd 0x2609(%rip), %xmm4     # 0x8008
movl $0xff, %ecx
andl %ecx, %eax
addl $-0x41, %eax
cmpl $0x13, %eax
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r10
ja 0x5c0e
movl $0x80045, %ecx          # imm = 0x80045
btl %eax, %ecx
jae 0x5c0e
movl $0xff, %eax
andl %eax, %r12d
xorl %ebx, %ebx
cmpl $0x60, %r12d
jg 0x5a4e
cmpl $0x46, %r12d
jg 0x5a67
cmpl $0x41, %r12d
je 0x5a92
cmpl $0x43, %r12d
je 0x5a60
jmp 0x5a73
cmpl $0x66, %r12d
jg 0x5a7a
cmpl $0x61, %r12d
je 0x5a92
cmpl $0x63, %r12d
jne 0x5a73
movl $0x1, %ebx
jmp 0x5a92
cmpl $0x47, %r12d
je 0x5a8d
cmpl $0x54, %r12d
je 0x5a86
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x5a92
cmpl $0x67, %r12d
je 0x5a8d
cmpl $0x74, %r12d
jne 0x5a73
movl $0x3, %ebx
jmp 0x5a92
movl $0x2, %ebx
movl %ebx, (%rdi,%r14,4)
movq 0x80(%rsp), %rax
movq (%rax), %rax
movsbl (%rax,%r13), %eax
leal -0x21(%rax), %ecx
movl %ecx, (%r10,%r14,4)
movl $0x21, %ecx
subl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x307d(%rip), %xmm0     # 0x8b40
divsd %xmm0, %xmm1
callq 0x2320
movsd 0x2534(%rip), %xmm4     # 0x8008
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
movq 0x48(%rsp), %rax
movsd %xmm1, (%rax,%r14,8)
movsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
movapd %xmm0, %xmm1
movq 0x58(%rsp), %rax
leal (%rbx,%rax,4), %eax
incq %r14
incq %r13
cmpq %r14, 0x50(%rsp)
movq 0x38(%rsp), %rbx
jne 0x59dc
cltq
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x30(%rsp), %rsi
jmp 0x5b2c
movapd %xmm4, %xmm1
xorl %eax, %eax
movsd (%rsi,%rax,8), %xmm0
addsd %xmm4, %xmm0
movsd %xmm0, (%rsi,%rax,8)
addsd (%rbp,%rax,8), %xmm1
movsd %xmm1, (%rbp,%rax,8)
cmpb $0x0, 0x6(%rsp)
movq 0x50(%rsp), %rsi
jne 0x5b7c
movq 0x68(%rsp), %rcx
movq (%rcx,%rax,8), %rax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sdl (%r10,%rcx,4), %xmm0
movslq (%rdi,%rcx,4), %rdx
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x5b5d
movq 0x70(%rsp), %rcx
testq %rcx, %rcx
je 0x5b9b
xorps %xmm0, %xmm0
cvtsi2sdl (%r10), %xmm0
movslq (%rdi), %rax
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
cmpb $0x0, 0x7(%rsp)
je 0x5c0e
xorl %eax, %eax
movl (%rdi,%rax,4), %ecx
movq 0x48(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm0
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
xorl %edx, %edx
movl 0x14(%rsp), %r8d
movsd (%r15,%rdx,8), %xmm2
mulsd %xmm1, %xmm2
xorl %esi, %esi
movl %r8d, %r9d
movapd %xmm4, %xmm3
mulsd %xmm2, %xmm3
leal (%rdx,%rsi,4), %esi
decl %r9d
jne 0x5bd3
movslq %esi, %rsi
addsd (%rbp,%rsi,8), %xmm3
movsd %xmm3, (%rbp,%rsi,8)
incq %rdx
cmpq $0x4, %rdx
jne 0x5bc0
movl %ecx, (%rdi,%rax,4)
movq 0x48(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq 0x50(%rsp), %rax
jne 0x5ba4
movq 0x78(%rsp), %r13
incq %r13
cmpq 0x8(%rsp), %r13
movl 0x14(%rsp), %r14d
movq 0x30(%rsp), %rsi
jne 0x59c5
movl 0x64(%rsp), %eax
cmpl $0x1, %eax
je 0x5d21
cmpl $0x2, %eax
movl 0x2c(%rsp), %edx
je 0x5cbd
cmpl $0x3, %eax
jne 0x5d84
testl %edx, %edx
jle 0x5ca8
movl %edx, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5c56
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5c65
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5c83
xorl %ecx, %ecx
movsd (%rsi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5c92
movq %rsi, %rdi
movq %rbp, %rsi
callq 0x5768
movq 0x18(%rsp), %rdi
jmp 0x5d84
testl %edx, %edx
jle 0x5d84
movl %edx, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5ccd
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5cdc
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5cfa
xorl %ecx, %ecx
movsd (%rsi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5d09
jmp 0x5d84
movl 0x2c(%rsp), %eax
testl %eax, %eax
jle 0x5d84
movl %eax, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rsi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5d31
xorl %ecx, %ecx
movsd (%rsi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5d40
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rbp,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x5d5c
xorl %ecx, %ecx
movsd (%rbp,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5d6c
callq 0x2270
movq 0x20(%rsp), %rdi
callq 0x2270
movq 0x48(%rsp), %rdi
callq 0x2270
cmpl $0x1, %r14d
jne 0x5dbc
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