// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: recover_utg_by_coverage(ma_ug_t**, asg_t*, ma_sub_t*, ma_hit_t_alloc*, R_to_U*, trans_chain*)
void recover_utg_by_coverage(ma_ug_t **ptg, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, trans_chain* t_ch)
{
    if(asm_opt.recover_atg_cov_min == -1) return;
    if(asm_opt.recover_atg_cov_max == -1) return;
    if(asm_opt.recover_atg_cov_min > asm_opt.recover_atg_cov_max) return;
    ma_ug_t *atg = NULL;
    atg = ma_ug_gen_primary(read_g, ALTER_LABLE);
    asg_t* nsg = atg->g;
    uint32_t v, n_vtx = nsg->n_seq, k, j, rId, available_reads = 0, keep_atg = 0, tn, is_Unitig;
    ma_utg_t* u = NULL;
    ma_hit_t *h;

    
    ///print_untig_by_read(atg, "SRR11606870.634978", -1, NULL, NULL, "debug");
    long long R_bases = 0, C_bases = 0, C_bases_primary = 0, C_bases_alter = 0;
    long long total_C_bases = 0, total_C_bases_alter = 0;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        u = &(atg->u.a[v]);
        if(u->m == 0) continue;
        total_C_bases = total_C_bases_alter = available_reads = 0;
        
        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            C_bases = C_bases_primary = C_bases_alter = 0;
            R_bases = coverage_cut[rId].e - coverage_cut[rId].s;
            for (j = 0; j < (uint64_t)(sources[rId].length); j++)
            {
                h = &(sources[rId].buffer[j]);
                if(h->el != 1) continue;
                tn = Get_tn((*h));
                if(read_g->seq[tn].del == 1)
                {
                    ///get the id of read that contains it 
                    get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                    if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
                }
                if(read_g->seq[tn].del == 1) continue;
                if(read_g->seq[tn].c == ALTER_LABLE)
                {
                    C_bases_alter += Get_qe((*h)) - Get_qs((*h));
                }
                else
                {
                    C_bases_primary += Get_qe((*h)) - Get_qs((*h));
                }
            }

            C_bases = C_bases_primary + C_bases_alter;
            total_C_bases += C_bases; 

            C_bases = C_bases/R_bases;
            if(C_bases >= asm_opt.recover_atg_cov_min && C_bases <= asm_opt.recover_atg_cov_max)
            {
                if(C_bases_alter >= (C_bases_primary + C_bases_alter) * ALTER_COV_THRES) available_reads++;
                total_C_bases_alter += C_bases_alter;
            }
        }


        if(((available_reads < (u->n * 0.8)) && (total_C_bases_alter < (total_C_bases * 0.8))) 
           || available_reads == 0)
        {
            asg_seq_del(nsg, v);

            if(u->m!=0)
            {
                u->m = u->n = 0;
                free(u->a);
                u->a = NULL;
            }
        }
        else
        {
            ///print_utg_coverage(atg, coverage_cut, v, sources);
            ///fprintf(stderr, "rId: %u\n", rId);
            ///fprintf(stderr, "%.*s\t%lld\n", (int)Get_NAME_LENGTH(R_INF, rId), Get_NAME(R_INF, rId), C_bases);
            keep_atg++;
        }
    }

    if(keep_atg > 0)
    {
        asg_cleanup(nsg);
        asg_symm(nsg);
        append_utg(*ptg, atg, t_ch);

        n_vtx = read_g->n_seq;
        for (v = 0; v < n_vtx; v++)
        {
            read_g->seq[v].c = ALTER_LABLE;
        }
        
        nsg = (*ptg)->g;
        n_vtx = nsg->n_seq;
        for (v = 0; v < n_vtx; ++v) 
        {
            if(nsg->seq[v].del) continue;
            u = &((*ptg)->u.a[v]);
            if(u->m == 0) continue;
            for (k = 0; k < u->n; k++)
            {
                rId = u->a[k]>>33;
                read_g->seq[rId].c = nsg->seq[v].c;
            }
        }


        n_vtx = read_g->n_seq;
        for (v = 0; v < n_vtx; v++)
        {
            if(read_g->seq[v].c == ALTER_LABLE)
            {
                asg_seq_drop(read_g, v);
            }
        }
    }

    ma_ug_destroy(atg);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
