// File: /workspace/github_cpp_makefile_star2_O1/nanjiangshu[P]calSOV/calSOV.cpp
// Function: CalQ3ConfAccumulate(char*, char*, double*, int, int**, int**, double**, int, double)
int CalQ3ConfAccumulate(char *obsSec, char *predSec, double *predConf, int seqLength, int **numCorrectHSR, int **numHSR, double **q3HSR, int numState , double halfBinWidth)/*{{{*/
/*=======================================================================
 * calculate Q3 for residues lying above a certain confidence
 * 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 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 = 0;
            iBinEnd = int(floor(predConf[i]));;
            /* so if predConf = 35.1, 
             * */
            
            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 $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movl 0x68(%rsp), %eax
testl %eax, %eax
js 0x422f
incl %eax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %r10d, %r10d
movq (%r8,%r10,8), %rdi
movl %ecx, (%rdi,%rdx,4)
movq (%r9,%r10,8), %rdi
movl %ecx, (%rdi,%rdx,4)
incq %r10
cmpq $0x65, %r10
jne 0x4210
incq %rdx
cmpq %rax, %rdx
jne 0x420d
movl 0xc(%rsp), %eax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
movq %r9, 0x18(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x42b0
leaq 0x6f16(%rip), %rax      # 0xb164
movb (%r15,%rax), %bpl
xorl %r14d, %r14d
movq 0x20(%rsp), %rax
movsd (%rax,%r14,8), %xmm0
movq %r8, %rbx
callq 0x2260
movq %rbx, %r8
movq 0x18(%rsp), %r9
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x42a6
incl %eax
xorl %ecx, %ecx
cmpb %bpl, (%r13,%r14)
jne 0x429e
movq (%r9,%rcx,8), %rdx
incl (%rdx,%r15,4)
movb (%r13,%r14), %dl
cmpb (%r12,%r14), %dl
jne 0x429e
movq (%r8,%rcx,8), %rdx
incl (%rdx,%r15,4)
incq %rcx
cmpq %rcx, %rax
jne 0x427c
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x4255
incq %r15
cmpq $0x3, %r15
jne 0x4240
movl 0x68(%rsp), %r14d
movslq %r14d, %rax
movl %r14d, %ecx
xorl %edx, %edx
movsd 0x2d3a(%rip), %xmm0     # 0x7008
movq 0x60(%rsp), %r15
testl %r14d, %r14d
jle 0x431c
movq (%r8,%rdx,8), %rsi
movq (%r9,%rdx,8), %rdi
movq (%r15,%rdx,8), %r11
xorl %ebx, %ebx
movl (%rsi,%rbx,4), %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl (%rdi,%rbx,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%r11,%rbx,8)
addl %r10d, (%rsi,%rax,4)
movl (%rdi,%rbx,4), %r10d
addl %r10d, (%rdi,%rax,4)
incq %rbx
cmpq %rbx, %rcx
jne 0x42e6
movq (%r8,%rdx,8), %rsi
xorps %xmm1, %xmm1
cvtsi2sdl (%rsi,%rax,4), %xmm1
movq (%r9,%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 0x42d3
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq