// File: /workspace/github_cpp_makefile_star2_O3/gcollet[P]MstatX/src/wentropy.cpp
// Function: WEntStat::calculate(Msa&)
void
WEntStat :: calculate(Msa & msa)
{
	string alphabet = msa.getAlphabet();
	
	/* Init sizes */
	int L = msa.getNcol();
	int N = msa.getNseq();
	int K = (int) alphabet.size();
	
	/* Allocate probabilities array */
	float ** p = (float **) calloc (L, sizeof(float*));
	if (p == NULL){
		fprintf(stderr,"Cannot Allocate probability matrix\n");
		exit(0);
	}
	for (int i(0); i < L; i++){
		p[i] = (float *) calloc (K, sizeof(float));
		if (p[i] == NULL){
			fprintf(stderr,"Cannot Allocate probability submatrix\n");
			exit(0);
		}
	}
	
	/* Calculate Sequence Weights */
	vector<float> w;
	for (int seq(0); seq < N; ++seq){
		w.push_back(calcSeqWeight(msa,seq));
	}
	
	/* Calculate aa proba and conservation score by columns */
	float lambda = 1.0 / log(MIN(K,N));
	
	for (int x(0); x < L; ++x){
		col_stat.push_back(0.0);
		for (int a(0); a < K; ++a){
			for (int j(0); j < N; ++j){
				if(msa.getSymbol(j, x) == alphabet[a]){
					p[x][a] += w[j];
				}
			}
			if (p[x][a] != 0.0){
				col_stat[x] -= p[x][a] * log(p[x][a]);
			}
		}
		col_stat[x] *= lambda;
	}
	
	for (int i(0); i < L; ++i) {
		free (p[i]);
	}
	free(p);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %rbx
movq %rdi, 0x48(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rsi), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x5cca
movslq 0xcc(%rbx), %r14
movq %rbx, 0x58(%rsp)
movl 0xc8(%rbx), %r12d
movq 0x8(%r13), %rbp
movl $0x8, %esi
movq %r14, %rdi
callq 0x4170
testq %rax, %rax
je 0xf671
movq %rax, %r15
testl %r14d, %r14d
jle 0xf433
movslq %ebp, %r13
xorl %ebx, %ebx
movl $0x4, %esi
movq %r13, %rdi
callq 0x4170
movq %rax, (%r15,%rbx,8)
testq %rax, %rax
je 0xf66c
incq %rbx
cmpq %rbx, %r14
jne 0xf411
movq %rbp, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testl %r12d, %r12d
jle 0xf499
xorl %ebp, %ebp
leaq 0x20(%rsp), %rbx
leaq 0xc(%rsp), %r13
movq 0x58(%rsp), %rsi
movl %ebp, %edx
callq 0xf2f4
movss %xmm0, 0xc(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0xf487
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0xf492
movq %rbx, %rdi
movq %r13, %rdx
callq 0x51cc
incl %ebp
cmpl %ebp, %r12d
jne 0xf45a
movq 0x50(%rsp), %rbp
cmpl %ebp, %r12d
movl %ebp, %eax
cmovll %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x41d0
testl %r14d, %r14d
jle 0xf61f
movq %r15, 0x40(%rsp)
movsd 0xba7(%rip), %xmm1      # 0x10070
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq %r12, %r13
leaq (,%r12,4), %r12
xorl %ebx, %ebx
movq %r14, 0x80(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x10(%rax), %rsi
cmpq 0x18(%rax), %rsi
je 0xf525
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rax)
jmp 0xf534
movq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x51cc
cmpl $0x0, 0x50(%rsp)
jle 0xf5d1
movq 0x40(%rsp), %rax
movq (%rax,%rbx,8), %r15
xorl %r14d, %r14d
testl %r13d, %r13d
jle 0xf58d
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x60(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movb (%rdi,%rbx), %dil
cmpb (%rcx,%r14), %dil
jne 0xf584
movss (%rdx,%rsi), %xmm0
addss (%r15,%r14,4), %xmm0
movss %xmm0, (%r15,%r14,4)
addq $0x4, %rsi
cmpq %rsi, %r12
jne 0xf565
movss (%r15,%r14,4), %xmm0
ucomiss 0x1336(%rip), %xmm0     # 0x108d0
jne 0xf59e
jnp 0xf5c5
movss %xmm0, 0x1c(%rsp)
callq 0x44b0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movss (%rax,%rbx,4), %xmm1
mulss 0x1c(%rsp), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%rax,%rbx,4)
incq %r14
cmpq %rbp, %r14
jne 0xf54b
movq 0x10(%rsp), %rax
movq (%rax), %rax
movss (%rax,%rbx,4), %xmm0
mulss 0x18(%rsp), %xmm0
movss %xmm0, (%rax,%rbx,4)
incq %rbx
movq 0x80(%rsp), %r14
cmpq %r14, %rbx
movq 0x48(%rsp), %rax
jne 0xf503
testl %r14d, %r14d
movq 0x40(%rsp), %r15
jle 0xf61f
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
callq 0x42d0
incq %rbx
cmpq %rbx, %r14
jne 0xf60e
movq %r15, %rdi
callq 0x42d0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xf63e
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x4250
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xf65a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4250
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4910
callq 0x4934
jmp 0xf678
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xf692
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x4250
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xf6ae
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4250
movq %rbx, %rdi
callq 0x4440