// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x4350
movl 0xc8(%r14), %eax
movl %eax, (%rsp)
movl 0xcc(%r14), %r13d
movq 0x8(%rbx), %rbp
movslq %r13d, %rdi
pushq $0x8
popq %rsi
callq 0x4190
testq %rax, %rax
je 0xc857
movq %rax, %rbx
movslq %ebp, %r12
xorl %r15d, %r15d
testl %r13d, %r13d
cmovlel %r15d, %r13d
movq %r13, 0x10(%rsp)
pushq $0x4
popq %r13
cmpq %r15, 0x10(%rsp)
je 0xc6b8
movq %r12, %rdi
movq %r13, %rsi
callq 0x4190
movq %rax, (%rbx,%r15,8)
incq %r15
testq %rax, %rax
jne 0xc677
movq 0x692c(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x9f6(%rip), %rdi       # 0xd09c
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x44c0
xorl %edi, %edi
callq 0x42d0
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
xorl %r13d, %r13d
movl (%rsp), %eax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
leaq 0x4(%rsp), %r12
cmpl %r13d, %r15d
je 0xc709
movq %r14, %rsi
movl %r13d, %edx
callq 0xc56c
movss %xmm0, 0x4(%rsp)
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x4c80
incl %r13d
jmp 0xc6e1
movl (%rsp), %edi
cmpl %ebp, %edi
cmovgel %ebp, %edi
callq 0xc415
movsd 0x952(%rip), %xmm1      # 0xd070
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
addq $0x8, 0x8(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
cmovlel %r13d, %ebp
movl %r15d, %r12d
shlq $0x2, %r12
cmpq 0x10(%rsp), %r13
je 0xc811
andl $0x0, 0x4(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x4c80
xorl %r15d, %r15d
cmpq %rbp, %r15
je 0xc7ef
movq 0x38(%r14), %rax
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %r12
je 0xc7ab
movq (%rax,%rsi,8), %rdi
movb (%rdi,%r13), %dil
cmpb (%rcx,%r15), %dil
jne 0xc7a5
movss (%rdx,%rsi), %xmm0
movq (%rbx,%r13,8), %rdi
addss (%rdi,%r15,4), %xmm0
movss %xmm0, (%rdi,%r15,4)
addq $0x4, %rsi
jmp 0xc77d
movq (%rbx,%r13,8), %rax
movss (%rax,%r15,4), %xmm0
ucomiss 0x10e4(%rip), %xmm0     # 0xd8a0
jne 0xc7c0
jnp 0xc7e7
movss %xmm0, (%rsp)
callq 0x4490
movq 0x8(%rsp), %rax
movq (%rax), %rax
movss (%rax,%r13,4), %xmm1
mulss (%rsp), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%rax,%r13,4)
incq %r15
jmp 0xc764
movq 0x8(%rsp), %rax
movq (%rax), %rax
movss (%rax,%r13,4), %xmm0
mulss 0x1c(%rsp), %xmm0
movss %xmm0, (%rax,%r13,4)
incq %r13
jmp 0xc742
xorl %r14d, %r14d
movq 0x10(%rsp), %r15
cmpq %r14, %r15
je 0xc82c
movq (%rbx,%r14,8), %rdi
callq 0x42c0
incq %r14
jmp 0xc819
movq %rbx, %rdi
callq 0x42c0
leaq 0x20(%rsp), %rdi
callq 0x49cc
leaq 0x38(%rsp), %rdi
callq 0x4110
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x676a(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x810(%rip), %rdi       # 0xd078
pushq $0x23
jmp 0xc6a8
jmp 0xc871
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x49cc
leaq 0x38(%rsp), %rdi
callq 0x4110
movq %rbx, %rdi
callq 0x4430