leaq 0x275f7e(%rip), %rax    # 0x3c47f8
cmpl $-0x1, 0x13c(%rax)
jne 0x14e888
jmp 0x14ee71
leaq 0x275f69(%rip), %rax    # 0x3c47f8
cmpl $-0x1, 0x140(%rax)
jne 0x14e89d
jmp 0x14ee71
leaq 0x275f54(%rip), %rax    # 0x3c47f8
movl 0x13c(%rax), %eax
leaq 0x275f47(%rip), %rcx    # 0x3c47f8
cmpl 0x140(%rcx), %eax
jle 0x14e8be
jmp 0x14ee71
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xe7ae0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x14ece1
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x14e976
jmp 0x14ecd3
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x14e99b
jmp 0x14ecd3
movl $0x0, -0x58(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x14ec31
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x54(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x18(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
movl -0x54(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x20(%rbp), %rcx
movl -0x54(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x14eb63
movq -0x20(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x50(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
je 0x14ea8a
jmp 0x14eb55
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x14eae4
movq -0x28(%rbp), %rdi
movl -0x60(%rbp), %esi
leaq -0x60(%rbp), %rdx
leaq -0x64(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x60(%rbp)
je 0x14eae0
cmpl $0x1, -0x64(%rbp)
je 0x14eae0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x14eae2
jmp 0x14eb55
jmp 0x14eae4
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x14eafc
jmp 0x14eb55
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x14eb33
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x78(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x14eb53
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x78(%rbp), %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x14eb55
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x14ea3c
movq -0x90(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
cqto
idivq -0x80(%rbp)
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
leaq 0x275c49(%rip), %rcx    # 0x3c47f8
movslq 0x13c(%rcx), %rcx
cmpq %rcx, %rax
jl 0x14ec21
movq -0x88(%rbp), %rax
leaq 0x275c2f(%rip), %rcx    # 0x3c47f8
movslq 0x140(%rcx), %rcx
cmpq %rcx, %rax
jg 0x14ec21
cvtsi2sdq -0x98(%rbp), %xmm0
movq -0x90(%rbp), %rax
addq -0x98(%rbp), %rax
cvtsi2sd %rax, %xmm1
movsd 0x23366f(%rip), %xmm2   # 0x382268
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14ec0c
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x98(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x14ec23
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x14e9bf
movl -0x58(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x70(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x2335e3(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14ec7b
cvtsi2sdq -0xa8(%rbp), %xmm1
cvtsi2sdq -0xa0(%rbp), %xmm0
movsd 0x2335bf(%rip), %xmm2   # 0x382230
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x14ec81
cmpl $0x0, -0x58(%rbp)
jne 0x14ecc8
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0xd9510
movq -0x70(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x14ecc6
movq -0x70(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x70(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2210
movq -0x70(%rbp), %rax
movq $0x0, 0x18(%rax)
jmp 0x14ecd1
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x14ecd3
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x14e94f
cmpl $0x0, -0x5c(%rbp)
jbe 0x14ee68
movq -0x40(%rbp), %rdi
callq 0xcdd10
movq -0x40(%rbp), %rdi
callq 0xce020
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x14e290
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x14ed4a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x14ed27
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x14ee1a
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x14ed93
jmp 0x14ee0c
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x14edb8
jmp 0x14ee0c
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x70(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x14ee0a
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x54(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movb 0x4(%rax,%rcx,8), %dl
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movb %dl, 0x4(%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x14edbf
jmp 0x14ee0c
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x14ed6f
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x14ee66
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x14ee59
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0xf6800
jmp 0x14ee5b
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x14ee30
jmp 0x14ee68
movq -0x38(%rbp), %rdi
callq 0xcd6f0
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)