// File: /workspace/github_cpp_makefile_star2_O3/gcollet[P]MstatX/src/scoring_matrix.cpp
// Function: ScoringMatrix::ScoringMatrix(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
ScoringMatrix :: ScoringMatrix(string fname)
{
  /* Open file */
	if(fname.empty()){
		cerr << "Error, score matrix file name is empty\n";
		exit(0);
	}
	if (Options::Get().verbose){
		cout << "Read Scoring Matrix in " << fname << "\n";
	}
	ifstream file(fname.c_str());
	if (!file.good()){
	  cerr << "Cannot open file " << fname << "\n";
		exit(0);
	}
	
	/* Read file */
	string s;
	getline(file,s);
	while (file.good() && s[0] != 'M'){
	  getline(file,s);
	}
	
	/* Read Alphabet */
	int alphabet_begin = (int) s.find('=') + 2;
	int alphabet_size = (int) s.find(',') - alphabet_begin;
	alphabet = s.substr(alphabet_begin,	alphabet_size);
	
	/* Allocate the matrix */
	matrix = (float **) calloc (alphabet_size, sizeof(float *));
	if (matrix == NULL){
	  cerr << "Cannot allocate scoring matrix\n";
		exit(0);
	}
	for (int i(0); i < alphabet_size; ++i) {
		matrix[i] = (float *) calloc(i + 1, sizeof(float));
		if (matrix[i] == NULL){
			cerr << "Cannot allocate scoring matrix[" << i << "]\n";
			exit(0);
		}
	}
	
	
	/* Read the matrix */
  min = 1000; max = -1000;
	for (int i(0); i < alphabet_size; ++i) {
		getline(file,s);
		for (int j(0); j <=i ; j++){
			matrix[i][j] = atof(s.substr(j*8, 8).c_str());
      if (matrix[i][j] < min)
        min = matrix[i][j];
      if (matrix[i][j] > max)
        max = matrix[i][j];
		}
	}
	
	
	/* Allocate the norm_matrix */
	norm_matrix = (float **) calloc (alphabet_size, sizeof(float *));
	if (norm_matrix == NULL){
	  cerr << "Cannot allocate the normalized amino acid vector\n";
		exit(0);
	}
	for (int i(0); i < alphabet_size; ++i) {
		norm_matrix[i] = (float *) calloc(i+1, sizeof(float));
		if (norm_matrix[i] == NULL){
			cerr << "Cannot allocate norm_matrix[" << i << "]\n";
			exit(0);
		}
	}
	
	/* Calculate normalized vector */
	
	for (int i(0); i < alphabet_size; ++i) {
		for (int j(0); j <= i; ++j){
			norm_matrix[i][j] = (score(alphabet[i], alphabet[j]) - min) / (max - min);
		}
	}
	
	if (Options::Get().verbose){
		cout << "Normalized :\n";
		for (int i(0); i < alphabet_size; ++i) {
			cout.width(9);
			cout << alphabet[i];
			for (int j(0); j <= i; j++){
				cout.width(9);
				cout << norm_matrix[i][j];
			}
			cout << "\n";
		}
		cout << "\n";
		cout.width(9);
		cout << ' ';
		for (int j(0); j <= alphabet_size ; j++){
			cout.width(9);
			cout << alphabet[j];
		} 
		cout << "\n\n";
	}
	
	is_set = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
leaq 0x8710(%rip), %rax      # 0x15cd8
movq %rax, (%rdi)
leaq 0x8(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x18(%rdi), %rax
movq %rax, 0x60(%rsp)
movq %rax, 0x8(%rdi)
movq $0x0, 0x10(%rdi)
movb $0x0, 0x18(%rdi)
cmpq $0x0, 0x8(%rsi)
je 0xdc08
movq %rsi, %rbx
movq %rdi, %r14
callq 0x4f1c
cmpb $0x1, 0xec(%rax)
jne 0xd64b
movq 0x89ad(%rip), %rdi      # 0x15fc0
leaq 0x31ae(%rip), %rsi      # 0x107c8
movl $0x17, %edx
callq 0x42a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x898e(%rip), %rdi      # 0x15fc0
callq 0x42a0
leaq 0x2ee6(%rip), %rsi      # 0x10524
movl $0x1, %edx
movq %rax, %rdi
callq 0x42a0
movq (%rbx), %rsi
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movl $0x8, %edx
callq 0x4480
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rcx
cmpl $0x0, 0xa0(%rsp,%rcx)
jne 0xdc20
leaq 0x50(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4220
movsbl %al, %edx
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4490
leaq 0x40(%rsp), %rbx
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xa0(%rsp,%rax)
jne 0xd706
movq 0x40(%rsp), %rcx
cmpb $0x4d, (%rcx)
je 0xd706
leaq (%rsp,%rax), %rdi
addq $0x80, %rdi
movl $0xa, %esi
callq 0x4220
movsbl %al, %edx
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0x4490
jmp 0xd6bc
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movl $0x3d, %esi
xorl %edx, %edx
callq 0x4330
movq %rax, %r15
leal 0x2(%rax), %ebp
movq %rbx, %rdi
movl $0x2c, %esi
xorl %edx, %edx
callq 0x4330
movq %rax, %r13
subl %ebp, %eax
movslq %ebp, %rdx
movl %eax, 0xc(%rsp)
movslq %eax, %rcx
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %rcx, 0x10(%rsp)
callq 0x4270
leaq 0x20(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x4300
movq (%rbx), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0xd77c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4250
movq %r15, 0x68(%rsp)
movl $0x8, %esi
movq 0x10(%rsp), %rdi
callq 0x4170
movq %rax, 0x28(%r14)
testq %rax, %rax
je 0xdc5f
movq %rax, %r12
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %ebp
negq %rbp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r15d
leaq (%r15,%rbp), %rax
cmpq $0x1, %rax
je 0xd821
movl $0x4, %esi
movq %r15, %rdi
callq 0x4170
movq %rax, -0x8(%r12,%r15,8)
movq 0x28(%r14), %r12
incl %ebx
cmpq $0x0, -0x8(%r12,%r15,8)
leaq 0x1(%r15), %r15
jne 0xd7b9
movq 0x87fa(%rip), %rdi      # 0x15fe8
leaq 0x300b(%rip), %rsi      # 0x10800
movl $0x1f, %edx
callq 0x42a0
movq 0x87e2(%rip), %rdi      # 0x15fe8
movl %ebx, %esi
callq 0x4420
leaq 0x300c(%rip), %rsi      # 0x10820
movq %rax, %rdi
callq 0x4230
jmp 0xd9b3
movq %rbp, 0x78(%rsp)
movq %r13, 0x70(%rsp)
movsd 0x2f5d(%rip), %xmm0     # 0x10790
movsd %xmm0, 0x40(%r14)
cmpl $0x0, 0xc(%rsp)
jle 0xd921
movl $0x1, %ebp
xorl %r13d, %r13d
leaq 0x40(%rsp), %r12
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0x80(%rsp), %rbx
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4220
movsbl %al, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4490
xorl %ebx, %ebx
leaq (,%rbx,8), %rdx
movl $0x8, %ecx
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x4270
movq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x4310
cvtsd2ss %xmm0, %xmm0
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %rcx
movss %xmm0, (%rcx,%rbx,4)
leaq 0x30(%rsp), %rcx
cmpq %rcx, %r15
je 0xd8d8
movq 0x30(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x4250
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %rax
movss (%rax,%rbx,4), %xmm0
movss 0x44(%r14), %xmm1
ucomiss %xmm0, %xmm1
jbe 0xd8f7
movss %xmm0, 0x44(%r14)
movss (%rax,%rbx,4), %xmm0
ucomiss 0x40(%r14), %xmm0
jbe 0xd904
movss %xmm0, 0x40(%r14)
incq %rbx
cmpq %rbp, %rbx
jne 0xd882
incq %r13
incq %rbp
cmpq 0x10(%rsp), %r13
jne 0xd851
movl $0x8, %esi
movq 0x10(%rsp), %rdi
callq 0x4170
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0xdc77
movq %rax, %r12
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r15d
movq 0x78(%rsp), %r13
leaq (%r15,%r13), %rax
cmpq $0x1, %rax
je 0xd9ba
movl $0x4, %esi
movq %r15, %rdi
callq 0x4170
movq %rax, -0x8(%r12,%r15,8)
movq 0x38(%r14), %r12
incl %ebx
cmpq $0x0, -0x8(%r12,%r15,8)
leaq 0x1(%r15), %r15
jne 0xd950
movq 0x8663(%rip), %rdi      # 0x15fe8
leaq 0x2ec9(%rip), %rsi      # 0x10855
movl $0x1c, %edx
callq 0x42a0
movq 0x864b(%rip), %rdi      # 0x15fe8
movl %ebx, %esi
callq 0x4420
leaq 0x2e75(%rip), %rsi      # 0x10820
movq %rax, %rdi
callq 0x4230
xorl %edi, %edi
callq 0x42e0
cmpl $0x0, 0xc(%rsp)
jle 0xda4b
movl $0x1, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movsbl (%rax,%r12), %esi
movb (%rax,%r13), %bpl
movq %r14, %rdi
callq 0xddea
movl %eax, %ebx
movsbl %bpl, %esi
movq %r14, %rdi
callq 0xddea
cmpl %eax, %ebx
movl %eax, %ecx
cmovll %ebx, %ecx
cmovgl %ebx, %eax
cltq
movq 0x28(%r14), %rdx
movq 0x38(%r14), %rsi
movq (%rdx,%rax,8), %rax
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss 0x44(%r14), %xmm1
subss %xmm1, %xmm0
movss 0x40(%r14), %xmm2
subss %xmm1, %xmm2
divss %xmm2, %xmm0
movq (%rsi,%r12,8), %rax
movss %xmm0, (%rax,%r13,4)
incq %r13
cmpq %r13, %r15
jne 0xd9d1
incq %r12
incq %r15
cmpq 0x10(%rsp), %r12
jne 0xd9ce
callq 0x4f1c
cmpb $0x1, 0xec(%rax)
jne 0xdbc8
movq 0x855c(%rip), %rbx      # 0x15fc0
leaq 0x2e07(%rip), %rsi      # 0x10872
movl $0xd, %edx
movq %rbx, %rdi
callq 0x42a0
cmpl $0x0, 0xc(%rsp)
jle 0xdb1a
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x2a91(%rip), %rbp      # 0x10524
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x10(%rbx,%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movb (%rax,%r13), %al
movb %al, 0x20(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x42a0
xorl %r12d, %r12d
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x10(%rbx,%rax)
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r12,4), %xmm0
movq %rbx, %rdi
callq 0x43c0
incq %r12
cmpq %r12, %r15
jne 0xdac8
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x42a0
incq %r13
incq %r15
cmpq 0x10(%rsp), %r13
jne 0xda93
movq 0x849f(%rip), %rbx      # 0x15fc0
leaq 0x29fc(%rip), %rsi      # 0x10524
movl $0x1, %edx
movq %rbx, %rdi
callq 0x42a0
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x10(%rbx,%rax)
leaq 0x20(%rsp), %rsi
movb $0x20, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x42a0
cmpl $0x0, 0xc(%rsp)
movq 0x70(%rsp), %r13
js 0xdbb0
movq 0x68(%rsp), %rax
notl %eax
addl %eax, %r13d
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x10(%rbx,%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movb (%rax,%r12), %al
movb %al, 0x20(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x42a0
incq %r12
cmpq %r12, %r13
jne 0xdb78
movq 0x8409(%rip), %rdi      # 0x15fc0
leaq 0x2965(%rip), %rsi      # 0x10523
movl $0x2, %edx
callq 0x42a0
movb $0x1, 0x30(%r14)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xdbe9
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4250
leaq 0x80(%rsp), %rdi
callq 0x4080
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x83d9(%rip), %rdi      # 0x15fe8
leaq 0x2b8a(%rip), %rsi      # 0x107a0
callq 0x4230
jmp 0xd9b3
movq 0x83c1(%rip), %rdi      # 0x15fe8
leaq 0x23f2(%rip), %rsi      # 0x10020
movl $0x11, %edx
callq 0x42a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x83a2(%rip), %rdi      # 0x15fe8
callq 0x42a0
leaq 0x28d2(%rip), %rsi      # 0x10524
movq %rax, %rdi
callq 0x4230
jmp 0xd9b3
movq 0x8382(%rip), %rdi      # 0x15fe8
leaq 0x2b73(%rip), %rsi      # 0x107e0
callq 0x4230
jmp 0xd9b3
movq 0x836a(%rip), %rdi      # 0x15fe8
leaq 0x2b9e(%rip), %rsi      # 0x10823
callq 0x4230
jmp 0xd9b3
movq %rax, %rbx
jmp 0xdcd0
jmp 0xdcb1
jmp 0xdc9a
jmp 0xdcb1
movq %rax, %rbx
jmp 0xdcdd
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
jmp 0xdcb1
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xdcd0
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4250
leaq 0x80(%rsp), %rdi
callq 0x4080
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xdcfa
movq (%rax), %rsi
incq %rsi
callq 0x4250
movq %rbx, %rdi
callq 0x4440