// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r9, %rbx
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
movl 0x68(%rsp), %eax
movslq %eax, %rsi
xorl %eax, %eax
cmpq %rsi, %rax
jg 0x3e56
xorl %edi, %edi
cmpq $0x65, %rdi
je 0x3e51
movq (%r8,%rdi,8), %rdx
andl $0x0, (%rdx,%rax,4)
movq (%rbx,%rdi,8), %rdx
andl $0x0, (%rdx,%rax,4)
incq %rdi
jmp 0x3e36
incq %rax
jmp 0x3e2f
movq %rsi, 0x10(%rsp)
xorl %r15d, %r15d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movq %rcx, 0x20(%rsp)
movq %r8, 0x8(%rsp)
cmpq $0x3, %r15
je 0x3ee2
leaq 0x7311(%rip), %rax      # 0xb18c
movb (%r15,%rax), %r12b
xorl %r14d, %r14d
cmpq 0x20(%rsp), %r14
je 0x3ed8
movq 0x18(%rsp), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x22b0
cvttsd2si %xmm0, %eax
cltq
xorl %ecx, %ecx
movq 0x8(%rsp), %rsi
cmpq %rax, %rcx
jg 0x3ed3
cmpb %r12b, (%rbp,%r14)
jne 0x3ece
movq (%rbx,%rcx,8), %rdx
incl (%rdx,%r15,4)
movb (%rbp,%r14), %dl
cmpb (%r13,%r14), %dl
jne 0x3ece
movq (%rsi,%rcx,8), %rdx
incl (%rdx,%r15,4)
incq %rcx
jmp 0x3ea6
incq %r14
jmp 0x3e82
incq %r15
movq 0x8(%rsp), %r8
jmp 0x3e6e
xorl %eax, %eax
movl 0x68(%rsp), %r10d
testl %r10d, %r10d
cmovlel %eax, %r10d
movsd 0x3110(%rip), %xmm0     # 0x7008
movq 0x60(%rsp), %r11
movq 0x10(%rsp), %r14
cmpq $0x65, %rax
je 0x3f66
movq (%r8,%rax,8), %rdx
movq (%rbx,%rax,8), %rsi
movq (%r11,%rax,8), %rcx
xorl %edi, %edi
cmpq %rdi, %r10
je 0x3f47
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 0x3f16
cvtsi2sdl (%rdx,%r14,4), %xmm1
cvtsi2sdl (%rsi,%r14,4), %xmm2
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, (%rcx,%r14,8)
incq %rax
jmp 0x3f02
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq