// File: /workspace/github_cpp_makefile_star2_O2/CK7[P]abawaca/src/abawaca-build.cpp
// Function: Scaf_segment::Scaf_segment(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long, unsigned long, Bio::DNAString const&)
Scaf_segment::Scaf_segment(string _display_id, string _desc, size_t s, size_t e, const Bio::DNAString& _seq) : start(s), end(e), Sequence(_display_id, _seq), desc(_desc)
{
	int k = 4;
	if(dimension2index.size() == 0) {
		prepare_mer_indexes(k, dimension2index);
	}
	dimensions.resize(dimension_order.size());

	// Compute 1,2,3 and 4-mer frequencies
	int factor = 10;
	int N_limit = 1;
	for(auto i=1; i<=k; i++, N_limit*=10, factor*=10) {
		// Shouldn't happen...
		if(seq.size() < i)
			continue;

		unordered_map<size_t, size_t>	counts;
		int 	key=0, total=0;
		auto	it_start = seq.begin(), it_end = seq.begin();

		// Add first mer
		while(it_end-it_start < i) {
			key *= 10;	// 0=00000, 2=00010, 6=00110, 19=10011
			switch(*(it_end++)) {
				case 'A':	key += 2;	break;
				case 'C':	key += 3;	break;
				case 'G':	key += 4;	break;
				case 'T':	key += 5;	break;
				default:	key = 0;
			}
		}
		if(key > N_limit) {
			counts[key]++;
 			total++;
		}
		// Rest of the mers
		while(it_end != seq.end()) {
			key *= 10;
			key = key%factor;
			switch(*(it_end++)) {
				case 'A':	key += 2;	break;
				case 'C':	key += 3;	break;
				case 'G':	key += 4;	break;
				case 'T':	key += 5;	break;
				default:	key = 0;
			}

			// The following means that there are no N's in mer)
			if(key > N_limit) {
				counts[key]++;
				total++;
			}
		}

		for(auto it=counts.begin(); it!=counts.end(); it++) {
			string mer;
			for(int i=factor; i>0; i/=10)
			switch((it->first/i)%10) {
				case 2:	mer.push_back('A');	break;
				case 3:	mer.push_back('C');	break;
				case 4:	mer.push_back('G');	break;
				case 5:	mer.push_back('T');	break;
			}
			auto sit = dimension2index.find(mer);
			if(sit == dimension2index.end()) {
				cerr << "Error: could not find index for " << mer << endl;
				exit(-1);
			}
			dimensions[sit->second] += (total != 0)? (double(it->second)/total): 0;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r13
movq %r8, %r15
movq %rcx, %r12
movq %rdx, -0x40(%rbp)
movq %rdi, %rbx
leaq -0xd8(%rbp), %r14
movq %r14, %rdi
callq 0x43a0
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x9562
leaq -0xd8(%rbp), %rdi
callq 0x4130
leaq 0xdad2(%rip), %rax      # 0x14b00
movq %rax, (%rbx)
movq %r12, 0x48(%rbx)
movq %r15, 0x50(%rbx)
leaq 0x58(%rbx), %rdi
movq %rdi, -0x78(%rbp)
movq -0x40(%rbp), %rsi
movq %rbx, %r12
callq 0x43a0
leaq 0x78(%r12), %rax
movq %rax, -0x50(%rbp)
andq $0x0, 0x88(%r12)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x78(%r12)
cmpq $0x0, 0xe2ce(%rip)      # 0x15340
jne 0x7083
leaq 0xe29d(%rip), %rsi      # 0x15318
pushq $0x4
popq %rdi
callq 0x6f54
movq 0xe2c6(%rip), %rsi      # 0x15350
subq 0xe2b7(%rip), %rsi      # 0x15348
sarq $0x5, %rsi
movq -0x50(%rbp), %rdi
callq 0x95b6
pushq $0x1
popq %rax
pushq $0xa
popq %rbx
pushq $0x1
popq %r14
leaq -0x70(%rbp), %r13
pushq $0xa
popq %r15
movl %ebx, -0x40(%rbp)
movl %eax, -0x2c(%rbp)
movq %r12, -0x38(%rbp)
cmpq $0x5, %r14
je 0x7319
cmpq %r14, 0x30(%r12)
jb 0x72ff
leaq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq $0x1, -0xb0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x98(%rbp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq 0x28(%r12), %rsi
xorl %eax, %eax
xorl %r12d, %r12d
cmpq %r14, %rax
jae 0x7148
imull $0xa, %r12d, %ecx
movzbl (%rsi,%rax), %edx
cmpl $0x41, %edx
je 0x713d
cmpl $0x43, %edx
je 0x7133
cmpl $0x47, %edx
je 0x7138
xorl %r12d, %r12d
cmpl $0x54, %edx
jne 0x7143
addl $0x5, %ecx
jmp 0x7140
addl $0x3, %ecx
jmp 0x7140
addl $0x4, %ecx
jmp 0x7140
addl $0x2, %ecx
movl %ecx, %r12d
incq %rax
jmp 0x710a
movl $0x0, -0x30(%rbp)
cmpl -0x2c(%rbp), %r12d
jle 0x717c
movq %rsi, -0x48(%rbp)
movslq %r12d, %rax
movq %rax, -0x70(%rbp)
leaq -0xb8(%rbp), %rdi
movq %r13, %rsi
callq 0xad52
incq (%rax)
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %rsi
movq %r14, -0x80(%rbp)
addq %r14, %rsi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rcx
addq 0x30(%rax), %rcx
movl %r12d, %eax
cmpq %rcx, %rsi
je 0x71ff
movq %rsi, %r14
imull $0xa, %eax, %eax
cltd
idivl -0x40(%rbp)
movl %edx, %r12d
incq %rsi
movzbl (%r14), %edx
pushq $0x2
popq %rax
cmpl $0x41, %edx
je 0x71cf
cmpl $0x54, %edx
je 0x71c8
cmpl $0x47, %edx
je 0x71cc
xorl %eax, %eax
cmpl $0x43, %edx
jne 0x7192
pushq $0x3
jmp 0x71ce
pushq $0x5
jmp 0x71ce
pushq $0x4
popq %rax
addl %eax, %r12d
movl %r12d, %eax
cmpl -0x2c(%rbp), %r12d
jle 0x7192
movslq %r12d, %rax
movq %rax, -0x70(%rbp)
leaq -0xb8(%rbp), %rdi
movq %r13, %rsi
callq 0xad52
incq (%rax)
incq %r14
incl -0x30(%rbp)
movq %r14, %rsi
jmp 0x7183
movq -0xa8(%rbp), %r14
cvtsi2sdl -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x38(%rbp), %r12
testq %r14, %r14
je 0x72ef
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
andq $0x0, -0x68(%rbp)
movb $0x0, -0x60(%rbp)
movl -0x40(%rbp), %r12d
testl %r12d, %r12d
je 0x7275
movq 0x8(%r14), %rax
movl %r12d, %ecx
xorl %edx, %edx
divq %rcx
xorl %edx, %edx
divq %r15
addq $-0x2, %rdx
cmpq $0x4, %rdx
jae 0x7269
shll $0x3, %edx
movl $0x54474341, %eax       # imm = 0x54474341
movl %edx, %ecx
shrl %cl, %eax
movzbl %al, %esi
movq %r13, %rdi
callq 0x4200
movl %r12d, %eax
xorl %edx, %edx
divl %ebx
movl %eax, %r12d
jmp 0x7232
leaq 0xe09c(%rip), %rdi      # 0x15318
movq %r13, %rsi
movq -0x38(%rbp), %r12
callq 0xa702
leaq 0xe091(%rip), %rcx      # 0x15320
cmpq %rcx, %rax
je 0x732b
cmpl $0x0, -0x30(%rbp)
je 0x72c6
movsd 0x10(%r14), %xmm1
unpcklps 0x7025(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x702d(%rip), %xmm1     # 0xe2e0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd -0x48(%rbp), %xmm0
jmp 0x72ca
xorpd %xmm0, %xmm0
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rcx), %rcx
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
movq %r13, %rdi
callq 0x4130
movq (%r14), %r14
jmp 0x7214
leaq -0xb8(%rbp), %rdi
callq 0x9e1e
movq -0x80(%rbp), %r14
incq %r14
movl -0x2c(%rbp), %eax
imull $0xa, %eax, %eax
movl %eax, -0x2c(%rbp)
movl -0x40(%rbp), %eax
imull $0xa, %eax, %eax
movl %eax, -0x40(%rbp)
jmp 0x70ba
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdcbe(%rip), %rdi      # 0x14ff0
leaq 0x7224(%rip), %rsi      # 0xe55d
callq 0x4260
leaq -0x70(%rbp), %rsi
movq %rax, %rdi
callq 0x4220
movq %rax, %rdi
callq 0x4140
pushq $-0x1
popq %rdi
callq 0x4330
movq %rax, %r14
jmp 0x73b5
movq %rax, %r14
leaq -0xd8(%rbp), %rdi
callq 0x4130
jmp 0x73bd
movq %rax, %r14
jmp 0x73a3
jmp 0x7377
movq %rax, %r14
movq -0x38(%rbp), %r12
jmp 0x7397
jmp 0x7382
movq %rax, %r14
jmp 0x738e
movq %rax, %r14
movq -0x38(%rbp), %r12
leaq -0x70(%rbp), %rdi
callq 0x4130
leaq -0xb8(%rbp), %rdi
callq 0x9e1e
movq -0x50(%rbp), %rdi
callq 0xa6c6
movq -0x78(%rbp), %rdi
callq 0x4130
movq %r12, %rdi
callq 0x9dd4
movq %r14, %rdi
callq 0x44b0
nop