// File: /workspace/github_cpp_makefile_star2_O1/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, 0x50(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rsi), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x5ccc
movslq 0xcc(%rbx), %r12
movq %rbx, 0x58(%rsp)
movl 0xc8(%rbx), %eax
movq %rax, 0x10(%rsp)
movq 0x8(%r14), %rax
movq %rax, 0x18(%rsp)
movl $0x8, %esi
movq %r12, %rdi
callq 0x4170
testq %rax, %rax
je 0xf41a
movq %rax, %r14
testl %r12d, %r12d
jle 0xf1da
movslq 0x18(%rsp), %r13
xorl %ebx, %ebx
movl $0x4, %esi
movq %r13, %rdi
callq 0x4170
movq %rax, (%r14,%rbx,8)
testq %rax, %rax
je 0xf415
incq %rbx
cmpq %rbx, %r12
jne 0xf1b8
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0xf23e
xorl %ebp, %ebp
leaq 0x30(%rsp), %rbx
leaq 0xc(%rsp), %r13
movq 0x58(%rsp), %rsi
movl %ebp, %edx
callq 0xf090
movss %xmm0, 0xc(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xf22b
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xf236
movq %rbx, %rdi
movq %r13, %rdx
callq 0x51ca
incl %ebp
cmpl %ebp, 0x10(%rsp)
jne 0xf1fe
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
cmpl %eax, %ecx
cmovll %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x41d0
testl %r12d, %r12d
movq 0x50(%rsp), %rax
jle 0xf3b0
movsd 0xe01(%rip), %xmm1      # 0x10070
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x28(%rsp)
leaq 0x8(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x18(%rsp), %rbp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rcx
leaq (,%rcx,4), %rbx
xorl %r15d, %r15d
movq %r12, 0x80(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x10(%rax), %rsi
cmpq 0x18(%rax), %rsi
je 0xf2ce
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rax)
jmp 0xf2dd
movq 0x20(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x51ca
cmpl $0x0, 0x18(%rsp)
jle 0xf37d
movq (%r14,%r15,8), %r12
xorl %r13d, %r13d
cmpl $0x0, 0x10(%rsp)
jle 0xf337
movq 0x58(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x60(%rsp), %rcx
movq 0x30(%rsp), %rdx
xorl %esi, %esi
movq (%rax,%rsi,8), %rdi
movb (%rdi,%r15), %dil
cmpb (%rcx,%r13), %dil
jne 0xf32e
movss (%rdx,%rsi), %xmm0
movq (%r14,%r15,8), %rdi
addss (%rdi,%r13,4), %xmm0
movss %xmm0, (%rdi,%r13,4)
addq $0x4, %rsi
cmpq %rsi, %rbx
jne 0xf30b
movss (%r12,%r13,4), %xmm0
ucomiss 0x1548(%rip), %xmm0     # 0x1088c
jne 0xf348
jnp 0xf371
movss %xmm0, 0x2c(%rsp)
callq 0x44b0
movq 0x20(%rsp), %rax
movq (%rax), %rax
movss (%rax,%r15,4), %xmm1
mulss 0x2c(%rsp), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%rax,%r15,4)
incq %r13
cmpq %rbp, %r13
jne 0xf2ef
movq 0x20(%rsp), %rax
movq (%rax), %rax
movss (%rax,%r15,4), %xmm0
mulss 0x28(%rsp), %xmm0
movss %xmm0, (%rax,%r15,4)
incq %r15
movq 0x80(%rsp), %r12
cmpq %r12, %r15
movq 0x50(%rsp), %rax
jne 0xf2ac
testl %r12d, %r12d
jle 0xf3c8
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x42d0
incq %rbx
cmpq %rbx, %r12
jne 0xf3b7
movq %r14, %rdi
callq 0x42d0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
leaq 0x70(%rsp), %rbx
je 0xf3ec
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4250
movq 0x60(%rsp), %rdi
cmpq %rbx, %rdi
je 0xf403
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 0xf421
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xf43b
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4250
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0xf457
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4250
movq %rbx, %rdi
callq 0x4440
nop