// File: nanjiangshu[P]calSOV/calSOV.cpp
// Function: CalQ3ConfBin(char*, char*, double*, int, int**, int**, double**, int, double)
int CalQ3ConfBin(char *obsSec, char *predSec, double *predConf, int seqLength, int **numCorrectHSR, int **numHSR, double **q3HSR, int numState , double halfBinWidth)/*{{{*/
/*=======================================================================
 * calculate Q3 for residues lying in each confidence bin
 * numCorrectHSR[N][4]: N is the number of bins, and [4] is for Helix, Sheet, Coil and All
 * numHSR[N][4] Helix, Sheet, Coil, All
 * q3HSR[N][4]
 * numState = 3 by default
 *
 * predConf is in the range of [0,100]
 *
 * bins are setting like this. 
 * for numCorrectHSR[50][j], residues with predConf >= 50 - halfBinWidth and
 * predConf < 50 + halfBinWidth are included
 * predConf has two format: 
 *  1: rawConfidence: raw confidence for secondary structure is calculated as
 *  maxProb / sumProb
 *  2: nomalized confidence:  the normalization function is generated by
 *  second order polynomial regression 
 *  2009-07-31 
 *
 *====================================================================*/
{

    int iterHSRState = 0; /*iterator for the HSR state, H, S and R*/
    char thisHSRstate = ' '; /*current HSR state*/
    int i = 0;

    int iBin;         /*iterator of the bin*/
    for (i=0;i<=numState; i++)
    {
        for (iBin = 0; iBin <=100; iBin++)
        {
            numCorrectHSR[iBin][i] = 0;
            numHSR[iBin][i] = 0;
        }
    }

    int iBinBeg = 0;
    int iBinEnd = 0;
    for (iterHSRState=0; iterHSRState<3; iterHSRState++)//corrsponding to H, S, R
    {
        thisHSRstate = HSRalphabet[iterHSRState];
        for(i=0; i<seqLength; i++)
        { 
            iBinBeg = int(ceil(predConf[i]-halfBinWidth));
            iBinEnd = int(floor(predConf[i]+halfBinWidth));
            /* so if predConf = 35.1, halfBinWidth = 0.8
             * iBinBeg = ceil(35.1-0.8) = ceil(34.3) = 35
             * iBinEnd = floor(35.1+0.8) = floor(35.9) = 35
             * then this residue lies only in the bin [35]
             * */
            
            for (iBin = iBinBeg ; iBin <= iBinEnd; iBin++)
            { 
                if (  obsSec[i] == thisHSRstate  )
                {
                    numHSR[iBin][iterHSRState] ++;
                    if (obsSec[i] == predSec[i])
                    {
                        numCorrectHSR[iBin][iterHSRState] ++;
                    }
                }
            }
        } 
    }

    for (iBin = 0; iBin <= 100; iBin++)
    {
        for (i=0; i<numState; i++)
        {   
            q3HSR[iBin][i] = numCorrectHSR[iBin][i] / double (numHSR[iBin][i]+1e-6);
            numCorrectHSR[iBin][numState]  += numCorrectHSR[iBin][i];
            numHSR[iBin][numState]  += numHSR[iBin][i];
        }
        q3HSR[iBin][numState] = numCorrectHSR[iBin][numState] / double (numHSR[iBin][numState]+1e-6); 
    }

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movb $0x20, -0x3d(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jg 0x533f
movl $0x0, -0x48(%rbp)
cmpl $0x64, -0x48(%rbp)
jg 0x5332
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x52f3
jmp 0x5334
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x52e4
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x3, -0x3c(%rbp)
jge 0x545d
movslq -0x3c(%rbp), %rcx
leaq 0xde8b(%rip), %rax      # 0x131f4
movb (%rax,%rcx), %al
movb %al, -0x3d(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x544d
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd -0x38(%rbp), %xmm0
callq 0x2080
cvttsd2si %xmm0, %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x38(%rbp), %xmm0
callq 0x2370
cvttsd2si %xmm0, %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x50(%rbp), %eax
jg 0x543d
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movsbl -0x3d(%rbp), %ecx
cmpl %ecx, %eax
jne 0x5430
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x542e
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x5430
jmp 0x5432
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x53c4
jmp 0x543f
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5376
jmp 0x544f
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x5354
movl $0x0, -0x48(%rbp)
cmpl $0x64, -0x48(%rbp)
jg 0x558d
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x5530
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movsd 0x7b55(%rip), %xmm2     # 0xd008
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5475
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movsd 0x7aa6(%rip), %xmm2     # 0xd008
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x5464
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)