// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r9, %rbx
movq %rdx, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
movl 0x78(%rsp), %eax
movslq %eax, %rsi
xorl %eax, %eax
cmpq %rsi, %rax
jg 0x3cb9
xorl %edi, %edi
cmpq $0x65, %rdi
je 0x3cb4
movq (%r8,%rdi,8), %rdx
andl $0x0, (%rdx,%rax,4)
movq (%rbx,%rdi,8), %rdx
andl $0x0, (%rdx,%rax,4)
incq %rdi
jmp 0x3c99
incq %rax
jmp 0x3c92
movq %rsi, 0x18(%rsp)
xorl %r15d, %r15d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movq %rcx, 0x28(%rsp)
movq %r8, 0x8(%rsp)
cmpq $0x3, %r15
je 0x3d75
leaq 0x74aa(%rip), %rax      # 0xb18c
movb (%r15,%rax), %r12b
xorl %r14d, %r14d
cmpq 0x28(%rsp), %r14
je 0x3d68
movq 0x20(%rsp), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x30(%rsp)
subsd 0x10(%rsp), %xmm0
callq 0x2080
cvttsd2si %xmm0, %eax
movl %eax, 0x4(%rsp)
movsd 0x30(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
callq 0x22b0
cvttsd2si %xmm0, %ecx
movslq 0x4(%rsp), %rax
movslq %ecx, %rcx
movq 0x8(%rsp), %rsi
cmpq %rcx, %rax
jg 0x3d63
cmpb %r12b, (%rbp,%r14)
jne 0x3d5e
movq (%rbx,%rax,8), %rdx
incl (%rdx,%r15,4)
movb (%rbp,%r14), %dl
cmpb (%r13,%r14), %dl
jne 0x3d5e
movq (%rsi,%rax,8), %rdx
incl (%rdx,%r15,4)
incq %rax
jmp 0x3d36
incq %r14
jmp 0x3ce9
incq %r15
movq 0x8(%rsp), %r8
jmp 0x3cd1
xorl %eax, %eax
movl 0x78(%rsp), %r10d
testl %r10d, %r10d
cmovlel %eax, %r10d
movsd 0x327d(%rip), %xmm0     # 0x7008
movq 0x70(%rsp), %r11
movq 0x18(%rsp), %r14
cmpq $0x65, %rax
je 0x3df9
movq (%r8,%rax,8), %rdx
movq (%rbx,%rax,8), %rsi
movq (%r11,%rax,8), %rcx
xorl %edi, %edi
cmpq %rdi, %r10
je 0x3dda
movl (%rdx,%rdi,4), %r9d
cvtsi2sd %r9d, %xmm1
cvtsi2sdl (%rsi,%rdi,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%rcx,%rdi,8)
addl %r9d, (%rdx,%r14,4)
movl (%rsi,%rdi,4), %r9d
addl %r9d, (%rsi,%r14,4)
incq %rdi
jmp 0x3da9
cvtsi2sdl (%rdx,%r14,4), %xmm1
cvtsi2sdl (%rsi,%r14,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%rcx,%r14,8)
incq %rax
jmp 0x3d95
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq