// File: /workspace/github_cpp_makefile_star2_O1/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 0x897c(%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 0xd9a6
movq %rsi, %rbx
movq %rdi, %r14
callq 0x4f1c
cmpb $0x1, 0xec(%rax)
jne 0xd3df
movq 0x8c19(%rip), %rdi      # 0x15fc0
leaq 0x33d7(%rip), %rsi      # 0x10785
movl $0x17, %edx
callq 0x42a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8bfa(%rip), %rdi      # 0x15fc0
callq 0x42a0
leaq 0x311a(%rip), %rsi      # 0x104ec
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 0xd9be
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 0xd49a
movq 0x40(%rsp), %rcx
cmpb $0x4d, (%rcx)
je 0xd49a
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 0xd450
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 0xd510
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 0xd9fd
xorl %ebp, %ebp
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %ebp
movq %rbp, %r15
negq %r15
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r12d
leaq (%r15,%r12), %rax
cmpq $0x1, %rax
je 0xd5ba
movl $0x4, %esi
movq %r12, %rdi
callq 0x4170
movq 0x28(%r14), %rcx
movq %rax, -0x8(%rcx,%r12,8)
movq 0x28(%r14), %rax
incl %ebx
cmpq $0x0, -0x8(%rax,%r12,8)
leaq 0x1(%r12), %r12
jne 0xd54d
movq 0x8a61(%rip), %rdi      # 0x15fe8
leaq 0x322f(%rip), %rsi      # 0x107bd
movl $0x1f, %edx
callq 0x42a0
movq 0x8a49(%rip), %rdi      # 0x15fe8
movl %ebx, %esi
callq 0x4420
leaq 0x3230(%rip), %rsi      # 0x107dd
movq %rax, %rdi
callq 0x4230
jmp 0xd751
movq %rbp, 0x78(%rsp)
movq %r13, 0x70(%rsp)
movabsq $0x447a0000c47a0000, %rax # imm = 0x447A0000C47A0000
movq %rax, 0x40(%r14)
cmpl $0x0, 0xc(%rsp)
jle 0xd6ba
movl $0x1, %r13d
xorl %ebp, %ebp
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,%rbp,8), %rax
movss %xmm0, (%rax,%rbx,4)
leaq 0x30(%rsp), %rax
cmpq %rax, %r15
je 0xd66d
movq 0x30(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x4250
movq 0x28(%r14), %rax
movq (%rax,%rbp,8), %rax
movss (%rax,%rbx,4), %xmm0
movss 0x44(%r14), %xmm1
ucomiss %xmm0, %xmm1
jbe 0xd68b
movss %xmm0, 0x44(%r14)
movss (%rax,%rbx,4), %xmm0
ucomiss 0x40(%r14), %xmm0
jbe 0xd69d
movss %xmm0, 0x40(%r14)
incq %rbx
cmpq %r13, %rbx
jne 0xd61b
incq %rbp
incq %r13
cmpq 0x10(%rsp), %rbp
jne 0xd5ea
movl $0x8, %esi
movq 0x10(%rsp), %rdi
callq 0x4170
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0xda15
movl 0x78(%rsp), %r15d
negq %r15
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r12d
leaq (%r15,%r12), %rax
cmpq $0x1, %rax
je 0xd758
movl $0x4, %esi
movq %r12, %rdi
callq 0x4170
movq 0x38(%r14), %rcx
movq %rax, -0x8(%rcx,%r12,8)
movq 0x38(%r14), %rax
incl %ebx
cmpq $0x0, -0x8(%rax,%r12,8)
leaq 0x1(%r12), %r12
jne 0xd6e9
movq 0x88c5(%rip), %rdi      # 0x15fe8
leaq 0x30e8(%rip), %rsi      # 0x10812
movl $0x1c, %edx
callq 0x42a0
movq 0x88ad(%rip), %rdi      # 0x15fe8
movl %ebx, %esi
callq 0x4420
leaq 0x3094(%rip), %rsi      # 0x107dd
movq %rax, %rdi
callq 0x4230
xorl %edi, %edi
callq 0x42e0
cmpl $0x0, 0xc(%rsp)
jle 0xd7e9
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 0xdb88
movl %eax, %ebx
movsbl %bpl, %esi
movq %r14, %rdi
callq 0xdb88
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 0xd76f
incq %r12
incq %r15
cmpq 0x10(%rsp), %r12
jne 0xd76c
callq 0x4f1c
cmpb $0x1, 0xec(%rax)
jne 0xd966
movq 0x87be(%rip), %rbx      # 0x15fc0
leaq 0x3026(%rip), %rsi      # 0x1082f
movl $0xd, %edx
movq %rbx, %rdi
callq 0x42a0
cmpl $0x0, 0xc(%rsp)
jle 0xd8b8
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x2cbb(%rip), %rbp      # 0x104ec
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 0xd866
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x42a0
incq %r13
incq %r15
cmpq 0x10(%rsp), %r13
jne 0xd831
movq 0x8701(%rip), %rbx      # 0x15fc0
leaq 0x2c26(%rip), %rsi      # 0x104ec
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 0xd94e
movq 0x68(%rsp), %rax
notl %eax
addl %eax, %r13d
xorl %r15d, %r15d
leaq 0x20(%rsp), %r12
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x9, 0x10(%rbx,%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movb (%rax,%r15), %al
movb %al, 0x20(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x42a0
incq %r15
cmpq %r15, %r13
jne 0xd916
movq 0x866b(%rip), %rdi      # 0x15fc0
leaq 0x2b8f(%rip), %rsi      # 0x104eb
movl $0x2, %edx
callq 0x42a0
movb $0x1, 0x30(%r14)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xd987
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 0x863b(%rip), %rdi      # 0x15fe8
leaq 0x2da9(%rip), %rsi      # 0x1075d
callq 0x4230
jmp 0xd751
movq 0x8623(%rip), %rdi      # 0x15fe8
leaq 0x2654(%rip), %rsi      # 0x10020
movl $0x11, %edx
callq 0x42a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq 0x8604(%rip), %rdi      # 0x15fe8
callq 0x42a0
leaq 0x2afc(%rip), %rsi      # 0x104ec
movq %rax, %rdi
callq 0x4230
jmp 0xd751
movq 0x85e4(%rip), %rdi      # 0x15fe8
leaq 0x2d92(%rip), %rsi      # 0x1079d
callq 0x4230
jmp 0xd751
movq 0x85cc(%rip), %rdi      # 0x15fe8
leaq 0x2dbd(%rip), %rsi      # 0x107e0
callq 0x4230
jmp 0xd751
movq %rax, %rbx
jmp 0xda6e
jmp 0xda4f
jmp 0xda38
jmp 0xda4f
movq %rax, %rbx
jmp 0xda7b
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
jmp 0xda4f
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xda6e
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 0xda98
movq (%rax), %rsi
incq %rsi
callq 0x4250
movq %rbx, %rdi
callq 0x4440