// File: /workspace/github_cpp_makefile_star2_O2/gcollet[P]MstatX/src/trident.cpp
// Function: TridStat::calculate(Msa&)
void
TridStat :: calculate(Msa & msa)
{
	/* Declare the vectors */
	vector<float> w;					/**< Weight of each sequence in the msa (size = nb sequences) */
	vector<float> t;					/**< t(x) = Shannon entropy score  + Weighted sequence Score */
	vector<float> r;					/**< r(x) = Stereochemical score */
	vector<float> g;					/**< g(x) = Gap Score */

	/* Init size */
	int L = msa.getNcol();
	int N = msa.getNseq();
	string alphabet = msa.getAlphabet();
	int K = (int) alphabet.size();

  //cerr << "Seq Weights\n";
	/* Calculate Sequence Weights */
	for (int seq(0); seq < N; ++seq){
		w.push_back(calcSeqWeight(msa,seq));
	}

	/* Calculate t(x) = \frac{\sum_{a=1}^{K}p_a log(p_a)}{log(min(N,K))}
	 *						p_a = \sum_{i \in \{i|s(i) = a\}} w_i
	 *						w_i = \frac{1}{L} \sum_{x=1}^{L}\frac{1}{K_x n_{x_i}}
	 * Like in wentropy
	 */
	float lambda = 1.0 / log(MIN(K,N));

  for (int x(0); x < L; x++){
		t.push_back(0.0);
		for (int a(0); a < K; a++){
			float tmp_proba(0.0);
		  for (int j(0); j < N; j++){
				if(msa.getSymbol(j, x) == alphabet[a]){
					tmp_proba += w[j];
				}
			}
			if (tmp_proba != 0.0){
				t[x] -= tmp_proba * log(tmp_proba);
			}
		}
		t[x] *= lambda;
    //cerr << "t[" << x << "] = " << t[x] << "\n";
	}

	/* Calculate g(x) = nb_gap / nb_seq
	 * Represents the proportion of gaps in the column
	 */
	for (int x(0); x < L; x++){
		g.push_back((float) msa.getGap(x) / (float) N);
		//cerr << "g[" << x << "] = " << g[x] << "\n";
	}


	/* Calculate r(x) = \lambda_r \frac{1}{k_x}\sum_{a=1}^{k_x}|\bar{X}(x) - X_a|
	 *      \lambda_r = \frac{1}{\sqrt{20(max(M)-min(M))^2}}
	 *					  X_a = \left[ \begin{array}{c}M(a,a_1)\\M(a,a_2)\\.\\.\\.\\M(a,a_{20})\end{array}\right]
	 *							M is a normalized scoring matrix
	 */
	ScoringMatrix score_mat(Options::Get().matrix_fname);
	int alph_size = score_mat.getAlphabetSize();
	string sm_alphabet = score_mat.getAlphabet();

	//msa.fitToAlphabet(sm_alphabet);

	for (int x(0); x < L; x++){

		int ntype = msa.getNtype(x);
		string type_list = msa.getTypeList(x);
		if (type_list.empty()) {
			std::cerr << "Error: No amino acid type found in column " << x << "\n";
			exit(1);
		}
		if (type_list.find("-") < (int) type_list.size()){
			type_list.erase(type_list.find("-"), 1);
			ntype--;
		}
		if (ntype){
			/* Calculate Mean vector */
			vector<float> mean(alph_size, 0.0);
			for (int i(0); i < ntype; ++i){
				for (int a(0); a < alph_size; ++a){
					mean[a] += score_mat.normScore(sm_alphabet[a], type_list[i]);
				}
			}
			for (int a(0); a < alph_size; ++a){
				mean[a] /= ntype;
			}

			/* Calculate Score */
			float lambda = sqrt(alph_size * (score_mat.getMax() - score_mat.getMin()) * (score_mat.getMax() - score_mat.getMin()));
			float tmp_score = 0.0;
			for (int i(0); i < ntype; ++i){
				vector<float> diff_vect;
				for(int a(0); a < alph_size; ++a){
					diff_vect.push_back(mean[a] - score_mat.normScore(sm_alphabet[a], type_list[i]));
				}
				tmp_score += normVect(diff_vect);
			}
			tmp_score /= ntype;
			tmp_score /= lambda;
			r.push_back(tmp_score);
		} else {
			r.push_back(0.0);
		}
    //cerr << "r[" << x << "] = " << r[x] << "\n";
	}


	/*
	 * Combine the three scores
	 */
	for (int x(0); x < L; x++){
		col_stat.push_back(pow((1-t[x]),Options::Get().factor_a)*pow((1-r[x]),Options::Get().factor_b)*pow((1-g[x]),Options::Get().factor_c));
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, %r13
andq $0x0, 0xe0(%rsp)
xorpd %xmm0, %xmm0
andq $0x0, 0x70(%rsp)
movapd %xmm0, 0xd0(%rsp)
andq $0x0, 0xa0(%rsp)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0xc0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0xb0(%rsp)
movl 0xc8(%rsi), %r15d
movl 0xcc(%rsi), %eax
movq %rax, 0x20(%rsp)
leaq 0x180(%rsp), %rdi
movq %rsi, 0x18(%rsp)
callq 0x4350
movl 0x188(%rsp), %r14d
xorl %ebx, %ebx
testl %r15d, %r15d
movl $0x0, %ebp
movl %r15d, 0xc(%rsp)
cmovgl %r15d, %ebp
leaq 0xd0(%rsp), %r15
leaq 0x118(%rsp), %r12
cmpl %ebx, %ebp
je 0xbd2f
movq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0xbb78
movss %xmm0, 0x118(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4c80
incl %ebx
jmp 0xbd07
movq %r13, 0xf0(%rsp)
movl 0xc(%rsp), %eax
cmpl %r14d, %eax
movl %r14d, %edi
cmovll %eax, %edi
callq 0xc415
movsd 0x131f(%rip), %xmm1     # 0xd070
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
movq 0x20(%rsp), %rax
testl %eax, %eax
cmovlel %r12d, %eax
movq %rax, 0x20(%rsp)
movl %ebp, %r13d
shlq $0x2, %r13
leaq 0x60(%rsp), %rbx
leaq 0x118(%rsp), %r15
movq 0x20(%rsp), %rbp
cmpq %rbp, %r12
je 0xbe46
andl $0x0, 0x118(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4c80
xorl %ebp, %ebp
cmpq %r14, %rbp
je 0xbe27
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x180(%rsp), %rcx
xorps %xmm0, %xmm0
movq 0xd0(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %r13
je 0xbdf1
movq (%rax,%rsi,8), %rdi
movb (%rdi,%r12), %dil
cmpb (%rcx,%rbp), %dil
jne 0xbdeb
addss (%rdx,%rsi), %xmm0
addq $0x4, %rsi
jmp 0xbdd3
ucomiss 0x1aa8(%rip), %xmm0     # 0xd8a0
jne 0xbdfc
jnp 0xbe22
movss %xmm0, 0x4(%rsp)
callq 0x4490
movq 0x60(%rsp), %rax
movss (%rax,%r12,4), %xmm1
mulss 0x4(%rsp), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%rax,%r12,4)
incq %rbp
jmp 0xbdb0
movq 0x60(%rsp), %rax
movss (%rax,%r12,4), %xmm0
mulss 0x10(%rsp), %xmm0
movss %xmm0, (%rax,%r12,4)
incq %r12
jmp 0xbd8d
cvtsi2ssl 0xc(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
xorl %ebx, %ebx
leaq 0xb0(%rsp), %r15
leaq 0x118(%rsp), %r12
cmpl %ebx, %ebp
je 0xbe96
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x9c0c
cvtsi2ss %eax, %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0x118(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4c80
incl %ebx
jmp 0xbe64
callq 0x4a00
addq $0x88, %rax
leaq 0x1a0(%rsp), %rdi
movq %rax, %rsi
callq 0x4350
leaq 0x118(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xadc0
leaq 0x1a0(%rsp), %rdi
callq 0x4110
leaq 0x120(%rsp), %rsi
movq 0x8(%rsi), %rbx
leaq 0x160(%rsp), %rdi
callq 0x4350
movslq %ebx, %rax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x30(%rsp)
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %r15d
movq %rax, 0xf8(%rsp)
cmovgl %eax, %r15d
leaq 0x100(%rsp), %r12
leaq 0x118(%rsp), %rbx
leaq 0x34(%rsp), %r13
cmpq %rbp, %rdx
je 0xc1bb
movq 0x18(%rsp), %rsi
movq 0xb0(%rsi), %rax
movl (%rax,%rdx,4), %ebp
leaq 0x40(%rsp), %rdi
movq %rdx, 0x38(%rsp)
callq 0x5b36
cmpq $0x0, 0x48(%rsp)
leaq 0x17d3(%rip), %rsi      # 0xd72b
je 0xc318
leaq 0x40(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x44f0
movslq 0x48(%rsp), %rcx
cmpq %rcx, %rax
jae 0xbf98
movq %r14, %rdi
leaq 0x17aa(%rip), %rsi      # 0xd72b
xorl %edx, %edx
callq 0x44f0
movq %r14, %rdi
movq %rax, %rsi
pushq $0x1
popq %rdx
callq 0x4070
decl %ebp
testl %ebp, %ebp
je 0xc07e
movl %ebp, 0x4(%rsp)
andl $0x0, 0x100(%rsp)
leaq 0x78(%rsp), %rdi
movq 0xf8(%rsp), %rsi
movq %r12, %rdx
leaq 0x8(%rsp), %rcx
callq 0xa1ac
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
cmpq 0x10(%rsp), %rbp
je 0xc023
xorl %r14d, %r14d
cmpq %r14, %r15
je 0xc01e
movq 0x160(%rsp), %rax
movsbl (%rax,%r14), %esi
movq 0x40(%rsp), %rax
movsbl (%rax,%rbp), %edx
movq %rbx, %rdi
callq 0xb47c
movq 0x78(%rsp), %rax
addss (%rax,%r14,4), %xmm0
movss %xmm0, (%rax,%r14,4)
incq %r14
jmp 0xbfe5
incq %rbp
jmp 0xbfdb
cvtsi2ssl 0x4(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x78(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0xc050
movss (%rax,%rcx,4), %xmm0
divss 0xc(%rsp), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0xc036
movss 0x158(%rsp), %xmm1
subss 0x15c(%rsp), %xmm1
movaps %xmm1, %xmm0
mulss 0x30(%rsp), %xmm0
mulss %xmm1, %xmm0
ucomiss 0x182a(%rip), %xmm0     # 0xd8a0
jb 0xc0a4
sqrtss %xmm0, %xmm0
jmp 0xc0a9
andl $0x0, 0x78(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x4c80
leaq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rbp
jmp 0xc1a9
callq 0x4460
movss %xmm0, 0x2c(%rsp)
andl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
cmpq 0x10(%rsp), %rbp
je 0xc16b
xorps %xmm0, %xmm0
movaps %xmm0, 0x100(%rsp)
andq $0x0, 0x110(%rsp)
xorl %r14d, %r14d
cmpq %r14, %r15
je 0xc12c
movq 0x78(%rsp), %rax
movss (%rax,%r14,4), %xmm0
movss %xmm0, 0x4(%rsp)
movq 0x160(%rsp), %rax
movsbl (%rax,%r14), %esi
movq 0x40(%rsp), %rax
movsbl (%rax,%rbp), %edx
movq %rbx, %rdi
callq 0xb47c
movss 0x4(%rsp), %xmm1
subss %xmm0, %xmm1
movss %xmm1, 0x34(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4c80
incq %r14
jmp 0xc0d8
leaq 0x1c0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0xc41e
movq %r14, %rsi
callq 0xbc1e
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movq %r14, %rdi
callq 0x49cc
movq %r12, %rdi
callq 0x49cc
incq %rbp
jmp 0xc0b6
movss 0x8(%rsp), %xmm0
divss 0xc(%rsp), %xmm0
divss 0x2c(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc46c
leaq 0x78(%rsp), %rdi
callq 0x49cc
movq 0x20(%rsp), %rbp
leaq 0x40(%rsp), %rdi
callq 0x4110
movq 0x38(%rsp), %rdx
incq %rdx
jmp 0xbf24
movq 0xf0(%rsp), %r15
addq $0x8, %r15
xorl %r14d, %r14d
leaq 0x40(%rsp), %rbx
cmpq %r14, %rbp
je 0xc2ae
movq 0x60(%rsp), %rax
movss (%rax,%r14,4), %xmm0
movss %xmm0, 0x4(%rsp)
callq 0x4a00
movss 0xe5e(%rip), %xmm0      # 0xd054
subss 0x4(%rsp), %xmm0
movss 0xf4(%rax), %xmm1
callq 0x43a0
movss %xmm0, 0x4(%rsp)
movq 0x90(%rsp), %rax
movss (%rax,%r14,4), %xmm0
movss %xmm0, 0x10(%rsp)
callq 0x4a00
movss 0xe24(%rip), %xmm0      # 0xd054
subss 0x10(%rsp), %xmm0
movss 0xf8(%rax), %xmm1
callq 0x43a0
movss %xmm0, 0x10(%rsp)
movq 0xb0(%rsp), %rax
movss (%rax,%r14,4), %xmm0
movss %xmm0, 0x18(%rsp)
callq 0x4a00
movss 0xdea(%rip), %xmm0      # 0xd054
subss 0x18(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
mulss 0x10(%rsp), %xmm1
movss %xmm1, 0x4(%rsp)
movss 0xfc(%rax), %xmm1
callq 0x43a0
mulss 0x4(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4c80
incq %r14
jmp 0xc1cf
leaq 0x160(%rsp), %rdi
callq 0x4110
leaq 0x118(%rsp), %rdi
callq 0xb3a0
leaq 0x180(%rsp), %rdi
callq 0x4110
leaq 0xb0(%rsp), %rdi
callq 0x49cc
leaq 0x90(%rsp), %rdi
callq 0x49cc
leaq 0x60(%rsp), %rdi
callq 0x49cc
leaq 0xd0(%rsp), %rdi
callq 0x49cc
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6cc9(%rip), %rdi      # 0x12fe8
leaq 0x159a(%rip), %rsi      # 0xd8c0
callq 0x4250
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x4420
leaq 0x11b7(%rip), %rsi      # 0xd4f6
movq %rax, %rdi
callq 0x4250
pushq $0x1
popq %rdi
callq 0x42d0
movq %rax, %rbx
jmp 0xc3c2
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
callq 0x4110
jmp 0xc3cf
movq %rax, %rbx
jmp 0xc3dc
jmp 0xc380
jmp 0xc375
jmp 0xc375
jmp 0xc38c
jmp 0xc375
movq %rax, %rbx
jmp 0xc3ab
jmp 0xc385
jmp 0xc380
jmp 0xc380
movq %rax, %rbx
jmp 0xc3cf
movq %rax, %rbx
jmp 0xc3b5
jmp 0xc391
movq %rax, %rbx
jmp 0xc3a1
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x49cc
leaq 0x78(%rsp), %rdi
callq 0x49cc
leaq 0x40(%rsp), %rdi
callq 0x4110
leaq 0x160(%rsp), %rdi
callq 0x4110
leaq 0x118(%rsp), %rdi
callq 0xb3a0
leaq 0x180(%rsp), %rdi
callq 0x4110
leaq 0xb0(%rsp), %rdi
callq 0x49cc
leaq 0x90(%rsp), %rdi
callq 0x49cc
leaq 0x60(%rsp), %rdi
callq 0x49cc
leaq 0xd0(%rsp), %rdi
callq 0x49cc
movq %rbx, %rdi
callq 0x4430