// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movl 0x78(%rsp), %eax
testl %eax, %eax
js 0x4069
incl %eax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
movq (%r8,%rsi,8), %rdi
movl %ecx, (%rdi,%rdx,4)
movq (%r9,%rsi,8), %rdi
movl %ecx, (%rdi,%rdx,4)
incq %rsi
cmpq $0x65, %rsi
jne 0x404a
incq %rdx
cmpq %rax, %rdx
jne 0x4048
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movl 0x4(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x4(%rsp)
jle 0x4123
leaq 0x70d3(%rip), %rax      # 0xb164
movb (%r15,%rax), %bl
xorl %r14d, %r14d
movq 0x28(%rsp), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x30(%rsp)
subsd 0x18(%rsp), %xmm0
callq 0x2080
cvttsd2si %xmm0, %ebp
movsd 0x30(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
callq 0x2260
cvttsd2si %xmm0, %eax
cmpl %eax, %ebp
jg 0x4115
movslq %ebp, %rdx
movq 0x10(%rsp), %rcx
leaq (%rcx,%rdx,8), %rcx
movq 0x8(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
subl %ebp, %eax
incl %eax
xorl %esi, %esi
cmpb %bl, (%r13,%r14)
jne 0x410e
movq (%rcx,%rsi,8), %rdi
incl (%rdi,%r15,4)
movb (%r13,%r14), %dil
cmpb (%r12,%r14), %dil
jne 0x410e
movq (%rdx,%rsi,8), %rdi
incl (%rdi,%r15,4)
incq %rsi
cmpl %esi, %eax
jne 0x40ec
incq %r14
cmpq 0x20(%rsp), %r14
jne 0x4098
incq %r15
cmpq $0x3, %r15
jne 0x407f
movl 0x78(%rsp), %r14d
movslq %r14d, %rax
movl %r14d, %ecx
xorl %edx, %edx
movsd 0x2ec3(%rip), %xmm0     # 0x7008
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rbx
movq 0x70(%rsp), %r15
testl %r14d, %r14d
jle 0x419f
movq (%rbx,%rdx,8), %rsi
movq (%r11,%rdx,8), %rdi
movq (%r15,%rdx,8), %r8
xorl %r9d, %r9d
movl (%rsi,%r9,4), %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl (%rdi,%r9,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%r8,%r9,8)
addl %r10d, (%rsi,%rax,4)
movl (%rdi,%r9,4), %r10d
addl %r10d, (%rdi,%rax,4)
incq %r9
cmpq %r9, %rcx
jne 0x4168
movq (%rbx,%rdx,8), %rsi
xorps %xmm1, %xmm1
cvtsi2sdl (%rsi,%rax,4), %xmm1
movq (%r11,%rdx,8), %rsi
xorps %xmm2, %xmm2
cvtsi2sdl (%rsi,%rax,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movq (%r15,%rdx,8), %rsi
movsd %xmm1, (%rsi,%rax,8)
incq %rdx
cmpq $0x65, %rdx
jne 0x4154
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq