// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam2bcf.c
// Function: calc_ReadPosBias
void calc_ReadPosBias(bcf_callaux_t *bca, bcf_call_t *call)
{
    int i, nref = 0, nalt = 0;
    unsigned long int U = 0;
    for (i=0; i<bca->npos; i++) 
    {
        nref += bca->ref_pos[i];
        nalt += bca->alt_pos[i];
        U += nref*bca->alt_pos[i];
        bca->ref_pos[i] = 0;
        bca->alt_pos[i] = 0;
    }
#if 0
//todo
    double var = 0, avg = (double)(nref+nalt)/bca->npos;
    for (i=0; i<bca->npos; i++) 
    {
        double ediff = bca->ref_pos[i] + bca->alt_pos[i] - avg;
        var += ediff*ediff;
        bca->ref_pos[i] = 0;
        bca->alt_pos[i] = 0;
    }
    call->read_pos.avg = avg;
    call->read_pos.var = sqrt(var/bca->npos);
    call->read_pos.dp  = nref+nalt;
#endif
    if ( !nref || !nalt )
    {
        call->read_pos_bias = -1;
        return;
    }

    if ( nref>=8 || nalt>=8 )
    {
        // normal approximation
        double mean = ((double)nref*nalt+1.0)/2.0;
        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
        double z    = (U-mean)/sqrt(var2);
        call->read_pos_bias = z;
        //fprintf(stderr,"nref=%d  nalt=%d  U=%ld  mean=%e  var=%e  zval=%e\n", nref,nalt,U,mean,sqrt(var2),call->read_pos_bias);
    }
    else
    {
        double p = mann_whitney_1947(nalt,nref,U);
        // biased form claimed by GATK to behave better empirically
        // double var2 = (1.0+1.0/(nref+nalt+1.0))*(double)nref*nalt*(nref+nalt+1.0)/12.0;
        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
        double z;
        if ( p >= 1./sqrt(var2*2*M_PI) ) z = 0;   // equal to mean
        else
        {
            if ( U >= nref*nalt/2. ) z = sqrt(-2*log(sqrt(var2*2*M_PI)*p));
            else z = -sqrt(-2*log(sqrt(var2*2*M_PI)*p));
        }
        call->read_pos_bias = z;
        //fprintf(stderr,"nref=%d  nalt=%d  U=%ld  p=%e var2=%e  zval=%e\n", nref,nalt,U, p,var2,call->read_pos_bias);
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
cmpl $0x0, 0x48(%rdi)
jle 0x10f6e
movq 0x38(%rdi), %rax
movq 0x40(%rdi), %rcx
xorl %edx, %edx
xorl %esi, %esi
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
addl (%rax,%rsi,4), %r12d
movl (%rcx,%rsi,4), %r8d
addl %r8d, %r15d
imull %r12d, %r8d
movslq %r8d, %r8
addq %r8, %r14
movl %edx, (%rax,%rsi,4)
movl %edx, (%rcx,%rsi,4)
incq %rsi
movslq 0x48(%rdi), %r8
cmpq %r8, %rsi
jl 0x10f45
jmp 0x10f77
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movss 0x2fac1(%rip), %xmm0    # 0x40a40
testl %r12d, %r12d
je 0x1119c
testl %r15d, %r15d
je 0x1119c
leal (%r12,%r15), %eax
cvtsi2sd %eax, %xmm2
addsd 0x2fa17(%rip), %xmm2    # 0x409b8
cmpl $0x7, %r12d
jg 0x11005
cmpl $0x8, %r15d
jge 0x11005
movl %r15d, %edi
movl %r12d, %esi
movl %r14d, %edx
movsd %xmm2, (%rsp)
callq 0x10e6d
cvtsi2sd %r12d, %xmm1
cvtsi2sd %r15d, %xmm3
mulsd %xmm1, %xmm3
mulsd (%rsp), %xmm3
divsd 0x2f9ed(%rip), %xmm3    # 0x409c8
addsd %xmm3, %xmm3
mulsd 0x2f9e9(%rip), %xmm3    # 0x409d0
movapd %xmm0, %xmm2
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm3
jb 0x1108a
xorps %xmm0, %xmm0
sqrtsd %xmm3, %xmm0
jmp 0x110ad
cvtsi2sd %r12d, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd %xmm1, %xmm0
movsd 0x2f99a(%rip), %xmm1    # 0x409b8
addsd %xmm0, %xmm1
mulsd 0x2f9b6(%rip), %xmm1    # 0x409e0
mulsd %xmm2, %xmm0
divsd 0x2f992(%rip), %xmm0    # 0x409c8
movq %r14, %xmm2
punpckldq 0x2eeed(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x2eef5(%rip), %xmm2    # 0x3ff40
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
addsd %xmm1, %xmm3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1106b
sqrtsd %xmm0, %xmm0
jmp 0x1107a
movapd %xmm3, (%rsp)
callq 0x37a0
movapd (%rsp), %xmm3
divsd %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
jmp 0x1119c
movapd %xmm3, %xmm0
movsd %xmm2, (%rsp)
movsd %xmm3, 0x20(%rsp)
callq 0x37a0
xorpd %xmm4, %xmm4
movsd 0x20(%rsp), %xmm3
movsd (%rsp), %xmm2
movsd 0x2f903(%rip), %xmm1    # 0x409b8
divsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jae 0x11195
movq %r14, %xmm5
punpckldq 0x2ee60(%rip), %xmm5 # xmm5 = xmm5[0],mem[0],xmm5[1],mem[1]
subpd 0x2ee68(%rip), %xmm5    # 0x3ff40
movapd %xmm5, %xmm1
imull %r15d, %r12d
xorps %xmm4, %xmm4
cvtsi2sd %r12d, %xmm4
unpckhpd %xmm5, %xmm1            # xmm1 = xmm1[1],xmm5[1]
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jb 0x110ff
xorps %xmm0, %xmm0
sqrtsd %xmm3, %xmm0
jmp 0x11136
movapd %xmm3, %xmm0
movsd %xmm2, (%rsp)
movapd %xmm1, 0x20(%rsp)
movsd %xmm4, 0x18(%rsp)
movapd %xmm5, 0x30(%rsp)
callq 0x37a0
movapd 0x30(%rsp), %xmm5
movsd 0x18(%rsp), %xmm4
movapd 0x20(%rsp), %xmm1
movsd (%rsp), %xmm2
addsd %xmm5, %xmm1
movapd %xmm1, 0x20(%rsp)
mulsd 0x2cee8(%rip), %xmm4    # 0x3e030
movsd %xmm4, 0x18(%rsp)
mulsd %xmm0, %xmm2
movapd %xmm2, %xmm0
callq 0x3330
mulsd 0x2f875(%rip), %xmm0    # 0x409d8
ucomisd 0x2f87d(%rip), %xmm0    # 0x409e8
jb 0x11176
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x1117f
callq 0x37a0
movapd %xmm0, %xmm4
movapd 0x20(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jae 0x11195
xorpd 0x2f8bb(%rip), %xmm4    # 0x40a50
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, 0x94(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq