// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movl %r9d, 0x40(%rsp)
movq %r8, %r14
movq %rcx, 0x10(%rsp)
movq %rsi, 0x78(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xd0(%rsp), %r12
movb 0xd8(%rsp), %al
movb %al, 0xb(%rsp)
movq %rdi, 0x50(%rsp)
movl 0x8(%rdi), %eax
movl %eax, 0x28(%rsp)
movl %edx, 0xc(%rsp)
movslq %edx, %rbx
leaq (,%rbx,4), %r15
testl %ebx, %ebx
pushq $-0x1
popq %rax
cmovsq %rax, %r15
leaq (,%rbx,8), %r13
cmovsq %rax, %r13
movq %r15, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movq %r13, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
pushq $0x20
popq %rdi
callq 0x2030
cmpl $0x1, %ebx
jne 0x4c3e
xorl %edx, %edx
movabsq $0x3fd0000000000000, %rcx # imm = 0x3FD0000000000000
cmpq $0x4, %rdx
je 0x4c71
movq %rcx, (%rax,%rdx,8)
incq %rdx
jmp 0x4c2f
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x4c55
addsd (%r12,%rcx,8), %xmm0
incq %rcx
jmp 0x4c44
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x4c71
movsd (%r12,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
jmp 0x4c57
movq %rax, 0x48(%rsp)
xorl %r15d, %r15d
movl 0x28(%rsp), %eax
testl %eax, %eax
movl $0x0, %r12d
cmovgl %eax, %r12d
cmpq %r15, %r12
je 0x4cab
movq 0x50(%rsp), %rax
movq (%rax), %r13
movsbl (%r13,%r15), %edi
callq 0x2360
movb %al, (%r13,%r15)
incq %r15
jmp 0x4c89
xorl %eax, %eax
movl 0xc(%rsp), %r8d
testl %r8d, %r8d
cmovgl %r8d, %eax
pushq $0x1
popq %rdx
movl %eax, %ecx
movsd 0x18(%rsp), %xmm0
subl $0x1, %ecx
jb 0x4cce
shll $0x2, %edx
jmp 0x4cc4
movl %edx, %ecx
xorl %edi, %edi
movq 0x10(%rsp), %rsi
cmpq %rdi, %rcx
je 0x4cec
movsd %xmm0, (%rsi,%rdi,8)
movsd %xmm0, (%r14,%rdi,8)
incq %rdi
jmp 0x4cd7
movl 0x28(%rsp), %ecx
subl %r8d, %ecx
movslq %ecx, %rcx
movq %rcx, 0x68(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
addq %rax, %rax
movq %rax, 0x80(%rsp)
xorl %eax, %eax
movsd 0x22f1(%rip), %xmm4     # 0x7008
movl $0xff, %r12d
movl %edx, 0x44(%rsp)
cmpq 0x68(%rsp), %rax
jg 0x4f5f
xorl %r15d, %r15d
movq %rax, 0x70(%rsp)
movq %rax, %r13
movapd %xmm4, %xmm1
xorl %ebp, %ebp
cmpq %r15, 0x80(%rsp)
je 0x4e53
movsd %xmm1, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq %r13, %rbx
movsbl (%rax,%r13), %r13d
movl %r13d, %edi
callq 0x2360
movsd 0x2297(%rip), %xmm4     # 0x7008
andl %r12d, %eax
addl $-0x41, %eax
cmpl $0x13, %eax
movq 0x38(%rsp), %rcx
ja 0x4f4e
movl $0x80045, %edx          # imm = 0x80045
btl %eax, %edx
jae 0x4f4e
andl %r12d, %r13d
xorl %r12d, %r12d
cmpl $0x41, %r13d
je 0x4ddb
cmpl $0x43, %r13d
je 0x4dc9
cmpl $0x47, %r13d
je 0x4dcd
cmpl $0x54, %r13d
je 0x4dd1
cmpl $0x61, %r13d
je 0x4ddb
cmpl $0x74, %r13d
je 0x4dd1
cmpl $0x67, %r13d
je 0x4dcd
cmpl $0x63, %r13d
jne 0x4dd5
pushq $0x1
jmp 0x4dd7
pushq $0x2
jmp 0x4dd7
pushq $0x3
jmp 0x4dd7
pushq $-0x1
popq %rax
movl %eax, %r12d
movl %r12d, (%rcx,%r15,2)
movq 0x78(%rsp), %rax
movq (%rax), %rax
movq %rbx, %r13
movsbl (%rax,%rbx), %eax
leal -0x21(%rax), %ecx
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx,%r15,2)
pushq $0x21
popq %rcx
subl %eax, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x2d05(%rip), %xmm0     # 0x7b10
divsd %xmm0, %xmm1
callq 0x2310
movsd 0x21ec(%rip), %xmm4     # 0x7008
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
movq 0x30(%rsp), %rax
movsd %xmm1, (%rax,%r15,4)
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movapd %xmm0, %xmm1
leal (%r12,%rbp,4), %ebp
addq $0x2, %r15
incq %r13
movl $0xff, %r12d
jmp 0x4d3d
movslq %ebp, %rax
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd %xmm4, %xmm0
movsd %xmm0, (%rcx,%rax,8)
addsd (%r14,%rax,8), %xmm1
movsd %xmm1, (%r14,%rax,8)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
movq 0x38(%rsp), %r8
je 0x4eae
xorl %ecx, %ecx
cmpq %rcx, 0x18(%rsp)
je 0x4eae
movq 0x20(%rsp), %rdx
cvtsi2sdl (%rdx,%rcx,4), %xmm0
movq (%rdi,%rax,8), %rdx
movslq (%r8,%rcx,4), %rsi
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rcx
jmp 0x4e86
movq 0x60(%rsp), %rcx
testq %rcx, %rcx
je 0x4ece
movq 0x20(%rsp), %rax
cvtsi2sdl (%rax), %xmm0
movslq (%r8), %rax
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
cmpb $0x0, 0xb(%rsp)
je 0x4f4e
xorl %eax, %eax
cmpq 0x18(%rsp), %rax
je 0x4f4e
movl (%r8,%rax,4), %ecx
movq 0x30(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm0
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
xorl %edx, %edx
movl 0xc(%rsp), %r9d
movq 0x48(%rsp), %r10
cmpq $0x4, %rdx
je 0x4f3b
movsd (%r10,%rdx,8), %xmm2
mulsd %xmm1, %xmm2
xorl %esi, %esi
movl %r9d, %edi
movapd %xmm4, %xmm3
subl $0x1, %edi
jb 0x4f27
mulsd %xmm2, %xmm3
leal (%rdx,%rsi,4), %esi
jmp 0x4f19
movslq %esi, %rsi
addsd (%r14,%rsi,8), %xmm3
movsd %xmm3, (%r14,%rsi,8)
incq %rdx
jmp 0x4f00
movl %ecx, (%r8,%rax,4)
movq 0x30(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x4ed7
movq 0x70(%rsp), %rax
incq %rax
movl 0x44(%rsp), %edx
jmp 0x4d21
movl 0x40(%rsp), %eax
cmpl $0x1, %eax
je 0x4fbb
cmpl $0x2, %eax
je 0x4fa0
cmpl $0x3, %eax
jne 0x4fd5
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl %edx, %r12d
callq 0x4ae5
movq %r15, %rdi
movl %r12d, %esi
callq 0x4ab0
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x4b1a
jmp 0x4fd5
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl %edx, %r14d
callq 0x4ae5
movq %r15, %rdi
movl %r14d, %esi
jmp 0x4fd0
movq 0x10(%rsp), %rdi
movl %edx, %esi
movl %edx, %r15d
callq 0x4ab0
movq %r14, %rdi
movl %r15d, %esi
callq 0x4ab0
movq 0x38(%rsp), %rdi
callq 0x2250
movq 0x20(%rsp), %rdi
callq 0x2250
movq 0x30(%rsp), %rdi
callq 0x2250
cmpl $0x1, 0xc(%rsp)
jne 0x5015
movq 0x48(%rsp), %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2250
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq